gaia-project/engine/src/algorithms/spanning-tree.ts
coyotte508 bb39d47586 fix: RangeError on Rotate sectors + bundle load crash (viewer 5.13.1, engine 4.10.3)
- Replace all iterator spreads ([...map.values()] etc) with Array.from() across
  engine and viewer: under the TS->babel downlevel chain they compiled to
  __spreadArrays, which reads .length on an iterator and produced
  'RangeError: invalid array length' on 'Sector rotations finished' (the
  reported boardgamers.space crash), plus several latent same-class sites.
- Break the research-tracks.ts -> ../index barrel import cycle (imports
  Event/Expansion from leaf modules now): the cycle left Engine in TDZ and
  the whole bundle failed with 'Cannot access engine_Engine before
  initialization' at module eval under the rebuilt module order.
- Fix stale cache-loader output producing raw vue_1.default accesses for the
  vue webpack external (window.Vue has no .default): 'Cannot read properties
  of undefined (reading extend)' at load. Clean rebuild emits babel interop
  wrappers correctly. vue-global-shim.js added as belt-and-braces doc for the
  external's shape.
- Add autoprefixer as an explicit viewer devDependency (pnpm 11 no longer
  hoists it; the lib build failed on postCSS plugin load).
- Update stale spec type signatures surfaced by the import change (tile
  position as EventSource, loadFaction arity, ChargeRequest literal cast,
  AvailableCommand data narrowing, FederationInfo new fields).

Verified: engine 750 passing / 4 pending; viewer 809 passing; lint + prettier
clean; rebuilt UMD has zero iterator-spread call sites and loads + completes
sector rotation (with and without rotations) in Chromium against the real
bundle with CDN-provided Vue 2.
2026-09-01 08:09:31 +00:00

229 lines
7.3 KiB
TypeScript

