W7: The drop: sky barge and wing-sail
Simulation.start_drop() plans a seeded straight route across the island at BARGE_ALTITUDE, through a point near the middle pulled onto land, and puts every player aboard. Riders move with the barge; jump leaps off with some of its speed. Freefall steers and tops out at FREEFALL_MAX_SPEED; jump opens the wing-sail, which also opens by itself WING_AUTO_DEPLOY_HEIGHT above the ground or water. Gliding sinks slowly and steers at GLIDE_SPEED, and the sail folds just above the ground or sea so landing does no damage. Anyone aboard at the end of the route is thrown off. Drop state is hashed. Every server match now starts with the drop. Bots leap at a time fixed by their player ID. The carrier's name and look are open in #72. Closes #14 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+113
@@ -0,0 +1,113 @@
|
||||
## The start of a match: a sky barge drifts across the island on a seeded straight route,
|
||||
## carrying every player. Each one leaps when they choose, falls, opens a wing-sail
|
||||
## (by hand, or automatically near the ground) and steers down. Anyone still aboard
|
||||
## when the barge reaches the end of its route is thrown off.
|
||||
##
|
||||
## The route is drawn from the "drop" stream: a heading, and a point near the island's
|
||||
## middle that the route passes through, so it always crosses land. Movement handles
|
||||
## falling and gliding; this owns the barge, boarding and leaping.
|
||||
class_name Drop
|
||||
extends RefCounted
|
||||
|
||||
## Player drop states, in SimEntity.drop_state.
|
||||
const NONE: int = 0
|
||||
const ABOARD: int = 1
|
||||
const FREEFALL: int = 2
|
||||
const GLIDING: int = 3
|
||||
|
||||
## Where the route starts and ends, at barge altitude.
|
||||
var start: Vector3 = Vector3.ZERO
|
||||
var finish: Vector3 = Vector3.ZERO
|
||||
## Ticks the barge takes from start to finish.
|
||||
var duration_ticks: int = 0
|
||||
## Simulation tick the drop began on.
|
||||
var began_tick: int = 0
|
||||
## True once the barge has reached the end and thrown everyone off.
|
||||
var finished: bool = false
|
||||
|
||||
|
||||
## Plans the route for `island` from the simulation's "drop" stream.
|
||||
static func plan(island_ground: SimGround, rng: SimRng, tick: int) -> Drop:
|
||||
var drop: Drop = Drop.new()
|
||||
var stream: RandomNumberGenerator = rng.stream("drop")
|
||||
var size: float = Tuning.WORLD_SIZE_METRES
|
||||
var centre: Vector2 = Vector2(size, size) * 0.5
|
||||
var heading: float = stream.randf() * TAU
|
||||
var jitter_angle: float = stream.randf() * TAU
|
||||
var jitter: float = sqrt(stream.randf()) * Tuning.BARGE_CENTRE_JITTER
|
||||
var through: Vector2 = centre + Vector2(cos(jitter_angle), sin(jitter_angle)) * jitter
|
||||
# Pull the crossing point onto land if the jitter put it over water.
|
||||
var tries: int = 0
|
||||
while island_ground.terrain_height(through.x, through.y) <= Tuning.ISLAND_BEACH_TOP and tries < 16:
|
||||
through = through.lerp(centre, 0.5)
|
||||
tries += 1
|
||||
var direction: Vector2 = Vector2(cos(heading), sin(heading))
|
||||
var margin: float = Tuning.BARGE_ROUTE_MARGIN
|
||||
var back: float = _distance_to_edge(through, -direction, margin, size)
|
||||
var ahead: float = _distance_to_edge(through, direction, margin, size)
|
||||
var a: Vector2 = through - direction * back
|
||||
var b: Vector2 = through + direction * ahead
|
||||
drop.start = Vector3(a.x, Tuning.BARGE_ALTITUDE, a.y)
|
||||
drop.finish = Vector3(b.x, Tuning.BARGE_ALTITUDE, b.y)
|
||||
drop.duration_ticks = maxi(ceili(a.distance_to(b) / Tuning.BARGE_SPEED * Tuning.TICK_RATE_HZ), 1)
|
||||
drop.began_tick = tick
|
||||
return drop
|
||||
|
||||
|
||||
## How far along the route the barge is at `tick`, from 0 to 1.
|
||||
func progress(tick: int) -> float:
|
||||
return clampf(float(tick - began_tick) / duration_ticks, 0.0, 1.0)
|
||||
|
||||
|
||||
func barge_position(tick: int) -> Vector3:
|
||||
return start.lerp(finish, progress(tick))
|
||||
|
||||
|
||||
func barge_velocity() -> Vector3:
|
||||
return (finish - start) / (float(duration_ticks) / Tuning.TICK_RATE_HZ)
|
||||
|
||||
|
||||
## True once the barge is at the end of its route.
|
||||
func is_over(tick: int) -> bool:
|
||||
return tick - began_tick >= duration_ticks
|
||||
|
||||
|
||||
## Points along the route every `step` metres, at ground level, for tests and maps.
|
||||
func route_samples(step: float) -> PackedVector2Array:
|
||||
var points: PackedVector2Array = PackedVector2Array()
|
||||
var a: Vector2 = Vector2(start.x, start.z)
|
||||
var b: Vector2 = Vector2(finish.x, finish.z)
|
||||
var count: int = maxi(ceili(a.distance_to(b) / step), 1)
|
||||
for index: int in count + 1:
|
||||
points.append(a.lerp(b, float(index) / count))
|
||||
return points
|
||||
|
||||
|
||||
## Puts a player aboard: carried with the barge, not moving on their own.
|
||||
static func board(player: SimEntity, at: Vector3) -> void:
|
||||
player.drop_state = ABOARD
|
||||
player.position = at
|
||||
player.velocity = Vector3.ZERO
|
||||
player.on_ground = false
|
||||
player.swimming = false
|
||||
|
||||
|
||||
## Throws a player off the barge into freefall, keeping some of the barge's speed.
|
||||
func leap(player: SimEntity, tick: int) -> void:
|
||||
player.drop_state = FREEFALL
|
||||
player.position = barge_position(tick)
|
||||
var carried: Vector3 = barge_velocity() * Tuning.LEAP_CARRY
|
||||
player.velocity = Vector3(carried.x, 0.0, carried.z)
|
||||
player.on_ground = false
|
||||
|
||||
|
||||
## Distance from `from` along unit `direction` to the square [margin, size - margin].
|
||||
static func _distance_to_edge(from: Vector2, direction: Vector2, margin: float, size: float) -> float:
|
||||
var best: float = INF
|
||||
for axis: int in 2:
|
||||
var d: float = direction[axis]
|
||||
if absf(d) < 0.000001:
|
||||
continue
|
||||
var edge: float = (size - margin) if d > 0.0 else margin
|
||||
best = minf(best, (edge - from[axis]) / d)
|
||||
return maxf(best, 0.0)
|
||||
@@ -0,0 +1 @@
|
||||
uid://1jnfkm281f0h
|
||||
@@ -22,6 +22,13 @@ static func step(player: SimEntity, command: PlayerCommand, ground: SimGround, s
|
||||
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.
|
||||
@@ -247,3 +254,58 @@ static func _swim(player: SimEntity, command: PlayerCommand, ground: SimGround,
|
||||
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
|
||||
|
||||
@@ -20,6 +20,8 @@ var swimming: bool = false
|
||||
var breath_ticks: int = 0
|
||||
var stamina_ticks: int = 0
|
||||
var drown_ticks: int = 0
|
||||
## Where the player is in the drop: Drop.NONE, ABOARD, FREEFALL or GLIDING.
|
||||
var drop_state: int = 0
|
||||
|
||||
|
||||
func _init(entity_id: int, entity_archetype: int) -> void:
|
||||
|
||||
+41
-1
@@ -26,6 +26,8 @@ var props: PropSet = PropSet.new()
|
||||
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"]
|
||||
## The sky barge, from start_drop() on. Null in matches and tests that start on foot.
|
||||
var drop: Drop = null
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
@@ -65,6 +67,8 @@ func add_player() -> int:
|
||||
player.on_ground = true
|
||||
player.health = Tuning.PLAYER_MAX_HEALTH
|
||||
Water.reset(player)
|
||||
if drop != null and not drop.finished:
|
||||
Drop.board(player, drop.barge_position(tick))
|
||||
_players.append(player.id)
|
||||
_pending[player.id] = {}
|
||||
return player.id
|
||||
@@ -89,16 +93,45 @@ func submit_command(player_id: int, command: PlayerCommand) -> bool:
|
||||
return true
|
||||
|
||||
|
||||
## Starts the match's drop: plans the barge's route and puts every player aboard.
|
||||
func start_drop() -> void:
|
||||
drop = Drop.plan(ground, rng, tick)
|
||||
for player_id: int in _players:
|
||||
Drop.board(entities.find(player_id), drop.barge_position(tick))
|
||||
|
||||
|
||||
## Advances the simulation by one tick.
|
||||
func step() -> void:
|
||||
for player_id: int in _players:
|
||||
var queued: Dictionary = _pending[player_id]
|
||||
var command: PlayerCommand = queued.get(tick)
|
||||
queued.erase(tick)
|
||||
_apply_command(entities.find(player_id), command)
|
||||
var player: SimEntity = entities.find(player_id)
|
||||
if player.drop_state == Drop.ABOARD:
|
||||
_ride(player, command)
|
||||
else:
|
||||
_apply_command(player, command)
|
||||
if drop != null and not drop.finished and drop.is_over(tick + 1):
|
||||
# The end of the route: everyone still aboard goes over the side.
|
||||
for player_id: int in _players:
|
||||
var player: SimEntity = entities.find(player_id)
|
||||
if player.drop_state == Drop.ABOARD:
|
||||
drop.leap(player, tick + 1)
|
||||
drop.finished = true
|
||||
tick += 1
|
||||
|
||||
|
||||
## A tick aboard the barge: jump leaps off, anything else rides along.
|
||||
func _ride(player: SimEntity, command: PlayerCommand) -> void:
|
||||
if command != null:
|
||||
player.yaw = command.yaw
|
||||
player.pitch = command.pitch
|
||||
if command.is_pressed(PlayerCommand.BUTTON_JUMP):
|
||||
drop.leap(player, tick)
|
||||
return
|
||||
player.position = drop.barge_position(tick + 1)
|
||||
|
||||
|
||||
func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
||||
Movement.step(player, command, ground, sea_level)
|
||||
var sprinting: bool = command != null and command.is_pressed(PlayerCommand.BUTTON_SPRINT)
|
||||
@@ -136,11 +169,18 @@ func state_hash() -> String:
|
||||
buffer.put_32(entity.breath_ticks)
|
||||
buffer.put_32(entity.stamina_ticks)
|
||||
buffer.put_32(entity.drown_ticks)
|
||||
buffer.put_u8(entity.drop_state)
|
||||
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))
|
||||
|
||||
if drop != null:
|
||||
buffer.put_64(drop.began_tick)
|
||||
buffer.put_64(drop.duration_ticks)
|
||||
buffer.put_u8(1 if drop.finished else 0)
|
||||
for value: float in [drop.start.x, drop.start.z, drop.finish.x, drop.finish.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())
|
||||
|
||||
@@ -77,6 +77,31 @@ const SWIM_SPRINT_STAMINA_COST: int = 3
|
||||
const STAMINA_RECOVERY_RATE: int = 3
|
||||
const BREATH_RECOVERY_RATE: int = 4
|
||||
|
||||
# The drop, in metres and seconds. The sky barge crosses the island on a straight
|
||||
# seeded route through a point within BARGE_CENTRE_JITTER of the middle.
|
||||
const BARGE_ALTITUDE: float = 150.0
|
||||
const BARGE_SPEED: float = 24.0
|
||||
## The route starts and ends this far inside the world's edge.
|
||||
const BARGE_ROUTE_MARGIN: float = 40.0
|
||||
const BARGE_CENTRE_JITTER: float = 140.0
|
||||
## Every route crosses at least this much dry land.
|
||||
const BARGE_MIN_LAND_METRES: float = 250.0
|
||||
## Fraction of the barge's speed a leaping player keeps.
|
||||
const LEAP_CARRY: float = 0.4
|
||||
const FREEFALL_MAX_SPEED: float = 32.0
|
||||
## Horizontal speed a falling player can steer to, before and after the sail opens.
|
||||
const FREEFALL_STEER_SPEED: float = 9.0
|
||||
const GLIDE_SPEED: float = 13.0
|
||||
## How fast horizontal speed changes while steering a fall, in metres per second squared.
|
||||
const DROP_STEER_ACCEL: float = 20.0
|
||||
## Sink rate under an open wing-sail, and how hard opening it brakes the fall.
|
||||
const GLIDE_FALL_SPEED: float = 5.5
|
||||
const GLIDE_BRAKE: float = 60.0
|
||||
## The sail opens by itself this high above the ground or water.
|
||||
const WING_AUTO_DEPLOY_HEIGHT: float = 40.0
|
||||
## Within this height of the ground or water, the sail folds and the player drops the rest.
|
||||
const DROP_LAND_HANDOFF: float = 1.0
|
||||
|
||||
# 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.
|
||||
|
||||
Reference in New Issue
Block a user