mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 22:51:57 +00:00
85def73bd9
# Pathfinding pt. 3 ## Description: This PR introduces final change to the pathfinding - path refinement. It optimizes Line of Sight refinement by searching with for the best tile with a binary search instead of linearly. And then spends the recovered budget on better refinement of the first and last 50 tiles of the journey - the place where user is most likely to look at. Additionally this PR re-introduces magnitude check and makes the ships prefer sailing close to the coast, but not too close. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## What? | Before | After | | :--- | :--- | | <img width="1097" height="1117" alt="image" src="https://github.com/user-attachments/assets/4a0b300d-10ef-4151-b6dc-33acfb49f992" /> | <img width="1093" height="1119" alt="image" src="https://github.com/user-attachments/assets/cf81c515-c145-40f4-91e5-a4353986907b" /> | | <img width="1096" height="1129" alt="image" src="https://github.com/user-attachments/assets/21b46bce-f961-4259-88f6-fe4a66180270" /> | <img width="1098" height="1126" alt="image" src="https://github.com/user-attachments/assets/d92587d1-e6b6-4353-b4a4-1efe71bca43d" /> | ## Performance There is actually a severe performance impact of these changes. The path initial path takes almost 2x as long to generate - this is because pre processing can only do so much if the initial path is ugly. Luckily in real gameplay we only need to do this calculation once per edge, so the actual observed performance impact should be much smaller. Cache FTW. | | No Cache | Cache | | :--- | :--- | :--- | | Before | 277.04ms | 208.58ms | | After | 498.34ms | 264.27ms | ## DebugSpan Small utility, it allows any code to be easily instrumented for performance. The idea is the same as with [OTEL Spans](https://opentelemetry.io/docs/concepts/signals/traces/). Produce a span, create sub-spans, measure whatever you need. Works only when `globalThis.__DEBUG_SPAN_ENABLED__ === true`, otherwise no-op. Cool stuff, try it out: ```ts // Convenient wrapper, small performance impact return DebugSpan.wrap('add', () => a + b) // Synchronous API, basically free DebugSpan.start('work') work() DebugSpan.end() // Create sub spans DebugSpan.wrap('complex', () => { const aPlusB = DebugSpan.wrap('add', () => a + b) DebugSpan.set('additionResult', () => aPlusB) // Store data return aPlusB * c }) // Access spans, data and timing const span = DebugSpan.getLast() const compelxSpan = DebugSpan.getLast('complex') console.log(complexSpan.duration, complexSpan.data['additionResult']) ``` These are virtually free and can be enabled on-demand **in production** and available in the devtools. Under the hood devtools integration is just a wrapper around [Performance API](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API). For clarity data keys not prefixed by `$` are omitted from the integration. Every key prefixed with `$` must be fully JSON serializable. <img width="977" height="799" alt="image" src="https://github.com/user-attachments/assets/b4d43506-1639-4f78-a611-30e61de12a07" />
282 lines
8.2 KiB
TypeScript
282 lines
8.2 KiB
TypeScript
import fs from "fs";
|
|
import path, { dirname } from "path";
|
|
import { fileURLToPath } from "url";
|
|
import {
|
|
Difficulty,
|
|
Game,
|
|
GameMapSize,
|
|
GameMapType,
|
|
GameMode,
|
|
GameType,
|
|
PlayerInfo,
|
|
} from "../../src/core/game/Game";
|
|
import { createGame, GameImpl } from "../../src/core/game/GameImpl";
|
|
import { TileRef } from "../../src/core/game/GameMap";
|
|
import {
|
|
genTerrainFromBin,
|
|
MapManifest,
|
|
} from "../../src/core/game/TerrainMapLoader";
|
|
import { UserSettings } from "../../src/core/game/UserSettings";
|
|
import { AStarWater } from "../../src/core/pathfinding/algorithms/AStar.Water";
|
|
import { AStarWaterHierarchical } from "../../src/core/pathfinding/algorithms/AStar.WaterHierarchical";
|
|
import { PathFinding } from "../../src/core/pathfinding/PathFinder";
|
|
import { PathFinderBuilder } from "../../src/core/pathfinding/PathFinderBuilder";
|
|
import { StepperConfig } from "../../src/core/pathfinding/PathFinderStepper";
|
|
import { MiniMapTransformer } from "../../src/core/pathfinding/transformers/MiniMapTransformer";
|
|
import {
|
|
PathStatus,
|
|
SteppingPathFinder,
|
|
} from "../../src/core/pathfinding/types";
|
|
import { GameConfig } from "../../src/core/Schemas";
|
|
import { TestConfig } from "../util/TestConfig";
|
|
|
|
export type BenchmarkRoute = {
|
|
name: string;
|
|
from: TileRef;
|
|
to: TileRef;
|
|
};
|
|
|
|
export type BenchmarkResult = {
|
|
route: string;
|
|
executionTime: number | null;
|
|
pathLength: number | null;
|
|
};
|
|
|
|
export type BenchmarkSummary = {
|
|
totalRoutes: number;
|
|
successfulRoutes: number;
|
|
timedRoutes: number;
|
|
totalDistance: number;
|
|
totalTime: number;
|
|
avgTime: number;
|
|
};
|
|
|
|
function tileStepperConfig(game: Game): StepperConfig<TileRef> {
|
|
return {
|
|
equals: (a, b) => a === b,
|
|
distance: (a, b) => game.manhattanDist(a, b),
|
|
preCheck: (from, to) =>
|
|
typeof from !== "number" ||
|
|
typeof to !== "number" ||
|
|
!game.isValidRef(from) ||
|
|
!game.isValidRef(to)
|
|
? { status: PathStatus.NOT_FOUND }
|
|
: null,
|
|
};
|
|
}
|
|
|
|
export function getAdapter(
|
|
game: Game,
|
|
name: string,
|
|
): SteppingPathFinder<TileRef> {
|
|
switch (name) {
|
|
case "a.baseline": {
|
|
return PathFinderBuilder.create(new AStarWater(game.miniMap()))
|
|
.wrap((pf) => new MiniMapTransformer(pf, game, game.miniMap()))
|
|
.buildWithStepper(tileStepperConfig(game));
|
|
}
|
|
case "a.generic": {
|
|
// Same as baseline - uses AStarWater on minimap
|
|
return PathFinderBuilder.create(new AStarWater(game.miniMap()))
|
|
.wrap((pf) => new MiniMapTransformer(pf, game, game.miniMap()))
|
|
.buildWithStepper(tileStepperConfig(game));
|
|
}
|
|
case "a.full": {
|
|
return PathFinderBuilder.create(
|
|
new AStarWater(game.map()),
|
|
).buildWithStepper(tileStepperConfig(game));
|
|
}
|
|
case "hpa": {
|
|
// Recreate AStarWaterHierarchical without cache, this approach was chosen
|
|
// over adding cache toggles to the existing game instance
|
|
// to avoid adding side effect from benchmark to the game
|
|
|
|
const originalGame = game as any;
|
|
const clonedGame = new GameImpl(
|
|
originalGame._humans,
|
|
originalGame._nations,
|
|
originalGame._map,
|
|
originalGame.miniGameMap,
|
|
originalGame._config,
|
|
originalGame._stats,
|
|
);
|
|
|
|
(clonedGame as any)._miniWaterHPA = new AStarWaterHierarchical(
|
|
clonedGame.miniMap(),
|
|
(clonedGame as any)._miniWaterGraph!,
|
|
{ cachePaths: false },
|
|
);
|
|
|
|
return PathFinding.Water(clonedGame);
|
|
}
|
|
case "hpa.cached":
|
|
return PathFinding.Water(game);
|
|
default:
|
|
throw new Error(`Unknown pathfinding adapter: ${name}`);
|
|
}
|
|
}
|
|
|
|
export async function getScenario(
|
|
scenarioName: string,
|
|
adapterName: string = "hpa",
|
|
) {
|
|
const scenario = await import(`./benchmark/scenarios/${scenarioName}.js`);
|
|
const enableNavMesh = adapterName.startsWith("hpa");
|
|
|
|
// Time game creation (includes NavMesh initialization for default adapter)
|
|
const start = performance.now();
|
|
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
const projectRoot = path.join(currentDir, "../..");
|
|
const mapsDirectory = path.join(projectRoot, "resources/maps");
|
|
const game = await setupFromPath(mapsDirectory, scenario.MAP_NAME, {
|
|
disableNavMesh: !enableNavMesh,
|
|
});
|
|
const initTime = performance.now() - start;
|
|
|
|
const routes = scenario.ROUTES.map(([fromName, toName]: [string, string]) => {
|
|
const fromCoord: [number, number] = scenario.PORTS[fromName];
|
|
const toCoord: [number, number] = scenario.PORTS[toName];
|
|
|
|
return {
|
|
name: `${fromName} → ${toName}`,
|
|
from: game.ref(fromCoord[0], fromCoord[1]),
|
|
to: game.ref(toCoord[0], toCoord[1]),
|
|
};
|
|
});
|
|
|
|
return {
|
|
game,
|
|
routes,
|
|
initTime,
|
|
};
|
|
}
|
|
|
|
export function measurePathLength(
|
|
adapter: SteppingPathFinder<TileRef>,
|
|
route: BenchmarkRoute,
|
|
): number | null {
|
|
const path = adapter.findPath(route.from, route.to);
|
|
return path ? path.length : null;
|
|
}
|
|
|
|
export function measureTime<T>(fn: () => T): { result: T; time: number } {
|
|
const start = performance.now();
|
|
const result = fn();
|
|
const end = performance.now();
|
|
return { result, time: end - start };
|
|
}
|
|
|
|
export function measureExecutionTime(
|
|
adapter: SteppingPathFinder<TileRef>,
|
|
route: BenchmarkRoute,
|
|
executions: number = 1,
|
|
): number | null {
|
|
const { time } = measureTime(() => {
|
|
for (let i = 0; i < executions; i++) {
|
|
adapter.findPath(route.from, route.to);
|
|
}
|
|
});
|
|
|
|
return time / executions;
|
|
}
|
|
|
|
export function calculateStats(results: BenchmarkResult[]): BenchmarkSummary {
|
|
const successful = results.filter((r) => r.pathLength !== null);
|
|
const timed = results.filter((r) => r.executionTime !== null);
|
|
|
|
const totalDistance = successful.reduce((sum, r) => sum + r.pathLength!, 0);
|
|
const totalTime = timed.reduce((sum, r) => sum + r.executionTime!, 0);
|
|
const avgTime = timed.length > 0 ? totalTime / timed.length : 0;
|
|
|
|
return {
|
|
totalRoutes: results.length,
|
|
successfulRoutes: successful.length,
|
|
timedRoutes: timed.length,
|
|
totalDistance,
|
|
totalTime,
|
|
avgTime,
|
|
};
|
|
}
|
|
|
|
export function printRow(columns: (string | number)[], widths: number[]): void {
|
|
const formatted = columns.map((col, i) => {
|
|
const str = typeof col === "number" ? col.toString() : col;
|
|
return str.padEnd(widths[i]);
|
|
});
|
|
|
|
console.log(formatted.join(" "));
|
|
}
|
|
|
|
export function printSeparator(width: number = 80): void {
|
|
console.log("-".repeat(width));
|
|
}
|
|
|
|
export function printHeader(title: string, width: number = 80): void {
|
|
printSeparator(width);
|
|
console.log(title);
|
|
printSeparator(width);
|
|
console.log("");
|
|
}
|
|
|
|
export async function setupFromPath(
|
|
mapDirectory: string,
|
|
mapName: string,
|
|
gameConfig: Partial<GameConfig> = {},
|
|
humans: PlayerInfo[] = [],
|
|
): Promise<Game> {
|
|
// Suppress console.debug for tests
|
|
console.debug = () => {};
|
|
|
|
// Load map files from specified directory
|
|
const mapBinPath = path.join(mapDirectory, mapName, "map.bin");
|
|
const miniMapBinPath = path.join(mapDirectory, mapName, "map4x.bin");
|
|
const manifestPath = path.join(mapDirectory, mapName, "manifest.json");
|
|
|
|
// Check if files exist
|
|
if (!fs.existsSync(mapBinPath)) {
|
|
throw new Error(`Map not found: ${mapBinPath}`);
|
|
}
|
|
|
|
if (!fs.existsSync(miniMapBinPath)) {
|
|
throw new Error(`Mini map not found: ${miniMapBinPath}`);
|
|
}
|
|
|
|
if (!fs.existsSync(manifestPath)) {
|
|
throw new Error(`Manifest not found: ${manifestPath}`);
|
|
}
|
|
|
|
const mapBinBuffer = fs.readFileSync(mapBinPath);
|
|
const miniMapBinBuffer = fs.readFileSync(miniMapBinPath);
|
|
const manifest = JSON.parse(
|
|
fs.readFileSync(manifestPath, "utf8"),
|
|
) satisfies MapManifest;
|
|
|
|
const gameMap = await genTerrainFromBin(manifest.map, mapBinBuffer);
|
|
const miniGameMap = await genTerrainFromBin(manifest.map4x, miniMapBinBuffer);
|
|
|
|
// Configure the game
|
|
const config = new TestConfig(
|
|
new (await import("../util/TestServerConfig")).TestServerConfig(),
|
|
{
|
|
gameMap: GameMapType.Asia,
|
|
gameMapSize: GameMapSize.Normal,
|
|
gameMode: GameMode.FFA,
|
|
gameType: GameType.Singleplayer,
|
|
difficulty: Difficulty.Medium,
|
|
disableNations: false,
|
|
donateGold: false,
|
|
donateTroops: false,
|
|
bots: 0,
|
|
infiniteGold: false,
|
|
infiniteTroops: false,
|
|
instantBuild: false,
|
|
randomSpawn: false,
|
|
...gameConfig,
|
|
},
|
|
new UserSettings(),
|
|
false,
|
|
);
|
|
|
|
return createGame(humans, [], gameMap, miniGameMap, config);
|
|
}
|