M1: Match state machine
Match (src/sim/match.gd) runs a server's matches back to back: lobby, countdown, drop, play and results, then a fresh island and the lobby again. It is stepped once per server tick and has no scene-tree or clock dependency. - MatchRules holds max and min players, team size, the lobby, countdown and results timers (defaults in Tuning.MATCH_*) and whether bots fill empty places. - Participants are server-chosen handles. Joining during a match waits for the next; leaving during the drop or play places the leaver as eliminated. - Placements, winners (last team standing) and events for phase changes, joins, leaves, places, match start and end. - Match 0 uses the server's seed; later matches derive their seed from it and their index. The next match's Simulation is built when the lobby opens. - ServerLoop now drives a Match (immediate rules by default, so the smoke match is unchanged: same state hash), rebuilds bots each match, and counts server ticks across matches. The headless server logs match events. Tests: test_match (13) covers every transition, leaving in each phase, an empty lobby, an empty match, teams, late joiners, and ten consecutive matches that each start identical to a freshly built simulation. A ServerLoop test checks the server goes back to the lobby and starts the next match. Closes #24 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
## A server's match flow: lobby, countdown, drop, play and results, then back to the
|
||||
## lobby for a new match on a new island. Everything is driven by step(), one call per
|
||||
## server tick, so it is deterministic and has no scene-tree or clock dependency.
|
||||
##
|
||||
## Participants are identified by handles the server chooses (a connection, a local
|
||||
## player or a bot). Joining and leaving can happen in any phase: a join during a match
|
||||
## waits in the lobby for the next one, and leaving during a match counts as being
|
||||
## eliminated where they stood. Each match gets a fresh Simulation, built while the
|
||||
## lobby waits, so nothing carries over from the last one.
|
||||
class_name Match
|
||||
extends RefCounted
|
||||
|
||||
const LOBBY: int = 0
|
||||
const COUNTDOWN: int = 1
|
||||
const DROP: int = 2
|
||||
const PLAY: int = 3
|
||||
const RESULTS: int = 4
|
||||
const PHASE_NAMES: Array[String] = ["lobby", "countdown", "drop", "play", "results"]
|
||||
## Bots added to fill a match get handles counting down from here, so they never clash
|
||||
## with a server's own handles, which are positive.
|
||||
const FIRST_BOT_HANDLE: int = -1
|
||||
|
||||
var rules: MatchRules
|
||||
## The seed the server was started with. Match 0 uses it as is; later matches derive
|
||||
## their own from it and their index.
|
||||
var session_seed: int = 0
|
||||
## How many matches have started. The current or next match's index is this, or this
|
||||
## minus one once it has started.
|
||||
var matches_started: int = 0
|
||||
var phase: int = LOBBY
|
||||
## Ticks spent in the current phase.
|
||||
var phase_ticks: int = 0
|
||||
## The current match's simulation during the drop, play and results. In the lobby and
|
||||
## countdown, the next match's simulation, generated and waiting for players.
|
||||
var sim: Simulation
|
||||
## Participant handles in join order, and which of them are bots.
|
||||
var participants: Array[int] = []
|
||||
var bot_handles: Dictionary = {}
|
||||
## Handles playing in the current match, and their entity IDs both ways.
|
||||
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.
|
||||
var placements: Array[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.
|
||||
var events: Array[Dictionary] = []
|
||||
|
||||
var _next_bot_handle: int = FIRST_BOT_HANDLE
|
||||
## How many started the current match.
|
||||
var _match_size: int = 0
|
||||
|
||||
|
||||
func _init(seed_value: int, match_rules: MatchRules = null) -> void:
|
||||
session_seed = seed_value
|
||||
rules = match_rules if match_rules != null else MatchRules.new()
|
||||
sim = Simulation.new(match_seed(0))
|
||||
|
||||
|
||||
## The seed of match `index`: the session seed for the first, then a hash of it and the
|
||||
## index, kept non-negative.
|
||||
func match_seed(index: int) -> int:
|
||||
if index == 0:
|
||||
return session_seed
|
||||
return (session_seed ^ SimRng._hash_name("match:%d" % index)) & 0x7FFFFFFFFFFFFFFF
|
||||
|
||||
|
||||
## The current or next match's seed.
|
||||
func current_seed() -> int:
|
||||
return sim.rng.match_seed
|
||||
|
||||
|
||||
func phase_name() -> String:
|
||||
return PHASE_NAMES[phase]
|
||||
|
||||
|
||||
func in_match() -> bool:
|
||||
return phase == DROP or phase == PLAY
|
||||
|
||||
|
||||
## Adds a participant. Returns false if the handle is already here or the lobby is
|
||||
## full. During a match they wait for the next one.
|
||||
func join(handle: int, bot: bool = false) -> bool:
|
||||
if participants.has(handle) or participants.size() >= rules.max_players:
|
||||
return false
|
||||
participants.append(handle)
|
||||
if bot:
|
||||
bot_handles[handle] = true
|
||||
events.append({"type": "joined", "handle": handle, "bot": bot})
|
||||
return true
|
||||
|
||||
|
||||
## Removes a participant. In a match, they're placed as if eliminated and their player
|
||||
## leaves the simulation. Returns false if the handle isn't here.
|
||||
func leave(handle: int) -> bool:
|
||||
if not participants.has(handle):
|
||||
return false
|
||||
participants.erase(handle)
|
||||
bot_handles.erase(handle)
|
||||
events.append({"type": "left", "handle": handle})
|
||||
if entity_of.has(handle):
|
||||
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)
|
||||
sim.remove_player(entity_id)
|
||||
handle_of.erase(entity_id)
|
||||
entity_of.erase(handle)
|
||||
return true
|
||||
|
||||
|
||||
## Passes a participant's command to the simulation. Returns false outside a match, for
|
||||
## a handle that isn't playing, or if the simulation rejected it.
|
||||
func submit_command(handle: int, command: PlayerCommand) -> bool:
|
||||
if not in_match() or not entity_of.has(handle):
|
||||
return false
|
||||
return sim.submit_command(entity_of[handle], command)
|
||||
|
||||
|
||||
## Handles still in the match and not eliminated, in join order.
|
||||
func alive() -> Array[int]:
|
||||
var living: Array[int] = []
|
||||
if not in_match() and phase != RESULTS:
|
||||
return living
|
||||
for handle: int in entity_of:
|
||||
var player: SimEntity = sim.entities.find(entity_of[handle])
|
||||
if player != null and not player.eliminated:
|
||||
living.append(handle)
|
||||
return living
|
||||
|
||||
|
||||
## Advances one server tick.
|
||||
func step() -> void:
|
||||
events.clear()
|
||||
phase_ticks += 1
|
||||
match phase:
|
||||
LOBBY:
|
||||
if participants.size() < rules.min_players:
|
||||
phase_ticks = 0
|
||||
elif participants.size() >= rules.max_players or phase_ticks >= rules.lobby_ticks:
|
||||
_enter(COUNTDOWN)
|
||||
if rules.countdown_ticks <= 0:
|
||||
_start()
|
||||
COUNTDOWN:
|
||||
if participants.size() < rules.min_players:
|
||||
_enter(LOBBY)
|
||||
elif phase_ticks >= rules.countdown_ticks:
|
||||
_start()
|
||||
DROP, PLAY:
|
||||
sim.step()
|
||||
_collect_eliminations()
|
||||
if phase == DROP and sim.drop.finished:
|
||||
_enter(PLAY)
|
||||
_check_finished()
|
||||
RESULTS:
|
||||
if phase_ticks >= rules.results_ticks:
|
||||
_reset()
|
||||
|
||||
|
||||
func _enter(next: int) -> void:
|
||||
events.append({"type": "phase", "from": PHASE_NAMES[phase], "to": PHASE_NAMES[next], "match": current_seed()})
|
||||
phase = next
|
||||
phase_ticks = 0
|
||||
|
||||
|
||||
## Fills empty places with bots, puts every participant on the barge and places loot.
|
||||
func _start() -> void:
|
||||
if rules.fill_with_bots:
|
||||
while participants.size() < rules.max_players:
|
||||
var handle: int = _next_bot_handle
|
||||
_next_bot_handle -= 1
|
||||
join(handle, true)
|
||||
var size: int = maxi(rules.team_size, 1)
|
||||
for index: int in participants.size():
|
||||
var handle: int = participants[index]
|
||||
var entity_id: int = sim.add_player()
|
||||
entity_of[handle] = entity_id
|
||||
handle_of[entity_id] = handle
|
||||
team_of[handle] = index / size
|
||||
_match_size = participants.size()
|
||||
sim.start_drop()
|
||||
sim.place_loot()
|
||||
matches_started += 1
|
||||
_enter(DROP)
|
||||
events.append({"type": "match_started", "match": current_seed(), "index": matches_started - 1, "players": participants.size()})
|
||||
_check_finished()
|
||||
|
||||
|
||||
func _collect_eliminations() -> void:
|
||||
for event: Dictionary in sim.events:
|
||||
if event["type"] != "eliminated" or not handle_of.has(event["player"]):
|
||||
continue
|
||||
var by: int = int(handle_of.get(event["by"], 0))
|
||||
_place(handle_of[event["player"]], by, false)
|
||||
|
||||
|
||||
## Records a participant's finishing place: one more than the players left after them.
|
||||
func _place(handle: int, by: int, left: bool) -> 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}
|
||||
placements.append(record)
|
||||
var event: Dictionary = record.duplicate()
|
||||
event["type"] = "placed"
|
||||
events.append(event)
|
||||
|
||||
|
||||
## Ends the match when at most one team has anyone standing.
|
||||
func _check_finished() -> void:
|
||||
if not in_match():
|
||||
return
|
||||
var teams: Dictionary = {}
|
||||
for handle: int in alive():
|
||||
teams[team_of[handle]] = true
|
||||
if teams.size() > 1:
|
||||
return
|
||||
winners.clear()
|
||||
for handle: int in alive():
|
||||
winners.append(handle)
|
||||
# Winners take first place, after everyone eliminated.
|
||||
for handle: int in winners:
|
||||
placements.append({"handle": handle, "place": 1, "by": 0, "left": false})
|
||||
_enter(RESULTS)
|
||||
events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate()})
|
||||
|
||||
|
||||
## Back to the lobby: bots leave, the match's bookkeeping is cleared, and the next
|
||||
## match's island is generated.
|
||||
func _reset() -> void:
|
||||
for handle: int in bot_handles.keys():
|
||||
participants.erase(handle)
|
||||
bot_handles.clear()
|
||||
entity_of.clear()
|
||||
handle_of.clear()
|
||||
team_of.clear()
|
||||
placements.clear()
|
||||
winners.clear()
|
||||
_next_bot_handle = FIRST_BOT_HANDLE
|
||||
_match_size = 0
|
||||
sim = Simulation.new(match_seed(matches_started))
|
||||
_enter(LOBBY)
|
||||
Reference in New Issue
Block a user