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)