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:
@@ -330,3 +330,49 @@ func test_watchers_take_places_ahead_of_bots_in_the_next_match() -> void:
|
||||
assert_eq(m.bot_handles.size(), 1, "one bot fills the one empty place")
|
||||
for handle: int in [1, 7, 9]:
|
||||
assert_true(m.entity_of.has(handle))
|
||||
|
||||
|
||||
## H12: the next match's island is built on a worker thread during the results, so
|
||||
## the step that opens the lobby doesn't stall, and the world is exactly the one the
|
||||
## main thread would have built.
|
||||
func test_the_next_island_is_built_off_the_main_thread_and_matches_a_fresh_one() -> void:
|
||||
var rules: MatchRules = _rules(4, false)
|
||||
rules.min_players = 0
|
||||
rules.results_ticks = 4
|
||||
var m: Match = Match.new(SEED, rules)
|
||||
_until(m, Match.RESULTS, 20)
|
||||
assert_true(m._next_task >= 0, "the results start the next island's task")
|
||||
var built_on: Simulation = m.sim
|
||||
_until(m, Match.LOBBY, 10)
|
||||
assert_true(m.sim != built_on, "the lobby has a new simulation")
|
||||
assert_eq(m._next_task, -1, "the task was collected")
|
||||
var fresh: Simulation = Simulation.new(m.match_seed(1))
|
||||
assert_eq(m.sim.rng.match_seed, fresh.rng.match_seed)
|
||||
assert_eq(m.sim.state_hash(), fresh.state_hash())
|
||||
assert_true(m.sim.ground.heights == fresh.ground.heights, "same terrain")
|
||||
assert_eq(m.sim.ground.solids, fresh.ground.solids, "same solids")
|
||||
assert_eq(m.sim.props.count(), fresh.props.count(), "same props")
|
||||
assert_eq(m.sim._planned_levels, SafeGround.new(fresh.ground).plan_levels(), "its tide is planned already, and the same")
|
||||
|
||||
|
||||
func test_a_match_freed_during_the_results_waits_for_its_task() -> void:
|
||||
var rules: MatchRules = _rules(4, false)
|
||||
rules.min_players = 0
|
||||
rules.results_ticks = 1000
|
||||
var m: Match = Match.new(SEED, rules)
|
||||
_until(m, Match.RESULTS, 20)
|
||||
assert_true(m._next_task >= 0)
|
||||
m = null
|
||||
assert_true(true, "freeing it with a task running raises no error")
|
||||
|
||||
|
||||
func test_the_prepare_hook_runs_with_the_next_island() -> void:
|
||||
var rules: MatchRules = _rules(4, false)
|
||||
rules.min_players = 0
|
||||
var m: Match = Match.new(SEED, rules)
|
||||
m.prepare_next = func(next_sim: Simulation) -> Dictionary:
|
||||
return {"seed": next_sim.rng.match_seed, "props": next_sim.props.count()}
|
||||
_until(m, Match.RESULTS, 20)
|
||||
_until(m, Match.LOBBY, 10)
|
||||
assert_eq(m.prepared.get("seed"), m.sim.rng.match_seed, "prepared for the lobby's own island")
|
||||
assert_eq(m.prepared.get("props"), m.sim.props.count())
|
||||
|
||||
Reference in New Issue
Block a user