pathfinder will create path to closest node to target if no direct path is found

This commit is contained in:
GameHerobrine 2024-04-14 05:05:51 +03:00
parent 22f3c3f640
commit 27f50cc4ab
2 changed files with 23 additions and 2 deletions

View file

@ -63,6 +63,9 @@ class TileNavigator
}
$visited = [];
$maxDist*=$maxDist; //no square root
$closestToTarget = null;
$closestToTargetDist = INF;
while(!$open->isEmpty())
{
$current = $open->top();
@ -99,6 +102,12 @@ class TileNavigator
}
$visited[$neighbor] = true;
$diffX = ($toX - ($neighbor >> 16 & 0xff));
$diffY = ($toY - ($neighbor & 0xff));
$diffZ = ($toZ - ($neighbor >> 8 & 0xff));
$targetDist = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ;
$diffX = ($currentX - ($neighbor >> 16 & 0xff));
$diffY = ($currentY - ($neighbor & 0xff));
$diffZ = ($currentZ - ($neighbor >> 8 & 0xff));
@ -109,6 +118,7 @@ class TileNavigator
{
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
//$open[-$tentativeG] = $neighbor;
$open->insert($neighbor, -$tentativeG);
$has[$neighbor] = true;
}
@ -118,12 +128,23 @@ class TileNavigator
}
if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){
$path[$neighbor] = $current;
if($targetDist < $closestToTargetDist){
console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)");
$closestToTarget = $neighbor;
$closestToTargetDist = $targetDist;
}
}
$gScore[$neighbor] = $tentativeG;
}
}
if($closestToTarget != null){
return $this->reconstructPath($path, $closestToTarget);
}
return null;
}