Files
tidewall/test/sim/test_movement.gd
claudeandClaude Opus 5.5 5021d13686 W4: Points of interest
PoiSet.generate(island) places 10 to 16 points of interest per island from
the seed's "poi" stream: hilltop shrines, lighthouses, docks, salt works,
quarries and fishing hamlets, at least 85 m apart. Each kind has a siting
rule (height band, flatness, coast or summit) that loosens step by step
until its minimum is placed. Piers are sited so their deck clears the
ground all the way out to water 1.5 m deep.

Poi assembles each from modular parts in a local frame turned in quarter
turns, so every solid stays an axis-aligned box: walls with a door and
lintel, plinths, steps down to the ground, roofs, a stair to a loft. Each
POI carries loot spawn markers, exposed through PoiSet.loot_markers(),
and walking routes from open ground into its interiors.

- Simulation adds every POI solid to its ground and never spawns players
  inside one.
- SimGround indexes solids in 8 m cells, so collision queries only test
  nearby boxes. Smoke p95 tick is 0.47 ms with about 300 solids.
- Movement tests the body where it will stand after stepping up, so
  stairs with treads narrower than the body can be climbed. The
  traversal test found the lighthouse stair impassable without it.
- PoiView draws parts as one MultiMesh per look, in the seafaring
  palette. Capture scenarios poi_<kind> frame each kind.
- Summit stone is darker so it no longer reads as snow.

Tests: 30 seeds get every kind within its limits, 10 to 16 in total,
spaced and sited; placement is deterministic; loot markers stand on
floors and are not inside solids; a player walks every route of every
POI on 4 seeds through doors, up steps and the lighthouse stair; stairs
with 0.45 m treads are climbed and a step doesn't carry the body into a
wall; the solid index matches a full scan on 500 queries.

Closes #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:47:10 -05:00

303 lines
13 KiB
GDScript

