diff --git a/docs/GDD.md b/docs/GDD.md index 3d12203..85fb0cf 100644 --- a/docs/GDD.md +++ b/docs/GDD.md @@ -105,6 +105,18 @@ Each prop has the health in `PROP_KINDS` and blocks players with one box. When i destroyed its box goes at once. Both server and client place the same props from the seed, so only damage is match state. +### The drop + +Every match starts with the drop, simulated in [`src/sim/drop.gd`](../src/sim/drop.gd). +A carrier crosses the island at `BARGE_ALTITUDE` on a straight route drawn from the seed. +The route passes within `BARGE_CENTRE_JITTER` of the middle and always crosses at least +`BARGE_MIN_LAND_METRES` of dry land. Players ride it until they press jump, then fall, +steering at `FREEFALL_STEER_SPEED`, up to `FREEFALL_MAX_SPEED`. Jump opens the sail, and +it opens by itself `WING_AUTO_DEPLOY_HEIGHT` above the ground or water. Under the sail +they sink at `GLIDE_FALL_SPEED` and steer at `GLIDE_SPEED`, and touch down without damage. +Anyone still aboard at the end of the route is thrown off. The carrier's name and look, +sky barge or the packet ship and mortar of the core loop above, are open in #72. + ## Tide phases `TIDE_PHASES` is an ordered list of phases. Each one sets: diff --git a/src/bots/wander_bot.gd b/src/bots/wander_bot.gd index 6380ecd..10029f9 100644 --- a/src/bots/wander_bot.gd +++ b/src/bots/wander_bot.gd @@ -24,4 +24,10 @@ func think(sim: Simulation) -> PlayerCommand: _yaw = rng.randf() * TAU _sprint = rng.randf() < 0.3 var buttons: int = PlayerCommand.BUTTON_SPRINT if _sprint else 0 + # Leap from the barge at a time fixed by the player ID, between 2 and 22 seconds in. + var player: SimEntity = sim.entities.find(player_id) + if player != null and player.drop_state == Drop.ABOARD and sim.drop != null: + var leap_tick: int = sim.drop.began_tick + Tuning.TICK_RATE_HZ * 2 + (player_id * 53) % (Tuning.TICK_RATE_HZ * 20) + if sim.tick >= leap_tick: + buttons = PlayerCommand.BUTTON_JUMP return PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), _yaw, 0.0, buttons) diff --git a/src/server/server_loop.gd b/src/server/server_loop.gd index cd5faa4..a4ae4ec 100644 --- a/src/server/server_loop.gd +++ b/src/server/server_loop.gd @@ -22,6 +22,8 @@ func _init(match_seed: int, human_players: int, bot_count: int) -> void: var id: int = sim.add_player() player_ids.append(id) bots.append(WanderBot.new(id)) + # Every match starts on the sky barge. + sim.start_drop() ## Runs one tick and records how long it took. diff --git a/src/sim/drop.gd b/src/sim/drop.gd new file mode 100644 index 0000000..e04cf9d --- /dev/null +++ b/src/sim/drop.gd @@ -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) diff --git a/src/sim/drop.gd.uid b/src/sim/drop.gd.uid new file mode 100644 index 0000000..0b09fb4 --- /dev/null +++ b/src/sim/drop.gd.uid @@ -0,0 +1 @@ +uid://1jnfkm281f0h diff --git a/src/sim/movement.gd b/src/sim/movement.gd index d7f927e..be075ed 100644 --- a/src/sim/movement.gd +++ b/src/sim/movement.gd @@ -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 diff --git a/src/sim/sim_entity.gd b/src/sim/sim_entity.gd index fb4d168..3427f37 100644 --- a/src/sim/sim_entity.gd +++ b/src/sim/sim_entity.gd @@ -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: diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index 2fc7a99..0ce67d6 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -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()) diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index f5ce2e0..1697c50 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -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. diff --git a/test/server/test_server_loop.gd b/test/server/test_server_loop.gd index 2f56cac..e2131d8 100644 --- a/test/server/test_server_loop.gd +++ b/test/server/test_server_loop.gd @@ -40,16 +40,20 @@ func test_percentile_uses_nearest_rank() -> void: assert_eq(ServerLoop._percentile(PackedInt64Array([9]), 0.99), 9) -func test_bots_move_and_humans_without_commands_stay_put() -> void: +func test_bots_leap_and_humans_without_commands_stay_aboard() -> void: + # Every match starts on the drop carrier. A human who sends nothing rides it; the bot + # leaps at its own fixed moment and falls away. var loop: ServerLoop = ServerLoop.new(99, 1, 1) var human: SimEntity = loop.sim.entities.find(1) var bot: SimEntity = loop.sim.entities.find(2) - var human_start: Vector3 = human.position - var bot_start: Vector3 = bot.position - for _index: int in 90: + assert_eq(human.drop_state, Drop.ABOARD) + assert_eq(bot.drop_state, Drop.ABOARD) + for _index: int in Tuning.TICK_RATE_HZ * 24: loop.run_tick() - assert_eq(human.position, human_start) - assert_true(bot.position.distance_to(bot_start) > 1.0, "the bot should have walked") + assert_eq(human.drop_state, Drop.ABOARD) + assert_true(human.position.distance_to(loop.sim.drop.barge_position(loop.sim.tick)) < 0.001) + assert_ne(bot.drop_state, Drop.ABOARD, "the bot should have leapt") + assert_true(bot.position.y < Tuning.BARGE_ALTITUDE - 20.0, "the bot should be falling, at %s" % bot.position) func test_same_seed_gives_the_same_match() -> void: diff --git a/test/sim/test_drop.gd b/test/sim/test_drop.gd new file mode 100644 index 0000000..e3eaa0f --- /dev/null +++ b/test/sim/test_drop.gd @@ -0,0 +1,252 @@ +extends TestCase + +## The drop from issue W7: the sky barge's route, leaping, freefall, the wing-sail, +## steering, landing and the forced ejection at the end of the route. + +const SECOND: int = Tuning.TICK_RATE_HZ + +var _islands: Dictionary = {} + + +func _sim(seed_value: int) -> Simulation: + return Simulation.new(seed_value) + + +func _flat_sim(seed_value: int = 1) -> Simulation: + return Simulation.new(seed_value, SimGround.flat()) + + +func _command(sim: Simulation, move: Vector2, yaw: float, buttons: int = 0) -> PlayerCommand: + return PlayerCommand.from_input(sim.tick, move, yaw, 0.0, buttons) + + +func test_routes_always_cross_land() -> void: + # 60 seeds: every route starts and ends inside the world, flies at barge altitude, and + # spends a good stretch over dry land, so every player can reach the island. + for seed_value: int in range(1, 61): + var island: Island = Island.generate(seed_value) + var drop: Drop = Drop.plan(island.ground, SimRng.new(seed_value), 0) + var size: float = Tuning.WORLD_SIZE_METRES + for end: Vector3 in [drop.start, drop.finish]: + assert_true(end.x >= 0.0 and end.x <= size and end.z >= 0.0 and end.z <= size, "seed %d route leaves the world" % seed_value) + assert_eq(end.y, Tuning.BARGE_ALTITUDE) + var land: int = 0 + var samples: PackedVector2Array = drop.route_samples(4.0) + for point: Vector2 in samples: + if island.ground.terrain_height(point.x, point.y) > Tuning.ISLAND_BEACH_TOP: + land += 1 + var land_metres: float = land * 4.0 + assert_true(land_metres >= Tuning.BARGE_MIN_LAND_METRES, "seed %d route crosses only %.0f m of land" % [seed_value, land_metres]) + var seconds: float = float(drop.duration_ticks) / SECOND + assert_true(seconds >= 20.0 and seconds <= 75.0, "seed %d route takes %.1f s" % [seed_value, seconds]) + + +func test_route_is_seeded() -> void: + var island: Island = Island.generate(5) + var a: Drop = Drop.plan(island.ground, SimRng.new(5), 0) + var b: Drop = Drop.plan(island.ground, SimRng.new(5), 0) + assert_eq(a.start, b.start) + assert_eq(a.finish, b.finish) + var c: Drop = Drop.plan(island.ground, SimRng.new(6), 0) + assert_ne(a.start, c.start) + + +func test_players_ride_the_barge_until_they_leap() -> void: + var sim: Simulation = _flat_sim() + var id: int = sim.add_player() + sim.start_drop() + var player: SimEntity = sim.entities.find(id) + assert_eq(player.drop_state, Drop.ABOARD) + for _i: int in SECOND * 2: + # Moving and crouching do nothing aboard. + sim.submit_command(id, _command(sim, Vector2(1.0, 1.0), 0.3, PlayerCommand.BUTTON_SPRINT | PlayerCommand.BUTTON_CROUCH)) + sim.step() + assert_eq(player.drop_state, Drop.ABOARD) + assert_true(player.position.distance_to(sim.drop.barge_position(sim.tick)) < 0.001, "rider isn't on the barge") + sim.submit_command(id, _command(sim, Vector2.ZERO, 0.0, PlayerCommand.BUTTON_JUMP)) + sim.step() + assert_eq(player.drop_state, Drop.FREEFALL) + var height: float = player.position.y + for _i: int in SECOND: + sim.step() + assert_true(player.position.y < height - 10.0, "freefall too slow: %.1f to %.1f" % [height, player.position.y]) + # Freefall tops out at its terminal speed. + for _i: int in SECOND * 2: + sim.step() + assert_true(-player.velocity.y <= Tuning.FREEFALL_MAX_SPEED + 0.001) + + +func test_wing_sail_slows_the_fall_and_steers() -> void: + var sim: Simulation = _flat_sim() + var id: int = sim.add_player() + sim.start_drop() + var player: SimEntity = sim.entities.find(id) + sim.submit_command(id, _command(sim, Vector2.ZERO, 0.0, PlayerCommand.BUTTON_JUMP)) + sim.step() + for _i: int in SECOND: + sim.step() + # Open the sail by hand, high up. + sim.submit_command(id, _command(sim, Vector2.ZERO, 0.0, PlayerCommand.BUTTON_JUMP)) + sim.step() + assert_eq(player.drop_state, Drop.GLIDING) + for _i: int in SECOND: + sim.step() + assert_near(player.velocity.y, -Tuning.GLIDE_FALL_SPEED, 0.01) + # Steer towards the middle of the world at glide speed, so no edge gets in the way. + var centre: Vector2 = Vector2(Tuning.WORLD_SIZE_METRES, Tuning.WORLD_SIZE_METRES) * 0.5 + var to_centre: Vector2 = centre - Vector2(player.position.x, player.position.z) + var yaw: float = atan2(to_centre.x, to_centre.y) + var heading: Vector2 = Vector2(sin(yaw), cos(yaw)) + for _i: int in SECOND * 3: + sim.submit_command(id, _command(sim, Vector2(0.0, 1.0), yaw)) + sim.step() + var p1: Vector2 = Vector2(player.position.x, player.position.z) + for _i: int in SECOND: + sim.submit_command(id, _command(sim, Vector2(0.0, 1.0), yaw)) + sim.step() + var p2: Vector2 = Vector2(player.position.x, player.position.z) + assert_near((p2 - p1).dot(heading), Tuning.GLIDE_SPEED, 0.1, "glide speed ahead") + assert_near(absf((p2 - p1).cross(heading)), 0.0, 0.1, "drifting sideways") + # Turn a quarter: strafing right goes right at the same speed. + var right: Vector2 = Vector2(-cos(yaw), sin(yaw)) + for _i: int in SECOND * 2: + sim.submit_command(id, _command(sim, Vector2(1.0, 0.0), yaw)) + sim.step() + var p3: Vector2 = Vector2(player.position.x, player.position.z) + for _i: int in SECOND: + sim.submit_command(id, _command(sim, Vector2(1.0, 0.0), yaw)) + sim.step() + var p4: Vector2 = Vector2(player.position.x, player.position.z) + assert_near((p4 - p3).dot(right), Tuning.GLIDE_SPEED, 0.1, "glide speed to the right") + # Letting go stops the sail drifting. + for _i: int in SECOND: + sim.step() + assert_near(Vector2(player.velocity.x, player.velocity.z).length(), 0.0, 0.01) + assert_eq(player.drop_state, Drop.GLIDING) + + +func test_sail_opens_by_itself_near_the_ground_and_landing_is_safe() -> void: + var sim: Simulation = _flat_sim() + var id: int = sim.add_player() + sim.start_drop() + var player: SimEntity = sim.entities.find(id) + sim.submit_command(id, _command(sim, Vector2.ZERO, 0.0, PlayerCommand.BUTTON_JUMP)) + sim.step() + var opened_at: float = -1.0 + for _i: int in SECOND * 60: + sim.step() + if opened_at < 0.0 and player.drop_state == Drop.GLIDING: + opened_at = player.position.y + if player.on_ground: + break + assert_true(opened_at > 0.0, "the sail never opened") + assert_true(opened_at <= Tuning.WING_AUTO_DEPLOY_HEIGHT + 0.001 and opened_at > Tuning.WING_AUTO_DEPLOY_HEIGHT - 2.0, "opened at %.1f m" % opened_at) + assert_true(player.on_ground, "never landed, at %s" % player.position) + assert_eq(player.drop_state, Drop.NONE) + assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH) + # Back to walking once down. + var x0: float = player.position.x + for _i: int in SECOND: + sim.submit_command(id, _command(sim, Vector2(0.0, 1.0), PI * 0.5)) + sim.step() + assert_near(player.position.x - x0, Tuning.WALK_SPEED, 0.05) + + +func test_gliding_into_the_sea_ends_in_the_water_unhurt() -> void: + var sim: Simulation = _flat_sim() + sim.set_sea_level(10.0) + var id: int = sim.add_player() + sim.start_drop() + var player: SimEntity = sim.entities.find(id) + sim.submit_command(id, _command(sim, Vector2.ZERO, 0.0, PlayerCommand.BUTTON_JUMP)) + sim.step() + for _i: int in SECOND * 60: + sim.step() + if player.swimming: + break + assert_true(player.swimming) + assert_eq(player.drop_state, Drop.NONE) + assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH) + + +func test_riders_are_thrown_off_at_the_end_of_the_route() -> void: + var sim: Simulation = _flat_sim() + var id: int = sim.add_player() + sim.start_drop() + var player: SimEntity = sim.entities.find(id) + for _i: int in sim.drop.duration_ticks - 1: + sim.step() + assert_eq(player.drop_state, Drop.ABOARD) + sim.step() + sim.step() + assert_eq(player.drop_state, Drop.FREEFALL, "still aboard after the route ended") + assert_true(sim.drop.finished) + assert_true(player.position.distance_to(sim.drop.finish) < Tuning.FREEFALL_MAX_SPEED) + + +func test_forced_landing_brings_every_player_down_alive() -> void: + # A full drop on a real island: 24 players, a third leaping early, a third steering + # and opening their sails high, the rest never pressing anything and thrown off at + # the end. Everyone must reach the ground or the water, unhurt, before the route + # time plus a generous fall. + var sim: Simulation = _sim(11) + var ids: Array[int] = [] + for _i: int in Tuning.SOLO_PLAYERS: + ids.append(sim.add_player()) + sim.start_drop() + var limit: int = sim.drop.duration_ticks + SECOND * 60 + var all_down: bool = false + while sim.tick < limit: + var elapsed: int = sim.tick - sim.drop.began_tick + for index: int in ids.size(): + var id: int = ids[index] + var player: SimEntity = sim.entities.find(id) + var buttons: int = 0 + var move: Vector2 = Vector2.ZERO + var yaw: float = TAU * index / ids.size() + if index % 3 == 0 and elapsed == SECOND * (3 + index): + buttons = PlayerCommand.BUTTON_JUMP + elif index % 3 == 1: + move = Vector2(0.0, 1.0) + if player.drop_state == Drop.ABOARD and elapsed == sim.drop.duration_ticks / 2: + buttons = PlayerCommand.BUTTON_JUMP + elif player.drop_state == Drop.FREEFALL and elapsed % SECOND == 0: + buttons = PlayerCommand.BUTTON_JUMP + sim.submit_command(id, _command(sim, move, yaw, buttons)) + sim.step() + all_down = true + for id: int in ids: + var player: SimEntity = sim.entities.find(id) + if player.drop_state != Drop.NONE or not (player.on_ground or player.swimming): + all_down = false + break + if all_down: + break + assert_true(all_down, "not everyone landed within %d ticks" % limit) + assert_true(sim.drop.finished) + for id: int in ids: + var player: SimEntity = sim.entities.find(id) + assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "player %d hurt landing at %s" % [id, player.position]) + var size: float = Tuning.WORLD_SIZE_METRES + assert_true(player.position.x >= 0.0 and player.position.x <= size and player.position.z >= 0.0 and player.position.z <= size) + if player.on_ground: + assert_near(player.position.y, sim.ground.support_height(player.position.x, player.position.z, player.position.y + 0.01), 0.01) + + +func test_drop_is_deterministic_and_hashed() -> void: + var a: Simulation = _flat_sim(3) + var b: Simulation = _flat_sim(3) + for sim: Simulation in [a, b]: + var id: int = sim.add_player() + sim.start_drop() + for _i: int in SECOND * 3: + var buttons: int = PlayerCommand.BUTTON_JUMP if sim.tick == SECOND else 0 + sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.3, 1.0), 1.0, 0.0, buttons)) + sim.step() + assert_eq(a.state_hash(), b.state_hash()) + var c: Simulation = _flat_sim(3) + c.add_player() + c.start_drop() + var before: String = c.state_hash() + c.step() + assert_ne(c.state_hash(), before) diff --git a/test/sim/test_drop.gd.uid b/test/sim/test_drop.gd.uid new file mode 100644 index 0000000..8b34888 --- /dev/null +++ b/test/sim/test_drop.gd.uid @@ -0,0 +1 @@ +uid://dq1tvj7bplb7d