diff --git a/src/server/server_loop.gd b/src/server/server_loop.gd index c35a648..70a5feb 100644 --- a/src/server/server_loop.gd +++ b/src/server/server_loop.gd @@ -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: diff --git a/src/sim/match.gd b/src/sim/match.gd index 405da0f..def003a 100644 --- a/src/sim/match.gd +++ b/src/sim/match.gd @@ -74,6 +74,18 @@ var _match_size: int = 0 var _roster: Array[int] = [] ## Handle -> item code of the weapon that last hit them, this match. var _last_weapon: Dictionary = {} +## The next match's simulation, built on a WorkerThreadPool task while the results +## show, so the server's tick never stalls for the island (H12). The task writes +## its result into _next_box[0]; nothing reads it until the task is waited on. +var _next_task: int = -1 +var _next_box: Array = [] +## Called on the worker with the next match's new Simulation, after it is built: +## func(sim: Simulation) -> Dictionary. The server uses it to prepare what its bots +## need for the island at the same time. Must only read the simulation. Its result is +## `prepared` once the lobby opens: empty if there is no hook, or if the simulation was +## built on the main thread instead. +var prepare_next: Callable = Callable() +var prepared: Dictionary = {} func _init(seed_value: int, match_rules: MatchRules = null) -> void: @@ -82,6 +94,13 @@ func _init(seed_value: int, match_rules: MatchRules = null) -> void: sim = Simulation.new(match_seed(0)) +func _notification(what: int) -> void: + # A task still running must be waited on before its pool slot is forgotten. + if what == NOTIFICATION_PREDELETE and _next_task >= 0: + WorkerThreadPool.wait_for_task_completion(_next_task) + _next_task = -1 + + ## 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: @@ -354,6 +373,7 @@ func _check_finished() -> void: }) spectating.erase(handle) _enter(RESULTS) + _prepare_next() events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate(), "timed_out": timed_out}) @@ -398,7 +418,7 @@ func _reset() -> void: winners.clear() _next_bot_handle = FIRST_BOT_HANDLE _match_size = 0 - sim = Simulation.new(match_seed(matches_started)) + sim = _take_next() _enter(LOBBY) # People who watched the last match take places first; bots only ever fill # what is left when the next one starts. @@ -409,6 +429,45 @@ func _reset() -> void: watchers.append(handle) +## Starts building the next match's simulation on a worker thread. Its seed is +## fixed by now, and generation reads nothing but the seed, so the result is the +## same as building it on the main thread. +func _prepare_next() -> void: + if _next_task >= 0: + return + # Build the shared item table here, so the worker never races to fill it. + ItemCatalog.all() + var box: Array = [null, {}] + _next_box = box + _next_task = WorkerThreadPool.add_task(_build_into.bind(box, match_seed(matches_started), prepare_next), false, "Tidewall next island") + + +static func _build_into(box: Array, seed_value: int, hook: Callable) -> void: + var built: Simulation = Simulation.new(seed_value) + built.plan_tide() + box[0] = built + if hook.is_valid(): + box[1] = hook.call(built) + + +## The next match's simulation: the worker's, once it has finished (normally long +## before the results end), or one built here if none was started. +func _take_next() -> Simulation: + var seed_value: int = match_seed(matches_started) + prepared = {} + if _next_task < 0: + return Simulation.new(seed_value) + WorkerThreadPool.wait_for_task_completion(_next_task) + _next_task = -1 + var built: Simulation = _next_box[0] as Simulation + var extras: Dictionary = _next_box[1] + _next_box = [] + if built == null or built.rng.match_seed != seed_value: + return Simulation.new(seed_value) + prepared = extras + return built + + ## 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: diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index f4034bb..d3a0973 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -56,6 +56,8 @@ var hit_history: HitHistory = HitHistory.new() ## Player entity ID -> { command tick -> PlayerCommand } var _pending: Dictionary = {} +## The tide levels plan_tide() worked out, or empty. +var _planned_levels: PackedFloat32Array = PackedFloat32Array() ## How many entries of props.destroyed have been checked for crate loot. var _destroyed_checked: int = 0 ## Player entity IDs, ascending. @@ -154,11 +156,20 @@ static func crate_spill_points(prop_set: PropSet) -> Dictionary: ## Starts the tide's schedule from this tick. func start_tide() -> void: if island != null: - tide = Tide.new(tick, SafeGround.new(ground).plan_levels()) + plan_tide() + tide = Tide.new(tick, _planned_levels) else: tide = Tide.new(tick) +## Works out the island's tide levels now, if it hasn't been done, so start_tide() is +## quick. They depend only on the island, so the server does this on the worker that +## builds the next match's island; anything that changes the ground first must not. +func plan_tide() -> void: + if island != null and _planned_levels.is_empty(): + _planned_levels = SafeGround.new(ground).plan_levels() + + ## Height of the water surface over a point: the sea level where the flood reaches, ## Flood.NO_WATER where it doesn't. On a test ground, the sea level everywhere. func water_level_at(point: Vector3) -> float: diff --git a/test/server/test_server_loop.gd b/test/server/test_server_loop.gd index 4395fbc..d372f7d 100644 --- a/test/server/test_server_loop.gd +++ b/test/server/test_server_loop.gd @@ -89,3 +89,28 @@ func test_server_returns_to_the_lobby_and_starts_the_next_match() -> void: assert_eq(loop.bots.size(), 3, "bots are rebuilt for the new match") assert_eq(loop.player_ids, [1, 2, 3] as Array[int]) assert_eq(loop.ticks, 5) + + +## H12: the next island's bot navigation is made on the worker with the island, and +## is the same as navigation made on the main thread. +func test_the_next_islands_navigation_is_made_in_the_background() -> void: + var rules: MatchRules = MatchRules.new() + rules.max_players = 2 + rules.min_players = 0 + rules.lobby_ticks = 2 + rules.countdown_ticks = 2 + rules.results_ticks = 2 + rules.fill_with_bots = false + var loop: ServerLoop = ServerLoop.new(77, 0, 0, rules) + var first: NavGrid = loop.nav + while loop.match_flow.phase != Match.RESULTS: + loop.run_tick() + while loop.match_flow.phase != Match.LOBBY: + loop.run_tick() + assert_true(loop.nav != first, "new navigation for the new island") + assert_true(loop.match_flow.prepared.has("nav"), "made by the worker") + var fresh: NavGrid = NavGrid.build(loop.match_flow.sim) + assert_true(loop.nav.heights == fresh.heights, "the same heights") + assert_true(loop.nav.blocked == fresh.blocked, "the same blocked cells") + assert_eq(loop.nav.main_region, fresh.main_region) + assert_true(loop.world.walkable == BotWorld.build(loop.match_flow.sim, fresh).walkable) diff --git a/test/sim/test_match.gd b/test/sim/test_match.gd index 6989412..76ee03f 100644 --- a/test/sim/test_match.gd +++ b/test/sim/test_match.gd @@ -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())