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:
+97
-7
@@ -1,6 +1,6 @@
|
||||
## Command-driven player movement: walk, sprint, crouch, jump, gravity, slopes,
|
||||
## step-up, sliding and fall damage. Deterministic: it depends only on the entity, the
|
||||
## command and the ground.
|
||||
## step-up, sliding, fall damage, wading and swimming. Deterministic: it depends only on
|
||||
## the entity, the command, the ground and the sea level.
|
||||
class_name Movement
|
||||
extends RefCounted
|
||||
|
||||
@@ -9,7 +9,9 @@ const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
||||
|
||||
## Advances one player by one tick. `command` may be null, meaning no input this tick:
|
||||
## the player keeps looking the same way and stops trying to move, but still falls.
|
||||
static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -> void:
|
||||
## `sea_level` is the water surface; the default means no water at all. Breath, stamina
|
||||
## and drowning are Water's job, after this.
|
||||
static func step(player: SimEntity, command: PlayerCommand, ground: SimGround, sea_level: float = -INF) -> void:
|
||||
var wish: Vector3 = Vector3.ZERO
|
||||
var wants_jump: bool = false
|
||||
player.crouching = false
|
||||
@@ -20,6 +22,20 @@ static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -
|
||||
wants_jump = command.is_pressed(PlayerCommand.BUTTON_JUMP) and not player.crouching
|
||||
wish = _wish_velocity(command, player.crouching)
|
||||
|
||||
if player.swimming:
|
||||
if wants_jump and player.stamina_ticks > 0 and _at_surface(player, sea_level):
|
||||
# Kick up out of the water. Airborne from here; back in if it lands in deep water.
|
||||
player.swimming = false
|
||||
player.on_ground = false
|
||||
player.velocity.y = Tuning.JUMP_VELOCITY
|
||||
else:
|
||||
_swim(player, command, ground, sea_level)
|
||||
return
|
||||
|
||||
# Wading: water over the feet slows walkers and stops sprinting.
|
||||
if player.on_ground and sea_level - player.position.y > Tuning.WADE_DEPTH:
|
||||
wish = wish.limit_length(Tuning.WADE_SPEED)
|
||||
|
||||
var velocity: Vector3 = player.velocity
|
||||
if player.on_ground:
|
||||
velocity.x = wish.x
|
||||
@@ -33,7 +49,10 @@ static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -
|
||||
velocity.z += (wish.z - velocity.z) * Tuning.AIR_CONTROL
|
||||
|
||||
velocity = _move_horizontally(player, velocity, ground)
|
||||
_move_vertically(player, velocity, ground)
|
||||
_move_vertically(player, velocity, ground, sea_level)
|
||||
# Walked, or the tide rose, into water deep enough to swim.
|
||||
if not player.swimming and player.on_ground and Water.is_deep(sea_level, player.position.y):
|
||||
_enter_water(player)
|
||||
|
||||
|
||||
## Height of the player's body, which shrinks while crouching.
|
||||
@@ -82,12 +101,13 @@ static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: Sim
|
||||
|
||||
|
||||
static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool:
|
||||
var step: float = Tuning.STEP_HEIGHT if player.on_ground else 0.0
|
||||
var supported: bool = player.on_ground or player.swimming
|
||||
var step: float = Tuning.STEP_HEIGHT if supported else 0.0
|
||||
# On foot, the body is tested where it will stand: on whatever it steps up onto at
|
||||
# the target. Otherwise the next riser of a stair narrower than the body would block
|
||||
# a player who hasn't been lifted onto the current one yet.
|
||||
var body: Vector3 = target
|
||||
if player.on_ground:
|
||||
if supported:
|
||||
body.y = maxf(target.y, ground.support_height(target.x, target.z, target.y + step))
|
||||
if ground.body_blocked(body, Tuning.PLAYER_RADIUS, body_height(player), step):
|
||||
return false
|
||||
@@ -100,7 +120,7 @@ static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround)
|
||||
return true
|
||||
|
||||
|
||||
static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround) -> void:
|
||||
static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround, sea_level: float) -> void:
|
||||
var feet: Vector3 = player.position
|
||||
if player.on_ground:
|
||||
var reach: float = feet.y + Tuning.STEP_HEIGHT
|
||||
@@ -125,6 +145,12 @@ static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGr
|
||||
player.position.y = new_y
|
||||
|
||||
var support: float = ground.support_height(feet.x, feet.z, maxf(feet.y, new_y))
|
||||
# Falling into deep water: the water catches the player, with no fall damage.
|
||||
if velocity.y <= 0.0 and Water.is_deep(sea_level, support) and new_y <= sea_level - Tuning.SWIM_DEPTH:
|
||||
player.position.y = maxf(new_y, support)
|
||||
player.velocity = velocity
|
||||
_enter_water(player)
|
||||
return
|
||||
if new_y > support:
|
||||
player.velocity = velocity
|
||||
return
|
||||
@@ -157,3 +183,67 @@ static func _land(player: SimEntity, impact_speed: float) -> void:
|
||||
var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES
|
||||
if excess > 0.0:
|
||||
player.health = maxi(player.health - roundi(excess * Tuning.FALL_DAMAGE_PER_METRE), 0)
|
||||
|
||||
|
||||
static func _enter_water(player: SimEntity) -> void:
|
||||
player.swimming = true
|
||||
player.on_ground = false
|
||||
player.velocity.y *= Tuning.WATER_ENTRY_DAMP
|
||||
|
||||
|
||||
## True when a swimmer is up at the surface, head out, and so can kick out of the water.
|
||||
static func _at_surface(player: SimEntity, sea_level: float) -> bool:
|
||||
return player.position.y >= sea_level - Tuning.SWIM_FLOAT_DEPTH - 0.2
|
||||
|
||||
|
||||
## One tick in deep water. Swimmers move at SWIM_SPEED (faster sprinting while stamina
|
||||
## lasts), float with the head out, dive while crouch is held, and rest on the sea floor
|
||||
## rather than sinking through it. Out of stamina, they slip under. Where the water
|
||||
## over the floor gets shallow they stand up and walk.
|
||||
static func _swim(player: SimEntity, command: PlayerCommand, ground: SimGround, sea_level: float) -> void:
|
||||
var has_stamina: bool = player.stamina_ticks > 0
|
||||
var speed: float = Tuning.SWIM_SPEED
|
||||
var diving: bool = false
|
||||
var move: Vector3 = Vector3.ZERO
|
||||
if command != null:
|
||||
var input: Vector2 = command.move_vector()
|
||||
if input.length() > 1.0:
|
||||
input = input.normalized()
|
||||
var angle: float = command.yaw_radians()
|
||||
var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
|
||||
var right: Vector3 = Vector3(-cos(angle), 0.0, sin(angle))
|
||||
move = right * input.x + forward * input.y
|
||||
diving = command.is_pressed(PlayerCommand.BUTTON_CROUCH)
|
||||
if command.is_pressed(PlayerCommand.BUTTON_SPRINT) and has_stamina and input.y > 0.0:
|
||||
speed = Tuning.SWIM_SPRINT_SPEED
|
||||
if not has_stamina:
|
||||
speed *= Tuning.SWIM_EXHAUSTED_FACTOR
|
||||
var velocity: Vector3 = move * speed
|
||||
|
||||
var float_depth: float = Tuning.SWIM_FLOAT_DEPTH if has_stamina else Tuning.SWIM_EXHAUSTED_DEPTH
|
||||
var wanted: float = -Tuning.SWIM_VERTICAL_SPEED
|
||||
if not diving:
|
||||
wanted = clampf((sea_level - float_depth - player.position.y) * 4.0, -Tuning.SWIM_VERTICAL_SPEED, Tuning.SWIM_VERTICAL_SPEED)
|
||||
velocity.y = move_toward(player.velocity.y, wanted, Tuning.SWIM_VERTICAL_ACCEL * DT)
|
||||
|
||||
velocity = _move_horizontally(player, velocity, ground)
|
||||
var new_y: float = player.position.y + velocity.y * DT
|
||||
var floor_y: float = ground.support_height(player.position.x, player.position.z, maxf(player.position.y, new_y) + Tuning.STEP_HEIGHT)
|
||||
if new_y <= floor_y:
|
||||
new_y = floor_y
|
||||
velocity.y = maxf(velocity.y, 0.0)
|
||||
if velocity.y > 0.0 and ground.body_blocked(Vector3(player.position.x, new_y, player.position.z), Tuning.PLAYER_RADIUS, body_height(player), 0.0):
|
||||
new_y = player.position.y
|
||||
velocity.y = 0.0
|
||||
player.position.y = new_y
|
||||
player.velocity = velocity
|
||||
|
||||
# Shallow enough to stand: back on foot, without fall damage.
|
||||
if sea_level - floor_y < Tuning.SWIM_EXIT_DEPTH:
|
||||
player.swimming = false
|
||||
if floor_y >= player.position.y - Tuning.GROUND_SNAP:
|
||||
player.position.y = floor_y
|
||||
player.velocity.y = 0.0
|
||||
player.on_ground = _walkable_support(ground, player.position)
|
||||
else:
|
||||
player.on_ground = false
|
||||
|
||||
@@ -14,6 +14,12 @@ var health: int = 0
|
||||
## Standing on walkable ground. False while jumping, falling or sliding.
|
||||
var on_ground: bool = false
|
||||
var crouching: bool = false
|
||||
## Floating or diving in deep water. Swimmers can't build or fire.
|
||||
var swimming: bool = false
|
||||
## Breath, stamina and time spent without breath, in ticks. See Water.
|
||||
var breath_ticks: int = 0
|
||||
var stamina_ticks: int = 0
|
||||
var drown_ticks: int = 0
|
||||
|
||||
|
||||
func _init(entity_id: int, entity_archetype: int) -> void:
|
||||
|
||||
+20
-2
@@ -21,6 +21,11 @@ var pois: PoiSet = PoiSet.new()
|
||||
## Trees, scrub, boulders, wrecks and crates, whose boxes are part of `ground`. Empty
|
||||
## when a test supplied its own ground.
|
||||
var props: PropSet = PropSet.new()
|
||||
## Height of the sea surface, in metres. The tide changes it during a match; movement
|
||||
## and drowning read it every tick, so a change takes effect on the next step.
|
||||
var sea_level: float = Tuning.SEA_LEVEL_START
|
||||
## Damage per second to a player underwater with no breath left. Set by the tide phase.
|
||||
var drown_dps: int = Tuning.TIDE_PHASES[0]["drown_dps"]
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
@@ -59,6 +64,7 @@ func add_player() -> int:
|
||||
player.position = Vector3(x, ground.terrain_height(x, z), z)
|
||||
player.on_ground = true
|
||||
player.health = Tuning.PLAYER_MAX_HEALTH
|
||||
Water.reset(player)
|
||||
_players.append(player.id)
|
||||
_pending[player.id] = {}
|
||||
return player.id
|
||||
@@ -94,7 +100,14 @@ func step() -> void:
|
||||
|
||||
|
||||
func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
||||
Movement.step(player, command, ground)
|
||||
Movement.step(player, command, ground, sea_level)
|
||||
var sprinting: bool = command != null and command.is_pressed(PlayerCommand.BUTTON_SPRINT)
|
||||
Water.update(player, sea_level, drown_dps, sprinting)
|
||||
|
||||
|
||||
## Moves the sea surface. Takes effect on the next step.
|
||||
func set_sea_level(level: float) -> void:
|
||||
sea_level = level
|
||||
|
||||
|
||||
## Damages a prop. Returns true if this destroyed it; its collision goes at once.
|
||||
@@ -119,12 +132,17 @@ func state_hash() -> String:
|
||||
buffer.put_32(entity.health)
|
||||
buffer.put_32(entity.yaw)
|
||||
buffer.put_32(entity.pitch)
|
||||
buffer.put_u8((1 if entity.on_ground else 0) | (2 if entity.crouching else 0))
|
||||
buffer.put_u8((1 if entity.on_ground else 0) | (2 if entity.crouching else 0) | (4 if entity.swimming else 0))
|
||||
buffer.put_32(entity.breath_ticks)
|
||||
buffer.put_32(entity.stamina_ticks)
|
||||
buffer.put_32(entity.drown_ticks)
|
||||
for value: float in [entity.position.x, entity.position.y, entity.position.z]:
|
||||
buffer.put_64(roundi(value * 1000.0))
|
||||
for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]:
|
||||
buffer.put_64(roundi(value * 1000.0))
|
||||
|
||||
buffer.put_64(roundi(sea_level * 1000.0))
|
||||
buffer.put_32(drown_dps)
|
||||
buffer.put_64(props.count())
|
||||
buffer.put_data(props.health.to_byte_array())
|
||||
|
||||
|
||||
@@ -49,6 +49,34 @@ const CROUCH_HEIGHT: float = 1.2
|
||||
const FALL_DAMAGE_SAFE_METRES: float = 6.0
|
||||
const FALL_DAMAGE_PER_METRE: float = 8.0
|
||||
|
||||
# Water, in metres and seconds. Depths are water over the floor under the player.
|
||||
## The sea level a match starts at. The tide raises it from here.
|
||||
const SEA_LEVEL_START: float = 0.0
|
||||
## Deeper than this, walkers wade at WADE_SPEED and can't sprint.
|
||||
const WADE_DEPTH: float = 0.4
|
||||
## Water this deep over the floor lifts a player off it to swim.
|
||||
const SWIM_DEPTH: float = 1.35
|
||||
## A swimmer stands again where the water over the floor is shallower than this.
|
||||
const SWIM_EXIT_DEPTH: float = 1.2
|
||||
## A swimmer floats with the feet this far under the surface, head out.
|
||||
const SWIM_FLOAT_DEPTH: float = 1.3
|
||||
## With no stamina left a swimmer sinks until the feet are this far under: head under.
|
||||
const SWIM_EXHAUSTED_DEPTH: float = 2.2
|
||||
const SWIM_SPRINT_SPEED: float = 3.2
|
||||
## Fraction of SWIM_SPEED left to an exhausted swimmer.
|
||||
const SWIM_EXHAUSTED_FACTOR: float = 0.6
|
||||
## Diving and rising speed, and how fast vertical speed changes in the water.
|
||||
const SWIM_VERTICAL_SPEED: float = 2.0
|
||||
const SWIM_VERTICAL_ACCEL: float = 12.0
|
||||
## Fraction of downward speed kept on hitting deep water.
|
||||
const WATER_ENTRY_DAMP: float = 0.25
|
||||
## Seconds of paddling a full stamina bar allows. Sprint-swimming spends it faster.
|
||||
const SWIM_STAMINA_SECONDS: float = 45.0
|
||||
const SWIM_SPRINT_STAMINA_COST: int = 3
|
||||
## Ticks of stamina and breath regained per tick out of the water or above it.
|
||||
const STAMINA_RECOVERY_RATE: int = 3
|
||||
const BREATH_RECOVERY_RATE: int = 4
|
||||
|
||||
# The island. Worldgen fills the whole world square; the island is about a kilometre
|
||||
# across, sized for 24 players. Heights are metres above the starting sea level.
|
||||
## Spacing of the terrain heightfield, in metres. The grid covers WORLD_SIZE_METRES.
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
## The sea as the simulation sees it: one global level that the tide (M2) raises during
|
||||
## a match, and what it does to players. Movement handles swimming itself; this keeps
|
||||
## breath, stamina and drowning, and answers depth questions.
|
||||
##
|
||||
## Depths and states, for a player whose feet are at `y` over a floor at `floor`:
|
||||
## - wading: on the floor in water deeper than WADE_DEPTH. Slower, no sprint.
|
||||
## - swimming: water at least SWIM_DEPTH over the floor, so the player floats with the
|
||||
## head above the surface (feet SWIM_FLOAT_DEPTH under it). Swimming costs stamina;
|
||||
## with none left the player can't stay up and slips under.
|
||||
## - under: head below the surface. Breath runs down; at zero the phase's drown damage
|
||||
## lands once a second until the head is out again.
|
||||
## Breath and stamina are counted in whole ticks, so they hash and replay exactly.
|
||||
class_name Water
|
||||
extends RefCounted
|
||||
|
||||
const TICKS: int = Tuning.TICK_RATE_HZ
|
||||
|
||||
|
||||
## Water depth over a floor at `floor_y`, or 0 on dry ground.
|
||||
static func depth_over(sea_level: float, floor_y: float) -> float:
|
||||
return maxf(sea_level - floor_y, 0.0)
|
||||
|
||||
|
||||
## True when the water over `floor_y` is deep enough to swim in.
|
||||
static func is_deep(sea_level: float, floor_y: float) -> bool:
|
||||
return sea_level - floor_y >= Tuning.SWIM_DEPTH
|
||||
|
||||
|
||||
static func head_under(player: SimEntity, sea_level: float) -> bool:
|
||||
return player.position.y + Movement.body_height(player) < sea_level
|
||||
|
||||
|
||||
static func max_breath_ticks() -> int:
|
||||
return roundi(Tuning.BREATH_SECONDS * TICKS)
|
||||
|
||||
|
||||
static func max_stamina_ticks() -> int:
|
||||
return roundi(Tuning.SWIM_STAMINA_SECONDS * TICKS)
|
||||
|
||||
|
||||
## Gives a new player full breath and stamina.
|
||||
static func reset(player: SimEntity) -> void:
|
||||
player.breath_ticks = max_breath_ticks()
|
||||
player.stamina_ticks = max_stamina_ticks()
|
||||
player.drown_ticks = 0
|
||||
|
||||
|
||||
## Updates breath, stamina and drowning for one tick, after the player has moved.
|
||||
## `sprinting` is whether the player asked to swim fast this tick.
|
||||
static func update(player: SimEntity, sea_level: float, drown_dps: int, sprinting: bool) -> void:
|
||||
if player.health <= 0:
|
||||
return
|
||||
# Stamina: swimming drains it, faster when sprinting; out of the water it returns.
|
||||
if player.swimming:
|
||||
var cost: int = Tuning.SWIM_SPRINT_STAMINA_COST if sprinting else 1
|
||||
player.stamina_ticks = maxi(player.stamina_ticks - cost, 0)
|
||||
else:
|
||||
player.stamina_ticks = mini(player.stamina_ticks + Tuning.STAMINA_RECOVERY_RATE, max_stamina_ticks())
|
||||
|
||||
if head_under(player, sea_level):
|
||||
if player.breath_ticks > 0:
|
||||
player.breath_ticks -= 1
|
||||
player.drown_ticks = 0
|
||||
else:
|
||||
player.drown_ticks += 1
|
||||
if player.drown_ticks >= TICKS:
|
||||
player.drown_ticks = 0
|
||||
player.health = maxi(player.health - drown_dps, 0)
|
||||
else:
|
||||
player.drown_ticks = 0
|
||||
player.breath_ticks = mini(player.breath_ticks + Tuning.BREATH_RECOVERY_RATE, max_breath_ticks())
|
||||
@@ -0,0 +1 @@
|
||||
uid://c7e4vegn1qf4y
|
||||
Reference in New Issue
Block a user