W6: Water and swimming

The simulation has a global sea level (Simulation.sea_level, changed with
set_sea_level and read every tick, so the tide can move it mid-match).
Walkers wade more slowly in water over WADE_DEPTH; in water SWIM_DEPTH deep
they swim: floating head-out, sprint-swimming, diving with crouch, kicking
out with jump, and standing again in the shallows. Falling into deep water
does no fall damage. Water tracks breath, stamina and drowning in whole
ticks: swimming spends stamina, an exhausted swimmer slips under, and with
no breath left the tide phase's drown damage lands once a second. All of
it, plus the sea level, is in the state hash.

WaterView draws the sea surface at the simulation's level with a
procedural shader: depth-tinted colour, foam at the waterline, ripples.

Closes #13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 12:24:50 -05:00
co-authored by Claude Opus 5.5
parent 5db53de4e8
commit be987b6f82
18 changed files with 641 additions and 10 deletions
+231
View File
@@ -0,0 +1,231 @@
extends TestCase
## Water and swimming from issue W6: wading, swimming, stamina, breath, drowning, and a
## sea level that can change mid-match.
const EAST: float = PI * 0.5
const WEST: float = -PI * 0.5
const SECOND: int = Tuning.TICK_RATE_HZ
var _tick: int = 0
func before_each() -> void:
_tick = 0
## Flat ground at height 0 for x < 20, sloping down at 1 in 4 to -10 at x = 60, flat beyond.
func _beach() -> SimGround:
var side: int = 128
var values: PackedFloat32Array = PackedFloat32Array()
values.resize(side * side)
for z: int in side:
for x: int in side:
values[z * side + x] = -clampf((float(x) - 20.0) * 0.25, 0.0, 10.0)
return SimGround.from_heights(values, side, 1.0)
func _player(at: Vector3) -> SimEntity:
var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER)
player.position = at
player.health = Tuning.PLAYER_MAX_HEALTH
player.on_ground = true
Water.reset(player)
return player
func _run(player: SimEntity, ground: SimGround, sea: float, ticks: int, move: Vector2, yaw: float, buttons: int = 0, drown_dps: int = 5) -> void:
for _index: int in ticks:
var command: PlayerCommand = PlayerCommand.from_input(_tick, move, yaw, 0.0, buttons)
_tick += 1
Movement.step(player, command, ground, sea)
Water.update(player, sea, drown_dps, command.is_pressed(PlayerCommand.BUTTON_SPRINT))
func test_wading_slows_walkers_and_stops_sprinting() -> void:
var ground: SimGround = _beach()
# Sea 0.8 m over the flat part of the beach: deeper than WADE_DEPTH, too shallow to swim.
var player: SimEntity = _player(Vector3(5.0, 0.0, 64.0))
_run(player, ground, 0.8, SECOND, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_SPRINT)
assert_false(player.swimming)
assert_true(player.on_ground)
assert_near(player.position.x - 5.0, Tuning.WADE_SPEED, 0.01)
# Ankle-deep water doesn't slow anyone.
var dry: SimEntity = _player(Vector3(5.0, 0.0, 70.0))
_run(dry, ground, Tuning.WADE_DEPTH - 0.1, SECOND, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_SPRINT)
assert_near(dry.position.x - 5.0, Tuning.SPRINT_SPEED, 0.01)
func test_walking_into_the_sea_swims_and_walking_out_stands_again() -> void:
var ground: SimGround = _beach()
var player: SimEntity = _player(Vector3(10.0, 0.0, 64.0))
_run(player, ground, -2.0, SECOND, Vector2(0.0, 1.0), EAST)
assert_near(player.position.x, 10.0 + Tuning.WALK_SPEED, 0.01)
var reached_swim: bool = false
for _tick: int in SECOND * 15:
_run(player, ground, -2.0, 1, Vector2(0.0, 1.0), EAST)
if player.swimming:
reached_swim = true
break
# The floor drops 1 in 4, so swimming starts where it is SWIM_DEPTH under the sea.
var expected_x: float = 20.0 + (2.0 + Tuning.SWIM_DEPTH) * 4.0
assert_near(player.position.x, expected_x, 0.3)
assert_true(reached_swim, "never started swimming, at %s" % player.position)
assert_false(player.on_ground)
# Floating: feet SWIM_FLOAT_DEPTH under the surface, head out.
_run(player, ground, -2.0, SECOND * 2, Vector2(0.0, 1.0), EAST)
assert_near(player.position.y, -2.0 - Tuning.SWIM_FLOAT_DEPTH, 0.05)
assert_false(Water.head_under(player, -2.0))
var x0: float = player.position.x
_run(player, ground, -2.0, SECOND, Vector2(0.0, 1.0), EAST)
assert_near(player.position.x - x0, Tuning.SWIM_SPEED, 0.05)
# Back to shore: standing again once the water is shallow.
_run(player, ground, -2.0, SECOND * 15, Vector2(0.0, 1.0), WEST)
assert_false(player.swimming, "still swimming at %s" % player.position)
assert_true(player.on_ground)
assert_true(player.position.x < 20.0)
assert_near(player.position.y, ground.terrain_height(player.position.x, 64.0), 0.01)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH)
func test_swimming_drains_stamina_and_exhaustion_drowns() -> void:
var ground: SimGround = _beach()
var sea: float = 0.0
var player: SimEntity = _player(Vector3(100.0, -10.0, 64.0))
_run(player, ground, sea, 2, Vector2.ZERO, EAST)
assert_true(player.swimming)
var full: int = Water.max_stamina_ticks()
# Sprint-swimming costs more than paddling.
_run(player, ground, sea, SECOND, Vector2(0.0, 1.0), WEST, PlayerCommand.BUTTON_SPRINT)
var after_sprint: int = player.stamina_ticks
assert_eq(full - after_sprint, (SECOND + 2) * Tuning.SWIM_SPRINT_STAMINA_COST - 2 * (Tuning.SWIM_SPRINT_STAMINA_COST - 1))
_run(player, ground, sea, SECOND, Vector2(0.0, 1.0), EAST)
assert_eq(after_sprint - player.stamina_ticks, SECOND)
# Tread water until exhausted: then the swimmer slips under and loses breath.
_run(player, ground, sea, player.stamina_ticks, Vector2.ZERO, EAST)
assert_eq(player.stamina_ticks, 0)
_run(player, ground, sea, SECOND * 2, Vector2.ZERO, EAST)
assert_true(Water.head_under(player, sea), "an exhausted swimmer should go under, feet at %.2f" % player.position.y)
assert_true(player.breath_ticks < Water.max_breath_ticks())
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "no damage while breath lasts")
# Breath runs out, then drown damage lands once a second.
_run(player, ground, sea, player.breath_ticks, Vector2.ZERO, EAST)
assert_eq(player.breath_ticks, 0)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH)
_run(player, ground, sea, SECOND * 3, Vector2.ZERO, EAST, 0, 7)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH - 21)
# Drowning continues to death and stops there.
_run(player, ground, sea, SECOND * 30, Vector2.ZERO, EAST, 0, 7)
assert_eq(player.health, 0)
func test_diving_costs_breath_and_surfacing_restores_it() -> void:
var ground: SimGround = _beach()
var player: SimEntity = _player(Vector3(100.0, -10.0, 64.0))
# Rise from the sea floor to the surface first.
_run(player, ground, 0.0, SECOND * 8, Vector2.ZERO, EAST)
assert_eq(player.breath_ticks, Water.max_breath_ticks())
_run(player, ground, 0.0, SECOND * 2, Vector2.ZERO, EAST, PlayerCommand.BUTTON_CROUCH)
assert_true(Water.head_under(player, 0.0), "diving should take the head under")
assert_true(player.position.y < -Tuning.SWIM_FLOAT_DEPTH - 1.0)
var low: int = player.breath_ticks
assert_true(low < Water.max_breath_ticks())
_run(player, ground, 0.0, SECOND * 3, Vector2.ZERO, EAST)
assert_false(Water.head_under(player, 0.0))
assert_true(player.breath_ticks > low)
_run(player, ground, 0.0, SECOND * 10, Vector2.ZERO, EAST)
assert_eq(player.breath_ticks, Water.max_breath_ticks())
# Divers rest on the sea floor rather than sinking through it.
_run(player, ground, 0.0, SECOND * 10, Vector2.ZERO, EAST, PlayerCommand.BUTTON_CROUCH)
assert_true(player.position.y >= ground.terrain_height(100.0, 64.0) - 0.001)
func test_falling_into_deep_water_does_no_fall_damage() -> void:
var ground: SimGround = _beach()
var player: SimEntity = _player(Vector3(100.0, 40.0, 64.0))
player.on_ground = false
_run(player, ground, 0.0, SECOND * 4, Vector2.ZERO, EAST)
assert_true(player.swimming)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH)
# The same fall onto dry ground hurts.
var dry: SimEntity = _player(Vector3(5.0, 40.0, 64.0))
dry.on_ground = false
_run(dry, ground, 0.0, SECOND * 4, Vector2.ZERO, EAST)
assert_true(dry.health < Tuning.PLAYER_MAX_HEALTH)
func test_jumping_from_the_water_leaves_it_and_lands_back_in() -> void:
var ground: SimGround = _beach()
var player: SimEntity = _player(Vector3(100.0, -10.0, 64.0))
_run(player, ground, 0.0, SECOND * 8, Vector2.ZERO, EAST)
var float_y: float = player.position.y
_run(player, ground, 0.0, 1, Vector2.ZERO, EAST, PlayerCommand.BUTTON_JUMP)
assert_false(player.swimming)
var peak: float = player.position.y
for _i: int in SECOND:
_run(player, ground, 0.0, 1, Vector2.ZERO, EAST)
peak = maxf(peak, player.position.y)
assert_true(peak > float_y + 1.0, "jumped only to %.2f from %.2f" % [peak, float_y])
_run(player, ground, 0.0, SECOND, Vector2.ZERO, EAST)
assert_true(player.swimming)
assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH)
# No jumping out with no stamina left.
player.stamina_ticks = 0
_run(player, ground, 0.0, 1, Vector2.ZERO, EAST, PlayerCommand.BUTTON_JUMP)
assert_true(player.swimming)
func test_raising_the_sea_takes_effect_on_the_next_tick() -> void:
var sim: Simulation = Simulation.new(1, _beach())
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(10.0, 0.0, 64.0)
player.on_ground = true
assert_eq(sim.sea_level, Tuning.SEA_LEVEL_START)
sim.step()
assert_false(player.swimming)
sim.set_sea_level(3.0)
sim.step()
assert_true(player.swimming, "the sea rose over the player's head but they aren't swimming")
for _i: int in SECOND * 3:
sim.step()
assert_near(player.position.y, 3.0 - Tuning.SWIM_FLOAT_DEPTH, 0.05)
assert_eq(player.breath_ticks, Water.max_breath_ticks(), "a swimmer floating at the surface breathes again")
# Wading depth slows walking at once.
sim.set_sea_level(0.9)
for _i: int in SECOND * 2:
sim.step()
assert_false(player.swimming)
assert_true(player.on_ground)
var x0: float = player.position.x
for _i: int in SECOND:
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), WEST, 0.0, PlayerCommand.BUTTON_SPRINT))
sim.step()
assert_near(x0 - player.position.x, Tuning.WADE_SPEED, 0.05)
# Dropping the sea leaves a dry player walking at full pace.
sim.set_sea_level(-5.0)
x0 = player.position.x
for _i: int in SECOND:
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), WEST, 0.0, 0))
sim.step()
assert_near(x0 - player.position.x, Tuning.WALK_SPEED, 0.05)
func test_water_state_is_hashed_and_deterministic() -> void:
var a: Simulation = Simulation.new(2, _beach())
var b: Simulation = Simulation.new(2, _beach())
var a_id: int = 0
for sim: Simulation in [a, b]:
var id: int = sim.add_player()
a_id = id
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(100.0, -10.0, 64.0)
for _i: int in SECOND * 3:
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), EAST, 0.0, PlayerCommand.BUTTON_CROUCH))
sim.step()
assert_eq(a.state_hash(), b.state_hash())
(a.entities.find(a_id) as SimEntity).breath_ticks -= 1
assert_ne(a.state_hash(), b.state_hash(), "breath must be part of the hash")
(a.entities.find(a_id) as SimEntity).breath_ticks += 1
a.set_sea_level(0.001)
assert_ne(a.state_hash(), b.state_hash(), "the sea level must be part of the hash")
+1
View File
@@ -0,0 +1 @@
uid://cko341rttixdi