extends TestCase ## Eliminations, spectating and results from issue M3: shared places for players who ## fall together, the kill feed and its causes, spectators who follow on when their ## target falls, and the match summary. const SEED: int = 4242 ## Flat ground well above any early tide, so nobody drowns by accident. const FLOOR: float = 20.0 func _rules(max_players: int = 4, team_size: int = 1) -> MatchRules: var rules: MatchRules = MatchRules.new() rules.max_players = max_players rules.min_players = 1 rules.lobby_ticks = 1 rules.countdown_ticks = 1 rules.results_ticks = 2 rules.team_size = team_size return rules ## A match on flat ground with one human (handle 1) and bots, already in play with ## everyone standing in a row 4 m apart, facing +z. func _match(max_players: int = 4, team_size: int = 1) -> Match: var m: Match = Match.new(SEED, _rules(max_players, team_size)) m.sim = Simulation.new(SEED, SimGround.flat(FLOOR)) m.join(1) for _index: int in 10: if m.phase == Match.DROP: break m.step() assert_eq(m.phase, Match.DROP) var index: int = 0 for handle: int in m.participants: var player: SimEntity = _player(m, handle) player.drop_state = Drop.NONE player.position = Vector3(100.0 + index * 4.0, FLOOR, 100.0) player.velocity = Vector3.ZERO player.on_ground = true index += 1 m.sim.drop.finished = true m.step() assert_eq(m.phase, Match.PLAY) return m func _player(m: Match, handle: int) -> SimEntity: return m.sim.entities.find(m.entity_of[handle]) ## Knocks `handle` out as if `by` (a handle, or 0) had landed the last hit. func _eliminate(m: Match, handle: int, by: int = 0) -> void: var player: SimEntity = _player(m, handle) player.health = 0 if by != 0: player.last_attacker = m.entity_of[by] player.last_cause = SimEntity.CAUSE_WEAPON func _record(m: Match, handle: int) -> Dictionary: for record: Dictionary in m.placements: if record["handle"] == handle: return record fail("no placement for %d" % handle) return {} func _row(m: Match, handle: int) -> Dictionary: for row: Dictionary in m.summary(): if row["handle"] == handle: return row fail("no summary row for %d" % handle) return {} func test_players_falling_on_the_same_tick_share_the_better_place() -> void: var m: Match = _match(6) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[5]) m.step() assert_eq(_record(m, hs[5])["place"], 6) assert_false(_record(m, hs[5])["tied"]) _eliminate(m, hs[3]) _eliminate(m, hs[4]) m.step() assert_eq(_record(m, hs[3])["place"], 4, "two fell with five standing: both 4th") assert_eq(_record(m, hs[4])["place"], 4) assert_true(_record(m, hs[3])["tied"] and _record(m, hs[4])["tied"]) _eliminate(m, hs[2]) m.step() assert_eq(_record(m, hs[2])["place"], 3, "the next to fall is 3rd, after a shared 4th") _eliminate(m, hs[1]) m.step() assert_eq(m.phase, Match.RESULTS) assert_eq(_record(m, hs[1])["place"], 2) assert_eq(m.winners, [hs[0]] as Array[int]) func test_the_last_players_falling_together_leave_one_winner_and_a_shared_second() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[3]) m.step() # The other three fall together. The tie-break picks a winner (health is 0 for all, # so it's height, then join order) and the other two share 2nd. _player(m, hs[2]).position.y += 0.5 for handle: int in [hs[0], hs[1], hs[2]]: _eliminate(m, handle) m.step() assert_eq(m.phase, Match.RESULTS) assert_eq(m.winners, [hs[2]] as Array[int], "the highest of the three wins the tie") assert_eq(_record(m, hs[2])["place"], 1) assert_eq(_record(m, hs[0])["place"], 2) assert_eq(_record(m, hs[1])["place"], 2) assert_true(_record(m, hs[0])["tied"]) assert_eq(_record(m, hs[3])["place"], 4, "the earlier faller keeps 4th") assert_eq(m.placements.size(), 4, "one record each") func test_two_last_players_falling_together_place_first_and_second() -> void: var m: Match = _match(2) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[0]) _eliminate(m, hs[1]) m.step() assert_eq(m.winners, [hs[0]] as Array[int], "equal in everything: the first to join wins") assert_eq(_record(m, hs[1])["place"], 2) assert_false(_record(m, hs[1])["tied"], "a lone second isn't a tie") func test_a_trade_credits_both_eliminations_and_feeds_both() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[1], hs[2]) _eliminate(m, hs[2], hs[1]) m.step() assert_eq(m.phase, Match.PLAY) assert_eq(_row(m, hs[1])["eliminations"], 1) assert_eq(_row(m, hs[2])["eliminations"], 1) assert_eq(m.kill_feed.size(), 2) for entry: Dictionary in m.kill_feed: assert_eq(entry["cause"], "weapon") assert_eq(entry["place"], 3) assert_true(entry["tied"]) assert_eq(_record(m, hs[1])["by"], hs[2]) func test_teammates_are_not_credited_for_each_other() -> void: var m: Match = _match(4, 2) var hs: Array[int] = m.participants.duplicate() assert_eq(m.team_of[hs[0]], m.team_of[hs[1]]) _eliminate(m, hs[1], hs[0]) m.step() assert_eq(_row(m, hs[0])["eliminations"], 0) func test_the_kill_feed_names_the_weapon_and_damage_counts_between_opponents() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() var shooter: SimEntity = _player(m, 1) var target_handle: int = hs[1] var target: SimEntity = _player(m, target_handle) # Stand the target 10 m in front of the shooter, with a revolver in hand. target.position = shooter.position + Vector3(0.0, 0.0, 10.0) var revolver: int = ItemCatalog.code_of("signal_revolver") Inventory.give(shooter, revolver, 1) shooter.selected = 1 var eye: Vector3 = Harvest.eye(shooter) var aim: Vector3 = target.position + Vector3(0.0, 0.9, 0.0) - eye var pitch: float = atan2(aim.y, Vector2(aim.x, aim.z).length()) var buttons: int = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM for _index: int in 10: m.submit_command(1, PlayerCommand.from_input(m.sim.tick, Vector2.ZERO, 0.0, pitch, buttons, 1)) m.step() var dealt: int = Tuning.PLAYER_MAX_HEALTH - target.health assert_true(dealt > 0, "the revolver hit") assert_eq(_row(m, 1)["damage_dealt"], dealt) assert_eq(_row(m, target_handle)["damage_taken"], dealt) # Finish them off: the feed credits the shooter with the revolver. target.health = 1 for _index: int in 40: if target.eliminated: break m.submit_command(1, PlayerCommand.from_input(m.sim.tick, Vector2.ZERO, 0.0, pitch, buttons, 1)) m.step() assert_true(target.eliminated, "the second shot landed") var entry: Dictionary = m.kill_feed[m.kill_feed.size() - 1] assert_eq(entry["handle"], target_handle) assert_eq(entry["by"], 1) assert_eq(entry["cause"], "weapon") assert_eq(entry["item"], revolver) assert_eq(_row(m, 1)["eliminations"], 1) func test_the_simulation_names_the_cause_of_each_elimination() -> void: # A long fall onto dry ground. var dry: Simulation = Simulation.new(SEED, SimGround.flat(10.0)) var faller: SimEntity = dry.entities.find(dry.add_player()) faller.position = Vector3(50.0, 80.0, 50.0) faller.on_ground = false faller.health = 1 assert_eq(_cause_of_end(dry, faller), "fall") # An exhausted swimmer on a sea floor 10 m down, out of breath. var wet: Simulation = Simulation.new(SEED, SimGround.flat(-10.0)) var swimmer: SimEntity = wet.entities.find(wet.add_player()) swimmer.position = Vector3(80.0, -10.0, 80.0) swimmer.on_ground = true swimmer.stamina_ticks = 0 swimmer.breath_ticks = 0 swimmer.health = 1 wet.drown_dps = 5 assert_eq(_cause_of_end(wet, swimmer), "drown") ## Steps until `player` is eliminated and returns the cause the simulation gave. func _cause_of_end(sim: Simulation, player: SimEntity) -> String: for _index: int in Tuning.TICK_RATE_HZ * 15: sim.step() for event: Dictionary in sim.events: if event["type"] == "eliminated" and event["player"] == player.id: return event["cause"] fail("player %d was never eliminated" % player.id) return "" func test_spectators_follow_their_eliminator_then_move_on_when_the_target_falls() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() _eliminate(m, 1, hs[2]) m.step() assert_eq(m.spectate_target(1), hs[2], "watching whoever knocked them out") assert_true(m.events.any(func(e: Dictionary) -> bool: return e["type"] == "spectating")) # hs[2] falls to hs[3]: the view follows hs[3]. _eliminate(m, hs[2], hs[3]) m.step() assert_eq(m.spectate_target(1), hs[3]) assert_eq(m.spectate_target(hs[2]), hs[3]) # hs[3] falls to the tide: no eliminator standing, so the next living player. _eliminate(m, hs[3]) m.step() assert_eq(m.phase, Match.RESULTS) assert_eq(m.spectate_target(1), hs[1], "the winner") assert_eq(m.spectate_target(hs[3]), hs[1]) assert_eq(m.spectate_target(hs[1]), 0, "the winner isn't spectating") func test_spectators_move_on_when_their_target_leaves() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[3], hs[1]) m.step() assert_eq(m.spectate_target(hs[3]), hs[1]) m.leave(hs[1]) var target: int = m.spectate_target(hs[3]) assert_true(target != hs[1] and m.alive().has(target), "watching someone still standing") func test_spectators_cycle_through_living_players() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[0]) m.step() var first: int = m.spectate_target(hs[0]) assert_true(m.alive().has(first)) var seen: Dictionary = {first: true} for _index: int in 2: seen[m.cycle_spectate(hs[0])] = true assert_eq(seen.size(), 3, "cycling visits every living player") assert_eq(m.cycle_spectate(hs[0]), first, "and wraps around") var back: int = m.cycle_spectate(hs[0], -1) assert_ne(back, first) assert_eq(m.cycle_spectate(hs[1]), 0, "a living player can't spectate") func test_teammates_spectate_their_living_team() -> void: var m: Match = _match(4, 2) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[0]) m.step() assert_eq(m.spectate_target(hs[0]), hs[1], "with no eliminator, the teammate") func test_the_summary_has_place_eliminations_damage_and_survival() -> void: var m: Match = _match(4) var hs: Array[int] = m.participants.duplicate() for _index: int in Tuning.TICK_RATE_HZ: m.step() _eliminate(m, hs[3], hs[0]) m.step() var fell_at: int = m.sim.tick for _index: int in Tuning.TICK_RATE_HZ: m.step() _eliminate(m, hs[2], hs[0]) _eliminate(m, hs[1], hs[0]) m.step() assert_eq(m.phase, Match.RESULTS) var rows: Array[Dictionary] = m.summary() assert_eq(rows.size(), 4) assert_eq(rows[0]["handle"], 1, "best place first") assert_eq(rows[0]["place"], 1) assert_eq(rows[0]["eliminations"], 3) assert_false(rows[0]["bot"]) assert_true(rows[3]["bot"]) assert_eq(rows[3]["handle"], hs[3]) assert_eq(rows[3]["place"], 4) assert_eq(rows[3]["survival_ticks"], fell_at) assert_near(rows[3]["survival_seconds"], float(fell_at) / Tuning.TICK_RATE_HZ, 0.001) assert_true(rows[0]["survival_ticks"] > rows[3]["survival_ticks"]) for row: Dictionary in rows: for key: String in ["handle", "bot", "team", "place", "tied", "eliminations", "damage_dealt", "damage_taken", "survival_ticks", "survival_seconds", "by", "cause", "left"]: assert_true(row.has(key), "summary has %s" % key) func test_a_new_match_starts_with_clean_results() -> void: var m: Match = _match(2) var hs: Array[int] = m.participants.duplicate() _eliminate(m, hs[1], 1) m.step() assert_eq(m.phase, Match.RESULTS) for _index: int in 10: if m.phase == Match.LOBBY: break m.step() assert_eq(m.phase, Match.LOBBY) assert_true(m.kill_feed.is_empty() and m.stats.is_empty() and m.spectating.is_empty()) assert_true(m.summary().is_empty())