From 6e1c87fe3269508ab40d7e2a6ff175cdb6e4e7fa Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 23:13:15 -0500 Subject: [PATCH] M3: Eliminations, spectating and results The simulation now names what ended each player: weapon, fall, drown or tide, from the cause of the blow that took the last health. Hits carry the weapon's item code. Match keeps a kill feed (eliminator, cause, weapon, tick), per-player stats (damage dealt and taken between opponents, eliminations, never teammates), and a summary() with place, eliminations, damage and survival time. Players who fall on the same tick share the better place and are marked tied. When the last players fall together, the tie-break raises one team to first and the rest share the next place. Eliminated players spectate: first whoever knocked them out, then a living teammate, then the next living player in join order. When a target falls or leaves, the view follows its eliminator or moves on, on the same step. Spectators can cycle forwards and back. In results everyone watches the winner. The headless server logs every placement and the match summary. Closes #26 Co-Authored-By: Claude Opus 5.5 --- src/server/headless_server.gd | 4 + src/sim/match.gd | 201 ++++++++++++++++++--- src/sim/movement.gd | 5 +- src/sim/sim_entity.gd | 11 ++ src/sim/simulation.gd | 7 +- src/sim/water.gd | 2 + src/sim/weapons.gd | 10 +- test/sim/test_results.gd | 327 ++++++++++++++++++++++++++++++++++ test/sim/test_results.gd.uid | 1 + 9 files changed, 542 insertions(+), 26 deletions(-) create mode 100644 test/sim/test_results.gd create mode 100644 test/sim/test_results.gd.uid diff --git a/src/server/headless_server.gd b/src/server/headless_server.gd index 4cdb7a0..fa04fd3 100644 --- a/src/server/headless_server.gd +++ b/src/server/headless_server.gd @@ -82,6 +82,10 @@ func _tick() -> void: for event: Dictionary in loop.match_flow.events: if event["type"] in ["phase", "match_started", "match_ended"]: _log("match", event) + elif event["type"] == "placed": + _log("eliminated", event) + if event["type"] == "match_ended": + _log("results", {"match": event["match"], "summary": loop.match_flow.summary()}) if config.log_every > 0 and loop.ticks % config.log_every == 0: var stats: Dictionary = loop.timing_stats() _log("progress", {"tick": loop.ticks, "p95_us": stats["p95_us"], "max_us": stats["max_us"]}) diff --git a/src/sim/match.gd b/src/sim/match.gd index 326a103..3d1b5d8 100644 --- a/src/sim/match.gd +++ b/src/sim/match.gd @@ -41,9 +41,21 @@ var entity_of: Dictionary = {} var handle_of: Dictionary = {} ## Handle -> team index, for the current match. var team_of: Dictionary = {} -## Finishing order of the current match, last place first: {"handle", "place", "by", -## "left"}. "by" is the eliminating handle, or 0. +## Finishing order of the current match, last place first: {"handle", "place", "tied", +## "by", "cause", "item", "left", "tick"}. "by" is the eliminating handle, or 0; "cause" a +## SimEntity.CAUSE_NAMES entry, or "left"; "item" the weapon that last hit them, or -1; +## "tick" the simulation tick they were placed on. Players who fall on the same tick +## share the better place and are marked "tied" (standard competition ranking). var placements: Array[Dictionary] = [] +## The kill feed: one entry per elimination or leave, oldest first, with the fields of a +## placement record. +var kill_feed: Array[Dictionary] = [] +## Handle -> {"eliminations", "damage_dealt", "damage_taken"} for the current match. +## Damage counts only hits between opponents. +var stats: Dictionary = {} +## Eliminated participant handle -> the living handle they're watching. A target that +## falls is replaced on the same step; see _retarget(). +var spectating: Dictionary = {} ## The winning handles, empty for no winner (everyone left, or no one played). var winners: Array[int] = [] ## What happened during the last step(): phase changes, joins, leaves, placements. @@ -55,6 +67,10 @@ var _next_bot_handle: int = FIRST_BOT_HANDLE var _placed_this_step: Array[int] = [] ## How many started the current match. var _match_size: int = 0 +## The current match's handles in join order, leavers included. +var _roster: Array[int] = [] +## Handle -> item code of the weapon that last hit them, this match. +var _last_weapon: Dictionary = {} func _init(seed_value: int, match_rules: MatchRules = null) -> void: @@ -108,10 +124,13 @@ func leave(handle: int) -> bool: var entity_id: int = entity_of[handle] var player: SimEntity = sim.entities.find(entity_id) if in_match() and player != null and not player.eliminated: - _place(handle, 0, true) + _place(handle, 0, true, _match_size - placements.size(), false) sim.remove_player(entity_id) handle_of.erase(entity_id) entity_of.erase(handle) + spectating.erase(handle) + if in_match(): + _update_spectators() return true @@ -155,10 +174,12 @@ func step() -> void: _start() DROP, PLAY: sim.step() + _collect_damage() _collect_eliminations() if phase == DROP and sim.drop.finished: _enter(PLAY) _check_finished() + _update_spectators() RESULTS: if phase_ticks >= rules.results_ticks: _reset() @@ -184,7 +205,9 @@ func _start() -> void: entity_of[handle] = entity_id handle_of[entity_id] = handle team_of[handle] = index / size + stats[handle] = {"eliminations": 0, "damage_dealt": 0, "damage_taken": 0} _match_size = participants.size() + _roster = participants.duplicate() sim.start_drop() sim.start_tide() sim.place_loot() @@ -194,24 +217,55 @@ func _start() -> void: _check_finished() -func _collect_eliminations() -> void: +## Adds up damage between opponents and remembers each victim's last weapon. +func _collect_damage() -> void: for event: Dictionary in sim.events: - if event["type"] != "eliminated" or not handle_of.has(event["player"]): + if event["type"] != "player_hit" or not handle_of.has(event["victim"]): continue + var victim: int = handle_of[event["victim"]] + var attacker: int = int(handle_of.get(event["player"], 0)) + if attacker == 0 or attacker == victim or team_of[attacker] == team_of[victim]: + continue + var amount: int = int(event["armor"]) + int(event["health"]) + stats[attacker]["damage_dealt"] += amount + stats[victim]["damage_taken"] += amount + _last_weapon[victim] = int(event.get("item", -1)) + + +## Places everyone the simulation eliminated this tick. They share the best place of +## the range they fall into: with five standing and two falling together, both are 4th. +func _collect_eliminations() -> void: + var fallen: Array[Dictionary] = [] + for event: Dictionary in sim.events: + if event["type"] == "eliminated" and handle_of.has(event["player"]): + fallen.append(event) + if fallen.is_empty(): + return + var place: int = _match_size - placements.size() - fallen.size() + 1 + for event: Dictionary in fallen: var by: int = int(handle_of.get(event["by"], 0)) - _place(handle_of[event["player"]], by, false) + var handle: int = handle_of[event["player"]] + _place(handle, by, false, place, fallen.size() > 1, String(event.get("cause", "none"))) + if by != 0 and by != handle and team_of[by] != team_of[handle]: + stats[by]["eliminations"] += 1 -## Records a participant's finishing place: one more than the players left after them. -func _place(handle: int, by: int, left: bool) -> void: +## Records a participant's finishing place, adds them to the kill feed and gives them +## someone to spectate. +func _place(handle: int, by: int, left: bool, place: int, tied: bool, cause: String = "left") -> void: for placed: Dictionary in placements: if placed["handle"] == handle: return - var place: int = _match_size - placements.size() - var record: Dictionary = {"handle": handle, "place": place, "by": by, "left": left} + var record: Dictionary = { + "handle": handle, "place": place, "tied": tied, "by": by, + "cause": "left" if left else cause, "item": int(_last_weapon.get(handle, -1)) if by != 0 else -1, + "left": left, "tick": sim.tick, + } placements.append(record) + kill_feed.append(record) if not left: _placed_this_step.append(handle) + spectating[handle] = 0 var event: Dictionary = record.duplicate() event["type"] = "placed" events.append(event) @@ -245,25 +299,32 @@ func _check_finished() -> void: while not losers.is_empty(): var worst: int = _tie_break(losers, true) losers.erase(worst) - _place(worst, 0, false) + _place(worst, 0, false, _match_size - placements.size(), false, "tide") elif not _placed_this_step.is_empty(): - # Everyone fell on the same tick: the tie-break raises one team back to first. + # Everyone fell on the same tick: the tie-break raises one team back to first, + # and the rest of that tick's fallers share the place below the winners. var best: int = _tie_break(_placed_this_step) for handle: int in _placed_this_step: if team_of[handle] == team_of[best]: winners.append(handle) var kept: Array[Dictionary] = [] + var shared: Array[Dictionary] = [] for record: Dictionary in placements: - if not winners.has(record["handle"]): - kept.append(record) + if winners.has(record["handle"]): + continue + kept.append(record) + if _placed_this_step.has(record["handle"]): + shared.append(record) + for record: Dictionary in shared: + record["place"] = winners.size() + 1 + record["tied"] = shared.size() > 1 placements = kept - # Renumber the rest of this tick's fallers below the winners. - var place: int = _match_size - for record: Dictionary in placements: - record["place"] = place - place -= 1 for handle: int in winners: - placements.append({"handle": handle, "place": 1, "by": 0, "left": false}) + placements.append({ + "handle": handle, "place": 1, "tied": winners.size() > 1, "by": 0, + "cause": "none", "item": -1, "left": false, "tick": sim.tick, + }) + spectating.erase(handle) _enter(RESULTS) events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate(), "timed_out": timed_out}) @@ -301,8 +362,108 @@ func _reset() -> void: handle_of.clear() team_of.clear() placements.clear() + kill_feed.clear() + stats.clear() + spectating.clear() + _roster.clear() + _last_weapon.clear() winners.clear() _next_bot_handle = FIRST_BOT_HANDLE _match_size = 0 sim = Simulation.new(match_seed(matches_started)) _enter(LOBBY) + + +## The living handle `handle` is watching, or 0 if they aren't spectating or nobody is +## left to watch. +func spectate_target(handle: int) -> int: + return int(spectating.get(handle, 0)) + + +## Moves an eliminated participant's view to the next (`direction` 1) or previous (-1) +## living player in join order. Returns the new target, or 0. +func cycle_spectate(handle: int, direction: int = 1) -> int: + if not spectating.has(handle): + return 0 + var living: Array[int] = alive() + if living.is_empty(): + return 0 + var step: int = 1 if direction >= 0 else -1 + var at: int = _roster.find(spectate_target(handle)) + if at < 0: + at = _roster.find(handle) + for offset: int in range(1, _roster.size() + 1): + var candidate: int = _roster[posmod(at + offset * step, _roster.size())] + if living.has(candidate): + spectating[handle] = candidate + return candidate + return 0 + + +## Gives every spectator whose target is gone or has fallen a new one. During results, +## spectators watch a winner who is still standing, if there is one. +func _update_spectators() -> void: + var living: Array[int] = alive() + if phase == RESULTS and not winners.is_empty(): + living = winners.duplicate() + for handle: int in spectating.keys(): + var target: int = spectating[handle] + if living.has(target): + continue + spectating[handle] = _retarget(handle, target, living) + if spectating[handle] != target: + events.append({"type": "spectating", "handle": handle, "target": spectating[handle]}) + + +## Who a spectator watches next: whoever eliminated the player they were watching (or +## themselves, the first time), then a living teammate, then the next living player in +## join order after the old target. 0 when nobody is left. +func _retarget(handle: int, old_target: int, living: Array[int]) -> int: + if living.is_empty(): + return 0 + var followed: int = old_target if old_target != 0 else handle + for record: Dictionary in placements: + if record["handle"] == followed and living.has(record["by"]): + return record["by"] + for candidate: int in living: + if team_of.get(candidate, -1) == team_of.get(handle, -2): + return candidate + var at: int = maxi(_roster.find(followed), 0) + for offset: int in range(1, _roster.size() + 1): + var candidate: int = _roster[(at + offset) % _roster.size()] + if living.has(candidate): + return candidate + return 0 + + +## The match summary, best place first: one Dictionary per participant of the current +## match with "handle", "bot", "team", "place", "tied", "eliminations", "damage_dealt", +## "damage_taken", "survival_ticks", "survival_seconds", "by", "cause" and "left". +## Players still standing have place 0 and survival up to now. +func summary() -> Array[Dictionary]: + var rows: Array[Dictionary] = [] + var record_of: Dictionary = {} + for record: Dictionary in placements: + record_of[record["handle"]] = record + for handle: int in _roster: + var record: Dictionary = record_of.get(handle, {}) + var placed_tick: int = int(record.get("tick", sim.tick)) + var row: Dictionary = stats.get(handle, {"eliminations": 0, "damage_dealt": 0, "damage_taken": 0}).duplicate() + row["handle"] = handle + row["bot"] = bot_handles.has(handle) + row["team"] = int(team_of.get(handle, -1)) + row["place"] = int(record.get("place", 0)) + row["tied"] = bool(record.get("tied", false)) + row["survival_ticks"] = placed_tick + row["survival_seconds"] = float(placed_tick) / Tuning.TICK_RATE_HZ + row["by"] = int(record.get("by", 0)) + row["cause"] = String(record.get("cause", "")) + row["left"] = bool(record.get("left", false)) + rows.append(row) + rows.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: + var pa: int = a["place"] if a["place"] > 0 else 0 + var pb: int = b["place"] if b["place"] > 0 else 0 + if pa != pb: + return pa < pb + return _roster.find(a["handle"]) < _roster.find(b["handle"])) + return rows diff --git a/src/sim/movement.gd b/src/sim/movement.gd index be075ed..5ee104e 100644 --- a/src/sim/movement.gd +++ b/src/sim/movement.gd @@ -189,7 +189,10 @@ static func _land(player: SimEntity, impact_speed: float) -> void: var drop: float = impact_speed * impact_speed / (2.0 * Tuning.GRAVITY) var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES if excess > 0.0: - player.health = maxi(player.health - roundi(excess * Tuning.FALL_DAMAGE_PER_METRE), 0) + var amount: int = roundi(excess * Tuning.FALL_DAMAGE_PER_METRE) + if amount > 0: + player.health = maxi(player.health - amount, 0) + player.last_cause = SimEntity.CAUSE_FALL static func _enter_water(player: SimEntity) -> void: diff --git a/src/sim/sim_entity.gd b/src/sim/sim_entity.gd index c3e24db..0d41413 100644 --- a/src/sim/sim_entity.gd +++ b/src/sim/sim_entity.gd @@ -8,6 +8,14 @@ const ARCHETYPE_PICKUP: int = 2 ## A shot in flight. See Weapons. const ARCHETYPE_PROJECTILE: int = 3 +## What damaged a player. Names for logs and the kill feed are in CAUSE_NAMES. +const CAUSE_NONE: int = 0 +const CAUSE_WEAPON: int = 1 +const CAUSE_FALL: int = 2 +const CAUSE_DROWN: int = 3 +const CAUSE_TIDE: int = 4 +const CAUSE_NAMES: Array[String] = ["none", "weapon", "fall", "drown", "tide"] + var id: int = 0 var archetype: int = 0 var position: Vector3 = Vector3.ZERO @@ -54,6 +62,9 @@ var prev_buttons: int = 0 var eliminated: bool = false ## The player who last damaged this one, or 0. var last_attacker: int = 0 +## What dealt this player's most recent damage: one of the CAUSE_ constants. The cause +## of an elimination is the cause of the blow that took the last health. +var last_cause: int = CAUSE_NONE # Pickups and projectiles: the item they carry. var item: int = -1 diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index 68ca4b0..2435d1e 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -267,7 +267,10 @@ func _check_eliminations() -> void: continue player.eliminated = true player.velocity = Vector3.ZERO - events.append({"type": "eliminated", "player": player_id, "by": player.last_attacker}) + events.append({ + "type": "eliminated", "player": player_id, "by": player.last_attacker, + "cause": SimEntity.CAUSE_NAMES[player.last_cause], + }) Inventory.drop_all(self, player) @@ -299,6 +302,7 @@ func _advance_tide() -> void: var player: SimEntity = entities.find(player_id) if not player.eliminated and player.drop_state == Drop.NONE: player.health = maxi(player.health - Tuning.TIDE_SUDDEN_DEATH_DPS, 0) + player.last_cause = SimEntity.CAUSE_TIDE ## Damages a piece. Returns true if this destroyed it; anything it held up collapses. @@ -351,6 +355,7 @@ func state_hash() -> String: buffer.put_32(entity.prev_buttons) buffer.put_u8(1 if entity.eliminated else 0) buffer.put_32(entity.last_attacker) + buffer.put_u8(entity.last_cause) buffer.put_32(entity.item) buffer.put_32(entity.item_count) buffer.put_32(entity.item_rounds) diff --git a/src/sim/water.gd b/src/sim/water.gd index c83830d..432e0f3 100644 --- a/src/sim/water.gd +++ b/src/sim/water.gd @@ -66,6 +66,8 @@ static func update(player: SimEntity, sea_level: float, drown_dps: int, sprintin if player.drown_ticks >= TICKS: player.drown_ticks = 0 player.health = maxi(player.health - drown_dps, 0) + if drown_dps > 0: + player.last_cause = SimEntity.CAUSE_DROWN else: player.drown_ticks = 0 player.breath_ticks = mini(player.breath_ticks + Tuning.BREATH_RECOVERY_RATE, max_breath_ticks()) diff --git a/src/sim/weapons.gd b/src/sim/weapons.gd index 2b0d994..ffe8ae9 100644 --- a/src/sim/weapons.gd +++ b/src/sim/weapons.gd @@ -216,7 +216,7 @@ static func _apply_hit(sim: Simulation, attacker: int, def: ItemDef, tier: int, HIT_PLAYER: if hit.headshot: amount *= def.headshot_multiplier - damage_player(sim, sim.entities.find(hit.target), roundi(amount), attacker, hit.headshot) + damage_player(sim, sim.entities.find(hit.target), roundi(amount), attacker, hit.headshot, def.code) HIT_PIECE: sim.damage_piece(hit.target, roundi(amount * def.structure_factor), "weapon") HIT_PROP: @@ -241,19 +241,21 @@ static func _pull(sim: Simulation, player: SimEntity, point: Vector3, speed: flo sim.events.append({"type": "line_pull", "player": player.id, "point": point}) -## Takes `amount` from armor, then health. Returns the damage actually done. -static func damage_player(sim: Simulation, victim: SimEntity, amount: int, attacker: int, headshot: bool = false) -> int: +## Takes `amount` from armor, then health. Returns the damage actually done. `item` is +## the weapon's item code, or -1, for the kill feed. +static func damage_player(sim: Simulation, victim: SimEntity, amount: int, attacker: int, headshot: bool = false, item: int = -1) -> int: if victim == null or victim.eliminated or amount <= 0: return 0 var to_armor: int = mini(victim.armor, amount) victim.armor -= to_armor var to_health: int = mini(victim.health, amount - to_armor) victim.health -= to_health + victim.last_cause = SimEntity.CAUSE_WEAPON if attacker != 0 and attacker != victim.id: victim.last_attacker = attacker sim.events.append({ "type": "player_hit", "player": attacker, "victim": victim.id, - "armor": to_armor, "health": to_health, "headshot": headshot, + "armor": to_armor, "health": to_health, "headshot": headshot, "item": item, }) return to_armor + to_health diff --git a/test/sim/test_results.gd b/test/sim/test_results.gd new file mode 100644 index 0000000..60d32a9 --- /dev/null +++ b/test/sim/test_results.gd @@ -0,0 +1,327 @@ +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()) diff --git a/test/sim/test_results.gd.uid b/test/sim/test_results.gd.uid new file mode 100644 index 0000000..a13cc9e --- /dev/null +++ b/test/sim/test_results.gd.uid @@ -0,0 +1 @@ +uid://ckljf1kscpcvx