import { assert, assertEquals } 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"; // a player on flat ground at y 0, maybe with a ceiling, and the positions it sends function setup(ceiling = Infinity) { const sent: { y: number }[] = []; const client = { level: { has_collision: (_x: number, y: number) => y < 0 || y >= ceiling }, screen: undefined, options: new Options(), connection: { send: (message: { type: string; y: number }) => 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("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 = {}; 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); });