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 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 23:13:15 -05:00
co-authored by Claude Opus 5.5
parent 802674687c
commit 6e1c87fe32
9 changed files with 542 additions and 26 deletions
+4
View File
@@ -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"]})
+181 -20
View File
@@ -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
+4 -1
View File
@@ -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:
+11
View File
@@ -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
+6 -1
View File
@@ -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)
+2
View File
@@ -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())
+6 -4
View File
@@ -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