This commit is contained in:
2026-10-02 11:07:09 -03:00
parent d5015edfa1
commit 9ca717d2d0
18 changed files with 449 additions and 38 deletions
+97 -7
View File
@@ -1,4 +1,4 @@
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
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";
@@ -9,7 +9,7 @@ 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 }[] = [];
const sent: { y: number; swimming?: boolean }[] = [];
const client = {
level: {
has_collision: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
@@ -17,7 +17,10 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
},
screen: undefined,
options: new Options(),
connection: { send: (message: { type: string; y: number }) => message.type === "move" && sent.push(message) },
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);
@@ -275,16 +278,14 @@ function speed_after(player: LocalPlayer, ticks: number, keys: string[]) {
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, sprints no faster, swims up holding jump and down sneaking", () => {
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);
const walking = speed_after(in_pool(), 60, ["KeyW"]).horizontal;
assertAlmostEquals(walking, 2, 0.01);
assertAlmostEquals(speed_after(in_pool(), 60, ["KeyW", "ControlLeft"]).horizontal, walking, 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);
@@ -310,3 +311,92 @@ Deno.test("swimming up against the edge of a pool hops out onto a ledge a block
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);
});
+101
View File
@@ -0,0 +1,101 @@
// swimming as the server sees it: each player's client says when they're swimming, which gives them the short box
// and low eyes, and tells everyone else. how they swim is the client's, see local_player_test.ts
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, PLAYER_SWIM_HEIGHT } from "$/common/constants.ts";
import { part_point, PARTS, type PlayerPose, pose_at, pose_to_world } from "$/client/rendering/player_model.ts";
import { test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
Deno.test("a player's client says when they swim, which shortens their box and tells everyone else", async () => {
const t = await test_game("mods");
t.join(1, "alice");
t.join(2, "bob");
const alice = t.game.players().find((player) => player.name === "alice")!;
t.take(2);
const api = t.game.mods.player(alice);
assertEquals(api.is_swimming(), false);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0, swimming: true });
assert(alice.swimming);
assert(api.is_swimming());
assertEquals(alice.height, PLAYER_SWIM_HEIGHT);
assertEquals(alice.eye_height, PLAYER_SWIM_EYE_HEIGHT);
const moved = t.take(2).findLast((m: Msg) => m.type === "player_move" && m.id === alice.id);
assertEquals(moved.swimming, true);
// someone joining now sees her swimming
const { players } = t.join(3, "carol");
assertEquals(players.find((player: Msg) => player.id === alice.id)?.swimming, true);
// a move without it stands her back up
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
assertEquals(api.is_swimming(), false);
assertEquals(alice.height, PLAYER_HEIGHT);
assertEquals(alice.eye_height, PLAYER_EYE_HEIGHT);
const stood = t.take(2).findLast((m: Msg) => m.type === "player_move" && m.id === alice.id);
assertEquals(stood.swimming, undefined);
});
// how a swimming player is drawn, see client/rendering/player_model.ts
const pose = (swim?: { amount: number; pitch: number }, pitch = 0, position = 0) =>
pose_at({ x: 0, y: 0, z: 0, yaw: 0, pitch, slim: false, walk: { position, speed: 1 }, swim });
const world = (p: PlayerPose, name: string, x: number, y: number, z: number) => {
const part = PARTS.find((part) => part.name === name)!;
return pose_to_world(p)(...part_point(p, part, x, y, z));
};
Deno.test("not swimming at all, the pose is the walking one", () => {
for (const position of [0, 1, 2.5]) {
const walking = pose(undefined, 0.3, position);
const still = pose({ amount: 0, pitch: -1 }, 0.3, position);
assertEquals(still, walking);
for (const part of PARTS) {
assertEquals(world(still, part.name, 1, 2, 3), world(walking, part.name, 1, 2, 3));
}
}
});
Deno.test("swimming, the body lies flat in the short box, its head forward and facing where they look", () => {
const flat = pose({ amount: 1, pitch: 0 });
// the neck is ahead (the model faces -z) and the feet behind
const [, head_y, head_z] = world(flat, "head", 0, 0, 0);
const [, feet_y, feet_z] = world(flat, "right_leg", 0, -12, 0);
assert(head_z < -0.4 && feet_z > 0.8, `head at ${head_z}, feet at ${feet_z}`);
// the body is inside the box
for (const [x, y, z] of [[-4, 0, -2], [4, 12, 2], [-4, 12, -2], [4, 0, 2]]) {
const [, by] = world(flat, "body", x, y, z);
assert(by >= 0 && by <= PLAYER_SWIM_HEIGHT, `body at ${by}`);
}
assert(Math.abs(head_y - PLAYER_SWIM_HEIGHT / 2) < 0.2 && Math.abs(feet_y - PLAYER_SWIM_HEIGHT / 2) < 0.2);
// the face points where they look, whichever way the body lies
for (const [moving, look] of [[0, 0], [0, -0.5], [-0.8, -0.8], [0.4, 0.2]]) {
const p = pose({ amount: 1, pitch: moving }, look);
const [, back_y, back_z] = world(p, "head", 0, 4, 4);
const [, front_y, front_z] = world(p, "head", 0, 4, -4);
assertAlmostEquals(Math.atan2(front_y - back_y, back_z - front_z), look, 1e-9);
}
// diving, the head goes down
const [, diving_y] = world(pose({ amount: 1, pitch: -1 }), "head", 0, 8, 0);
assert(diving_y < head_y);
});
Deno.test("swimming, the arms go around half a turn apart and the legs flutter", () => {
const arm_tip = (p: PlayerPose, name: string) => world(p, name, 1, -10, 0);
// a whole stroke
const strokes = [0, 3, 6, 9, 12, 15, 18].map((position) => pose({ amount: 1, pitch: 0 }, 0, position));
const right = strokes.map((p) => arm_tip(p, "right_arm"));
// it goes both above and below the body over a stroke
assert(right.some(([, y]) => y > PLAYER_SWIM_HEIGHT) && right.some(([, y]) => y < 0.2));
// the other arm is on the other side
const [, right_y] = arm_tip(strokes[1], "right_arm");
const [, left_y] = arm_tip(strokes[1], "left_arm");
assert(Math.sign(right_y - 0.4) !== Math.sign(left_y - 0.4), `arms at ${right_y}, ${left_y}`);
// legs opposite each other
const [, right_leg] = world(strokes[0], "right_leg", 0, -12, 0);
const [, left_leg] = world(strokes[0], "left_leg", 0, -12, 0);
assertNotEquals(right_leg, left_leg);
});