extends TestCase
## Yaw that faces +x, since forward is (sin yaw, 0, cos yaw).
const EAST: float = PI * 0.5
const NORTH: float = 0.0
var _tick: int = 0
func before_each() -> void:
_tick = 0
func _player(at: Vector3, on_ground: bool = true) -> SimEntity:
var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER)
player.position = at
player.health = Tuning.PLAYER_MAX_HEALTH
player.on_ground = on_ground
return player
func _command(move: Vector2, yaw: float, buttons: int = 0) -> PlayerCommand:
var command: PlayerCommand = PlayerCommand.from_input(_tick, move, yaw, 0.0, buttons)
_tick += 1
return command
func _run(player: SimEntity, ground: SimGround, ticks: int, move: Vector2, yaw: float, buttons: int = 0) -> void:
for _index: int in ticks:
Movement.step(player, _command(move, yaw, buttons), ground)
## A heightfield that is flat up to x = `start`, then rises at `degrees` along +x.
func _ramp(degrees: float, start: float = 10.0) -> SimGround:
var side: int = 64
var values: PackedFloat32Array = PackedFloat32Array()
values.resize(side * side)
var rise: float = tan(deg_to_rad(degrees))
for z: int in side:
for x: int in side:
values[z * side + x] = maxf(float(x) - start, 0.0) * rise
return SimGround.from_heights(values, side, 1.0)
func test_walk_sprint_and_crouch_speeds() -> void:
var ground: SimGround = SimGround.flat()
var cases: Array[Array] = [
[0, Tuning.WALK_SPEED],
[PlayerCommand.BUTTON_SPRINT, Tuning.SPRINT_SPEED],
[PlayerCommand.BUTTON_CROUCH, Tuning.CROUCH_SPEED],
[PlayerCommand.BUTTON_CROUCH | PlayerCommand.BUTTON_SPRINT, Tuning.CROUCH_SPEED],
]
for entry: Array in cases:
var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
_run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), EAST, entry[0])
assert_near(player.position.x - 100.0, entry[1], 0.01, "buttons %d" % entry[0])
assert_near(player.position.z, 100.0, 0.001)
assert_eq(player.position.y, 0.0)
func test_sprint_only_applies_moving_forward() -> void:
var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
_run(player, SimGround.flat(), Tuning.TICK_RATE_HZ, Vector2(1.0, 0.0), NORTH, PlayerCommand.BUTTON_SPRINT)
assert_near(player.position.distance_to(Vector3(100.0, 0.0, 100.0)), Tuning.WALK_SPEED, 0.01)
func test_diagonal_is_no_faster_than_straight() -> void:
var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
_run(player, SimGround.flat(), Tuning.TICK_RATE_HZ, Vector2(1.0, 1.0), NORTH)
assert_near(player.position.distance_to(Vector3(100.0, 0.0, 100.0)), Tuning.WALK_SPEED, 0.05)
func test_jump_height_and_airtime() -> void:
var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
var ground: SimGround = SimGround.flat()
Movement.step(player, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground)
assert_false(player.on_ground)
var peak: float = player.position.y
var ticks_in_air: int = 1
while not player.on_ground and ticks_in_air < 200:
Movement.step(player, _command(Vector2.ZERO, NORTH), ground)
peak = maxf(peak, player.position.y)
ticks_in_air += 1
var expected_peak: float = Tuning.JUMP_VELOCITY * Tuning.JUMP_VELOCITY / (2.0 * Tuning.GRAVITY)
assert_near(peak, expected_peak, 0.01, "jump height")
var expected_ticks: float = 2.0 * Tuning.JUMP_VELOCITY / Tuning.GRAVITY * Tuning.TICK_RATE_HZ
assert_near(ticks_in_air, expected_ticks, 1.5, "time in the air")
assert_eq(player.position.y, 0.0)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "a jump on flat ground must not hurt")
func test_no_jump_while_crouched_or_in_the_air() -> void:
var ground: SimGround = SimGround.flat()
var crouched: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
Movement.step(crouched, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP | PlayerCommand.BUTTON_CROUCH), ground)
assert_true(crouched.on_ground)
assert_eq(crouched.position.y, 0.0)
var jumper: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
Movement.step(jumper, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground)
var rising: float = jumper.velocity.y
Movement.step(jumper, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground)
assert_true(jumper.velocity.y < rising, "a second jump in the air must not add speed")
func test_without_a_command_an_airborne_player_still_falls() -> void:
var player: SimEntity = _player(Vector3(100.0, 3.0, 100.0), false)
for _index: int in 30:
Movement.step(player, null, SimGround.flat())
assert_true(player.on_ground)
assert_eq(player.position.y, 0.0)
func test_walks_up_a_gentle_slope() -> void:
var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(player, _ramp(30.0), Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST)
assert_true(player.position.x > 20.0, "should have climbed well up the slope, at x %f" % player.position.x)
assert_near(player.position.y, (player.position.x - 10.0) * tan(deg_to_rad(30.0)), 0.05)
assert_true(player.on_ground)
func test_walks_down_a_gentle_slope_without_leaving_the_ground() -> void:
var player: SimEntity = _player(Vector3(30.0, 20.0 * tan(deg_to_rad(30.0)), 20.0))
for _index: int in Tuning.TICK_RATE_HZ * 4:
Movement.step(player, _command(Vector2(0.0, 1.0), -EAST), _ramp(30.0))
assert_true(player.on_ground, "lost the ground at x %f" % player.position.x)
assert_true(player.position.x < 15.0)
func test_too_steep_a_slope_blocks_walking_up() -> void:
var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(player, _ramp(60.0), Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_SPRINT)
assert_true(player.position.x < 11.0, "climbed a 60 degree slope to x %f" % player.position.x)
assert_true(player.position.y < 1.0)
func test_cannot_climb_a_steep_slope_by_jumping_into_it() -> void:
var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
var ground: SimGround = _ramp(60.0)
for _index: int in Tuning.TICK_RATE_HZ * 6:
Movement.step(player, _command(Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_JUMP), ground)
assert_true(player.position.y < 3.0, "jumped up a cliff to height %f" % player.position.y)
func test_slides_down_ground_too_steep_to_stand_on() -> void:
var ground: SimGround = _ramp(60.0)
var x: float = 20.0
var player: SimEntity = _player(Vector3(x, ground.terrain_height(x, 20.0), 20.0))
for _index: int in Tuning.TICK_RATE_HZ * 5:
Movement.step(player, null, ground)
assert_true(player.position.x < 10.5, "should have slid to the foot, at x %f" % player.position.x)
assert_true(player.on_ground)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "a capped slide must not hurt")
func test_steps_up_low_ledges_but_not_high_ones() -> void:
var low: SimGround = SimGround.flat()
low.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, Tuning.STEP_HEIGHT - 0.05, 40.0)))
var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(player, low, Tuning.TICK_RATE_HZ * 2, Vector2(0.0, 1.0), EAST)
assert_true(player.position.x > 10.0)
assert_near(player.position.y, Tuning.STEP_HEIGHT - 0.05, 0.001)
var high: SimGround = SimGround.flat()
high.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, 1.0, 40.0)))
var blocked: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(blocked, high, Tuning.TICK_RATE_HZ * 2, Vector2(0.0, 1.0), EAST)
assert_near(blocked.position.x, 10.0 - Tuning.PLAYER_RADIUS, 0.15)
assert_eq(blocked.position.y, 0.0)
func test_jumps_onto_a_ledge_too_high_to_step() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, 1.0, 40.0)))
var player: SimEntity = _player(Vector3(8.0, 0.0, 20.0))
_run(player, ground, 1, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_JUMP)
_run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), EAST)
assert_true(player.position.x > 10.0)
assert_near(player.position.y, 1.0, 0.001)
func test_slides_along_a_wall() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(1.0, 3.0, 100.0)))
var player: SimEntity = _player(Vector3(9.0, 0.0, 20.0))
# North-east, into the wall: x stops, z keeps going.
_run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), PI * 0.25)
assert_true(player.position.x <= 10.0 - Tuning.PLAYER_RADIUS + 0.001)
assert_true(player.position.z > 22.0)
func test_ceiling_stops_a_jump() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(95.0, 2.2, 95.0), Vector3(10.0, 0.5, 10.0)))
var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0))
var top: float = 0.0
for _index: int in 40:
Movement.step(player, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground)
top = maxf(top, player.position.y + Tuning.PLAYER_HEIGHT)
assert_true(top <= 2.2 + 0.001, "head reached %f, through the ceiling" % top)
func test_crouching_fits_under_a_low_gap() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(10.0, 1.5, 0.0), Vector3(4.0, 1.0, 40.0)))
var standing: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(standing, ground, Tuning.TICK_RATE_HZ * 3, Vector2(0.0, 1.0), EAST)
assert_true(standing.position.x < 10.0)
var crouched: SimEntity = _player(Vector3(5.0, 0.0, 20.0))
_run(crouched, ground, Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_CROUCH)
assert_true(crouched.position.x > 14.0)
func test_fall_damage() -> void:
var cases: Array[Array] = [[3.0, 0], [6.0, 0], [10.0, 32], [16.0, 80]]
for entry: Array in cases:
var player: SimEntity = _player(Vector3(100.0, entry[0], 100.0), false)
for _index: int in 90:
Movement.step(player, null, SimGround.flat())
var damage: int = Tuning.PLAYER_MAX_HEALTH - player.health
assert_near(damage, entry[1], 3.0, "drop of %f m" % entry[0])
assert_true(player.on_ground)
func test_fall_damage_never_goes_below_zero_health() -> void:
var player: SimEntity = _player(Vector3(100.0, 80.0, 100.0), false)
for _index: int in 200:
Movement.step(player, null, SimGround.flat())
assert_eq(player.health, 0)
func test_stands_on_a_solid_and_falls_off_its_edge() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(10.0, 3.0, 40.0)))
var player: SimEntity = _player(Vector3(5.0, 3.0, 20.0))
_run(player, ground, Tuning.TICK_RATE_HZ * 3, Vector2(0.0, 1.0), EAST)
assert_true(player.position.x > 10.0)
assert_eq(player.position.y, 0.0)
assert_true(player.on_ground)
func test_same_commands_on_rough_ground_are_deterministic() -> void:
var first: Simulation = _rough_simulation()
var second: Simulation = _rough_simulation()
var random: RandomNumberGenerator = RandomNumberGenerator.new()
random.seed = 77
for at_tick: int in 1500:
for id: int in [1, 2, 3, 4]:
var command: PlayerCommand = PlayerCommand.new()
command.tick = at_tick
command.move_x = random.randi_range(-127, 127)
command.move_y = random.randi_range(-127, 127)
command.yaw = random.randi_range(0, 65535)
command.buttons = random.randi_range(0, 31)
first.submit_command(id, command)
second.submit_command(id, command)
first.step()
second.step()
assert_eq(first.state_hash(), second.state_hash())
func _rough_simulation() -> Simulation:
var sim: Simulation = Simulation.new(4242, SimGround.flat())
var side: int = 65
var values: PackedFloat32Array = PackedFloat32Array()
values.resize(side * side)
for z: int in side:
for x: int in side:
values[z * side + x] = 6.0 * sin(x * 0.31) * cos(z * 0.23) + 0.08 * x
sim.ground = SimGround.from_heights(values, side, 16.0)
for index: int in 12:
sim.ground.add_solid(AABB(Vector3(index * 70.0, 0.0, 500.0), Vector3(4.0, 3.0 + index, 4.0)))
for _index: int in 4:
sim.add_player()
return sim
func test_climbs_stairs_with_treads_narrower_than_the_body() -> void:
# Treads of 0.45 m are narrower than the 0.8 m body, so the next riser is under the
# body's front edge before its centre is lifted onto the current step. (Treads
# narrower than the 0.4 m radius can't be climbed: the second riser is reached
# before the centre is on the first.)
var ground: SimGround = SimGround.flat()
for index: int in 12:
ground.add_solid(AABB(Vector3(10.0 + index * 0.45, 0.0, 8.0), Vector3(0.45, 0.35 * (index + 1), 4.0)))
# A landing at the top, so the player stays up there.
ground.add_solid(AABB(Vector3(15.4, 0.0, 8.0), Vector3(20.0, 0.35 * 12, 4.0)))
var player: SimEntity = _player(Vector3(8.0, 0.0, 10.0))
_run(player, ground, 90, Vector2(0.0, 1.0), EAST)
assert_near(player.position.y, 0.35 * 12, 0.01, "should stand on the top step")
assert_true(player.position.x > 15.0, "should have walked up the flight")
func test_a_step_does_not_let_the_body_through_a_wall_behind_it() -> void:
# A climbable 0.4 m step with a 1.5 m wall right behind it: stepping up must not
# carry the body into the wall.
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(10.0, 0.0, 8.0), Vector3(0.5, 0.4, 4.0)))
ground.add_solid(AABB(Vector3(10.5, 0.0, 8.0), Vector3(1.0, 1.5, 4.0)))
var player: SimEntity = _player(Vector3(8.0, 0.0, 10.0))
_run(player, ground, 90, Vector2(0.0, 1.0), EAST)
assert_true(player.position.x < 10.5 - Tuning.PLAYER_RADIUS + 0.01, "walked into the wall to x %.2f" % player.position.x)