import { Grid, Hex } from "hexagrid";
import { difference, inRange, maxBy, minBy, sumBy, uniq } from "lodash";
import assert from "../utils/assert";
import { topologyOf } from "./grid-topology";
import minimumPathLength from "./minimum-path-length";
import shortestPath from "./shortest-path";
type algorithms = "exhaustive" | "heuristic";
export default function spanningTree<T>(
destGroups: Hex<T>[][],
grid: Grid,
maxAdditional,
algorithm: algorithms,
costOf: (hex: Hex<T>) => number
) {
// Choose which algorithm to use.
// By the way, the heuristic always gives the best solution if destGroups.length <= 3 (maybe even with 4?)
// The breadthwidth algorithm always gives the best solution anyway but is slower
if (algorithm === "exhaustive") {
assert(false, "Exhaustive spanning tree algorithm needs to be updated, use heuristic instead");
// return spanningTreeBreadthWidth(destGroups, grid, maxAdditional);
} else {
return spanningTreeWithHeuristic(destGroups, grid, maxAdditional, costOf);
}
}
function spanningTreeWithHeuristic<T>(
destGroups: Hex<T>[][],
grid: Grid<Hex<T>>,
maxAdditional = -1,
costOf = (hex: Hex<T>) => 1
): { path: Hex[]; cost: number } | { minCost: number } {
type DestGroup = Hex<T>[];
const minCost = minimumPathLength(destGroups);
if (maxAdditional > -1 && maxAdditional < minCost) {
return { minCost };
}
const destHexes: Hex<T>[] = [].concat(...destGroups);
const destHexesSet = new Set(destHexes);
if (destGroups.length <= 1) {
return { path: destHexes, cost: sumBy(destHexes, costOf) };
}
const destGroupsMap: Map<Hex<T>, DestGroup> = new Map();
for (const group of destGroups) {
for (const hex of group) {
destGroupsMap.set(hex, group);
}
}
const [minQ, maxQ] = [minBy(destHexes, "q").q, maxBy(destHexes, "q").q];
const [minR, maxR] = [minBy(destHexes, "r").r, maxBy(destHexes, "r").r];
const [minS, maxS] = [minBy(destHexes, "s").s, maxBy(destHexes, "s").s];
// Starting from the groups themselves usually finds a good solution right away,
// which then serves as an upper bound that massively prunes the remaining searches.
const startingPoints: Array<Hex<T>> = destGroups.map((group) => group[0]);
if (destGroups.length > 2) {
// We pick all the hexes that are in the middle of the federation buildings, and try to create a network from them
startingPoints.push(
...Array.from(grid.values()).filter(
(hex) =>
!destHexesSet.has(hex) &&
inRange(hex.q, minQ + 1, maxQ) &&
inRange(hex.r, minR + 1, maxR) &&
inRange(hex.s, minS + 1, maxS)
)
);
}
const topology = topologyOf(grid);
const groupAround = (hex: Hex<T>) => {
if (destGroupsMap.has(hex)) {
return destGroupsMap.get(hex);
} else {
// If it's an empty hex next to buildings, include those buildings
return uniq([hex, ...topology.neighbours(hex).map((nb) => destGroupsMap.get(nb) ?? [])].flat(1));
}
};
let minScore = maxAdditional === -1 ? grid.size + 1 : maxAdditional + 1;
let bestSolution: Hex<T>[];
const { hexList, indexOf } = topology.index();
const baseCosts = new Float64Array(hexList.length);
for (let i = 0; i < hexList.length; i++) {
baseCosts[i] = costOf(hexList[i]);
}
for (const startingPoint of startingPoints) {
let hexes = groupAround(startingPoint);
let cost = sumBy(hexes, costOf);
let toReach = difference(destHexes, hexes);
do {
// Hexes already part of the tree cost nothing to pass through
const costs = baseCosts.slice();
for (const hex of hexes) {
costs[indexOf.get(hex)] = 0;
}
// A partial solution only helps if it can be extended into a solution cheaper
// than the best known one, so cap the search at minScore - 1 - cost.
// (The -1 assumes integer hex costs — they are 0 or 1 in practice — so that
// "strictly cheaper than minScore" means "at least 1 cheaper".)
const path = shortestPath<T>(hexes, toReach, grid, costs, minScore - 1 - cost);
if (!path) {
hexes = undefined;
break;
}
hexes = uniq(hexes.concat(path.path.slice(1, -1), groupAround(path.path[path.path.length - 1])));
cost = sumBy(hexes, costOf);
toReach = difference(toReach, hexes);
} while (cost < minScore && toReach.length > 0);
if (hexes && cost < minScore && toReach.length === 0) {
minScore = cost;
bestSolution = hexes;
}
}
if (bestSolution) {
return { path: bestSolution, cost: minScore };
}
return { minCost };
}
// No heuristic, but exhaustive
// TO REDO to take into accoutn new parameters (passing through an occupied planet, even if not in destGroup, is 0 cost)
// function spanningTreeBreadthWidth(destGroups: Hex[][], grid: Grid, maxAdditional = -1): Hex[] {
// const destHexes: Hex[] = [].concat(...destGroups);
// const destHexesSet = new Set(destHexes);
// if (destGroups.length <= 1) {
// return destHexes;
// }
// const destGroupsMap: Map<Hex, Hex[]> = new Map();
// for (const group of destGroups) {
// for (const hex of group) {
// destGroupsMap.set(hex, group);
// }
// }
// class HexGroup {
// hexes: Hex[] = [];
// constructor(hexes: Hex[] = []) {
// this.hexes = sortBy(hexes, hex => hex.toString());
// }
// key(): string {
// return this.hexes.map(h => h.toString()).join(',');
// }
// withAdditionalHexes(hexes: Hex[]): HexGroup {
// return new HexGroup(uniq(this.hexes.concat(hexes)));
// }
// get winning(): boolean {
// return this.hexes.filter(hex => destHexesSet.has(hex)).length === destHexesSet.size;
// }
// }
// let minScore = maxAdditional === -1 ? grid.size + 1 : destHexes.length + maxAdditional + 1;
// let bestSolution: Hex[];
// const explored: Map<string, HexGroup> = new Map();
// const groupAround = hex => {
// if (destGroupsMap.has(hex)) {
// return destGroupsMap.get(hex);
// } else {
// return [hex];
// }
// };
// let toExplore: Map<string, HexGroup> = new Map();
// let toExploreNext: Map<string, HexGroup> = new Map();
// const startingGroup = new HexGroup(destGroups[0]);
// toExplore.set(startingGroup.key(), startingGroup);
// while (toExplore.size > 0) {
// for (const hexGroup of toExplore.values()) {
// if (!(hexGroup.hexes.length < minScore)) {
// continue;
// }
// for (const hex of hexGroup.hexes) {
// for (const neighbour of grid.neighbours(hex)) {
// if (hexGroup.hexes.includes(neighbour)) {
// continue;
// }
// const newHexGroup = hexGroup.withAdditionalHexes(groupAround(neighbour));
// const key = newHexGroup.key();
// if (explored.has(key)) {
// continue;
// }
// explored.set(key, newHexGroup);
// if (newHexGroup.hexes.length >= minScore) {
// continue;
// }
// if (newHexGroup.winning) {
// minScore = newHexGroup.hexes.length;
// bestSolution = newHexGroup.hexes;
// } else {
// toExploreNext.set(key, newHexGroup);
// }
// }
// }
// }
// toExplore = toExploreNext;
// toExploreNext = new Map();
// }
// return bestSolution;
// }