extends TestCase ## The client's picture of a match (issue M8): one view over a simulation, with every ## player, the ship, items and chests drawn from it. func _match(bots: int = 3) -> ServerLoop: return ServerLoop.new(2, 1, bots) func test_draws_every_player_and_the_ship() -> void: var loop: ServerLoop = _match() var view: MatchView = MatchView.new() view.setup(loop.sim, 1) assert_eq(view.entities.figure_count(), 4) assert_true(view.entities.ship().visible) assert_true(view.entities.ship().position.distance_to(loop.sim.drop.ship_position(loop.sim.tick)) < 0.001) # Passengers are spread over the deck, not stacked on one spot. assert_true(view.entities.figure_of(1).position.distance_to(view.entities.figure_of(2).position) > 1.0) assert_eq(view.entities.pickup_count(), loop.sim.pickups.size()) assert_eq(view.entities.get_node("Chests").multimesh.instance_count, loop.sim.loot.chest_positions.size()) assert_not_null(view.terrain) view.free() func test_follows_the_simulation() -> void: var loop: ServerLoop = _match() var view: MatchView = MatchView.new() view.setup(loop.sim, 1) while not loop.sim.drop.finished: loop.run_tick() loop.sim.entities.find(3).health = 0 loop.run_tick() loop.sim.set_sea_level(6.5) view.sync() assert_false(view.entities.ship().visible, "the ship is gone once the drop is over") assert_false(view.entities.figure_of(3).visible, "an eliminated player isn't drawn") var bot: SimEntity = loop.sim.entities.find(2) if not bot.eliminated: assert_true(view.entities.figure_of(2).position.distance_to(bot.position) < 0.001) assert_near(view.water.level, 6.5, 0.001) assert_eq(view.entities.pickup_count(), loop.sim.pickups.size()) view.free() func test_gliding_players_show_a_gull_kite() -> void: var loop: ServerLoop = _match(1) var view: MatchView = MatchView.new() view.setup(loop.sim, 1) var bot: SimEntity = loop.sim.entities.find(2) assert_false(view.entities.figure_of(2).kite().visible) var guard: int = 0 while bot.drop_state != Drop.GLIDING and guard < 3000: loop.run_tick() guard += 1 view.sync() view.entities.animate(0.1) assert_eq(bot.drop_state, Drop.GLIDING) assert_true(view.entities.figure_of(2).kite().visible) view.free() func test_a_player_climbing_the_arc_trails_a_lifeline_from_the_mortar() -> void: var loop: ServerLoop = _match(1) var view: MatchView = MatchView.new() view.setup(loop.sim, 1) var bot: SimEntity = loop.sim.entities.find(2) assert_eq(view.entities.lifeline_count(), 0) var guard: int = 0 while bot.drop_state == Drop.ABOARD and guard < 3000: loop.run_tick() guard += 1 loop.run_tick() view.sync() assert_true(bot.velocity.y > 0.0, "the bot should be climbing") assert_true(view.entities.lifeline_count() >= 1) # The line runs from the mortar's muzzle to the player. var line: MeshInstance3D = view.entities.get_node("Lifeline0") var from: Vector3 = view.entities.ship().transform * VesselModels.muzzle() var to: Vector3 = bot.position + Vector3(0.0, 1.0, 0.0) assert_true((line.transform * Vector3(0.0, 0.0, 0.5)).distance_to(from) < 0.05 or (line.transform * Vector3(0.0, 0.0, -0.5)).distance_to(from) < 0.05, "one end at the muzzle") assert_true((line.transform * Vector3(0.0, 0.0, 0.5)).distance_to(to) < 0.05 or (line.transform * Vector3(0.0, 0.0, -0.5)).distance_to(to) < 0.05, "the other end at the player") # The ship's mortar faces the island. var ship: Node3D = view.entities.ship() var muzzle: Vector3 = ship.transform * VesselModels.muzzle() var centre: Vector2 = Vector2.ONE * Tuning.WORLD_SIZE_METRES * 0.5 var out: Vector2 = Vector2(muzzle.x - ship.position.x, muzzle.z - ship.position.z) var to_centre: Vector2 = centre - Vector2(ship.position.x, ship.position.z) var across: Vector2 = Vector2(cos(ship.rotation.y), -sin(ship.rotation.y)) assert_true(signf(out.dot(across)) == signf(to_centre.dot(across)), "the mortar should face the island") while bot.velocity.y > 0.0 and guard < 6000: loop.run_tick() guard += 1 view.sync() assert_eq(view.entities.lifeline_count(), 0, "the line goes once the arc tops out") view.free() func test_the_camera_follows_a_player_and_frames_one() -> void: var loop: ServerLoop = _match() var view: MatchView = MatchView.new() view.setup(loop.sim, 1) while not loop.sim.drop.finished: loop.run_tick() view.sync() var bot: SimEntity = loop.sim.entities.find(2) assert_true(view.follow(2)) var gap: float = view.camera.position.distance_to(bot.position) assert_true(gap > 0.5 and gap < 6.0, "the camera is %f m from the player" % gap) assert_eq(view.terrain.focus, view.camera.position) assert_true(view.frame(2)) var to_player: Vector3 = (bot.position + Vector3(0.0, 1.2, 0.0) - view.camera.position).normalized() assert_true(to_player.dot(-view.camera.transform.basis.z) > 0.98, "the camera looks at the player") assert_false(view.follow(999)) assert_false(view.frame(999)) view.overview() assert_true(view.camera.position.y > 200.0) view.free() ## H11: a pine's canopy never hides the followed player from the shoulder camera. func test_pines_between_the_shoulder_camera_and_the_player_are_hidden() -> void: var loop: ServerLoop = _match() var sim: Simulation = loop.sim var view: MatchView = MatchView.new() view.setup(sim, 1) while not sim.drop.finished: loop.run_tick() # The pine with the most pines around it, and a player standing against its trunk. var best: int = -1 var crowd: int = -1 for prop: int in sim.props.count(): if sim.props.kind_name(prop) != "pine" or prop % 7 != 0: continue var around: int = sim.props.near(sim.props.boxes[prop].get_center(), 6.0).size() if around > crowd: crowd = around best = prop assert_true(best >= 0) var trunk: AABB = sim.props.boxes[best] var player: SimEntity = sim.entities.find(1) player.drop_state = Drop.NONE player.position = Vector3(trunk.end.x + Tuning.PLAYER_RADIUS + 0.05, sim.ground.terrain_height(trunk.end.x + 0.4, trunk.get_center().z), trunk.get_center().z) view.sync() var hid_any: bool = false var hidden_at_some_yaw: Dictionary = {} for step: int in 16: var yaw: float = TAU * step / 16.0 assert_true(view.follow(1, 0.0, yaw)) var eye: Vector3 = view.camera.transform.origin var height: float = Movement.body_height(player) var chest: Vector3 = player.position + Vector3(0.0, height * 0.6, 0.0) var head: Vector3 = player.position + Vector3(0.0, height, 0.0) for prop: int in sim.props.near(player.position, 20.0): if sim.props.kind_name(prop) != "pine": continue var shape: AABB = view.props.canopy_bounds(prop) var blocks: bool = shape.has_point(eye) or shape.intersects_segment(eye, chest) or shape.intersects_segment(eye, head) assert_eq(view.props.is_drawn(prop), not blocks, "yaw %d: pine %d %s" % [step, prop, "blocks the view" if blocks else "is hidden for nothing"]) if blocks: hidden_at_some_yaw[prop] = true hid_any = hid_any or not view.props.screened().is_empty() assert_true(hid_any, "standing against a pine, some yaw looks through a canopy") # Looking from anywhere else brings them all back. (Headless Godot keeps no MultiMesh # instance transforms, so the restored transform itself is checked by capture.) view.overview() assert_eq(view.props.screened().size(), 0) for prop: int in hidden_at_some_yaw: assert_true(view.props.is_drawn(prop)) view.free() ## B8: a thrown keg is drawn in flight and lying, with the item models, and stops being ## drawn once it has blown. func test_thrown_kegs_are_drawn_until_they_blow() -> void: var sim: Simulation = Simulation.new(5, SimGround.flat()) var thrower: SimEntity = sim.entities.find(sim.add_player()) thrower.position = Vector3(50.0, 0.0, 50.0) thrower.on_ground = true Inventory.give(thrower, ItemCatalog.code_of("powder_keg"), 2) thrower.selected = 1 var view: MatchView = MatchView.new() view.setup(sim, 1) sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, PlayerCommand.BUTTON_FIRE, 1)) sim.step() view.sync() assert_eq(sim.projectiles.size(), 1) assert_eq(view.entities.pickup_count(), 1, "the keg in flight") for _i: int in Explosives.fuse_ticks(ItemCatalog.get_def(ItemCatalog.code_of("powder_keg"))): sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 1)) sim.step() view.sync() assert_eq(sim.projectiles.size(), 0) assert_eq(view.entities.pickup_count(), 0, "gone once it blew") view.free()