mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-23 13:09:44 +00:00
pathfinder will create path to closest node to target if no direct path is found
This commit is contained in:
parent
22f3c3f640
commit
27f50cc4ab
2 changed files with 23 additions and 2 deletions
|
|
@ -63,6 +63,9 @@ class TileNavigator
|
||||||
}
|
}
|
||||||
$visited = [];
|
$visited = [];
|
||||||
$maxDist*=$maxDist; //no square root
|
$maxDist*=$maxDist; //no square root
|
||||||
|
$closestToTarget = null;
|
||||||
|
$closestToTargetDist = INF;
|
||||||
|
|
||||||
while(!$open->isEmpty())
|
while(!$open->isEmpty())
|
||||||
{
|
{
|
||||||
$current = $open->top();
|
$current = $open->top();
|
||||||
|
|
@ -99,6 +102,12 @@ class TileNavigator
|
||||||
}
|
}
|
||||||
$visited[$neighbor] = true;
|
$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));
|
$diffX = ($currentX - ($neighbor >> 16 & 0xff));
|
||||||
$diffY = ($currentY - ($neighbor & 0xff));
|
$diffY = ($currentY - ($neighbor & 0xff));
|
||||||
$diffZ = ($currentZ - ($neighbor >> 8 & 0xff));
|
$diffZ = ($currentZ - ($neighbor >> 8 & 0xff));
|
||||||
|
|
@ -109,6 +118,7 @@ class TileNavigator
|
||||||
{
|
{
|
||||||
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
|
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
|
||||||
//$open[-$tentativeG] = $neighbor;
|
//$open[-$tentativeG] = $neighbor;
|
||||||
|
|
||||||
$open->insert($neighbor, -$tentativeG);
|
$open->insert($neighbor, -$tentativeG);
|
||||||
$has[$neighbor] = true;
|
$has[$neighbor] = true;
|
||||||
}
|
}
|
||||||
|
|
@ -118,12 +128,23 @@ class TileNavigator
|
||||||
}
|
}
|
||||||
if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){
|
if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){
|
||||||
$path[$neighbor] = $current;
|
$path[$neighbor] = $current;
|
||||||
|
|
||||||
|
if($targetDist < $closestToTargetDist){
|
||||||
|
console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)");
|
||||||
|
|
||||||
|
$closestToTarget = $neighbor;
|
||||||
|
$closestToTargetDist = $targetDist;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$gScore[$neighbor] = $tentativeG;
|
$gScore[$neighbor] = $tentativeG;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($closestToTarget != null){
|
||||||
|
return $this->reconstructPath($path, $closestToTarget);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ class PathFollower{
|
||||||
$moveX = ($this->entity->currentNode >> 16 & 0xff) + 0.5;
|
$moveX = ($this->entity->currentNode >> 16 & 0xff) + 0.5;
|
||||||
$moveY = $this->entity->currentNode & 0xff;
|
$moveY = $this->entity->currentNode & 0xff;
|
||||||
$moveZ = ($this->entity->currentNode >> 8 & 0xff) + 0.5;
|
$moveZ = ($this->entity->currentNode >> 8 & 0xff) + 0.5;
|
||||||
$this->entity->ai->mobController->setMovingTarget($moveX + 0.5, $moveY, $moveZ + 0.5, 1.0);
|
$this->entity->ai->mobController->setMovingTarget($moveX, $moveY, $moveZ, 1.0);
|
||||||
if($this->entity->boundingBox->isXYZInsideNS($moveX + 0.5, $moveY, $moveZ + 0.5)){
|
if($this->entity->boundingBox->isXYZInsideNS($moveX, $moveY, $moveZ)){
|
||||||
++$this->entity->currentIndex;
|
++$this->entity->currentIndex;
|
||||||
console("next");
|
console("next");
|
||||||
$this->entity->currentNode = null;
|
$this->entity->currentNode = null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue