Files
tidewall/src/sim/movement.gd
T
claudeandClaude Opus 5.5 6e1c87fe32 M3: Eliminations, spectating and results
The simulation now names what ended each player: weapon, fall, drown or tide, from
the cause of the blow that took the last health. Hits carry the weapon's item code.

Match keeps a kill feed (eliminator, cause, weapon, tick), per-player stats (damage
dealt and taken between opponents, eliminations, never teammates), and a summary()
with place, eliminations, damage and survival time. Players who fall on the same
tick share the better place and are marked tied. When the last players fall
together, the tie-break raises one team to first and the rest share the next place.

Eliminated players spectate: first whoever knocked them out, then a living
teammate, then the next living player in join order. When a target falls or
leaves, the view follows its eliminator or moves on, on the same step. Spectators
can cycle forwards and back. In results everyone watches the winner.

The headless server logs every placement and the match summary.

Closes #26

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:13:15 -05:00

315 lines
13 KiB
GDScript

## Command-driven player movement: walk, sprint, crouch, jump, gravity, slopes,
## 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
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.
## `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
if command != null:
player.yaw = command.yaw
player.pitch = command.pitch
player.crouching = command.is_pressed(PlayerCommand.BUTTON_CROUCH)
wants_jump = command.is_pressed(PlayerCommand.BUTTON_JUMP) and not player.crouching
wish = _wish_velocity(command, player.crouching)
if player.drop_state == Drop.ABOARD:
# Riders move with the barge; Simulation places them.
return
if player.drop_state != Drop.NONE:
_fall_from_barge(player, command, ground, sea_level)
return
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
velocity.z = wish.z
velocity.y = 0.0
if wants_jump:
velocity.y = Tuning.JUMP_VELOCITY
player.on_ground = false
else:
velocity.x += (wish.x - velocity.x) * Tuning.AIR_CONTROL
velocity.z += (wish.z - velocity.z) * Tuning.AIR_CONTROL
velocity = _move_horizontally(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.
static func body_height(player: SimEntity) -> float:
return Tuning.CROUCH_HEIGHT if player.crouching else Tuning.PLAYER_HEIGHT
static func _wish_velocity(command: PlayerCommand, crouching: bool) -> Vector3:
var move: Vector2 = command.move_vector()
if move.length() > 1.0:
move = move.normalized()
var angle: float = command.yaw_radians()
var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
# Right is forward x up in Godot's right-handed, y-up frame.
var right: Vector3 = Vector3(-cos(angle), 0.0, sin(angle))
var speed: float = Tuning.WALK_SPEED
if crouching:
speed = Tuning.CROUCH_SPEED
elif command.is_pressed(PlayerCommand.BUTTON_SPRINT) and move.y > 0.0:
speed = Tuning.SPRINT_SPEED
return (right * move.x + forward * move.y) * speed
## Moves along x and z, sliding along whatever blocks the way. Returns the velocity with
## blocked components removed.
static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: SimGround) -> Vector3:
var delta: Vector3 = Vector3(velocity.x, 0.0, velocity.z) * DT
if delta.is_zero_approx():
return velocity
var result: Vector3 = velocity
if _can_move_to(player, player.position + delta, ground):
player.position += delta
elif _can_move_to(player, player.position + Vector3(delta.x, 0.0, 0.0), ground):
player.position.x += delta.x
result.z = 0.0
elif _can_move_to(player, player.position + Vector3(0.0, 0.0, delta.z), ground):
player.position.z += delta.z
result.x = 0.0
else:
result.x = 0.0
result.z = 0.0
var size: float = Tuning.WORLD_SIZE_METRES
player.position.x = clampf(player.position.x, 0.0, size)
player.position.z = clampf(player.position.z, 0.0, size)
return result
static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool:
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 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
# Moving uphill into terrain that's too steep is blocked, on foot or in the air, so
# nobody can climb a cliff by pushing into it. Downhill is always allowed.
var rise: float = ground.terrain_height(target.x, target.z) - player.position.y
if rise > 0.0 and not ground.walkable_terrain(target.x, target.z):
if not ground.support_is_solid(target.x, target.z, player.position.y + step):
return false
return true
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
var support: float = ground.support_height(feet.x, feet.z, reach)
if support >= feet.y - Tuning.GROUND_SNAP:
player.position.y = support
player.velocity = Vector3(velocity.x, 0.0, velocity.z)
if not _walkable_support(ground, player.position):
player.on_ground = false
return
# Walked off a ledge: start falling from here.
player.on_ground = false
# Airborne. Exact integration under constant gravity.
var new_y: float = feet.y + velocity.y * DT - 0.5 * Tuning.GRAVITY * DT * DT
velocity.y -= Tuning.GRAVITY * DT
var head_room: Vector3 = Vector3(feet.x, new_y, feet.z)
if velocity.y > 0.0 and ground.body_blocked(head_room, Tuning.PLAYER_RADIUS, body_height(player), 0.0):
# Bumped a ceiling.
new_y = feet.y
velocity.y = 0.0
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
player.position.y = support
if _walkable_support(ground, player.position):
_land(player, -velocity.y)
velocity.y = 0.0
player.on_ground = true
else:
# Too steep to stand on: keep only the motion along the slope.
var normal: Vector3 = ground.terrain_normal(feet.x, feet.z)
var into: float = velocity.dot(normal)
if into < 0.0:
velocity -= normal * into
velocity = velocity.limit_length(Tuning.SLIDE_MAX_SPEED)
player.velocity = velocity
static func _walkable_support(ground: SimGround, feet: Vector3) -> bool:
if ground.support_is_solid(feet.x, feet.z, feet.y + 0.001):
return true
return ground.walkable_terrain(feet.x, feet.z)
## Applies fall damage for landing at `impact_speed` metres per second, downward.
static func _land(player: SimEntity, impact_speed: float) -> void:
if impact_speed <= 0.0:
return
var drop: float = impact_speed * impact_speed / (2.0 * Tuning.GRAVITY)
var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES
if excess > 0.0:
var amount: int = roundi(excess * Tuning.FALL_DAMAGE_PER_METRE)
if amount > 0:
player.health = maxi(player.health - amount, 0)
player.last_cause = SimEntity.CAUSE_FALL
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
## One tick of the drop after leaping: freefall, then the wing-sail. Players steer with
## the move keys relative to where they look; jump opens the sail, and it opens by itself
## WING_AUTO_DEPLOY_HEIGHT above the ground or water. Near the bottom the sail folds and
## ordinary falling (or the water) takes over, slow enough to do no damage.
static func _fall_from_barge(player: SimEntity, command: PlayerCommand, ground: SimGround, sea_level: float) -> void:
var below: float = _drop_floor(player, ground, sea_level, player.position.y)
if player.drop_state == Drop.FREEFALL:
var opens: bool = command != null and command.is_pressed(PlayerCommand.BUTTON_JUMP)
if opens or player.position.y - below <= Tuning.WING_AUTO_DEPLOY_HEIGHT:
player.drop_state = Drop.GLIDING
var gliding: bool = player.drop_state == Drop.GLIDING
var wish: 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))
wish = (right * input.x + forward * input.y) * (Tuning.GLIDE_SPEED if gliding else Tuning.FREEFALL_STEER_SPEED)
var velocity: Vector3 = player.velocity
var flat: Vector3 = Vector3(velocity.x, 0.0, velocity.z).move_toward(wish, Tuning.DROP_STEER_ACCEL * DT)
velocity.x = flat.x
velocity.z = flat.z
if gliding:
velocity.y = move_toward(velocity.y, -Tuning.GLIDE_FALL_SPEED, Tuning.GLIDE_BRAKE * DT)
else:
velocity.y = maxf(velocity.y - Tuning.GRAVITY * DT, -Tuning.FREEFALL_MAX_SPEED)
velocity = _move_horizontally(player, velocity, ground)
var new_y: float = player.position.y + velocity.y * DT
var support: float = ground.support_height(player.position.x, player.position.z, maxf(player.position.y, new_y))
below = maxf(support, sea_level if Water.is_deep(sea_level, support) else support)
player.velocity = velocity
if new_y <= support:
# Came down on something in a single tick: set down gently.
player.position.y = support
player.velocity.y = 0.0
player.drop_state = Drop.NONE
player.on_ground = _walkable_support(ground, player.position)
return
player.position.y = new_y
if new_y - below <= Tuning.DROP_LAND_HANDOFF:
player.drop_state = Drop.NONE
player.on_ground = false
player.velocity.y = maxf(velocity.y, -Tuning.GLIDE_FALL_SPEED)
## The ground or deep water under a falling player.
static func _drop_floor(player: SimEntity, ground: SimGround, sea_level: float, from_y: float) -> float:
var support: float = ground.support_height(player.position.x, player.position.z, from_y)
return maxf(support, sea_level) if Water.is_deep(sea_level, support) else support