H12: Build the next match's island on a worker thread

When a match's results begin, Match starts the next match's Simulation on a
WorkerThreadPool task (its seed is fixed by then) and collects it when the
lobby opens. The same task plans the island's tide levels
(Simulation.plan_tide) and runs a prepare_next hook, which ServerLoop uses
to build the bots' NavGrid and BotWorld for the island. All of these read
only the new simulation, so the results are the ones the main thread would
have made: tests compare the state hash, terrain, solids, props, tide
levels and navigation with fresh ones, and the ten-matches test still
matches its references. A Match freed while its task runs waits for it.

Measured: a 24-bot server paced in real time through the results, lobby,
countdown and 3 s of the next drop has a worst tick of 5.4 ms; it was 1.8 s.
In the 24-client soak at 2% loss, moving the island alone left a 0.82 s tick
(the bots' navigation) and moving that too left 74 ms at the next match's
start (planning the tide), which this also moves. The soak hasn't been run
again since that last step.

Closes #79

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 01:27:39 -05:00
co-authored by Claude Opus 5.5
parent 3a76371760
commit ad0fd2695d
5 changed files with 158 additions and 4 deletions
+15 -2
View File
@@ -40,6 +40,8 @@ func _init(match_seed: int, human_players: int, bot_count: int, rules: MatchRule
if rules == null:
rules = immediate_rules(human_players, bot_count)
match_flow = Match.new(match_seed, rules)
# Each later island's bot navigation is made on the worker that makes the island.
match_flow.prepare_next = _prepare_bots
nav = NavGrid.build(match_flow.sim)
world = BotWorld.build(match_flow.sim, nav)
for handle: int in range(1, human_players + 1):
@@ -115,8 +117,19 @@ func _advance() -> void:
bots.clear()
scripted.clear()
if event["from"] == "results":
nav = NavGrid.build(match_flow.sim)
world = BotWorld.build(match_flow.sim, nav)
var ready: Dictionary = match_flow.prepared
if ready.has("nav"):
nav = ready["nav"]
world = ready["world"]
else:
nav = NavGrid.build(match_flow.sim)
world = BotWorld.build(match_flow.sim, nav)
## Runs on Match's worker thread with the next match's fresh simulation.
static func _prepare_bots(next_sim: Simulation) -> Dictionary:
var grid: NavGrid = NavGrid.build(next_sim)
return {"nav": grid, "world": BotWorld.build(next_sim, grid)}
func _track_players() -> void: