extends TestCase ## The drop from issues W7 and #72: the packet ship's route offshore, the lifeline ## mortar's charge and arc, freefall, the gull-kite, ## 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_pass_offshore_and_every_shore_is_in_reach() -> void: # 60 seeds: every route starts and ends inside the world at deck height, sails only # over open sea, passes close to the coast, and no land is further from the route # than the mortar's full charge carries, so every player can reach anywhere. var full: float = Drop.reach(1.0, true) 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.SHIP_DECK_HEIGHT) for point: Vector2 in drop.route_samples(4.0): assert_true(island.ground.terrain_height(point.x, point.y) < Tuning.SEA_LEVEL_START, "seed %d: the ship runs aground at %s" % [seed_value, point]) var a: Vector2 = Vector2(drop.start.x, drop.start.z) var b: Vector2 = Vector2(drop.finish.x, drop.finish.z) var nearest: float = INF var farthest: float = 0.0 var x: float = 8.0 while x < size: var z: float = 8.0 while z < size: if island.ground.terrain_height(x, z) > Tuning.ISLAND_BEACH_TOP: var at: Vector2 = Vector2(x, z) var gap: float = at.distance_to(Geometry2D.get_closest_point_to_segment(at, a, b)) nearest = minf(nearest, gap) farthest = maxf(farthest, gap) z += 16.0 x += 16.0 assert_true(nearest >= 15.0 and nearest <= 140.0, "seed %d: the ship passes %.0f m from land" % [seed_value, nearest]) assert_true(farthest <= full - 50.0, "seed %d: land %.0f m off, the mortar carries %.0f m" % [seed_value, farthest, full]) 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_charge_follows_the_aim_and_reach_grows_with_it() -> void: assert_eq(Drop.charge_for_pitch(deg_to_rad(-30.0)), 0.0) assert_eq(Drop.charge_for_pitch(deg_to_rad(80.0)), 1.0) for charge: float in [0.0, 0.3, 0.7, 1.0]: assert_near(Drop.charge_for_pitch(Drop.pitch_for_charge(charge)), charge, 0.0001) for kite: bool in [false, true]: var last: float = 0.0 for step: int in 11: var reach: float = Drop.reach(step / 10.0, kite) assert_true(reach > last, "reach should grow with the charge") last = reach for distance: float in [450.0, 600.0, 750.0]: assert_near(Drop.reach(Drop.charge_for_distance(distance, kite), kite), distance, 2.0) assert_eq(Drop.charge_for_distance(10.0, kite), 0.0) assert_eq(Drop.charge_for_distance(5000.0, kite), 1.0) # The kite opened at the top carries further than a fall, and aim_charge falls when # a fall carries far enough. assert_true(Drop.reach(1.0, true) > Drop.reach(1.0) + 100.0) assert_eq(Drop.aim_charge(600.0), Drop.charge_for_distance(600.0)) var far: float = Drop.reach(1.0) + 50.0 assert_eq(Drop.aim_charge(far), Drop.charge_for_distance(far, true)) func test_shots_land_where_reach_says() -> void: # On flat ground: fire straight along +z and hold forward, either opening the kite as # soon as the climb ends or letting it open by itself. The landing must match # Drop.reach(), which bots aim by. for case: int in 6: var charge: float = [0.0, 0.5, 1.0][case % 3] var kite: bool = case >= 3 var sim: Simulation = _flat_sim() var id: int = sim.add_player() sim.start_drop() var player: SimEntity = sim.entities.find(id) player.position = Vector3(10.0, Tuning.SHIP_DECK_HEIGHT, 10.0) sim.drop.start = player.position sim.drop.finish = player.position sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, Drop.pitch_for_charge(charge), PlayerCommand.BUTTON_JUMP)) sim.step() var from: Vector3 = player.position for _i: int in SECOND * 120: var buttons: int = PlayerCommand.BUTTON_JUMP if kite and player.drop_state == Drop.FREEFALL else 0 sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0, buttons)) sim.step() if player.drop_state == Drop.NONE: break var travelled: float = player.position.z - from.z assert_near(travelled, Drop.reach(charge, kite), Drop.reach(charge, kite) * 0.03, "charge %.1f, kite at the top %s" % [charge, kite]) assert_near(player.position.x, from.x, 0.01) 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_ship_until_the_mortar_fires_them_up_the_arc() -> 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.ship_position(sim.tick)) < 0.001, "rider isn't on the ship") var yaw: float = 0.8 sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, yaw, deg_to_rad(20.0), PlayerCommand.BUTTON_JUMP)) sim.step() assert_eq(player.drop_state, Drop.FREEFALL) var launch: Vector3 = Drop.launch_velocity(yaw, Drop.charge_for_pitch(deg_to_rad(20.0))) assert_true(player.velocity.distance_to(launch) < 0.05, "launched at %s, wanted %s" % [player.velocity, launch]) # The climb: steering and jump (the kite) do nothing until the arc tops out. var flat: Vector2 = Vector2(launch.x, launch.z) var top: float = player.position.y for _i: int in SECOND * 3: sim.submit_command(id, _command(sim, Vector2(1.0, -1.0), 2.5, PlayerCommand.BUTTON_JUMP)) sim.step() top = maxf(top, player.position.y) assert_eq(player.drop_state, Drop.FREEFALL, "the kite opened during the climb") assert_true(Vector2(player.velocity.x, player.velocity.z).distance_to(flat) < 0.05, "steered during the climb") while player.velocity.y > 0.0: sim.step() top = maxf(top, player.position.y) var apex: float = Tuning.SHIP_DECK_HEIGHT + Tuning.MORTAR_RISE_SPEED * Tuning.MORTAR_RISE_SPEED / (2.0 * Tuning.GRAVITY) assert_near(top, apex, 3.0, "the top of the arc") # Then freefall, which tops out at its terminal speed. for _i: int in SECOND * 4: sim.step() assert_true(player.velocity.y < -20.0, "should be falling fast, at %s" % player.velocity) assert_true(-player.velocity.y <= Tuning.FREEFALL_MAX_SPEED + 0.001) func _over_the_top(sim: Simulation, player: SimEntity) -> void: while player.velocity.y > 0.0: sim.step() func test_gull_kite_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() _over_the_top(sim, player) for _i: int in SECOND: sim.step() # Open the kite 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 kite 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_kite_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 kite never opened") assert_true(opened_at <= Tuning.KITE_AUTO_DEPLOY_HEIGHT + 0.001 and opened_at > Tuning.KITE_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_fired_inland_at_the_end_of_the_route() -> void: var sim: Simulation = _sim(11) 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.velocity.y > 0.0, "fired up the arc") var centre: Vector2 = Vector2.ONE * Tuning.WORLD_SIZE_METRES * 0.5 var to_centre: Vector2 = (centre - Vector2(sim.drop.finish.x, sim.drop.finish.z)).normalized() var heading: Vector2 = Vector2(player.velocity.x, player.velocity.z).normalized() assert_true(heading.dot(to_centre) > 0.999, "fired towards the middle of the island") 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 kites high, the rest never pressing anything and fired 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)