temporarily switched to dijkstra's algorithm

This commit is contained in:
TorgaW 2024-04-19 16:33:20 +03:00
parent c1831994a2
commit fdfa7bd7cb

View File

@ -110,7 +110,7 @@ export class PathFinder {
/** /**
* @type Map<String, PathFinderNode> * @type Map<String, PathFinderNode>
*/ */
let cameFrom = new Map(); let cameFrom = new Array();
let minFScoreNodeIndex = 0; let minFScoreNodeIndex = 0;
//START LOOP WOOOOAAAW //START LOOP WOOOOAAAW
@ -143,27 +143,22 @@ export class PathFinder {
* @type Array<PathFinderNode> * @type Array<PathFinderNode>
*/ */
let currentNeighbors = this._getNeighbors(currentNode.position); let currentNeighbors = this._getNeighbors(currentNode.position);
// if(!currentNeighbors) return undefined;
// console.log(currentNeighbors);
for (const neighbor of currentNeighbors) { for (const neighbor of currentNeighbors) {
this._closedSet.push(neighbor);
let tentativeGScore = currentNode.gScore + this._manhattanDistance(currentNode.position, neighbor.position); let tentativeGScore = currentNode.gScore + this._manhattanDistance(currentNode.position, neighbor.position);
if (this._existsInClosedSet(neighbor)) continue;
this._closedSet.push(neighbor);
if (tentativeGScore < neighbor.gScore) { if (tentativeGScore < neighbor.gScore) {
// console.log(neighbor.id, currentNode.id); cameFrom.push({ id: neighbor.id, node: currentNode });
cameFrom.set(neighbor.id, currentNode);
neighbor.gScore = tentativeGScore; neighbor.gScore = tentativeGScore;
neighbor.fScore = tentativeGScore + this._heuristic(neighbor.position, neighbor.dScore); neighbor.fScore = tentativeGScore + this._heuristic(neighbor.position, neighbor.dScore)*0;
let neighborFoundInOpenSet = false; if(!this._existsInOpenSet(neighbor))
for (const node of this._openSet) { {
if (node.id === neighbor.id) { this._openSet.push(neighbor);
neighborFoundInOpenSet = true;
break;
}
} }
if (!neighborFoundInOpenSet) this._openSet.push(neighbor);
} }
} }
// console.log([...this._openSet]);
} }
return new NavigationResult(false, "", new NavigationPath()); return new NavigationResult(false, "", new NavigationPath());
@ -176,13 +171,13 @@ export class PathFinder {
*/ */
_reconstructPath(cameFrom, current) { _reconstructPath(cameFrom, current) {
let totalPath = [current.position]; let totalPath = [current.position];
// console.log(cameFrom); console.log(cameFrom);
let keys = [...cameFrom.keys()]; // let keys = [...cameFrom.keys()];
while (keys.includes(current.id)) { // while (keys.includes(current.id)) {
if (current.id === cameFrom.get(current.id).id) break; // if (current.id === cameFrom.get(current.id).id) break;
current = cameFrom.get(current.id); // current = cameFrom.get(current.id);
totalPath.push(current.position); // totalPath.push(current.position);
} // }
return totalPath.reverse(); return totalPath.reverse();
} }
@ -201,7 +196,7 @@ export class PathFinder {
if (currentTile && !currentTile.isObstacle) { if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) { if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) {
neighbors.push(node); neighbors.push(node);
} }
} }
@ -210,7 +205,7 @@ export class PathFinder {
if (currentTile && !currentTile.isObstacle) { if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) { if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) {
neighbors.push(node); neighbors.push(node);
} }
} }
@ -219,7 +214,7 @@ export class PathFinder {
if (currentTile && !currentTile.isObstacle) { if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) { if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) {
neighbors.push(node); neighbors.push(node);
} }
} }
@ -228,7 +223,7 @@ export class PathFinder {
if (currentTile && !currentTile.isObstacle) { if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) { if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) {
neighbors.push(node); neighbors.push(node);
} }
} }
@ -256,7 +251,7 @@ export class PathFinder {
let x2 = this._start.getX() - this._goal.getX(); let x2 = this._start.getX() - this._goal.getX();
let y2 = this._start.getY() - this._goal.getY(); let y2 = this._start.getY() - this._goal.getY();
let cross = Math.abs(x1 * y2 - x2 * y1); let cross = Math.abs(x1 * y2 - x2 * y1);
return this._manhattanDistance(current, this._goal) * d + cross * 0.001; return this._manhattanDistance(current, this._goal) * d;
} }
/** /**