Skip to content

Commit

Permalink
Smooth boat paths twice to minimize impact of area borders
Browse files Browse the repository at this point in the history
  • Loading branch information
platz1de committed Oct 3, 2024
1 parent 3e35206 commit bfb8562
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/map/area/BoatPathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function calculateBoatWaypoints(start: number, end: number): number[] {
const startAreaId = areaCalculator.areaIndex[start];
const startX = start % gameMap.width, startY = Math.floor(start / gameMap.width);

//TODO: This is problematic in a lot of ways
let inSameArea = false;
onNeighborWater(end, tile => {
if (areaCalculator.areaIndex[tile] === startAreaId) {
Expand Down Expand Up @@ -56,7 +57,7 @@ export function calculateBoatWaypoints(start: number, end: number): number[] {
}
appendSmoothed(path, findPathInArea(last.node.x + last.node.y * gameMap.width, end));
path.push(end);
return path;
return smoothPath(path); //Smooth again for better results
}
for (const edge of node.edges) {
const newCost = cost + edge.cost + Math.sqrt((edge.node.x - startX) * (edge.node.x - startX) + (edge.node.y - startY) * (edge.node.y - startY));
Expand Down Expand Up @@ -287,4 +288,24 @@ function appendSmoothed(path: number[], points: number[]) {
}
}
path.push(points[points.length - 1]);
}

/**
* Smooths the path by removing unnecessary points.
* @param path The path to smooth
* @returns The smoothed path
*/
function smoothPath(path: number[]): number[] {
const result = [];
if (path.length < 2) return path;
let last = path[0];
result.push(last);
for (let i = 1; i < path.length - 1; i++) {
if (!checkLineOfSight(path[i + 1] % gameMap.width, Math.floor(path[i + 1] / gameMap.width), last % gameMap.width, Math.floor(last / gameMap.width))) {
result.push(path[i]);
last = path[i];
}
}
result.push(path[path.length - 1]);
return result;
}

0 comments on commit bfb8562

Please sign in to comment.