Pathfinding Refinement (#2878)

# 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"
/>
This commit is contained in:
Arkadiusz Sygulski
2026-01-13 21:39:54 +01:00
committed by GitHub
parent 35b7213c5c
commit 85def73bd9
20 changed files with 963 additions and 713 deletions
+2 -2
View File
@@ -11,10 +11,10 @@ import {
import { StationPathFinder } from "./PathFinder.Station";
import { PathFinderBuilder } from "./PathFinderBuilder";
import { StepperConfig } from "./PathFinderStepper";
import { BresenhamSmoothingTransformer } from "./smoothing/BresenhamPathSmoother";
import { ComponentCheckTransformer } from "./transformers/ComponentCheckTransformer";
import { MiniMapTransformer } from "./transformers/MiniMapTransformer";
import { ShoreCoercingTransformer } from "./transformers/ShoreCoercingTransformer";
import { SmoothingWaterTransformer } from "./transformers/SmoothingWaterTransformer";
import { PathStatus, SteppingPathFinder } from "./types";
/**
@@ -46,7 +46,7 @@ export class PathFinding {
return PathFinderBuilder.create(pf)
.wrap((pf) => new ComponentCheckTransformer(pf, componentCheckFn))
.wrap((pf) => new BresenhamSmoothingTransformer(pf, miniMap))
.wrap((pf) => new SmoothingWaterTransformer(pf, miniMap))
.wrap((pf) => new ShoreCoercingTransformer(pf, miniMap))
.wrap((pf) => new MiniMapTransformer(pf, game.map(), miniMap))
.buildWithStepper(tileStepperConfig(game));
+9 -1
View File
@@ -1,3 +1,4 @@
import { DebugSpan } from "../utilities/DebugSpan";
import { PathFinderStepper, StepperConfig } from "./PathFinderStepper";
import { PathFinder, SteppingPathFinder } from "./types";
@@ -27,10 +28,17 @@ export class PathFinderBuilder<T> {
}
build(): PathFinder<T> {
return this.wrappers.reduce(
const pathFinder = this.wrappers.reduce(
(pf, wrapper) => wrapper(pf),
this.core as PathFinder<T>,
);
const _findPath = pathFinder.findPath;
pathFinder.findPath = function (from: T | T[], to: T): T[] | null {
return DebugSpan.wrap("findPath", () => _findPath.call(this, from, to));
};
return pathFinder;
}
/**
@@ -1,4 +1,5 @@
import { GameMap } from "../../game/GameMap";
import { DebugSpan } from "../../utilities/DebugSpan";
import { PathFinder } from "../types";
import { AStar, AStarAdapter } from "./AStar";
@@ -11,7 +12,9 @@ export class AStarRail implements PathFinder<number> {
}
findPath(from: number | number[], to: number): number[] | null {
return this.aStar.findPath(from, to);
return DebugSpan.wrap("AStar.Rail:findPath", () =>
this.aStar.findPath(from, to),
);
}
}
+80 -26
View File
@@ -3,6 +3,16 @@ import { PathFinder } from "../types";
import { BucketQueue, PriorityQueue } from "./PriorityQueue";
const LAND_BIT = 7; // Bit 7 in terrain indicates land
const MAGNITUDE_MASK = 0x1f;
const COST_SCALE = 100;
const BASE_COST = 1 * COST_SCALE;
// Prefer magnitude 3-10 (3-10 tiles from shore)
function getMagnitudePenalty(magnitude: number): number {
if (magnitude < 3) return 10 * COST_SCALE; // too close to shore
if (magnitude <= 10) return 0; // sweet spot
return 1 * COST_SCALE; // deep water, slight penalty
}
export interface AStarWaterConfig {
heuristicWeight?: number;
@@ -27,7 +37,7 @@ export class AStarWater implements PathFinder<number> {
this.terrain = (map as any).terrain as Uint8Array;
this.width = map.width();
this.numNodes = map.width() * map.height();
this.heuristicWeight = config?.heuristicWeight ?? 15;
this.heuristicWeight = config?.heuristicWeight ?? 5;
this.maxIterations = config?.maxIterations ?? 1_000_000;
this.closedStamp = new Uint32Array(this.numNodes);
@@ -35,7 +45,10 @@ export class AStarWater implements PathFinder<number> {
this.gScore = new Uint32Array(this.numNodes);
this.cameFrom = new Int32Array(this.numNodes);
const maxF = this.heuristicWeight * (map.width() + map.height());
// Account for scaled costs + tie-breaker headroom
const maxDim = map.width() + map.height();
const maxF =
(this.heuristicWeight + 1) * BASE_COST * maxDim + COST_SCALE * maxDim;
this.queue = new BucketQueue(maxF);
}
@@ -64,13 +77,32 @@ export class AStarWater implements PathFinder<number> {
queue.clear();
const starts = Array.isArray(start) ? start : [start];
// For cross-product tie-breaker (prefer diagonal paths)
const s0 = starts[0];
const startX = s0 % width;
const startY = (s0 / width) | 0;
const dxGoal = goalX - startX;
const dyGoal = goalY - startY;
// Normalization factor to keep tie-breaker small (< COST_SCALE)
const crossNorm = Math.max(1, Math.abs(dxGoal) + Math.abs(dyGoal));
// Cross-product tie-breaker: measures deviation from start-goal line
const crossTieBreaker = (nx: number, ny: number): number => {
const dxN = nx - goalX;
const dyN = ny - goalY;
const cross = Math.abs(dxGoal * dyN - dyGoal * dxN);
return Math.floor((cross * (COST_SCALE - 1)) / crossNorm / crossNorm);
};
for (const s of starts) {
gScore[s] = 0;
gScoreStamp[s] = stamp;
cameFrom[s] = -1;
const sx = s % width;
const sy = (s / width) | 0;
const h = weight * (Math.abs(sx - goalX) + Math.abs(sy - goalY));
const h =
weight * BASE_COST * (Math.abs(sx - goalX) + Math.abs(sy - goalY));
queue.push(s, h);
}
@@ -91,15 +123,19 @@ export class AStarWater implements PathFinder<number> {
}
const currentG = gScore[current];
const tentativeG = currentG + 1;
const currentX = current % width;
const currentY = (current / width) | 0;
if (current >= width) {
const neighbor = current - width;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighbor] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighbor] !== stamp ||
tentativeG < gScore[neighbor]
@@ -107,11 +143,12 @@ export class AStarWater implements PathFinder<number> {
cameFrom[neighbor] = current;
gScore[neighbor] = tentativeG;
gScoreStamp[neighbor] = stamp;
const nx = neighbor % width;
const ny = (neighbor / width) | 0;
const f =
tentativeG +
weight * (Math.abs(nx - goalX) + Math.abs(ny - goalY));
const ny = currentY - 1;
const h =
weight *
BASE_COST *
(Math.abs(currentX - goalX) + Math.abs(ny - goalY));
const f = tentativeG + h + crossTieBreaker(currentX, ny);
queue.push(neighbor, f);
}
}
@@ -119,10 +156,14 @@ export class AStarWater implements PathFinder<number> {
if (current < numNodes - width) {
const neighbor = current + width;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighbor] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighbor] !== stamp ||
tentativeG < gScore[neighbor]
@@ -130,11 +171,12 @@ export class AStarWater implements PathFinder<number> {
cameFrom[neighbor] = current;
gScore[neighbor] = tentativeG;
gScoreStamp[neighbor] = stamp;
const nx = neighbor % width;
const ny = (neighbor / width) | 0;
const f =
tentativeG +
weight * (Math.abs(nx - goalX) + Math.abs(ny - goalY));
const ny = currentY + 1;
const h =
weight *
BASE_COST *
(Math.abs(currentX - goalX) + Math.abs(ny - goalY));
const f = tentativeG + h + crossTieBreaker(currentX, ny);
queue.push(neighbor, f);
}
}
@@ -142,10 +184,14 @@ export class AStarWater implements PathFinder<number> {
if (currentX !== 0) {
const neighbor = current - 1;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighbor] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighbor] !== stamp ||
tentativeG < gScore[neighbor]
@@ -153,10 +199,12 @@ export class AStarWater implements PathFinder<number> {
cameFrom[neighbor] = current;
gScore[neighbor] = tentativeG;
gScoreStamp[neighbor] = stamp;
const ny = (neighbor / width) | 0;
const f =
tentativeG +
weight * (Math.abs(currentX - 1 - goalX) + Math.abs(ny - goalY));
const nx = currentX - 1;
const h =
weight *
BASE_COST *
(Math.abs(nx - goalX) + Math.abs(currentY - goalY));
const f = tentativeG + h + crossTieBreaker(nx, currentY);
queue.push(neighbor, f);
}
}
@@ -164,10 +212,14 @@ export class AStarWater implements PathFinder<number> {
if (currentX !== width - 1) {
const neighbor = current + 1;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighbor] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighbor] !== stamp ||
tentativeG < gScore[neighbor]
@@ -175,10 +227,12 @@ export class AStarWater implements PathFinder<number> {
cameFrom[neighbor] = current;
gScore[neighbor] = tentativeG;
gScoreStamp[neighbor] = stamp;
const ny = (neighbor / width) | 0;
const f =
tentativeG +
weight * (Math.abs(currentX + 1 - goalX) + Math.abs(ny - goalY));
const nx = currentX + 1;
const h =
weight *
BASE_COST *
(Math.abs(nx - goalX) + Math.abs(currentY - goalY));
const f = tentativeG + h + crossTieBreaker(nx, currentY);
queue.push(neighbor, f);
}
}
@@ -3,6 +3,16 @@ import { PathFinder } from "../types";
import { BucketQueue } from "./PriorityQueue";
const LAND_BIT = 7;
const MAGNITUDE_MASK = 0x1f;
const COST_SCALE = 100;
const BASE_COST = 1 * COST_SCALE;
// Prefer magnitude 3-10 (3-10 tiles from shore)
function getMagnitudePenalty(magnitude: number): number {
if (magnitude < 3) return 3 * COST_SCALE; // too close to shore
if (magnitude <= 10) return 0; // sweet spot
return 1 * COST_SCALE; // deep water, slight penalty
}
export interface BoundedAStarConfig {
heuristicWeight?: number;
@@ -16,7 +26,7 @@ export interface SearchBounds {
maxY: number;
}
export class AStarBounded implements PathFinder<number> {
export class AStarWaterBounded implements PathFinder<number> {
private stamp = 1;
private readonly closedStamp: Uint32Array;
@@ -36,7 +46,7 @@ export class AStarBounded implements PathFinder<number> {
) {
this.terrain = (map as any).terrain as Uint8Array;
this.mapWidth = map.width();
this.heuristicWeight = config?.heuristicWeight ?? 1;
this.heuristicWeight = config?.heuristicWeight ?? 3;
this.maxIterations = config?.maxIterations ?? 100_000;
this.closedStamp = new Uint32Array(maxSearchArea);
@@ -45,7 +55,9 @@ export class AStarBounded implements PathFinder<number> {
this.cameFrom = new Int32Array(maxSearchArea);
const maxDim = Math.ceil(Math.sqrt(maxSearchArea));
const maxF = this.heuristicWeight * maxDim * 2;
// Account for scaled costs + tie-breaker headroom
const maxF =
(this.heuristicWeight + 1) * BASE_COST * maxDim * 2 + COST_SCALE * maxDim;
this.queue = new BucketQueue(maxF);
}
@@ -133,6 +145,24 @@ export class AStarBounded implements PathFinder<number> {
queue.clear();
const starts = Array.isArray(start) ? start : [start];
// For cross-product tie-breaker (prefer diagonal paths)
const s0 = starts[0];
const startX = s0 % mapWidth;
const startY = (s0 / mapWidth) | 0;
const dxGoal = goalX - startX;
const dyGoal = goalY - startY;
// Normalization factor to keep tie-breaker small (< COST_SCALE)
const crossNorm = Math.max(1, Math.abs(dxGoal) + Math.abs(dyGoal));
// Cross-product tie-breaker: measures deviation from start-goal line
const crossTieBreaker = (nx: number, ny: number): number => {
const dxN = nx - goalX;
const dyN = ny - goalY;
const cross = Math.abs(dxGoal * dyN - dyGoal * dxN);
return Math.floor((cross * (COST_SCALE - 1)) / crossNorm / crossNorm);
};
for (const s of starts) {
const startLocal = toLocal(s, true);
if (startLocal < 0 || startLocal >= numLocalNodes) {
@@ -143,7 +173,8 @@ export class AStarBounded implements PathFinder<number> {
cameFrom[startLocal] = -1;
const sx = s % mapWidth;
const sy = (s / mapWidth) | 0;
const h = weight * (Math.abs(sx - goalX) + Math.abs(sy - goalY));
const h =
weight * BASE_COST * (Math.abs(sx - goalX) + Math.abs(sy - goalY));
queue.push(startLocal, h);
}
@@ -164,7 +195,6 @@ export class AStarBounded implements PathFinder<number> {
}
const currentG = gScore[currentLocal];
const tentativeG = currentG + 1;
// Convert to global coords for neighbor calculation
const current = toGlobal(currentLocal);
@@ -174,10 +204,14 @@ export class AStarBounded implements PathFinder<number> {
if (currentY > minY) {
const neighbor = current - mapWidth;
const neighborLocal = currentLocal - boundsWidth;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighborLocal] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighborLocal] !== stamp ||
tentativeG < gScore[neighborLocal]
@@ -185,10 +219,12 @@ export class AStarBounded implements PathFinder<number> {
cameFrom[neighborLocal] = currentLocal;
gScore[neighborLocal] = tentativeG;
gScoreStamp[neighborLocal] = stamp;
const f =
tentativeG +
const ny = currentY - 1;
const h =
weight *
(Math.abs(currentX - goalX) + Math.abs(currentY - 1 - goalY));
BASE_COST *
(Math.abs(currentX - goalX) + Math.abs(ny - goalY));
const f = tentativeG + h + crossTieBreaker(currentX, ny);
queue.push(neighborLocal, f);
}
}
@@ -197,10 +233,14 @@ export class AStarBounded implements PathFinder<number> {
if (currentY < maxY) {
const neighbor = current + mapWidth;
const neighborLocal = currentLocal + boundsWidth;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighborLocal] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighborLocal] !== stamp ||
tentativeG < gScore[neighborLocal]
@@ -208,10 +248,12 @@ export class AStarBounded implements PathFinder<number> {
cameFrom[neighborLocal] = currentLocal;
gScore[neighborLocal] = tentativeG;
gScoreStamp[neighborLocal] = stamp;
const f =
tentativeG +
const ny = currentY + 1;
const h =
weight *
(Math.abs(currentX - goalX) + Math.abs(currentY + 1 - goalY));
BASE_COST *
(Math.abs(currentX - goalX) + Math.abs(ny - goalY));
const f = tentativeG + h + crossTieBreaker(currentX, ny);
queue.push(neighborLocal, f);
}
}
@@ -220,10 +262,14 @@ export class AStarBounded implements PathFinder<number> {
if (currentX > minX) {
const neighbor = current - 1;
const neighborLocal = currentLocal - 1;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighborLocal] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighborLocal] !== stamp ||
tentativeG < gScore[neighborLocal]
@@ -231,10 +277,12 @@ export class AStarBounded implements PathFinder<number> {
cameFrom[neighborLocal] = currentLocal;
gScore[neighborLocal] = tentativeG;
gScoreStamp[neighborLocal] = stamp;
const f =
tentativeG +
const nx = currentX - 1;
const h =
weight *
(Math.abs(currentX - 1 - goalX) + Math.abs(currentY - goalY));
BASE_COST *
(Math.abs(nx - goalX) + Math.abs(currentY - goalY));
const f = tentativeG + h + crossTieBreaker(nx, currentY);
queue.push(neighborLocal, f);
}
}
@@ -243,10 +291,14 @@ export class AStarBounded implements PathFinder<number> {
if (currentX < maxX) {
const neighbor = current + 1;
const neighborLocal = currentLocal + 1;
const neighborTerrain = terrain[neighbor];
if (
closedStamp[neighborLocal] !== stamp &&
(neighbor === goal || (terrain[neighbor] & landMask) === 0)
(neighbor === goal || (neighborTerrain & landMask) === 0)
) {
const magnitude = neighborTerrain & MAGNITUDE_MASK;
const cost = BASE_COST + getMagnitudePenalty(magnitude);
const tentativeG = currentG + cost;
if (
gScoreStamp[neighborLocal] !== stamp ||
tentativeG < gScore[neighborLocal]
@@ -254,10 +306,12 @@ export class AStarBounded implements PathFinder<number> {
cameFrom[neighborLocal] = currentLocal;
gScore[neighborLocal] = tentativeG;
gScoreStamp[neighborLocal] = stamp;
const f =
tentativeG +
const nx = currentX + 1;
const h =
weight *
(Math.abs(currentX + 1 - goalX) + Math.abs(currentY - goalY));
BASE_COST *
(Math.abs(nx - goalX) + Math.abs(currentY - goalY));
const f = tentativeG + h + crossTieBreaker(nx, currentY);
queue.push(neighborLocal, f);
}
}
@@ -1,39 +1,19 @@
import { GameMap, TileRef } from "../../game/GameMap";
import { DebugSpan } from "../../utilities/DebugSpan";
import { PathFinder } from "../types";
import { AbstractGraphAStar } from "./AStar.AbstractGraph";
import { AStarBounded } from "./AStar.Bounded";
import { AStarWaterBounded } from "./AStar.WaterBounded";
import { AbstractGraph, AbstractNode } from "./AbstractGraph";
import { BFSGrid } from "./BFS.Grid";
import { LAND_MARKER } from "./ConnectedComponents";
type PathDebugInfo = {
nodePath: TileRef[] | null;
initialPath: TileRef[] | null;
graph: {
clusterSize: number;
nodes: Array<{ id: number; tile: TileRef }>;
edges: Array<{
id: number;
nodeA: number;
nodeB: number;
from: TileRef;
to: TileRef;
cost: number;
}>;
};
timings: { [key: string]: number };
};
export class AStarWaterHierarchical implements PathFinder<number> {
private tileBFS: BFSGrid;
private abstractAStar: AbstractGraphAStar;
private localAStar: AStarBounded;
private localAStarMultiCluster: AStarBounded;
private localAStar: AStarWaterBounded;
private localAStarMultiCluster: AStarWaterBounded;
private sourceResolver: SourceResolver;
public debugInfo: PathDebugInfo | null = null;
public debugMode: boolean = false;
constructor(
private map: GameMap,
private graph: AbstractGraph,
@@ -51,23 +31,31 @@ export class AStarWaterHierarchical implements PathFinder<number> {
// BoundedAStar for cluster-bounded local pathfinding
const maxLocalNodes = clusterSize * clusterSize;
this.localAStar = new AStarBounded(map, maxLocalNodes);
this.localAStar = new AStarWaterBounded(map, maxLocalNodes);
// BoundedAStar for multi-cluster (3x3) local pathfinding
const multiClusterSize = clusterSize * 3;
const maxMultiClusterNodes = multiClusterSize * multiClusterSize;
this.localAStarMultiCluster = new AStarBounded(map, maxMultiClusterNodes);
this.localAStarMultiCluster = new AStarWaterBounded(
map,
maxMultiClusterNodes,
);
// SourceResolver for multi-source search
this.sourceResolver = new SourceResolver(this.map, this.graph);
}
findPath(from: number | number[], to: number): number[] | null {
if (Array.isArray(from)) {
return this.findPathMultiSource(from as TileRef[], to as TileRef);
}
return DebugSpan.wrap("AStar.WaterHierarchical:findPath", () => {
DebugSpan.set("$to", () => to);
DebugSpan.set("$from", () => from);
return this.findPathSingle(from as TileRef, to as TileRef, this.debugMode);
if (Array.isArray(from)) {
return this.findPathMultiSource(from as TileRef[], to as TileRef);
}
return this.findPathSingle(from as TileRef, to as TileRef);
});
}
private findPathMultiSource(
@@ -94,192 +82,66 @@ export class AStarWaterHierarchical implements PathFinder<number> {
return this.findPathSingle(winningSource, target);
}
findPathSingle(
from: TileRef,
to: TileRef,
debug: boolean = false,
): TileRef[] | null {
if (debug) {
const allEdges: Array<{
id: number;
nodeA: number;
nodeB: number;
from: TileRef;
to: TileRef;
cost: number;
}> = [];
for (let edgeId = 0; edgeId < this.graph.edgeCount; edgeId++) {
const edge = this.graph.getEdge(edgeId);
if (!edge) continue;
const nodeA = this.graph.getNode(edge.nodeA);
const nodeB = this.graph.getNode(edge.nodeB);
if (!nodeA || !nodeB) continue;
allEdges.push({
id: edge.id,
nodeA: edge.nodeA,
nodeB: edge.nodeB,
from: nodeA.tile,
to: nodeB.tile,
cost: edge.cost,
});
}
this.debugInfo = {
nodePath: null,
initialPath: null,
graph: {
clusterSize: this.graph.clusterSize,
nodes: this.graph
.getAllNodes()
.map((node) => ({ id: node.id, tile: node.tile })),
edges: allEdges,
},
timings: {
total: 0,
},
};
}
findPathSingle(from: TileRef, to: TileRef): TileRef[] | null {
const dist = this.map.manhattanDist(from, to);
// Early exit for very short distances
if (dist <= this.graph.clusterSize) {
performance.mark("hpa:findPath:earlyExitLocalPath:start");
DebugSpan.start("earlyExit");
const startX = this.map.x(from);
const startY = this.map.y(from);
const clusterX = Math.floor(startX / this.graph.clusterSize);
const clusterY = Math.floor(startY / this.graph.clusterSize);
const localPath = this.findLocalPath(from, to, clusterX, clusterY, true);
performance.mark("hpa:findPath:earlyExitLocalPath:end");
const measure = performance.measure(
"hpa:findPath:earlyExitLocalPath",
"hpa:findPath:earlyExitLocalPath:start",
"hpa:findPath:earlyExitLocalPath:end",
);
if (debug) {
this.debugInfo!.timings.earlyExitLocalPath = measure.duration;
this.debugInfo!.timings.total += measure.duration;
}
DebugSpan.end();
if (localPath) {
if (debug) {
console.log(
`[DEBUG] Direct local path found for dist=${dist}, length=${localPath.length}`,
);
}
return localPath;
}
if (debug) {
console.log(
`[DEBUG] Direct path failed for dist=${dist}, falling back to abstract graph`,
);
}
}
performance.mark("hpa:findPath:findNodes:start");
DebugSpan.start("nodeLookup");
const startNode = this.findNearestNode(from);
const endNode = this.findNearestNode(to);
performance.mark("hpa:findPath:findNodes:end");
const findNodesMeasure = performance.measure(
"hpa:findPath:findNodes",
"hpa:findPath:findNodes:start",
"hpa:findPath:findNodes:end",
);
if (debug) {
this.debugInfo!.timings.findNodes = findNodesMeasure.duration;
this.debugInfo!.timings.total += findNodesMeasure.duration;
}
DebugSpan.end();
if (!startNode) {
if (debug) {
console.log(
`[DEBUG] Cannot find start node for (${this.map.x(from)}, ${this.map.y(from)})`,
);
}
return null;
}
if (!endNode) {
if (debug) {
console.log(
`[DEBUG] Cannot find end node for (${this.map.x(to)}, ${this.map.y(to)})`,
);
}
return null;
}
if (startNode.id === endNode.id) {
if (debug) {
console.log(
`[DEBUG] Start and end nodes are the same (ID=${startNode.id}), finding local path with multi-cluster search`,
);
}
performance.mark("hpa:findPath:sameNodeLocalPath:start");
DebugSpan.start("sameNodeLocalPath");
const clusterX = Math.floor(startNode.x / this.graph.clusterSize);
const clusterY = Math.floor(startNode.y / this.graph.clusterSize);
const path = this.findLocalPath(from, to, clusterX, clusterY, true);
performance.mark("hpa:findPath:sameNodeLocalPath:end");
const sameNodeMeasure = performance.measure(
"hpa:findPath:sameNodeLocalPath",
"hpa:findPath:sameNodeLocalPath:start",
"hpa:findPath:sameNodeLocalPath:end",
);
if (debug) {
this.debugInfo!.timings.sameNodeLocalPath = sameNodeMeasure.duration;
this.debugInfo!.timings.total += sameNodeMeasure.duration;
}
DebugSpan.end();
return path;
}
performance.mark("hpa:findPath:findAbstractPath:start");
DebugSpan.start("abstractPath");
const nodePath = this.findAbstractPath(startNode.id, endNode.id);
performance.mark("hpa:findPath:findAbstractPath:end");
const findAbstractPathMeasure = performance.measure(
"hpa:findPath:findAbstractPath",
"hpa:findPath:findAbstractPath:start",
"hpa:findPath:findAbstractPath:end",
);
if (debug) {
this.debugInfo!.timings.findAbstractPath =
findAbstractPathMeasure.duration;
this.debugInfo!.timings.total += findAbstractPathMeasure.duration;
this.debugInfo!.nodePath = nodePath
? nodePath
.map((nodeId) => {
const node = this.graph.getNode(nodeId);
return node ? node.tile : -1;
})
.filter((tile) => tile !== -1)
: null;
}
DebugSpan.end();
if (!nodePath) {
if (debug) {
console.log(
`[DEBUG] No abstract path between nodes ${startNode.id} and ${endNode.id}`,
);
}
return null;
}
if (debug) {
console.log(`[DEBUG] Abstract path found: ${nodePath.length} waypoints`);
}
DebugSpan.set("nodePath", () =>
nodePath
.map((nodeId) => {
const node = this.graph.getNode(nodeId);
return node ? node.tile : -1;
})
.filter((tile) => tile !== -1),
);
const initialPath: TileRef[] = [];
performance.mark("hpa:findPath:buildInitialPath:start");
DebugSpan.start("initialPath");
// 1. Find path from start to first node
const firstNode = this.graph.getNode(nodePath[0])!;
@@ -324,6 +186,10 @@ export class AStarWaterHierarchical implements PathFinder<number> {
if (cachedPath && cachedPath.length > 0) {
// Path is cached for this exact direction, use as-is
initialPath.push(...cachedPath.slice(1));
DebugSpan.set(
"$cachedSegmentsUsed",
(prev) => ((prev as number) ?? 0) + 1,
);
continue;
}
}
@@ -368,20 +234,7 @@ export class AStarWaterHierarchical implements PathFinder<number> {
initialPath.push(...endSegment.slice(1));
performance.mark("hpa:findPath:buildInitialPath:end");
const buildInitialPathMeasure = performance.measure(
"hpa:findPath:buildInitialPath",
"hpa:findPath:buildInitialPath:start",
"hpa:findPath:buildInitialPath:end",
);
if (debug) {
this.debugInfo!.timings.buildInitialPath =
buildInitialPathMeasure.duration;
this.debugInfo!.timings.total += buildInitialPathMeasure.duration;
this.debugInfo!.initialPath = initialPath;
console.log(`[DEBUG] Initial path: ${initialPath.length} tiles`);
}
DebugSpan.set("initialPath", () => initialPath);
// Smoothing moved to SmoothingTransformer - return raw path
return initialPath;
+18 -120
View File
@@ -1,4 +1,5 @@
import { GameMap, TileRef } from "../../game/GameMap";
import { DebugSpan } from "../../utilities/DebugSpan";
import { BFSGrid } from "./BFS.Grid";
import { ConnectedComponents } from "./ConnectedComponents";
@@ -25,16 +26,6 @@ export interface Cluster {
nodeIds: number[];
}
export type BuildDebugInfo = {
clusters: number | null;
nodes: number | null;
edges: number | null;
actualBFSCalls: number | null;
potentialBFSCalls: number | null;
skippedByComponentFilter: number | null;
timings: { [key: string]: number };
};
export class AbstractGraph {
// Nodes (array indexed by id)
private readonly _nodes: AbstractNode[] = [];
@@ -97,6 +88,10 @@ export class AbstractGraph {
return edge.nodeA === nodeId ? edge.nodeB : edge.nodeA;
}
getAllEdges(): readonly AbstractEdge[] {
return this._edges;
}
get edgeCount(): number {
return this._edges.length;
}
@@ -217,8 +212,6 @@ export class AbstractGraphBuilder {
private nextEdgeId = 0;
private edgeBetween = new Map<number, Map<number, AbstractEdge>>();
public debugInfo: BuildDebugInfo | null = null;
constructor(
private readonly map: GameMap,
private readonly clusterSize: number = AbstractGraphBuilder.CLUSTER_SIZE,
@@ -231,8 +224,8 @@ export class AbstractGraphBuilder {
this.waterComponents = new ConnectedComponents(map);
}
build(debug: boolean = false): AbstractGraph {
performance.mark("abstractgraph:build:start");
build(): AbstractGraph {
DebugSpan.start("AbstractGraphBuilder:build");
this.graph = new AbstractGraph(
this.clusterSize,
@@ -240,37 +233,8 @@ export class AbstractGraphBuilder {
this.clustersY,
);
if (debug) {
console.log(
`[DEBUG] Building abstract graph with cluster size ${this.clusterSize} (${this.clustersX}x${this.clustersY} clusters)`,
);
this.debugInfo = {
clusters: null,
nodes: null,
edges: null,
actualBFSCalls: null,
potentialBFSCalls: null,
skippedByComponentFilter: null,
timings: {},
};
}
// Initialize water components
performance.mark("abstractgraph:build:water-component:start");
this.waterComponents.initialize();
performance.mark("abstractgraph:build:water-component:end");
const wcMeasure = performance.measure(
"abstractgraph:build:water-component",
"abstractgraph:build:water-component:start",
"abstractgraph:build:water-component:end",
);
if (debug) {
console.log(
`[DEBUG] Water Component Identification: ${wcMeasure.duration.toFixed(2)}ms`,
);
}
// Pre-create all clusters
for (let cy = 0; cy < this.clustersY; cy++) {
@@ -281,98 +245,30 @@ export class AbstractGraphBuilder {
}
// Find nodes (gateways) at cluster boundaries
performance.mark("abstractgraph:build:nodes:start");
DebugSpan.start("nodes");
for (let cy = 0; cy < this.clustersY; cy++) {
for (let cx = 0; cx < this.clustersX; cx++) {
this.processCluster(cx, cy);
}
}
performance.mark("abstractgraph:build:nodes:end");
const nodesMeasure = performance.measure(
"abstractgraph:build:nodes",
"abstractgraph:build:nodes:start",
"abstractgraph:build:nodes:end",
);
if (debug) {
console.log(
`[DEBUG] Node identification: ${nodesMeasure.duration.toFixed(2)}ms`,
);
this.debugInfo!.potentialBFSCalls = 0;
this.debugInfo!.skippedByComponentFilter = 0;
}
DebugSpan.end();
// Build edges between nodes in same cluster
performance.mark("abstractgraph:build:edges:start");
DebugSpan.start("edges");
for (let cy = 0; cy < this.clustersY; cy++) {
for (let cx = 0; cx < this.clustersX; cx++) {
const cluster = this.graph.getCluster(cx, cy);
if (!cluster || cluster.nodeIds.length === 0) continue;
if (debug) {
const n = cluster.nodeIds.length;
this.debugInfo!.potentialBFSCalls! += (n * (n - 1)) / 2;
// Count skipped by component filter
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const nodeI = this.graph.getNode(cluster.nodeIds[i])!;
const nodeJ = this.graph.getNode(cluster.nodeIds[j])!;
if (nodeI.componentId !== nodeJ.componentId) {
this.debugInfo!.skippedByComponentFilter!++;
}
}
}
}
this.buildClusterConnections(cx, cy);
}
}
performance.mark("abstractgraph:build:edges:end");
const edgesMeasure = performance.measure(
"abstractgraph:build:edges",
"abstractgraph:build:edges:start",
"abstractgraph:build:edges:end",
);
DebugSpan.end();
if (debug) {
this.debugInfo!.actualBFSCalls =
this.debugInfo!.potentialBFSCalls! -
this.debugInfo!.skippedByComponentFilter!;
console.log(
`[DEBUG] Edge identification: ${edgesMeasure.duration.toFixed(2)}ms`,
);
console.log(
`[DEBUG] Potential BFS calls: ${this.debugInfo!.potentialBFSCalls}`,
);
console.log(
`[DEBUG] Skipped by component filter: ${this.debugInfo!.skippedByComponentFilter} (${((this.debugInfo!.skippedByComponentFilter! / this.debugInfo!.potentialBFSCalls!) * 100).toFixed(1)}%)`,
);
console.log(
`[DEBUG] Actual BFS calls: ${this.debugInfo!.actualBFSCalls}`,
);
}
performance.mark("abstractgraph:build:end");
const totalMeasure = performance.measure(
"abstractgraph:build:total",
"abstractgraph:build:start",
"abstractgraph:build:end",
);
if (debug) {
console.log(
`[DEBUG] Abstract graph built in ${totalMeasure.duration.toFixed(2)}ms`,
);
console.log(`[DEBUG] Nodes: ${this.graph.nodeCount}`);
console.log(`[DEBUG] Edges: ${this.graph.edgeCount}`);
console.log(`[DEBUG] Clusters: ${this.clustersX * this.clustersY}`);
this.debugInfo!.clusters = this.clustersX * this.clustersY;
this.debugInfo!.nodes = this.graph.nodeCount;
this.debugInfo!.edges = this.graph.edgeCount;
}
DebugSpan.set("nodes", () => this.graph.getAllNodes());
DebugSpan.set("edges", () => this.graph.getAllEdges());
DebugSpan.set("nodesCount", () => this.graph.nodeCount);
DebugSpan.set("edgesCount", () => this.graph.edgeCount);
DebugSpan.set("clustersCount", () => this.clustersX * this.clustersY);
// Initialize path cache after all edges are built
this.graph._initPathCache();
@@ -380,6 +276,8 @@ export class AbstractGraphBuilder {
// Store water components for componentId lookups
this.graph.setWaterComponents(this.waterComponents);
DebugSpan.end(); // AbstractGraphBuilder:build
return this.graph;
}
@@ -1,6 +1,7 @@
// Connected Component Labeling using flood-fill
import { GameMap, TileRef } from "../../game/GameMap";
import { DebugSpan } from "../../utilities/DebugSpan";
export const LAND_MARKER = 0xff; // Must fit in Uint8Array
@@ -28,6 +29,7 @@ export class ConnectedComponents {
}
initialize(): void {
DebugSpan.start("ConnectedComponents:initialize");
let ids: Uint8Array | Uint16Array = this.createPrefilledIds();
let nextId = 0;
@@ -52,6 +54,7 @@ export class ConnectedComponents {
}
this.componentIds = ids;
DebugSpan.end();
}
/**
@@ -1,168 +0,0 @@
import { GameMap, TileRef } from "../../game/GameMap";
import { PathFinder } from "../types";
import { PathSmoother } from "./PathSmoother";
/**
* Path smoother using Bresenham line-of-sight algorithm.
* Greedily skips waypoints when direct traversal is possible.
*/
export class BresenhamPathSmoother implements PathSmoother<TileRef> {
constructor(
private map: GameMap,
private isTraversable: (tile: TileRef) => boolean,
) {}
smooth(path: TileRef[]): TileRef[] {
if (path.length <= 2) {
return path;
}
const smoothed: TileRef[] = [];
let current = 0;
while (current < path.length - 1) {
let farthest = current + 1;
let bestTrace: TileRef[] | null = null;
for (
let i = current + 2;
i < path.length;
i += Math.max(1, Math.floor(path.length / 20))
) {
const trace = this.tracePath(path[current], path[i]);
if (trace !== null) {
farthest = i;
bestTrace = trace;
} else {
break;
}
}
if (
farthest < path.length - 1 &&
(path.length - 1 - current) % 10 !== 0
) {
const trace = this.tracePath(path[current], path[path.length - 1]);
if (trace !== null) {
farthest = path.length - 1;
bestTrace = trace;
}
}
if (bestTrace !== null && farthest > current + 1) {
smoothed.push(...bestTrace.slice(0, -1));
} else {
smoothed.push(path[current]);
}
current = farthest;
}
smoothed.push(path[path.length - 1]);
return smoothed;
}
private tracePath(from: TileRef, to: TileRef): TileRef[] | null {
const x0 = this.map.x(from);
const y0 = this.map.y(from);
const x1 = this.map.x(to);
const y1 = this.map.y(to);
const tiles: TileRef[] = [];
const dx = Math.abs(x1 - x0);
const dy = Math.abs(y1 - y0);
const sx = x0 < x1 ? 1 : -1;
const sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
let x = x0;
let y = y0;
const maxTiles = 100000;
let iterations = 0;
while (true) {
if (iterations++ > maxTiles) {
return null;
}
const tile = this.map.ref(x, y);
if (!this.isTraversable(tile)) {
return null;
}
tiles.push(tile);
if (x === x1 && y === y1) {
break;
}
const e2 = 2 * err;
const shouldMoveX = e2 > -dy;
const shouldMoveY = e2 < dx;
if (shouldMoveX && shouldMoveY) {
x += sx;
err -= dy;
const intermediateTile = this.map.ref(x, y);
if (!this.isTraversable(intermediateTile)) {
x -= sx;
err += dy;
y += sy;
err += dx;
const altTile = this.map.ref(x, y);
if (!this.isTraversable(altTile)) {
return null;
}
tiles.push(altTile);
x += sx;
err -= dy;
} else {
tiles.push(intermediateTile);
y += sy;
err += dx;
}
} else {
if (shouldMoveX) {
x += sx;
err -= dy;
}
if (shouldMoveY) {
y += sy;
err += dx;
}
}
}
return tiles;
}
}
/**
* Ready-to-use transformer that applies Bresenham smoothing.
* Defaults to water traversability.
*/
export class BresenhamSmoothingTransformer implements PathFinder<TileRef> {
private smoother: BresenhamPathSmoother;
constructor(
private inner: PathFinder<TileRef>,
map: GameMap,
isTraversable: (tile: TileRef) => boolean = (t) => map.isWater(t),
) {
this.smoother = new BresenhamPathSmoother(map, isTraversable);
}
findPath(from: TileRef | TileRef[], to: TileRef): TileRef[] | null {
const path = this.inner.findPath(from, to);
return path ? this.smoother.smooth(path) : null;
}
}
@@ -1,7 +0,0 @@
/**
* PathSmoother - interface for path smoothing algorithms.
* Takes a path and returns a smoothed version.
*/
export interface PathSmoother<T> {
smooth(path: T[]): T[];
}
@@ -1,18 +0,0 @@
import { PathFinder } from "../types";
import { PathSmoother } from "./PathSmoother";
/**
* Transformer that applies path smoothing to any PathFinder.
* Wraps an inner PathFinder and smooths its output.
*/
export class SmoothingTransformer<T> implements PathFinder<T> {
constructor(
private inner: PathFinder<T>,
private smoother: PathSmoother<T>,
) {}
findPath(from: T | T[], to: T): T[] | null {
const path = this.inner.findPath(from, to);
return path ? this.smoother.smooth(path) : null;
}
}
@@ -0,0 +1,341 @@
import { GameMap, TileRef } from "../../game/GameMap";
import { DebugSpan } from "../../utilities/DebugSpan";
import {
AStarWaterBounded,
SearchBounds,
} from "../algorithms/AStar.WaterBounded";
import { PathFinder } from "../types";
const ENDPOINT_REFINEMENT_TILES = 50;
const LOCAL_ASTAR_MAX_AREA = 100 * 100;
const LOS_MIN_MAGNITUDE = 3;
const MAGNITUDE_MASK = 0x1f;
/**
* Water path smoother transformer with two passes:
* 1. Binary search LOS smoothing (avoids shallow water)
* 2. Local A* refinement on endpoints (first/last N tiles)
*/
export class SmoothingWaterTransformer implements PathFinder<TileRef> {
private readonly mapWidth: number;
private readonly localAStar: AStarWaterBounded;
private readonly terrain: Uint8Array;
private readonly isTraversable: (tile: TileRef) => boolean;
constructor(
private inner: PathFinder<TileRef>,
private map: GameMap,
isTraversable: (tile: TileRef) => boolean = (t) => map.isWater(t),
) {
this.mapWidth = map.width();
this.localAStar = new AStarWaterBounded(map, LOCAL_ASTAR_MAX_AREA);
this.terrain = (map as any).terrain as Uint8Array;
this.isTraversable = isTraversable;
}
findPath(from: TileRef | TileRef[], to: TileRef): TileRef[] | null {
const path = this.inner.findPath(from, to);
return DebugSpan.wrap("smoothingTransformer", () =>
path ? this.smooth(path) : null,
);
}
private smooth(path: TileRef[]): TileRef[] {
if (path.length <= 2) {
return path;
}
// Pass 1: LOS smoothing with binary search
let smoothed = DebugSpan.wrap("smoother:los", () => this.losSmooth(path));
// Pass 2: Local A* refinement on endpoints
smoothed = DebugSpan.wrap("smoother:refine", () =>
this.refineEndpoints(smoothed),
);
return smoothed;
}
private losSmooth(path: TileRef[]): TileRef[] {
const result: TileRef[] = [path[0]];
let current = 0;
while (current < path.length - 1) {
// Binary search for farthest visible waypoint
let lo = current + 1;
let hi = path.length - 1;
let farthest = lo;
while (lo <= hi) {
const mid = (lo + hi) >>> 1;
if (this.canSee(path[current], path[mid])) {
farthest = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
// Trace the path to farthest visible point
if (farthest > current + 1) {
const trace = this.tracePath(path[current], path[farthest]);
if (trace) {
// Add all intermediate tiles except the last (will be added in next iteration or at end)
for (let i = 1; i < trace.length - 1; i++) {
result.push(trace[i]);
}
}
}
current = farthest;
if (current < path.length - 1) {
result.push(path[current]);
}
}
result.push(path[path.length - 1]);
return result;
}
private refineEndpoints(path: TileRef[]): TileRef[] {
if (path.length <= 2) {
return path;
}
const refineDist = ENDPOINT_REFINEMENT_TILES;
let result = path;
// Find the index where cumulative distance reaches refineDist from start
const startEndIdx = this.findTileAtDistance(path, 0, refineDist, true);
// Refine start segment if it's more than 2 tiles and not already optimal
if (startEndIdx > 1) {
const startSegment = this.refineSegment(path[0], path[startEndIdx]);
if (startSegment && startSegment.length > 0) {
result = [...startSegment.slice(0, -1), ...result.slice(startEndIdx)];
}
}
// Find the index where cumulative distance reaches refineDist from end
const endStartIdx = this.findTileAtDistance(
result,
result.length - 1,
refineDist,
false,
);
// Refine end segment if it's more than 2 tiles and not already optimal
// Search in reverse (from destination backwards) so path approaches target naturally
if (endStartIdx < result.length - 2) {
const endSegment = this.refineSegment(
result[result.length - 1],
result[endStartIdx],
);
if (endSegment && endSegment.length > 0) {
endSegment.reverse();
result = [...result.slice(0, endStartIdx), ...endSegment];
}
}
return result;
}
private findTileAtDistance(
path: TileRef[],
startIdx: number,
distance: number,
forward: boolean,
): number {
let cumDist = 0;
let idx = startIdx;
if (forward) {
while (idx < path.length - 1 && cumDist < distance) {
cumDist += this.manhattanDist(path[idx], path[idx + 1]);
idx++;
}
} else {
while (idx > 0 && cumDist < distance) {
cumDist += this.manhattanDist(path[idx], path[idx - 1]);
idx--;
}
}
return idx;
}
private refineSegment(from: TileRef, to: TileRef): TileRef[] | null {
const x0 = this.map.x(from);
const y0 = this.map.y(from);
const x1 = this.map.x(to);
const y1 = this.map.y(to);
// Calculate bounds with padding
const padding = 10;
const bounds: SearchBounds = {
minX: Math.max(0, Math.min(x0, x1) - padding),
maxX: Math.min(this.map.width() - 1, Math.max(x0, x1) + padding),
minY: Math.max(0, Math.min(y0, y1) - padding),
maxY: Math.min(this.map.height() - 1, Math.max(y0, y1) + padding),
};
return this.localAStar.searchBounded(from, to, bounds);
}
private canSee(from: TileRef, to: TileRef): boolean {
const x0 = from % this.mapWidth;
const y0 = (from / this.mapWidth) | 0;
const x1 = to % this.mapWidth;
const y1 = (to / this.mapWidth) | 0;
const dx = Math.abs(x1 - x0);
const dy = Math.abs(y1 - y0);
const sx = x0 < x1 ? 1 : -1;
const sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
let x = x0;
let y = y0;
const maxTiles = 100000;
let iterations = 0;
while (true) {
if (iterations++ > maxTiles) return false;
const tile = (y * this.mapWidth + x) as TileRef;
if (!this.isTraversable(tile)) return false;
// Check magnitude - avoid shallow water
const magnitude = this.terrain[tile] & MAGNITUDE_MASK;
if (magnitude < LOS_MIN_MAGNITUDE) return false;
if (x === x1 && y === y1) return true;
const e2 = 2 * err;
const shouldMoveX = e2 > -dy;
const shouldMoveY = e2 < dx;
if (shouldMoveX && shouldMoveY) {
// Diagonal move - check intermediate tile
x += sx;
err -= dy;
const intermediateTile = (y * this.mapWidth + x) as TileRef;
const intMag = this.terrain[intermediateTile] & MAGNITUDE_MASK;
if (
!this.isTraversable(intermediateTile) ||
intMag < LOS_MIN_MAGNITUDE
) {
// Try alternative path
x -= sx;
err += dy;
y += sy;
err += dx;
const altTile = (y * this.mapWidth + x) as TileRef;
const altMag = this.terrain[altTile] & MAGNITUDE_MASK;
if (!this.isTraversable(altTile) || altMag < LOS_MIN_MAGNITUDE)
return false;
x += sx;
err -= dy;
} else {
y += sy;
err += dx;
}
} else {
if (shouldMoveX) {
x += sx;
err -= dy;
}
if (shouldMoveY) {
y += sy;
err += dx;
}
}
}
}
private tracePath(from: TileRef, to: TileRef): TileRef[] | null {
const x0 = from % this.mapWidth;
const y0 = (from / this.mapWidth) | 0;
const x1 = to % this.mapWidth;
const y1 = (to / this.mapWidth) | 0;
const tiles: TileRef[] = [];
const dx = Math.abs(x1 - x0);
const dy = Math.abs(y1 - y0);
const sx = x0 < x1 ? 1 : -1;
const sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
let x = x0;
let y = y0;
const maxTiles = 100000;
let iterations = 0;
while (true) {
if (iterations++ > maxTiles) return null;
const tile = (y * this.mapWidth + x) as TileRef;
if (!this.isTraversable(tile)) return null;
tiles.push(tile);
if (x === x1 && y === y1) break;
const e2 = 2 * err;
const shouldMoveX = e2 > -dy;
const shouldMoveY = e2 < dx;
if (shouldMoveX && shouldMoveY) {
x += sx;
err -= dy;
const intermediateTile = (y * this.mapWidth + x) as TileRef;
if (!this.isTraversable(intermediateTile)) {
x -= sx;
err += dy;
y += sy;
err += dx;
const altTile = (y * this.mapWidth + x) as TileRef;
if (!this.isTraversable(altTile)) return null;
tiles.push(altTile);
x += sx;
err -= dy;
} else {
tiles.push(intermediateTile);
y += sy;
err += dx;
}
} else {
if (shouldMoveX) {
x += sx;
err -= dy;
}
if (shouldMoveY) {
y += sy;
err += dx;
}
}
}
return tiles;
}
private manhattanDist(a: TileRef, b: TileRef): number {
const ax = a % this.mapWidth;
const ay = (a / this.mapWidth) | 0;
const bx = b % this.mapWidth;
const by = (b / this.mapWidth) | 0;
return Math.abs(ax - bx) + Math.abs(ay - by);
}
}