Files
OpenFrontIO/tests/pathfinding/playground/public/client.js
Arkadiusz Sygulski 6bd95d4884 Pathfinding - optimize naval invasions (#2932)
# 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
2026-01-16 23:10:55 +00:00

2018 lines
60 KiB
JavaScript

// Application State
const state = {
currentMap: null,
mapData: null,
mapWidth: 0,
mapHeight: 0,
startPoint: null,
endPoint: null,
hpaPath: null,
hpaResult: null, // Store full HPA* result including timing
comparisons: [], // Array of comparison results
visibleComparisons: new Set(), // Which comparison paths are visible
adapters: [], // Available comparison adapters (loaded from backend)
graphDebug: null, // Static graph data (allNodes, edges, clusterSize) - loaded once per map
debugInfo: null, // Per-path debug data (timings, nodePath, initialPath)
isMapLoading: false, // Loading state for map switching
isHpaLoading: false, // Separate loading state for HPA*
activeRefreshButton: null, // Track which refresh button is spinning
// Transport Ship mode
mode: "pathfinding", // "pathfinding" | "transport"
paintedTiles: new Set(), // Set of tile indices (y * width + x)
brushSize: 5,
transportResult: null, // Result from spatial query
};
// Colors for comparison paths
const COMPARISON_COLORS = {
"hpa.cached": "#00ffff", // cyan
hpa: "#ff8800", // orange
"a.baseline": "#ff00ff", // magenta
"a.generic": "#88ff00", // lime
"a.full": "#ffff00", // yellow
};
// Canvas state
let zoomLevel = 1.0;
let panX = 0;
let panY = 0;
let isDragging = false;
let dragStartX = 0;
let dragStartY = 0;
let dragStartPanX = 0;
let dragStartPanY = 0;
let isPainting = false;
let isErasing = false;
let mapCanvas, overlayCanvas, interactiveCanvas;
let mapCtx, overlayCtx, interactiveCtx;
let mapRendered = false;
let hoveredNode = null;
let hoveredPoint = null; // 'start', 'end', or null
let draggingPoint = null; // 'start', 'end', or null
let draggingPointPosition = null; // [x, y] canvas position while dragging
let lastPathRecalcTime = 0;
let renderRequested = false;
// Save current state to URL query string
function updateURLState() {
const params = new URLSearchParams();
if (state.currentMap) {
params.set("map", state.currentMap);
}
if (state.startPoint) {
params.set("start", `${state.startPoint[0]},${state.startPoint[1]}`);
}
if (state.endPoint) {
params.set("end", `${state.endPoint[0]},${state.endPoint[1]}`);
}
const newURL = `${window.location.pathname}?${params.toString()}`;
window.history.replaceState({}, "", newURL);
}
// Restore state from URL query string
function restoreFromURL() {
const params = new URLSearchParams(window.location.search);
const mapName = params.get("map");
const startStr = params.get("start");
const endStr = params.get("end");
const result = {
map: mapName,
start: null,
end: null,
};
if (startStr) {
const [x, y] = startStr.split(",").map(Number);
if (!isNaN(x) && !isNaN(y)) {
result.start = [x, y];
}
}
if (endStr) {
const [x, y] = endStr.split(",").map(Number);
if (!isNaN(x) && !isNaN(y)) {
result.end = [x, y];
}
}
return result;
}
// Initialize on DOM load
window.addEventListener("DOMContentLoaded", () => {
initializeCanvases();
initializeControls();
initializeDragControls();
initializeTimingsPanel();
loadMaps();
});
// Initialize canvas elements
function initializeCanvases() {
mapCanvas = document.getElementById("mapCanvas");
mapCtx = mapCanvas.getContext("2d");
overlayCanvas = document.getElementById("overlayCanvas");
overlayCtx = overlayCanvas.getContext("2d");
// Create interactive canvas OUTSIDE the CSS transform wrapper
// This canvas is viewport-sized and renders paths/points at screen coordinates
const canvasContainer = document.querySelector(".canvas-container");
interactiveCanvas = document.createElement("canvas");
interactiveCanvas.id = "interactiveCanvas";
interactiveCanvas.style.position = "absolute";
interactiveCanvas.style.top = "0";
interactiveCanvas.style.left = "0";
interactiveCanvas.style.width = "100%";
interactiveCanvas.style.height = "100%";
interactiveCanvas.style.zIndex = "3";
interactiveCanvas.style.pointerEvents = "none";
canvasContainer.appendChild(interactiveCanvas);
interactiveCtx = interactiveCanvas.getContext("2d");
// Size interactive canvas to viewport
const resizeInteractiveCanvas = () => {
const rect = canvasContainer.getBoundingClientRect();
interactiveCanvas.width = rect.width;
interactiveCanvas.height = rect.height;
};
resizeInteractiveCanvas();
window.addEventListener("resize", resizeInteractiveCanvas);
}
// Initialize control event listeners
function initializeControls() {
// Map selector (top panel)
document.getElementById("scenarioSelect").addEventListener("change", (e) => {
switchMap(e.target.value);
});
// Map selector (welcome screen)
document
.getElementById("welcomeMapSelect")
.addEventListener("change", (e) => {
const mapName = e.target.value;
if (mapName) {
switchMap(mapName);
}
});
// Refresh HPA* button
document.getElementById("refreshHpa").addEventListener("click", (e) => {
if (state.startPoint && state.endPoint) {
const btn = e.currentTarget;
btn.classList.add("spinning");
state.activeRefreshButton = btn;
requestPathfinding(state.startPoint, state.endPoint);
}
});
// Visualization toggles - all buttons
[
"showInitialPath",
"showUsedNodes",
"showColoredMap",
"showNodes",
"showSectorGrid",
"showEdges",
].forEach((id) => {
const button = document.getElementById(id);
button.addEventListener("click", () => {
const isActive = button.dataset.active === "true";
button.dataset.active = !isActive;
// Map coloring affects map canvas
if (id === "showColoredMap") {
renderMapBackground(2);
}
// Static overlays (sectors, edges, all nodes) go on overlay canvas
if (["showNodes", "showSectorGrid", "showEdges"].includes(id)) {
renderOverlay(2);
}
// Dynamic elements (paths, highlighted nodes) go on interactive canvas
renderInteractive();
});
});
// Zoom control
document.getElementById("zoom").addEventListener("input", (e) => {
zoomLevel = parseFloat(e.target.value);
document.getElementById("zoomValue").textContent =
zoomLevel.toFixed(1) + "x";
updateTransform();
});
// Clear points button
document.getElementById("clearPoints").addEventListener("click", () => {
clearPoints();
});
// Mode switch buttons
document.querySelectorAll(".mode-button").forEach((btn) => {
btn.addEventListener("click", () => {
const newMode = btn.dataset.mode;
if (newMode !== state.mode) {
setMode(newMode);
}
});
});
// Transport controls
const brushSizeInput = document.getElementById("brushSize");
const brushSizeValue = document.getElementById("brushSizeValue");
brushSizeInput.addEventListener("input", (e) => {
state.brushSize = parseInt(e.target.value);
brushSizeValue.textContent = state.brushSize;
});
document.getElementById("clearTerritory").addEventListener("click", () => {
state.paintedTiles.clear();
state.transportResult = null;
updateTransportInfo();
renderInteractive();
});
}
// Set application mode
function setMode(newMode) {
state.mode = newMode;
// Update UI
document.querySelectorAll(".mode-button").forEach((btn) => {
btn.classList.toggle("active", btn.dataset.mode === newMode);
});
const transportControls = document.getElementById("transportControls");
const timingsPanel = document.getElementById("timingsPanel");
const debugPanel = document.querySelector(".debug-panel");
if (newMode === "transport") {
transportControls.style.display = "block";
timingsPanel.style.top = "280px";
debugPanel.style.display = "none";
setStatus("Paint territory, then click water target");
} else {
transportControls.style.display = "none";
timingsPanel.style.top = "280px";
debugPanel.style.display = "flex";
if (state.startPoint && state.endPoint) {
setStatus("Path computed successfully");
} else if (state.startPoint) {
setStatus("Click on map to set end point");
} else {
setStatus("Click on map to set start point");
}
}
renderInteractive();
}
// Update transport info display
function updateTransportInfo() {
const paintedCount = document.getElementById("paintedCount");
const shoreCount = document.getElementById("shoreCount");
paintedCount.textContent = state.paintedTiles.size;
// Count shore tiles
let shores = 0;
if (state.mapData) {
for (const idx of state.paintedTiles) {
if (isLandShore(idx)) {
shores++;
}
}
}
shoreCount.textContent = shores;
}
// Check if tile is a land shore (land adjacent to water)
function isLandShore(tileIdx) {
const x = tileIdx % state.mapWidth;
const y = Math.floor(tileIdx / state.mapWidth);
// Must be land
if (state.mapData[tileIdx] !== 0) return false;
// Check 4 neighbors for water
const neighbors = [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1],
];
for (const [nx, ny] of neighbors) {
if (nx < 0 || nx >= state.mapWidth || ny < 0 || ny >= state.mapHeight)
continue;
const nIdx = ny * state.mapWidth + nx;
if (state.mapData[nIdx] === 1) return true;
}
return false;
}
// Helper function to check if mouse is over a start/end point
function getPointAtPosition(canvasX, canvasY) {
const scale = zoomLevel;
const zoomFactor = 3 / zoomLevel;
const hitRadius = Math.max(4, scale * 3 * zoomFactor) + 3; // Add 3px tolerance
// Check end point first (render on top)
if (state.endPoint) {
const dx = canvasX - (state.endPoint[0] + 0.5);
const dy = canvasY - (state.endPoint[1] + 0.5);
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= hitRadius / scale) {
return "end";
}
}
// Check start point
if (state.startPoint) {
const dx = canvasX - (state.startPoint[0] + 0.5);
const dy = canvasY - (state.startPoint[1] + 0.5);
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= hitRadius / scale) {
return "start";
}
}
return null;
}
// Throttled path recalculation (max once per 16ms ~60fps)
function schedulePathRecalc() {
const now = Date.now();
const timeSinceLastCall = now - lastPathRecalcTime;
if (timeSinceLastCall >= 16) {
// Enough time has passed, request immediately
lastPathRecalcTime = now;
if (state.startPoint && state.endPoint) {
// Skip comparisons during drag for snappy feel
requestPathfinding(state.startPoint, state.endPoint, true);
}
}
// If not enough time has passed, skip this call (throttle)
}
// Throttled spatial query recalculation (max once per 50ms for heavier computation)
let lastSpatialQueryTime = 0;
function scheduleSpatialQueryRecalc() {
const now = Date.now();
const timeSinceLastCall = now - lastSpatialQueryTime;
if (timeSinceLastCall >= 50) {
lastSpatialQueryTime = now;
if (state.endPoint && state.paintedTiles.size > 0) {
requestSpatialQuery(state.endPoint);
}
}
}
// Initialize drag and click controls
function initializeDragControls() {
const wrapper = document.getElementById("canvasWrapper");
const tooltip = document.getElementById("tooltip");
wrapper.addEventListener("mousedown", (e) => {
const rect = wrapper.getBoundingClientRect();
const canvasX = (e.clientX - rect.left - panX) / zoomLevel;
const canvasY = (e.clientY - rect.top - panY) / zoomLevel;
// Transport mode: check for dragging end point first, then painting
if (state.mode === "transport") {
// Check if clicking on end point to drag it
const pointAtMouse = getPointAtPosition(canvasX, canvasY);
if (pointAtMouse === "end") {
draggingPoint = "end";
wrapper.style.cursor = "move";
dragStartX = e.clientX;
dragStartY = e.clientY;
return;
}
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
if (
tileX >= 0 &&
tileX < state.mapWidth &&
tileY >= 0 &&
tileY < state.mapHeight
) {
const tileIdx = tileY * state.mapWidth + tileX;
const isLand = state.mapData[tileIdx] === 0;
if (isLand) {
// Start painting (or erasing with ctrl/right-click)
isErasing = e.ctrlKey || e.button === 2;
isPainting = true;
paintAtPosition(tileX, tileY, isErasing);
wrapper.style.cursor = isErasing ? "crosshair" : "pointer";
return;
}
}
// Fall through to panning if not on land
}
// Pathfinding mode: check if clicking on a point
const pointAtMouse = getPointAtPosition(canvasX, canvasY);
if (pointAtMouse && state.mode === "pathfinding") {
// Start dragging the point
draggingPoint = pointAtMouse;
wrapper.style.cursor = "move";
} else {
// Start panning the map
isDragging = true;
wrapper.style.cursor = "grabbing";
}
dragStartX = e.clientX;
dragStartY = e.clientY;
dragStartPanX = panX;
dragStartPanY = panY;
});
wrapper.addEventListener("mousemove", (e) => {
const rect = wrapper.getBoundingClientRect();
const canvasX = (e.clientX - rect.left - panX) / zoomLevel;
const canvasY = (e.clientY - rect.top - panY) / zoomLevel;
// Transport mode: continue painting
if (isPainting && state.mode === "transport") {
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
paintAtPosition(tileX, tileY, isErasing);
return;
}
// Transport mode: dragging end point
if (draggingPoint === "end" && state.mode === "transport") {
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
if (
tileX >= 0 &&
tileX < state.mapWidth &&
tileY >= 0 &&
tileY < state.mapHeight
) {
const tileIndex = tileY * state.mapWidth + tileX;
const isWater = state.mapData[tileIndex] === 1;
if (isWater) {
draggingPointPosition = [tileX, tileY];
state.endPoint = [tileX, tileY];
renderInteractive();
// Throttled spatial query recomputation
if (state.paintedTiles.size > 0) {
scheduleSpatialQueryRecalc();
}
}
}
return;
}
// Transport mode: check hover over end point
if (state.mode === "transport" && !isDragging) {
const pointAtMouse = getPointAtPosition(canvasX, canvasY);
if (pointAtMouse !== hoveredPoint) {
hoveredPoint = pointAtMouse;
renderInteractive();
wrapper.style.cursor = hoveredPoint ? "move" : "grab";
}
return;
}
if (draggingPoint) {
// Dragging a start/end point - snap to water tile
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
// Validate tile is within bounds and is water
if (
tileX >= 0 &&
tileX < state.mapWidth &&
tileY >= 0 &&
tileY < state.mapHeight
) {
const tileIndex = tileY * state.mapWidth + tileX;
const isWater = state.mapData[tileIndex] === 1;
if (isWater) {
// Snap to water tile center
draggingPointPosition = [tileX, tileY];
// Update the actual point position and trigger throttled path recalculation
if (draggingPoint === "start") {
state.startPoint = [tileX, tileY];
} else {
state.endPoint = [tileX, tileY];
}
// Trigger throttled path recalculation (16ms)
if (state.startPoint && state.endPoint) {
schedulePathRecalc();
}
}
// If not water, keep previous valid position (don't update)
}
renderInteractive();
} else if (isDragging) {
// Panning the map
const dx = e.clientX - dragStartX;
const dy = e.clientY - dragStartY;
panX = dragStartPanX + dx;
panY = dragStartPanY + dy;
updateTransform(); // Updates interactive layer at screen coordinates
} else {
// Check for point hover
const pointAtMouse = getPointAtPosition(canvasX, canvasY);
if (pointAtMouse !== hoveredPoint) {
hoveredPoint = pointAtMouse;
renderInteractive(); // Fast - only redraws points
// Update cursor
wrapper.style.cursor = hoveredPoint ? "move" : "grab";
}
// Check for node hover (only if node visualization is enabled)
const showNodes =
document.getElementById("showNodes").dataset.active === "true";
const showUsedNodes =
document.getElementById("showUsedNodes").dataset.active === "true";
if (
(showNodes || showUsedNodes) &&
state.graphDebug &&
state.graphDebug.allNodes
) {
// Filter nodes based on what's visible
let nodesToCheck = state.graphDebug.allNodes;
if (
showUsedNodes &&
!showNodes &&
state.debugInfo &&
state.debugInfo.nodePath
) {
// Only show tooltips for used nodes
// nodePath are coordinates [x, y] matching the map format
const usedNodeCoords = new Set(
state.debugInfo.nodePath.map(([x, y]) => `${x},${y}`),
);
nodesToCheck = state.graphDebug.allNodes.filter((node) =>
usedNodeCoords.has(`${node.x * 2},${node.y * 2}`),
);
}
const foundNode = findNodeAtPosition(canvasX, canvasY, nodesToCheck);
if (foundNode !== hoveredNode) {
hoveredNode = foundNode;
if (hoveredNode) {
showNodeTooltip(hoveredNode, e.clientX, e.clientY);
} else {
tooltip.classList.remove("visible");
}
renderInteractive();
} else if (hoveredNode) {
tooltip.style.left = e.clientX + 15 + "px";
tooltip.style.top = e.clientY + 15 + "px";
}
} else {
// No node visualization enabled, clear any existing tooltip
if (hoveredNode) {
hoveredNode = null;
tooltip.classList.remove("visible");
renderInteractive();
}
}
}
});
wrapper.addEventListener("mouseup", (e) => {
// Only treat as click if mouse didn't move much
const dx = Math.abs(e.clientX - dragStartX);
const dy = Math.abs(e.clientY - dragStartY);
// Transport mode: finish painting
if (isPainting) {
isPainting = false;
isErasing = false;
wrapper.style.cursor = "grab";
return;
}
// Transport mode: finish dragging end point
if (draggingPoint === "end" && state.mode === "transport") {
if (state.endPoint && state.paintedTiles.size > 0) {
requestSpatialQuery(state.endPoint);
}
draggingPoint = null;
draggingPointPosition = null;
renderInteractive();
wrapper.style.cursor = "grab";
return;
}
if (draggingPoint) {
// Finished dragging a point
// Request final path update to ensure we have the path for the final position
// (in case throttling skipped the last update during fast dragging)
if (state.startPoint && state.endPoint) {
requestPathfinding(state.startPoint, state.endPoint);
}
draggingPoint = null;
draggingPointPosition = null;
renderInteractive();
updateURLState();
} else if (isDragging && dx < 5 && dy < 5) {
// Was panning but didn't move much - treat as click
if (state.mode === "transport") {
handleTransportClick(e);
} else {
handleMapClick(e);
}
}
isDragging = false;
// Reset cursor based on current hover state
const rect = wrapper.getBoundingClientRect();
const canvasX = (e.clientX - rect.left - panX) / zoomLevel;
const canvasY = (e.clientY - rect.top - panY) / zoomLevel;
const pointAtMouse = getPointAtPosition(canvasX, canvasY);
wrapper.style.cursor =
pointAtMouse && state.mode === "pathfinding" ? "move" : "grab";
});
wrapper.addEventListener("mouseleave", () => {
isDragging = false;
draggingPoint = null;
draggingPointPosition = null;
isPainting = false;
isErasing = false;
tooltip.classList.remove("visible");
wrapper.style.cursor = "grab";
const needsRender = hoveredNode || hoveredPoint;
hoveredNode = null;
hoveredPoint = null;
if (needsRender) {
renderInteractive();
}
});
// Prevent context menu on right-click (for erasing)
wrapper.addEventListener("contextmenu", (e) => {
if (state.mode === "transport") {
e.preventDefault();
}
});
wrapper.addEventListener("wheel", (e) => {
e.preventDefault();
const rect = wrapper.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const oldZoom = zoomLevel;
const zoomDelta = e.deltaY > 0 ? 0.9 : 1.1;
zoomLevel = Math.max(0.1, Math.min(10, zoomLevel * zoomDelta));
panX = mouseX - (mouseX - panX) * (zoomLevel / oldZoom);
panY = mouseY - (mouseY - panY) * (zoomLevel / oldZoom);
document.getElementById("zoom").value = zoomLevel;
document.getElementById("zoomValue").textContent = zoomLevel.toFixed(1);
updateTransform();
renderInteractive();
});
}
// Initialize timings panel to default state
function initializeTimingsPanel() {
// Set initial state to match "no path" state
updateTimingsPanel({ primary: null, comparisons: [] });
}
// Handle map clicks for point selection
function handleMapClick(e) {
if (!state.currentMap || state.isMapLoading || state.isHpaLoading) return;
const wrapper = document.getElementById("canvasWrapper");
const rect = wrapper.getBoundingClientRect();
// Convert screen coordinates to tile coordinates
const canvasX = (e.clientX - rect.left - panX) / zoomLevel;
const canvasY = (e.clientY - rect.top - panY) / zoomLevel;
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
// Validate coordinates
if (
tileX < 0 ||
tileX >= state.mapWidth ||
tileY < 0 ||
tileY >= state.mapHeight
) {
return;
}
// Check if tile is water
const index = tileY * state.mapWidth + tileX;
const isWater = state.mapData[index] === 1;
if (!isWater) {
showError("Selected point must be on water");
return;
}
// Point selection state machine
if (!state.startPoint) {
// Set start point
state.startPoint = [tileX, tileY];
updatePointDisplay();
renderInteractive();
updateURLState();
} else if (!state.endPoint) {
// Set end point and trigger pathfinding
state.endPoint = [tileX, tileY];
updatePointDisplay();
renderInteractive();
updateURLState();
requestPathfinding(state.startPoint, state.endPoint);
} else {
// Reset and set new start point
clearPoints();
state.startPoint = [tileX, tileY];
updatePointDisplay();
renderInteractive();
updateURLState();
}
}
// Clear selected points
function clearPoints() {
state.startPoint = null;
state.endPoint = null;
state.hpaPath = null;
state.hpaResult = null;
state.comparisons = [];
state.debugInfo = null;
updatePointDisplay();
hidePathInfo();
updateURLState(); // Remove points from URL
renderInteractive();
}
// Paint tiles in a brush area
function paintAtPosition(centerX, centerY, erase = false) {
const radius = Math.floor(state.brushSize / 2);
let changed = false;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const x = centerX + dx;
const y = centerY + dy;
if (x < 0 || x >= state.mapWidth || y < 0 || y >= state.mapHeight)
continue;
const idx = y * state.mapWidth + x;
const isLand = state.mapData[idx] === 0;
if (!isLand) continue;
if (erase) {
if (state.paintedTiles.has(idx)) {
state.paintedTiles.delete(idx);
changed = true;
}
} else {
if (!state.paintedTiles.has(idx)) {
state.paintedTiles.add(idx);
changed = true;
}
}
}
}
if (changed) {
updateTransportInfo();
renderInteractive();
}
}
// Handle clicks in transport mode
function handleTransportClick(e) {
if (!state.currentMap || state.isMapLoading) return;
const wrapper = document.getElementById("canvasWrapper");
const rect = wrapper.getBoundingClientRect();
const canvasX = (e.clientX - rect.left - panX) / zoomLevel;
const canvasY = (e.clientY - rect.top - panY) / zoomLevel;
const tileX = Math.floor(canvasX);
const tileY = Math.floor(canvasY);
if (
tileX < 0 ||
tileX >= state.mapWidth ||
tileY < 0 ||
tileY >= state.mapHeight
) {
return;
}
const idx = tileY * state.mapWidth + tileX;
const isWater = state.mapData[idx] === 1;
if (!isWater) {
return;
}
// Clicked on water - run spatial query
if (state.paintedTiles.size === 0) {
showError("Paint some territory first");
return;
}
requestSpatialQuery([tileX, tileY]);
}
// Request spatial query computation
async function requestSpatialQuery(target) {
setStatus("Computing spatial query...", true);
try {
// Only send shore tiles (land adjacent to water) - much smaller payload
const ownedTiles = Array.from(state.paintedTiles).filter((idx) =>
isLandShore(idx),
);
const response = await fetch("/api/spatial-query", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
map: state.currentMap,
ownedTiles,
target,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "Spatial query failed");
}
const result = await response.json();
state.transportResult = result;
state.endPoint = target;
renderInteractive();
updateTransportTimings(result);
if (result.selectedShore) {
setStatus(
`Shore selected: (${result.selectedShore[0]}, ${result.selectedShore[1]})`,
);
} else {
setStatus("No valid shore found");
}
} catch (error) {
showError(`Spatial query failed: ${error.message}`);
}
}
// Update timings panel for transport mode
function updateTransportTimings(result) {
const hpaTimeEl = document.getElementById("hpaTime");
const hpaTilesEl = document.getElementById("hpaTiles");
if (result.path) {
hpaTilesEl.textContent = `- ${result.path.length} tiles`;
} else {
hpaTilesEl.textContent = "";
}
const totalTime =
result.debug?.timings?.["SpatialQuery.closestShoreByWater"] ?? 0;
if (totalTime > 0) {
hpaTimeEl.textContent = `${totalTime.toFixed(2)}ms`;
hpaTimeEl.classList.remove("faded");
} else {
hpaTimeEl.textContent = "0.00ms";
hpaTimeEl.classList.add("faded");
}
// Hide pathfinding-specific timing breakdown in transport mode
document.getElementById("timingEarlyExit").style.display = "none";
document.getElementById("timingFindNodes").style.display = "none";
document.getElementById("timingAbstractPath").style.display = "none";
document.getElementById("timingInitialPath").style.display = "none";
document.getElementById("timingSmoothPath").style.display = "none";
document.getElementById("comparisonsSection").style.display = "none";
}
// Update transform for pan/zoom
function updateTransform() {
const transform = `translate(${panX}px, ${panY}px) scale(${zoomLevel})`;
mapCanvas.style.transform = transform;
overlayCanvas.style.transform = transform;
// Interactive canvas is outside the transform - update it separately
renderInteractive();
}
// Load available maps
async function loadMaps() {
setStatus("Loading maps...", true);
try {
const response = await fetch("/api/maps");
if (!response.ok) throw new Error("Failed to load maps");
const data = await response.json();
// Featured maps to show in grid (in order)
const featuredMapNames = [
"giantworldmap",
"northamerica",
"southamerica",
"europe",
"asia",
"straitofgibraltar",
"manicouagan",
"mars",
];
// Get featured maps in the specified order
const gridMaps = featuredMapNames
.map((name) => data.maps.find((m) => m.name === name))
.filter((map) => map !== undefined);
// Populate map grid with featured maps - update placeholders
gridMaps.forEach((map, index) => {
const card = document.querySelector(`[data-map-index="${index}"]`);
if (!card) return;
// Update click handler
card.onclick = () => switchMap(map.name);
// Update image
const img = card.querySelector("img");
if (img) {
img.src = `/api/maps/${encodeURIComponent(map.name)}/thumbnail`;
img.alt = map.displayName;
}
// Update name
const nameEl = card.querySelector(".map-card-name");
if (nameEl) {
nameEl.textContent = map.displayName;
nameEl.style.opacity = "1";
}
});
// Populate both selectors (all maps)
const topSelect = document.getElementById("scenarioSelect");
const welcomeSelect = document.getElementById("welcomeMapSelect");
topSelect.innerHTML = '<option value="">Select a map</option>';
welcomeSelect.innerHTML = '<option value="">Select a map</option>';
data.maps.forEach((map) => {
// Top panel selector
const topOption = document.createElement("option");
topOption.value = map.name;
topOption.textContent = map.displayName;
topSelect.appendChild(topOption);
// Welcome screen selector
const welcomeOption = document.createElement("option");
welcomeOption.value = map.name;
welcomeOption.textContent = map.displayName;
welcomeSelect.appendChild(welcomeOption);
});
setStatus("Select a map to begin");
// Restore state from URL if present
const urlState = restoreFromURL();
if (urlState.map) {
// Load the map from URL
await switchMap(urlState.map, true); // Restore points from URL
// Points will be restored in switchMap after the map loads
}
} catch (error) {
showError(`Failed to load maps: ${error.message}`);
}
}
// Switch to a different map
async function switchMap(mapName, restorePointsFromURL = false) {
if (!mapName) return;
setStatus("Loading map...", true);
state.isMapLoading = true;
try {
const response = await fetch(`/api/maps/${encodeURIComponent(mapName)}`);
if (!response.ok) throw new Error("Failed to load map");
const data = await response.json();
// Update state
state.currentMap = mapName;
state.mapWidth = data.width;
state.mapHeight = data.height;
state.mapData = data.mapData;
state.graphDebug = data.graphDebug; // Store static graph debug data
state.adapters = data.adapters || []; // Store available comparison adapters
// Clear paths (but don't update URL yet if we're restoring from URL)
state.startPoint = null;
state.endPoint = null;
state.hpaPath = null;
state.hpaResult = null;
state.comparisons = [];
state.debugInfo = null;
updatePointDisplay();
hidePathInfo();
// Size canvases
mapCanvas.width = state.mapWidth * 2;
mapCanvas.height = state.mapHeight * 2;
mapCanvas.style.width = `${state.mapWidth}px`;
mapCanvas.style.height = `${state.mapHeight}px`;
overlayCanvas.width = state.mapWidth * 2;
overlayCanvas.height = state.mapHeight * 2;
overlayCanvas.style.width = `${state.mapWidth}px`;
overlayCanvas.style.height = `${state.mapHeight}px`;
// Render map and overlays
renderMapBackground(2);
renderOverlay(2);
renderInteractive();
// Reset view
zoomLevel = 1.0;
panX = 0;
panY = 0;
document.getElementById("zoom").value = 1.0;
document.getElementById("zoomValue").textContent = "1.0";
updateTransform();
// Hide welcome screen
hideWelcomeScreen();
// Sync both selectors
document.getElementById("scenarioSelect").value = mapName;
document.getElementById("welcomeMapSelect").value = mapName;
setStatus("Click on map to set start point");
mapRendered = true;
// Restore start/end points from URL if requested (initial page load)
if (restorePointsFromURL) {
const urlState = restoreFromURL();
if (urlState.start) {
const [x, y] = urlState.start;
if (x >= 0 && x < state.mapWidth && y >= 0 && y < state.mapHeight) {
const tileIndex = y * state.mapWidth + x;
const isWater = state.mapData[tileIndex] === 1;
if (isWater) {
state.startPoint = [x, y];
}
}
}
if (urlState.end) {
const [x, y] = urlState.end;
if (x >= 0 && x < state.mapWidth && y >= 0 && y < state.mapHeight) {
const tileIndex = y * state.mapWidth + x;
const isWater = state.mapData[tileIndex] === 1;
if (isWater) {
state.endPoint = [x, y];
}
}
}
// If both points are set, request pathfinding
if (state.startPoint && state.endPoint) {
renderInteractive();
requestPathfinding(state.startPoint, state.endPoint);
}
} else {
// User manually switched maps - update URL to clear points
updateURLState();
}
} catch (error) {
showError(`Failed to load map: ${error.message}`);
} finally {
state.isMapLoading = false;
}
}
// Show/hide welcome screen
function showWelcomeScreen() {
document.getElementById("welcomeScreen").classList.remove("hidden");
}
function hideWelcomeScreen() {
document.getElementById("welcomeScreen").classList.add("hidden");
}
// Request pathfinding computation (HPA* primary + comparisons)
async function requestPathfinding(from, to, skipComparisons = false) {
setStatus("Computing path...", true);
state.isHpaLoading = true;
try {
const body = {
map: state.currentMap,
from,
to,
};
// Skip comparisons during drag for snappy feel
if (skipComparisons) {
body.adapters = [];
}
const response = await fetch("/api/pathfind", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "Pathfinding failed");
}
const result = await response.json();
// Update state with new API format
state.hpaPath = result.primary.path;
state.hpaResult = result.primary;
state.comparisons = result.comparisons;
state.debugInfo = {
initialPath: result.primary.debug.initialPath,
nodePath: result.primary.debug.nodePath,
timings: result.primary.debug.timings,
};
// Update UI
updatePathInfo(result);
renderInteractive();
setStatus("Path computed successfully");
} catch (error) {
showError(`Pathfinding failed: ${error.message}`);
} finally {
state.isHpaLoading = false;
// Stop refresh button spinning
if (state.activeRefreshButton) {
state.activeRefreshButton.classList.remove("spinning");
state.activeRefreshButton = null;
}
}
}
// Update point display
function updatePointDisplay() {
// No-op now, kept for compatibility
}
// Update path info in UI
function updatePathInfo(result) {
// Update timings panel
updateTimingsPanel(result);
}
// Update the dedicated timings panel
function updateTimingsPanel(result) {
const primary = result.primary;
const timings = primary && primary.debug ? primary.debug.timings : {};
const hpaTilesEl = document.getElementById("hpaTiles");
if (primary && primary.length > 0) {
hpaTilesEl.textContent = `- ${primary.length} tiles`;
} else {
hpaTilesEl.textContent = "";
}
// Show timing breakdown - always visible with gray dashes when no data
// Early Exit
const earlyExitEl = document.getElementById("timingEarlyExit");
const earlyExitValueEl = document.getElementById("timingEarlyExitValue");
earlyExitEl.style.display = "flex";
const earlyExitTime = timings["earlyExit"];
if (earlyExitTime !== undefined) {
earlyExitValueEl.textContent = `${earlyExitTime.toFixed(2)}ms`;
earlyExitValueEl.style.color = "#f5f5f5";
} else {
earlyExitValueEl.textContent = "—";
earlyExitValueEl.style.color = "#666";
}
// Find Nodes
const findNodesEl = document.getElementById("timingFindNodes");
const findNodesValueEl = document.getElementById("timingFindNodesValue");
findNodesEl.style.display = "flex";
const nodeLookupTime = timings["nodeLookup"];
if (nodeLookupTime !== undefined) {
findNodesValueEl.textContent = `${nodeLookupTime.toFixed(2)}ms`;
findNodesValueEl.style.color = "#f5f5f5";
} else {
findNodesValueEl.textContent = "—";
findNodesValueEl.style.color = "#666";
}
// Abstract Path
const abstractPathEl = document.getElementById("timingAbstractPath");
const abstractPathValueEl = document.getElementById(
"timingAbstractPathValue",
);
abstractPathEl.style.display = "flex";
const abstractPathTime = timings["abstractPath"];
if (abstractPathTime !== undefined) {
abstractPathValueEl.textContent = `${abstractPathTime.toFixed(2)}ms`;
abstractPathValueEl.style.color = "#f5f5f5";
} else {
abstractPathValueEl.textContent = "—";
abstractPathValueEl.style.color = "#666";
}
// Initial Path
const initialPathEl = document.getElementById("timingInitialPath");
const initialPathValueEl = document.getElementById("timingInitialPathValue");
initialPathEl.style.display = "flex";
const initialPathTime = timings["initialPath"];
if (initialPathTime !== undefined) {
initialPathValueEl.textContent = `${initialPathTime.toFixed(2)}ms`;
initialPathValueEl.style.color = "#f5f5f5";
} else {
initialPathValueEl.textContent = "—";
initialPathValueEl.style.color = "#666";
}
// Smooth Path
const smoothPathEl = document.getElementById("timingSmoothPath");
const smoothPathValueEl = document.getElementById("timingSmoothPathValue");
smoothPathEl.style.display = "flex";
const smoothPathTime = timings["smoothingTransformer"];
if (smoothPathTime !== undefined) {
smoothPathValueEl.textContent = `${smoothPathTime.toFixed(2)}ms`;
smoothPathValueEl.style.color = "#f5f5f5";
} else {
smoothPathValueEl.textContent = "—";
smoothPathValueEl.style.color = "#666";
}
// Show comparisons section
const comparisonsSection = document.getElementById("comparisonsSection");
const comparisonsContainer = document.getElementById("comparisonsContainer");
// Only show comparisons section if we have adapters loaded
if (!state.adapters || state.adapters.length === 0) {
comparisonsSection.style.display = "none";
return;
}
comparisonsSection.style.display = "block";
// Build lookup map for comparison data
const compMap = {};
if (result.comparisons) {
for (const comp of result.comparisons) {
compMap[comp.adapter] = comp;
}
}
// Use total span time from DebugSpan
let hpaTime = timings["findPath"] || 0;
if (compMap["hpa.cached"]) {
hpaTime = compMap["hpa.cached"].time;
}
// Show HPA* time and path length (or 0.00 in light gray if no data)
const hpaTimeEl = document.getElementById("hpaTime");
if (hpaTime > 0) {
hpaTimeEl.textContent = `${hpaTime.toFixed(2)}ms`;
hpaTimeEl.classList.remove("faded");
} else {
hpaTimeEl.textContent = "0.00ms";
hpaTimeEl.classList.add("faded");
}
// Find fastest time overall (including HPA*) when we have data
const compTimes = result.comparisons
? result.comparisons.map((c) => c.time).filter((t) => t > 0)
: [];
const fastestCompTime =
compTimes.length > 0 ? Math.min(...compTimes) : Infinity;
// Update HPA* time color - green if fastest, red if slower than any comparison
const hpaIsFastest = hpaTime > 0 && hpaTime <= fastestCompTime;
const hpaSlower = hpaTime > 0 && fastestCompTime < hpaTime;
const fastestTime = Math.min(hpaTime || Infinity, fastestCompTime);
if (hpaIsFastest) {
hpaTimeEl.style.color = "#00ff88";
} else if (hpaSlower) {
hpaTimeEl.style.color = "#ff6666";
} else {
hpaTimeEl.style.color = "#f5f5f5";
}
// Build comparison rows for all known adapters
let html = "";
for (const adapter of state.adapters) {
const comp = compMap[adapter];
const pathColor = COMPARISON_COLORS[adapter] || "#ffffff";
const isActive = state.visibleComparisons.has(adapter);
// Show actual values or placeholders
const hasData = comp && comp.time > 0;
const isFastest = hasData && comp.time === fastestTime;
const timeColor = isFastest ? "#00ff88" : hasData ? "#f5f5f5" : "#666";
const tilesText = hasData ? comp.length : "—";
const timeText = hasData ? `${comp.time.toFixed(2)}ms` : "—";
html += `
<div class="comparison-row${isActive ? " active" : ""}" data-adapter="${adapter}">
<span class="comp-color" style="background: ${pathColor}"></span>
<span class="comp-name">${adapter}</span>
<span class="comp-tiles" style="color: ${hasData ? "#888" : "#666"}">${tilesText}</span>
<span class="comp-time" style="color: ${timeColor}">${timeText}</span>
</div>
`;
}
comparisonsContainer.innerHTML = html;
// Add click handlers to toggle path visibility
comparisonsContainer.querySelectorAll(".comparison-row").forEach((row) => {
row.addEventListener("click", () => {
const adapter = row.dataset.adapter;
if (state.visibleComparisons.has(adapter)) {
state.visibleComparisons.delete(adapter);
row.classList.remove("active");
} else {
state.visibleComparisons.add(adapter);
row.classList.add("active");
}
renderInteractive();
});
});
}
// Reset path info to show dashes
function hidePathInfo() {
// Don't hide the panel, just reset to show dashes
updateTimingsPanel({ primary: null, comparisons: [] });
}
// Set status message
function setStatus(message, loading = false) {
const statusEl = document.getElementById("status");
statusEl.textContent = message;
statusEl.className = loading ? "loading" : "";
}
// Show error message
function showError(message) {
const errorEl = document.getElementById("error");
errorEl.textContent = message;
errorEl.classList.add("visible");
setTimeout(() => {
errorEl.classList.remove("visible");
}, 5000);
setStatus(message, false);
}
// Render map background
function renderMapBackground(scale) {
mapCanvas.width = state.mapWidth * scale;
mapCanvas.height = state.mapHeight * scale;
mapCanvas.style.width = `${state.mapWidth}px`;
mapCanvas.style.height = `${state.mapHeight}px`;
// Use ImageData for much faster rendering
const imageData = mapCtx.createImageData(
state.mapWidth * scale,
state.mapHeight * scale,
);
const data = imageData.data;
// Check if colored map is enabled
const showColored =
document.getElementById("showColoredMap").dataset.active === "true";
let waterR, waterG, waterB, landR, landG, landB;
if (showColored) {
// Colored: Water = #2a5c8a (darker blue), Land = #a1bb75
waterR = 42;
waterG = 92;
waterB = 138;
landR = 161;
landG = 187;
landB = 117;
} else {
// Grayscale: Water = #3c3c3c (darker gray), Land = #777777 (slightly darker)
waterR = 60;
waterG = 60;
waterB = 60;
landR = 119;
landG = 119;
landB = 119;
}
for (let y = 0; y < state.mapHeight; y++) {
for (let x = 0; x < state.mapWidth; x++) {
const mapIndex = y * state.mapWidth + x;
const isWater = state.mapData[mapIndex] === 1;
const r = isWater ? waterR : landR;
const g = isWater ? waterG : landG;
const b = isWater ? waterB : landB;
// Fill all pixels for this tile (scale x scale block)
for (let dy = 0; dy < scale; dy++) {
for (let dx = 0; dx < scale; dx++) {
const px = x * scale + dx;
const py = y * scale + dy;
const pixelIndex = (py * state.mapWidth * scale + px) * 4;
data[pixelIndex] = r;
data[pixelIndex + 1] = g;
data[pixelIndex + 2] = b;
data[pixelIndex + 3] = 255; // Alpha
}
}
}
}
mapCtx.putImageData(imageData, 0, 0);
}
// Render static debug overlays (clusters, edges, all nodes) at map scale
function renderOverlay(scale) {
overlayCtx.clearRect(0, 0, overlayCanvas.width, overlayCanvas.height);
if (!state.mapData || !state.graphDebug) return;
const showSectorGrid =
document.getElementById("showSectorGrid").dataset.active === "true";
const showEdges =
document.getElementById("showEdges").dataset.active === "true";
const showNodes =
document.getElementById("showNodes").dataset.active === "true";
// Draw cluster grid (clusterSize is in mini map coords, scale 2x for real map)
if (showSectorGrid && state.graphDebug.clusterSize) {
const clusterSize = state.graphDebug.clusterSize * 2;
overlayCtx.strokeStyle = "#777777";
overlayCtx.lineWidth = scale * 0.5;
overlayCtx.globalAlpha = 0.7;
overlayCtx.setLineDash([5 * scale, 5 * scale]);
// Vertical lines
for (let x = 0; x <= state.mapWidth; x += clusterSize) {
overlayCtx.beginPath();
overlayCtx.moveTo(x * scale, 0);
overlayCtx.lineTo(x * scale, state.mapHeight * scale);
overlayCtx.stroke();
}
// Horizontal lines
for (let y = 0; y <= state.mapHeight; y += clusterSize) {
overlayCtx.beginPath();
overlayCtx.moveTo(0, y * scale);
overlayCtx.lineTo(state.mapWidth * scale, y * scale);
overlayCtx.stroke();
}
overlayCtx.setLineDash([]);
overlayCtx.globalAlpha = 1.0;
}
// Draw edges
if (showEdges && state.graphDebug.edges) {
overlayCtx.strokeStyle = "#00ff88";
overlayCtx.lineWidth = scale * 0.5;
overlayCtx.globalAlpha = 0.4;
for (const edge of state.graphDebug.edges) {
overlayCtx.beginPath();
overlayCtx.moveTo(
(edge.from[0] + 0.5) * scale,
(edge.from[1] + 0.5) * scale,
);
overlayCtx.lineTo((edge.to[0] + 0.5) * scale, (edge.to[1] + 0.5) * scale);
overlayCtx.stroke();
}
overlayCtx.globalAlpha = 1.0;
}
// Draw all nodes
if (showNodes && state.graphDebug.allNodes) {
overlayCtx.fillStyle = "#aaaaaa";
const nodeRadius = scale * 1.5;
for (const node of state.graphDebug.allNodes) {
overlayCtx.beginPath();
overlayCtx.arc(
(node.x * 2 + 0.5) * scale,
(node.y * 2 + 0.5) * scale,
nodeRadius,
0,
Math.PI * 2,
);
overlayCtx.fill();
}
}
}
// Convert map coordinates to screen coordinates
function mapToScreen(mapX, mapY) {
return {
x: mapX * zoomLevel + panX,
y: mapY * zoomLevel + panY,
};
}
// Render transport mode elements
function renderTransportMode() {
const tileSize = Math.max(1, zoomLevel);
// Draw painted territory
if (state.paintedTiles.size > 0) {
interactiveCtx.fillStyle = "rgba(66, 135, 245, 0.5)";
for (const idx of state.paintedTiles) {
const x = idx % state.mapWidth;
const y = Math.floor(idx / state.mapWidth);
const screen = mapToScreen(x, y);
interactiveCtx.fillRect(screen.x, screen.y, tileSize, tileSize);
}
}
// Draw all shore tiles (dark blue squares)
if (state.transportResult && state.transportResult.shores) {
interactiveCtx.fillStyle = "#2a4a6a";
for (const [x, y] of state.transportResult.shores) {
const screen = mapToScreen(x, y);
interactiveCtx.fillRect(screen.x, screen.y, tileSize, tileSize);
}
}
// Draw refinement candidates (muted yellow/gold squares)
if (state.transportResult?.debug?.candidates) {
interactiveCtx.fillStyle = "rgba(200, 170, 80, 0.7)";
for (const [x, y] of state.transportResult.debug.candidates) {
const screen = mapToScreen(x, y);
interactiveCtx.fillRect(screen.x, screen.y, tileSize, tileSize);
}
}
// Draw refined path (magenta)
if (state.transportResult?.debug?.refinedPath) {
interactiveCtx.strokeStyle = "#ff00ff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel * 0.8);
interactiveCtx.lineCap = "round";
interactiveCtx.lineJoin = "round";
interactiveCtx.beginPath();
for (let i = 0; i < state.transportResult.debug.refinedPath.length; i++) {
const [x, y] = state.transportResult.debug.refinedPath[i];
const screen = mapToScreen(x + 0.5, y + 0.5);
if (i === 0) {
interactiveCtx.moveTo(screen.x, screen.y);
} else {
interactiveCtx.lineTo(screen.x, screen.y);
}
}
interactiveCtx.stroke();
}
// Draw full path (cyan)
if (state.transportResult && state.transportResult.path) {
interactiveCtx.strokeStyle = "#00ffff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel);
interactiveCtx.lineCap = "round";
interactiveCtx.lineJoin = "round";
interactiveCtx.beginPath();
for (let i = 0; i < state.transportResult.path.length; i++) {
const [x, y] = state.transportResult.path[i];
const screen = mapToScreen(x + 0.5, y + 0.5);
if (i === 0) {
interactiveCtx.moveTo(screen.x, screen.y);
} else {
interactiveCtx.lineTo(screen.x, screen.y);
}
}
interactiveCtx.stroke();
}
// Draw original best tile (orange square) if different from new best
if (state.transportResult?.debug?.originalBestTile) {
const [ox, oy] = state.transportResult.debug.originalBestTile;
const newBest = state.transportResult.debug.newBestTile;
// Only show if different from new best
if (!newBest || ox !== newBest[0] || oy !== newBest[1]) {
const screen = mapToScreen(ox, oy);
interactiveCtx.fillStyle = "#ff8800";
interactiveCtx.fillRect(screen.x, screen.y, tileSize, tileSize);
}
}
// Draw selected shore (green square)
if (state.transportResult && state.transportResult.selectedShore) {
const [sx, sy] = state.transportResult.selectedShore;
const screen = mapToScreen(sx, sy);
interactiveCtx.fillStyle = "#44ff44";
interactiveCtx.fillRect(screen.x, screen.y, tileSize, tileSize);
}
// Draw target point (red circle, matching pathfinding mode style)
if (state.endPoint) {
const markerSize = Math.max(4, 3 * zoomLevel);
let mapX, mapY;
if (draggingPoint === "end" && draggingPointPosition) {
mapX = draggingPointPosition[0] + 0.5;
mapY = draggingPointPosition[1] + 0.5;
} else {
mapX = state.endPoint[0] + 0.5;
mapY = state.endPoint[1] + 0.5;
}
const screen = mapToScreen(mapX, mapY);
// Highlight ring if hovered
if (hoveredPoint === "end") {
interactiveCtx.strokeStyle = "#ff4444";
interactiveCtx.lineWidth = Math.max(2, zoomLevel * 0.5);
interactiveCtx.globalAlpha = 0.5;
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize + 3, 0, Math.PI * 2);
interactiveCtx.stroke();
interactiveCtx.globalAlpha = 1.0;
}
interactiveCtx.fillStyle = "#ff4444";
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize, 0, Math.PI * 2);
interactiveCtx.fill();
}
}
// Render truly interactive/dynamic overlay (paths, points, highlights) at screen coordinates
function renderInteractive() {
// Clear viewport-sized canvas (super fast!)
interactiveCtx.clearRect(
0,
0,
interactiveCanvas.width,
interactiveCanvas.height,
);
if (!state.mapData) return;
const markerSize = Math.max(4, 3 * zoomLevel);
// Transport mode: render painted territory and results
if (state.mode === "transport") {
renderTransportMode();
return;
}
// Check what to show
const showUsedNodes =
document.getElementById("showUsedNodes").dataset.active === "true";
const showInitialPath =
document.getElementById("showInitialPath").dataset.active === "true";
const showEdges =
document.getElementById("showEdges").dataset.active === "true";
const showNodes =
document.getElementById("showNodes").dataset.active === "true";
// Draw highlighted edges for hovered node only
if (hoveredNode && showEdges && state.graphDebug && state.graphDebug.edges) {
const connectedEdges = state.graphDebug.edges.filter(
(e) => e.fromId === hoveredNode.id || e.toId === hoveredNode.id,
);
interactiveCtx.strokeStyle = "#00ffaa";
interactiveCtx.lineWidth = Math.max(1, zoomLevel * 0.8);
interactiveCtx.globalAlpha = 1.0;
for (const edge of connectedEdges) {
const from = mapToScreen(edge.from[0], edge.from[1]);
const to = mapToScreen(edge.to[0], edge.to[1]);
interactiveCtx.beginPath();
interactiveCtx.moveTo(from.x, from.y);
interactiveCtx.lineTo(to.x, to.y);
interactiveCtx.stroke();
}
interactiveCtx.globalAlpha = 1.0;
}
// Draw highlighted nodes (hovered + connected) only
if (
hoveredNode &&
showNodes &&
state.graphDebug &&
state.graphDebug.allNodes
) {
// Get connected nodes
let connectedNodeIds = new Set();
if (state.graphDebug.edges) {
const connectedEdges = state.graphDebug.edges.filter(
(e) => e.fromId === hoveredNode.id || e.toId === hoveredNode.id,
);
connectedEdges.forEach((edge) => {
if (edge.fromId !== hoveredNode.id) connectedNodeIds.add(edge.fromId);
if (edge.toId !== hoveredNode.id) connectedNodeIds.add(edge.toId);
});
}
// Draw connected nodes
for (const nodeId of connectedNodeIds) {
const node = state.graphDebug.allNodes.find((n) => n.id === nodeId);
if (node) {
const screen = mapToScreen(node.x * 2, node.y * 2);
interactiveCtx.fillStyle = "#00ff88";
interactiveCtx.strokeStyle = "#ffffff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel * 0.3);
interactiveCtx.beginPath();
interactiveCtx.arc(
screen.x,
screen.y,
Math.max(3, zoomLevel * 2),
0,
Math.PI * 2,
);
interactiveCtx.fill();
interactiveCtx.stroke();
}
}
// Draw hovered node on top
const screen = mapToScreen(hoveredNode.x * 2, hoveredNode.y * 2);
interactiveCtx.fillStyle = "#ffff00";
interactiveCtx.strokeStyle = "#ffffff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel * 0.5);
interactiveCtx.beginPath();
interactiveCtx.arc(
screen.x,
screen.y,
Math.max(4, zoomLevel * 2.5),
0,
Math.PI * 2,
);
interactiveCtx.fill();
interactiveCtx.stroke();
}
// Draw initial path (unsmoothed)
if (
showInitialPath &&
state.debugInfo &&
state.debugInfo.initialPath &&
state.debugInfo.initialPath.length > 0
) {
interactiveCtx.strokeStyle = "#ff00ff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel);
interactiveCtx.lineCap = "round";
interactiveCtx.lineJoin = "round";
interactiveCtx.beginPath();
for (let i = 0; i < state.debugInfo.initialPath.length; i++) {
const [x, y] = state.debugInfo.initialPath[i];
const screen = mapToScreen(x + 0.5, y + 0.5);
if (i === 0) {
interactiveCtx.moveTo(screen.x, screen.y);
} else {
interactiveCtx.lineTo(screen.x, screen.y);
}
}
interactiveCtx.stroke();
}
// Draw comparison paths (before HPA* so primary is on top)
if (state.comparisons && state.visibleComparisons.size > 0) {
interactiveCtx.lineCap = "round";
interactiveCtx.lineJoin = "round";
for (const comp of state.comparisons) {
if (!state.visibleComparisons.has(comp.adapter)) continue;
if (!comp.path || comp.path.length === 0) continue;
const color = COMPARISON_COLORS[comp.adapter] || "#ffffff";
interactiveCtx.strokeStyle = color;
interactiveCtx.lineWidth = Math.max(1, zoomLevel);
interactiveCtx.beginPath();
for (let i = 0; i < comp.path.length; i++) {
const [x, y] = comp.path[i];
const screen = mapToScreen(x + 0.5, y + 0.5);
if (i === 0) {
interactiveCtx.moveTo(screen.x, screen.y);
} else {
interactiveCtx.lineTo(screen.x, screen.y);
}
}
interactiveCtx.stroke();
}
}
// Draw HPA* path
if (state.hpaPath && state.hpaPath.length > 0) {
interactiveCtx.strokeStyle = "#00ffff";
interactiveCtx.lineWidth = Math.max(1, zoomLevel);
interactiveCtx.lineCap = "round";
interactiveCtx.lineJoin = "round";
interactiveCtx.beginPath();
for (let i = 0; i < state.hpaPath.length; i++) {
const [x, y] = state.hpaPath[i];
const screen = mapToScreen(x + 0.5, y + 0.5);
if (i === 0) {
interactiveCtx.moveTo(screen.x, screen.y);
} else {
interactiveCtx.lineTo(screen.x, screen.y);
}
}
interactiveCtx.stroke();
}
// Draw used nodes (highlighted)
if (showUsedNodes && state.debugInfo && state.debugInfo.nodePath) {
interactiveCtx.fillStyle = "#ffff00";
const usedNodeRadius = Math.max(3, zoomLevel * 2.5);
for (const [x, y] of state.debugInfo.nodePath) {
// Nodes are coordinates [x, y] in the same format as path
const screen = mapToScreen(x + 0.5, y + 0.5);
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, usedNodeRadius, 0, Math.PI * 2);
interactiveCtx.fill();
}
}
// Start point
if (state.startPoint) {
let mapX, mapY;
if (draggingPoint === "start" && draggingPointPosition) {
// Dragging - snap to tile center
mapX = draggingPointPosition[0] + 0.5;
mapY = draggingPointPosition[1] + 0.5;
} else {
mapX = state.startPoint[0] + 0.5;
mapY = state.startPoint[1] + 0.5;
}
const screen = mapToScreen(mapX, mapY);
// Highlight ring if hovered
if (hoveredPoint === "start") {
interactiveCtx.strokeStyle = "#ff4444";
interactiveCtx.lineWidth = Math.max(2, zoomLevel * 0.5);
interactiveCtx.globalAlpha = 0.5;
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize + 3, 0, Math.PI * 2);
interactiveCtx.stroke();
interactiveCtx.globalAlpha = 1.0;
}
// Draw point
interactiveCtx.fillStyle = "#ff4444";
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize, 0, Math.PI * 2);
interactiveCtx.fill();
}
// End point
if (state.endPoint) {
let mapX, mapY;
if (draggingPoint === "end" && draggingPointPosition) {
// Dragging - snap to tile center
mapX = draggingPointPosition[0] + 0.5;
mapY = draggingPointPosition[1] + 0.5;
} else {
mapX = state.endPoint[0] + 0.5;
mapY = state.endPoint[1] + 0.5;
}
const screen = mapToScreen(mapX, mapY);
// Highlight ring if hovered
if (hoveredPoint === "end") {
interactiveCtx.strokeStyle = "#44ff44";
interactiveCtx.lineWidth = Math.max(2, zoomLevel * 0.5);
interactiveCtx.globalAlpha = 0.5;
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize + 3, 0, Math.PI * 2);
interactiveCtx.stroke();
interactiveCtx.globalAlpha = 1.0;
}
// Draw point
interactiveCtx.fillStyle = "#44ff44";
interactiveCtx.beginPath();
interactiveCtx.arc(screen.x, screen.y, markerSize, 0, Math.PI * 2);
interactiveCtx.fill();
}
}
function findNodeAtPosition(canvasX, canvasY, nodesToCheck = null) {
const nodes = nodesToCheck || (state.graphDebug && state.graphDebug.allNodes);
if (!nodes) {
return null;
}
const threshold = 10;
for (const node of nodes) {
const nodeX = node.x * 2;
const nodeY = node.y * 2;
const dx = Math.abs(canvasX - nodeX);
const dy = Math.abs(canvasY - nodeY);
if (dx < threshold && dy < threshold) {
return node;
}
}
return null;
}
// Show node tooltip
function showNodeTooltip(node, mouseX, mouseY) {
const tooltip = document.getElementById("tooltip");
const connectedEdges = state.graphDebug.edges.filter(
(e) => e.fromId === node.id || e.toId === node.id,
);
const selfLoops = connectedEdges.filter((e) => e.fromId === e.toId);
let html = `<strong>Node ${node.id}</strong><br>`;
html += `Position: (${node.x * 2}, ${node.y * 2})<br>`;
html += `<strong>Edges: ${connectedEdges.length}</strong>`;
if (selfLoops.length > 0) {
html += ` <span style="color: #ff4444;">(${selfLoops.length} self-loop!)</span>`;
}
if (connectedEdges.length > 0) {
html += '<br><div style="margin-top: 5px; font-size: 11px;">';
// Edges are bidirectional now, just show connected nodes
const connected = connectedEdges.filter((e) => e.fromId !== e.toId);
if (connected.length > 0) {
html += `<div style="color: #88ff88;">Connected (${connected.length}):</div>`;
connected.slice(0, 8).forEach((edge) => {
const otherId = edge.fromId === node.id ? edge.toId : edge.fromId;
html += ` ↔ Node ${otherId}: cost ${edge.cost.toFixed(1)}<br>`;
});
if (connected.length > 8) {
html += ` ... and ${connected.length - 8} more<br>`;
}
}
if (selfLoops.length > 0) {
html += `<div style="color: #ff4444;">Self-loops (${selfLoops.length}):</div>`;
selfLoops.forEach((edge) => {
html += ` ⟲ cost ${edge.cost.toFixed(1)}<br>`;
});
}
html += "</div>";
}
tooltip.innerHTML = html;
tooltip.style.left = mouseX + 15 + "px";
tooltip.style.top = mouseY + 15 + "px";
tooltip.classList.add("visible");
}