403 lines
15 KiB
TypeScript
403 lines
15 KiB
TypeScript
import { assert, assertAlmostEquals, assertEquals, assertFalse } from "@std/assert";
|
|
import { LocalPlayer } from "$/client/entity/local_player.ts";
|
|
import { InputManager } from "$/client/input_manager.ts";
|
|
import { Options } from "$/client/options.ts";
|
|
import type { Client } from "$/client/client.ts";
|
|
|
|
type Blocks = (x: number, y: number, z: number) => boolean;
|
|
|
|
// a player on flat ground at y 0, maybe with a ceiling, and the positions it sends. or in a world of its own: what's
|
|
// solid and what's water
|
|
function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
|
|
const sent: { y: number; swimming?: boolean }[] = [];
|
|
const client = {
|
|
level: {
|
|
has_collision: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
|
|
is_liquid: world?.water ?? (() => false),
|
|
},
|
|
screen: undefined,
|
|
options: new Options(),
|
|
connection: {
|
|
send: (message: { type: string; y: number; swimming?: boolean }) =>
|
|
message.type === "move" && sent.push(message),
|
|
},
|
|
};
|
|
const player = new LocalPlayer(client as unknown as Client, "id", "alice");
|
|
player.set_position(0.5, 0, 0.5);
|
|
return { player, sent };
|
|
}
|
|
|
|
// the ticks it jumped on
|
|
function run(player: LocalPlayer, ticks: number, space: (tick: number) => boolean) {
|
|
const jumps: number[] = [];
|
|
for (let tick = 0; tick < ticks; tick++) {
|
|
if (space(tick)) InputManager.keys_down.add("Space");
|
|
else InputManager.keys_down.delete("Space");
|
|
const was_on_ground = player.on_ground;
|
|
player.tick();
|
|
if (was_on_ground && !player.on_ground) jumps.push(tick);
|
|
}
|
|
InputManager.keys_down.clear();
|
|
return jumps;
|
|
}
|
|
|
|
Deno.test("holding jump jumps again as soon as it lands, but not sooner than minecraft's jump delay", () => {
|
|
const open = setup();
|
|
run(open.player, 1, () => false);
|
|
const landings: number[] = [];
|
|
const jumps = run(open.player, 100, (tick) => {
|
|
if (tick > 0 && open.player.on_ground) landings.push(tick);
|
|
return true;
|
|
});
|
|
assert(jumps.length >= 5, `jumped ${jumps.length} times`);
|
|
// a jump is longer in the air than the delay, so it's never waiting on the ground
|
|
for (const jump of jumps.slice(1)) {
|
|
assert(landings.includes(jump), `waited on the ground before jumping on tick ${jump}`);
|
|
}
|
|
|
|
// with a block right above its head it lands sooner, and waits out the delay on the ground
|
|
const low = setup(2);
|
|
run(low.player, 1, () => false);
|
|
let standing = 0;
|
|
const waited: number[] = [];
|
|
const bumped = run(low.player, 100, () => {
|
|
standing = low.player.on_ground ? standing + 1 : 0;
|
|
waited.push(standing);
|
|
return true;
|
|
});
|
|
assert(bumped.length >= 5, `jumped ${bumped.length} times`);
|
|
for (let i = 1; i < bumped.length; i++) {
|
|
assert(bumped[i] - bumped[i - 1] >= 10, `jumped again after ${bumped[i] - bumped[i - 1]} ticks`);
|
|
}
|
|
assert(bumped.slice(1).some((jump) => waited[jump] > 1), "never waited on the ground");
|
|
|
|
// letting go clears the delay
|
|
const { player } = setup(2);
|
|
run(player, 1, () => false);
|
|
run(player, 1, () => true);
|
|
while (!player.on_ground) run(player, 1, () => true);
|
|
run(player, 1, () => false);
|
|
assertEquals(run(player, 1, () => true), [0]);
|
|
});
|
|
|
|
Deno.test("the position is sent on the tick it lands, so the server sees it touch the ground between jumps", () => {
|
|
const { player, sent } = setup();
|
|
run(player, 1, () => false);
|
|
// jump, and press again the moment it lands, for the shortest time on the ground
|
|
let landings = 0;
|
|
for (let tick = 0; tick < 200; tick++) {
|
|
InputManager.keys_down.clear();
|
|
if (player.on_ground && tick % 2 === 0) InputManager.keys_down.add("Space");
|
|
const in_air = !player.on_ground;
|
|
const before = sent.length;
|
|
player.tick();
|
|
if (in_air && player.on_ground) {
|
|
landings++;
|
|
assertEquals(sent.length, before + 1, "sent when it landed");
|
|
assertEquals(sent.at(-1)!.y, 0);
|
|
}
|
|
}
|
|
InputManager.keys_down.clear();
|
|
assert(landings >= 5, `landed ${landings} times`);
|
|
});
|
|
|
|
Deno.test("walking speeds up until friction evens it out, at minecraft's speeds", () => {
|
|
for (const [keys, speed] of [[[], 4.317], [["ControlLeft"], 5.612], [["ShiftLeft"], 1.295]] as const) {
|
|
const { player } = setup();
|
|
run(player, 1, () => false);
|
|
InputManager.keys_down.add("KeyW");
|
|
for (const key of keys) InputManager.keys_down.add(key);
|
|
player.tick();
|
|
assert(-player.vz < speed / 2, "doesn't start at full speed");
|
|
for (let tick = 0; tick < 40; tick++) player.tick();
|
|
const z = player.z;
|
|
player.tick();
|
|
InputManager.keys_down.clear();
|
|
assertAlmostEquals((z - player.z) * 20, speed, 0.01);
|
|
}
|
|
});
|
|
|
|
Deno.test("jumping while sprinting is faster, and turning while sprinting is slower", () => {
|
|
// average speed over 200 ticks holding w and ctrl, and maybe space, turning this much each tick
|
|
const average = (jump: boolean, turn: number) => {
|
|
const { player } = setup();
|
|
run(player, 1, () => false);
|
|
InputManager.keys_down.add("KeyW");
|
|
InputManager.keys_down.add("ControlLeft");
|
|
if (jump) InputManager.keys_down.add("Space");
|
|
let distance = 0;
|
|
for (let tick = 0; tick < 240; tick++) {
|
|
player.yaw += turn;
|
|
const { x, z } = player;
|
|
player.tick();
|
|
if (tick >= 40) distance += Math.hypot(player.x - x, player.z - z);
|
|
}
|
|
InputManager.keys_down.clear();
|
|
return distance / 10;
|
|
};
|
|
const sprinting = average(false, 0);
|
|
const jumping = average(true, 0);
|
|
assertAlmostEquals(sprinting, 5.612, 0.01);
|
|
assertAlmostEquals(jumping, 7.127, 0.05);
|
|
assert(average(false, 0.1) < sprinting - 0.05, "turning slows it down");
|
|
});
|
|
|
|
Deno.test("sprinting starts going forward and keeps going until it stops, sneaks, gets hungry or hits a wall", () => {
|
|
const keys = (...keys: string[]) => {
|
|
InputManager.keys_down.clear();
|
|
for (const key of keys) InputManager.keys_down.add(key);
|
|
};
|
|
const { player } = setup();
|
|
run(player, 1, () => false);
|
|
|
|
// not backwards or sideways
|
|
keys("KeyS", "ControlLeft");
|
|
player.tick();
|
|
keys("KeyA", "ControlLeft");
|
|
player.tick();
|
|
assert(!player.sprinting, "sprinting without going forward");
|
|
|
|
// letting go of the key keeps it, letting go of forward stops it
|
|
keys("KeyW", "ControlLeft");
|
|
player.tick();
|
|
keys("KeyW");
|
|
player.tick();
|
|
assert(player.sprinting, "letting go of sprint stopped it");
|
|
keys();
|
|
player.tick();
|
|
assert(!player.sprinting, "still sprinting after stopping");
|
|
|
|
// sneaking stops it
|
|
keys("KeyW", "ControlLeft");
|
|
player.tick();
|
|
keys("KeyW", "ControlLeft", "ShiftLeft");
|
|
player.tick();
|
|
assert(!player.sprinting, "sprinting while sneaking");
|
|
|
|
// too hungry to start, and getting hungry stops it
|
|
player.food = 6;
|
|
keys("KeyW", "ControlLeft");
|
|
player.tick();
|
|
assert(!player.sprinting, "sprinting while hungry");
|
|
player.food = 20;
|
|
player.tick();
|
|
assert(player.sprinting);
|
|
player.food = 6;
|
|
player.tick();
|
|
assert(!player.sprinting, "kept sprinting after getting hungry");
|
|
InputManager.keys_down.clear();
|
|
|
|
// running into a wall stops it, running along one doesn't
|
|
for (const [yaw, stops] of [[0, true], [Math.PI / 2, false]] as const) {
|
|
const { player } = setup();
|
|
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
|
|
// a wall on the -z side
|
|
level.has_collision = (_x, y, z) => y < 0 || z < -2;
|
|
player.set_position(0.5, 0, -1);
|
|
player.yaw = yaw;
|
|
run(player, 1, () => false);
|
|
keys("KeyW", "ControlLeft");
|
|
if (yaw !== 0) keys("KeyW", "ControlLeft", "KeyD");
|
|
for (let tick = 0; tick < 20; tick++) player.tick();
|
|
InputManager.keys_down.clear();
|
|
assertEquals(player.sprinting, !stops, `yaw ${yaw}`);
|
|
}
|
|
});
|
|
|
|
Deno.test("opening a screen while walking stops the player", () => {
|
|
const { player } = setup();
|
|
const client = player.client as unknown as { screen: unknown };
|
|
InputManager.keys_down.add("KeyW");
|
|
for (let tick = 0; tick < 5; tick++) player.tick();
|
|
client.screen = {};
|
|
// it slides a little before friction stops it
|
|
for (let tick = 0; tick < 20; tick++) player.tick();
|
|
const z = player.z;
|
|
for (let tick = 0; tick < 20; tick++) player.tick();
|
|
InputManager.keys_down.clear();
|
|
assertEquals(player.z, z);
|
|
assertEquals([player.vx, player.vz], [0, 0]);
|
|
});
|
|
|
|
Deno.test("sneaking doesn't walk off edges, from any direction, but still jumps off them", () => {
|
|
for (const [key, yaw] of [["KeyW", 0], ["KeyW", Math.PI / 4], ["KeyD", 0], ["KeyS", 1], ["KeyA", 2.5]] as const) {
|
|
// a 3x3 platform with nothing around it
|
|
const { player } = setup();
|
|
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
|
|
level.has_collision = (x, y, z) => y === -1 && x >= 0 && x < 3 && z >= 0 && z < 3;
|
|
player.set_position(1.5, 0, 1.5);
|
|
player.yaw = yaw;
|
|
run(player, 1, () => false);
|
|
InputManager.keys_down.add("ShiftLeft");
|
|
InputManager.keys_down.add(key);
|
|
for (let tick = 0; tick < 60; tick++) player.tick();
|
|
assertEquals(player.y, 0, `fell off with ${key} at yaw ${yaw}`);
|
|
assert(player.on_ground);
|
|
|
|
InputManager.keys_down.add("Space");
|
|
for (let tick = 0; tick < 60; tick++) player.tick();
|
|
InputManager.keys_down.clear();
|
|
assert(player.y < -1, "jumped off");
|
|
}
|
|
});
|
|
|
|
Deno.test("stepping from the edge of a block onto the one diagonal to it doesn't fall between them", () => {
|
|
// floor only at (0, 0) and (1, 1)
|
|
const { player } = setup();
|
|
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
|
|
level.has_collision = (x, y, z) => y === -1 && ((x === 0 && z === 0) || (x === 1 && z === 1));
|
|
// on the +x edge of the first block, hanging over it, then walking diagonally to the other
|
|
player.set_position(1, 0, 0.3);
|
|
run(player, 1, () => false);
|
|
player.yaw = -3 * Math.PI / 4;
|
|
InputManager.keys_down.add("KeyW");
|
|
for (let tick = 0; tick < 20 && !(player.x > 1.3 && player.z > 1.3); tick++) player.tick();
|
|
InputManager.keys_down.clear();
|
|
assert(player.x > 1.3 && player.z > 1.3, "got over the second block");
|
|
assertEquals(player.y, 0);
|
|
assert(player.on_ground);
|
|
});
|
|
|
|
// a pool from y 0 to water_top, on a floor at y -1, with a wall at z -1 from y 0 to wall_top, its face at z 0. water
|
|
// over the wall when it's lower
|
|
function pool(water_top: number, wall_top = 1) {
|
|
const wall = (y: number, z: number) => z <= -1 && y >= 0 && y < wall_top;
|
|
return {
|
|
solid: (_x: number, y: number, z: number) => y < 0 || wall(y, z),
|
|
water: (_x: number, y: number, z: number) => y >= 0 && y < water_top && !wall(y, z),
|
|
};
|
|
}
|
|
|
|
// how fast it's going in blocks a second, from its last tick
|
|
function speed_after(player: LocalPlayer, ticks: number, keys: string[]) {
|
|
for (const key of keys) InputManager.keys_down.add(key);
|
|
for (let tick = 0; tick < ticks; tick++) player.tick();
|
|
const { x, y, z } = player;
|
|
player.tick();
|
|
InputManager.keys_down.clear();
|
|
return { horizontal: Math.hypot(player.x - x, player.z - z) * 20, vertical: (player.y - y) * 20 };
|
|
}
|
|
|
|
Deno.test("in water it sinks slowly, walks slowly, swims up holding jump and down sneaking", () => {
|
|
const in_pool = () => {
|
|
const { player } = setup(Infinity, pool(10));
|
|
player.set_position(0.5, 2, 0.5);
|
|
return player;
|
|
};
|
|
assertAlmostEquals(speed_after(in_pool(), 60, []).vertical, -0.45, 0.01);
|
|
assertAlmostEquals(speed_after(in_pool(), 60, ["KeyW"]).horizontal, 2, 0.01);
|
|
|
|
const rising = in_pool();
|
|
rising.set_position(0.5, 0, 0.5);
|
|
assertAlmostEquals(speed_after(rising, 30, ["Space"]).vertical, 3.55, 0.01);
|
|
|
|
const sinking = in_pool();
|
|
sinking.set_position(0.5, 9, 0.5);
|
|
assertAlmostEquals(speed_after(sinking, 30, ["ShiftLeft"]).vertical, -4.45, 0.01);
|
|
// holding both, they cancel out and it sinks like holding neither
|
|
assertAlmostEquals(speed_after(in_pool(), 60, ["Space", "ShiftLeft"]).vertical, -0.45, 0.01);
|
|
});
|
|
|
|
Deno.test("swimming up against the edge of a pool hops out onto a ledge a block above the water", () => {
|
|
// swimming up alone only gets the feet a little above the water
|
|
const { player } = setup(Infinity, pool(3, 4));
|
|
player.set_position(0.5, 1, 0.5);
|
|
for (let tick = 0; tick < 60; tick++) speed_after(player, 0, ["KeyW", "Space"]);
|
|
assert(player.z < -0.5 && player.y >= 4, `ended at z ${player.z}, y ${player.y}`);
|
|
|
|
// not against a wall under water
|
|
const under = setup(Infinity, pool(10)).player;
|
|
under.set_position(0.5, 0, 0.5);
|
|
for (let tick = 0; tick < 60; tick++) speed_after(under, 0, ["KeyW"]);
|
|
assert(under.z > 0 && under.y < 1, `went over it to z ${under.z}, y ${under.y}`);
|
|
});
|
|
|
|
// holding these keys for that many ticks
|
|
function hold(player: LocalPlayer, ticks: number, keys: string[]) {
|
|
for (const key of keys) InputManager.keys_down.add(key);
|
|
// like ClientLevel.tick
|
|
for (let tick = 0; tick < ticks; tick++) {
|
|
player.save_previous_position();
|
|
player.tick();
|
|
}
|
|
InputManager.keys_down.clear();
|
|
}
|
|
|
|
const SWIM = ["KeyW", "ControlLeft"];
|
|
|
|
Deno.test("sprinting with the eyes under water swims, in the short box with low eyes, and tells the server right away", () => {
|
|
const { player, sent } = setup(Infinity, pool(10));
|
|
player.set_position(0.5, 3, 0.5);
|
|
hold(player, 2, SWIM);
|
|
assert(player.swimming);
|
|
assertEquals(player.height, 0.8);
|
|
assertEquals(sent.at(-1)?.swimming, true);
|
|
hold(player, 10, SWIM);
|
|
assertAlmostEquals(player.eye_height, 0.6, 0.01);
|
|
|
|
// level, it doesn't sink, and it's faster than walking in water
|
|
const y = player.y;
|
|
assertAlmostEquals(speed_after(player, 80, SWIM).horizontal, 4, 0.01);
|
|
assertAlmostEquals(player.y, y, 0.01);
|
|
|
|
// letting go stands it back up, there's room
|
|
hold(player, 1, []);
|
|
assertFalse(player.swimming);
|
|
assertEquals(player.height, 1.79);
|
|
assertEquals(sent.at(-1)?.swimming, undefined);
|
|
});
|
|
|
|
Deno.test("it tips over to swim and stands back up over 10 ticks, lying the way it's going", () => {
|
|
const { player } = setup(Infinity, pool(10));
|
|
player.set_position(0.5, 5, 0.5);
|
|
player.pitch = -0.5;
|
|
hold(player, 6, SWIM);
|
|
assert(player.swim_amount > 0 && player.swim_amount < 1, `at ${player.swim_amount}`);
|
|
hold(player, 30, SWIM);
|
|
assertEquals(player.swim_amount, 1);
|
|
// diving, the body points down
|
|
assert(player.swim_pitch < -0.2, `pitch ${player.swim_pitch}`);
|
|
// still, it lies the way it looks
|
|
player.pitch = 0.3;
|
|
hold(player, 100, []);
|
|
assertEquals(player.swim_amount, 0);
|
|
assertAlmostEquals(player.swim_pitch, 0.3, 0.01);
|
|
});
|
|
|
|
Deno.test("swimming goes where it looks, down and up, but stays in the water at the surface", () => {
|
|
const diving = setup(Infinity, pool(10)).player;
|
|
diving.set_position(0.5, 5, 0.5);
|
|
diving.pitch = -0.5;
|
|
hold(diving, 30, SWIM);
|
|
assert(diving.swimming && diving.y < 3, `at y ${diving.y}`);
|
|
|
|
// water up to y 4: it comes up to the surface and swims along it with its head out, its feet in
|
|
const rising = setup(Infinity, pool(4)).player;
|
|
rising.set_position(0.5, 1, 0.5);
|
|
rising.pitch = 0.5;
|
|
hold(rising, 100, SWIM);
|
|
assert(rising.swimming && rising.in_water, "still swimming in the water");
|
|
assert(rising.y > 3 && rising.y < 4, `at y ${rising.y}`);
|
|
});
|
|
|
|
Deno.test("swimming fits through a 1 block gap, and stays lying down in it after letting go", () => {
|
|
// water up to y 3, and from z 0 on a ceiling at y 1, so the water there is a tunnel 1 block high
|
|
const { player } = setup(Infinity, {
|
|
solid: (_x, y, z) => y < 0 || (z < 0 && y >= 1),
|
|
water: (_x, y, z) => y >= 0 && (z < 0 ? y < 1 : y < 3),
|
|
});
|
|
player.set_position(0.5, 0, 0.5);
|
|
hold(player, 40, SWIM);
|
|
assert(player.z < -2, `stopped at z ${player.z}`);
|
|
hold(player, 10, []);
|
|
assert(player.swimming);
|
|
assertEquals(player.height, 0.8);
|
|
});
|
|
|
|
Deno.test("wading with the eyes out of the water doesn't start sprinting, so it doesn't swim", () => {
|
|
const { player } = setup(Infinity, pool(1));
|
|
hold(player, 20, SWIM);
|
|
assertFalse(player.sprinting);
|
|
assertFalse(player.swimming);
|
|
});
|