mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 21:34:14 +00:00
2fcca8ee26
# Pathfinding pt. 4 https://pf-pt-4.openfront.dev/ ## Description: Hello again! Pathfinding. It's fast, but inaccurate. This PR makes it more accurate and actually faster. Sadly it is _faster_ because of a blunder in previous PR (using BucketQueue where MinHeap would be better), not because of a new tech. More importantly, it is more accurate. And that's what people apparently want. ## What changed? Most of the functional changes relate to `SpatialQuery` module. This is the thingy that answers "we know the target, which tile of my territory is the best to launch an invasion". To make it compute a path from South America to the deep inland China river, it has to work on a coerced map, one with a very small resolution, so small in fact, that every 4096 map tiles gets compressed to just one pixel. I hope you see where this is going. Previously we selected a random coastal tile within this big pixel (honestly it wasn't random at all, but could very well be for the illustrative purposes). Now, we try to be a bit more deliberate. Since we already know the rough location of the probably best tile, we can exclude all other tiles from the computation. Imagine a player's territory spans both Americas on global map - that's a lot of shores. But since we already know the best tile is somewhere close to Miami, the problem space was greatly reduced, no need to consider all other shores. But pathing to the target in China from Miami is still crazy expensive. This is where second trick comes to play - instead of pathing all the way to China, we select a _waypoint_ in the rough direction of China, about 100 to 200 tiles away. This way we fairly cheaply select best tile to launch an invasion towards this abstract point. And chances are, this point is far enough, the newly computed path is very close to being optimal. When you throw a dart from far away, the difference between scoring 10 and missing is very small. This is why aiming in the general direction of the board - as opposed to the ceiling - is usually good enough. ## Okay, but what about the crazy paths when I send invasion to the opposed bank of a river?! Well, pathing from America to China is cool, but most players wouldn't notice the difference on such long paths, what about the short ones? We now try more accurate pathing first and defer to hierarchy only if it fails. This produces much better paths for short invasions. While the fix described above ensures the accuracy is improved also on medium-to-long routes. ## Playground Yes. https://github.com/user-attachments/assets/9cf9586f-c99a-416d-b856-8cf0a21c35ed ## CodeRabbit Grab a 🥕. Remember `tests/pathfinding/playground` is mostly generated code and go easy on it. It's enough for it to work and do it's job of visualizing the paths. No need for throughout review of these files. ## 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
Pathfinding Tests
This directory contains benchmarking tools, scenario generators, and an interactive playground for testing and optimizing pathfinding algorithms in OpenFrontIO.
TLDR
npx tsx tests/pathfinding/benchmark/run.ts --synthetic --all
npx tsx tests/pathfinding/playground/server.ts
Directory Structure
tests/pathfinding/
├── benchmark.ts # Benchmarking tool
├── scenarios/ # Scenarios for benchmarks
│ ├── default.ts # Hand-picked scenario
│ └── synthetic/ # Auto-generated synthetic scenarios
└── playground/ # Interactive web-based visualization
Available algorithms
- NavSat - future implementation - NavigationSatellite (HPA*)
- PF.Mini - current implementation - PathFinder.Mini (A*)
Benchmarking
Running a Single Scenario
# Run default scenario with default adapter (NavSat)
npx tsx tests/pathfinding/benchmark/run.ts
# Run specific scenario
npx tsx tests/pathfinding/benchmark/run.ts default
# Run with specific adapter
npx tsx tests/pathfinding/benchmark/run.ts default legacy
Running Synthetic Scenarios
Synthetic scenarios are auto-generated from maps with random port selections and routes.
# Run single synthetic scenario
npx tsx tests/pathfinding/benchmark/run.ts --synthetic iceland
# Run single synthetic scenario with specific adapter
npx tsx tests/pathfinding/benchmark/run.ts --synthetic iceland legacy
# Run ALL synthetic scenarios (comprehensive benchmark)
npx tsx tests/pathfinding/benchmark/run.ts --synthetic --all
# Run all with specific adapter
npx tsx tests/pathfinding/benchmark/run.ts --synthetic --all legacy
Benchmark Metrics
The benchmark measures three key metrics:
- Initialization Time - How long it takes to preprocess the map
- Path Distance - Total distance across all routes (quality metric)
- Pathfinding Time - How long it takes to compute paths (performance metric)
Example Output
================================================================================
METRIC 1: INITIALIZATION TIME
================================================================================
Initialization time: 45.32ms
================================================================================
METRIC 2: PATH DISTANCE
================================================================================
Route Path Length
Miami → Boston 346 tiles
Miami → Houston 212 tiles
...
Total distance: 52432 tiles
Routes completed: 22 / 22
================================================================================
METRIC 3: PATHFINDING TIME
================================================================================
Route Time
Miami → Boston 2.45ms
Miami → Houston 1.82ms
...
Total time: 156.34ms
Average time: 7.11ms
Routes benchmarked: 22 / 22
================================================================================
SUMMARY
================================================================================
Adapter: default
Scenario: default
Scores:
Initialization: 45.32ms
Pathfinding: 156.34ms
Distance: 52432 tiles
Generating Scenarios
Generate Synthetic Scenarios
Synthetic scenarios are generated by:
- Finding all water shoreline tiles on a map
- Randomly selecting 200 ports
- Creating 1000 routes connecting nearby ports
# Generate scenario for a single map
npx tsx tests/pathfinding/benchmark/generate.ts iceland
# Generate scenarios for all maps
npx tsx tests/pathfinding/benchmark/generate.ts --all
# Force overwrite existing scenarios
npx tsx tests/pathfinding/benchmark/generate.ts iceland --force
npx tsx tests/pathfinding/benchmark/generate.ts --all --force
Interactive Playground
The playground provides a web-based UI for visualizing pathfinding results, comparing algorithms, and debugging.
Starting the Playground
# Start with path caching enabled (default)
npx tsx tests/pathfinding/playground/server.ts
# Start without path caching (to measure uncached performance)
npx tsx tests/pathfinding/playground/server.ts --no-cache
Then open http://localhost:5555 in your browser.