Pathfinding refinements (#2959)

## Description:

### Short path for multi-source HPA*

Math was not mathing, increased the bounds to 260x260, it is a bit
slower but should work better. The short path was breaking when player
owned a lot of shores. This is because the bounding box of tiles with
less than 120 distance + 10 padding could be as big as 260x260 and the
optimized array was set to 140x140. I made mistake of calculating it as
`2 * (60 + 10)` instead of `2 * (120 + 10)`.

### LoS path refinement

Previously, we ran 2 passes of LoS smoothing on the path. However, since
we are effectively tracing the same path, the line of sight is
essentially the same. This PR makes second line of sight stop on water
tiles with magnitude `n + 1` compared to first path. Practically, this
means it'll attempt LoS exactly 1 tile after previous corner. See
screenshot.

<img width="1299" height="1151" alt="image"
src="https://github.com/user-attachments/assets/726be236-1ff8-406c-896a-02902a762ab0"
/>

### SendBoatAttackIntentEvent

The flow of sending transport ships is currently strange. This PR makes
the flow more sane.

**Old flow**
```
- Player clicks TARGET tile, it can be deep inland
- Client asks Worker for the best START tile to TARGET tile
- Worker answers `false`, since the tile is inland
- Client sends BoatAttackIntent with START=false and TARGET tiles set
- Worker accepts BoatAttackIntent, computes DESTINATION as closest shore to TARGET
- Worker re-computes best START to DESTINATION
- Worker sends boat from START to DESTINATION
```

**New flow**
```
- Player clicks TARGET tile, it can be deep inland
- Client sends BoatAttackIntent with TARGET
- Worker accepts BoatAttackIntent, computes DESTINATION as closest shore to TARGET
- Worker computes START as the best tile to DESTINATION
- Worker sends boat from START to DESTINATION
```

## 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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

moleole
This commit is contained in:
Arkadiusz Sygulski
2026-01-20 04:28:28 +01:00
committed by GitHub
parent 957d0562e1
commit 18fb513326
12 changed files with 67 additions and 183 deletions
@@ -8,13 +8,15 @@ import { PathFinder } from "../types";
const ENDPOINT_REFINEMENT_TILES = 50;
const LOCAL_ASTAR_MAX_AREA = 100 * 100;
const LOS_MIN_MAGNITUDE = 3;
const LOS_MIN_MAGNITUDE_PASS1 = 2;
const LOS_MIN_MAGNITUDE_PASS2 = 3;
const MAGNITUDE_MASK = 0x1f;
/**
* Water path smoother transformer with two passes:
* Water path smoother transformer:
* 1. Binary search LOS smoothing (avoids shallow water)
* 2. Local A* refinement on endpoints (first/last N tiles)
* 3. Binary search LOS smoothing again (farther from shore)
*/
export class SmoothingWaterTransformer implements PathFinder<TileRef> {
private readonly mapWidth: number;
@@ -47,20 +49,24 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
}
// Pass 1: LOS smoothing with binary search
let smoothed = DebugSpan.wrap("smoother:los", () => this.losSmooth(path));
let smoothed = DebugSpan.wrap("smoother:los", () =>
this.losSmooth(path, LOS_MIN_MAGNITUDE_PASS1),
);
// Pass 2: Local A* refinement on endpoints
smoothed = DebugSpan.wrap("smoother:refine", () =>
this.refineEndpoints(smoothed),
);
// Pass 3: LOS smoothing again (refinement may create new shortcut opportunities)
smoothed = DebugSpan.wrap("smoother:los2", () => this.losSmooth(smoothed));
// Pass 3: LOS smoothing again, farther from the shore
smoothed = DebugSpan.wrap("smoother:los2", () =>
this.losSmooth(smoothed, LOS_MIN_MAGNITUDE_PASS2),
);
return smoothed;
}
private losSmooth(path: TileRef[]): TileRef[] {
private losSmooth(path: TileRef[], minMagnitude: number): TileRef[] {
const result: TileRef[] = [path[0]];
let current = 0;
@@ -72,7 +78,7 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
while (lo <= hi) {
const mid = (lo + hi) >>> 1;
if (this.canSee(path[current], path[mid])) {
if (this.canSee(path[current], path[mid], minMagnitude)) {
farthest = mid;
lo = mid + 1;
} else {
@@ -188,7 +194,7 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
return this.localAStar.searchBounded(from, to, bounds);
}
private canSee(from: TileRef, to: TileRef): boolean {
private canSee(from: TileRef, to: TileRef, minMagnitude: number): boolean {
const x0 = from % this.mapWidth;
const y0 = (from / this.mapWidth) | 0;
const x1 = to % this.mapWidth;
@@ -214,7 +220,7 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
// Check magnitude - avoid shallow water
const magnitude = this.terrain[tile] & MAGNITUDE_MASK;
if (magnitude < LOS_MIN_MAGNITUDE) return false;
if (magnitude < minMagnitude) return false;
if (x === x1 && y === y1) return true;
@@ -229,10 +235,7 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
const intermediateTile = (y * this.mapWidth + x) as TileRef;
const intMag = this.terrain[intermediateTile] & MAGNITUDE_MASK;
if (
!this.isTraversable(intermediateTile) ||
intMag < LOS_MIN_MAGNITUDE
) {
if (!this.isTraversable(intermediateTile) || intMag < minMagnitude) {
// Try alternative path
x -= sx;
err += dy;
@@ -241,7 +244,7 @@ export class SmoothingWaterTransformer implements PathFinder<TileRef> {
const altTile = (y * this.mapWidth + x) as TileRef;
const altMag = this.terrain[altTile] & MAGNITUDE_MASK;
if (!this.isTraversable(altTile) || altMag < LOS_MIN_MAGNITUDE)
if (!this.isTraversable(altTile) || altMag < minMagnitude)
return false;
x += sx;