From 8f88d543d79421f29c2f67d44006290a24e98d7c Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 25 Jan 2024 13:29:38 +0100 Subject: Optimize search: generate list of hexes to search for pass 2 in pass 1. Start pass 2 at the fringes to repeat as little work as possible during recursion. --- rules.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'rules.js') diff --git a/rules.js b/rules.js index a436101..0bb851a 100644 --- a/rules.js +++ b/rules.js @@ -1215,6 +1215,8 @@ function forward_supply_next_move_type(here_move, road) { function trace_supply_network_1(start) { + let search_order = [] + supply_net[start] = 1 supply_visited.fill(8) @@ -1226,6 +1228,9 @@ function trace_supply_network_1(start) let here = item >> 3 let here_move = item & 7 + if (supply_friendly[here] > 0) + search_order.unshift(here) + for (let s = 0; s < 6; ++s) { let next = here + hexnext[s] @@ -1259,6 +1264,8 @@ function trace_supply_network_1(start) queue.push((next << 3) | next_move) } } + + return search_order } // PASS 2: Filter net to only include supply lines used by units. @@ -1452,20 +1459,15 @@ function all_hexes_sorted_by_distance_to_base(x) { }) } -const all_hexes_for_axis_supply = all_hexes_sorted_by_distance_to_base(EL_AGHEILA) -const all_hexes_for_allied_supply = all_hexes_sorted_by_distance_to_base(ALEXANDRIA) - function trace_supply_network(start) { check_timeout() - let search_order = (start === EL_AGHEILA ? all_hexes_for_axis_supply : all_hexes_for_allied_supply) - supply_net.fill(0) supply_line.fill(0) // Pass 1: Find hexes that are in supply range. - trace_supply_network_1(start) + let search_order = trace_supply_network_1(start) // Pass 2: Filter hexes that have actual supply lines from units. supply_visited.fill(0) -- cgit v1.2.3