mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 20:19:44 +00:00
pathfinder rewrite
This commit is contained in:
parent
cb0fa06e12
commit
22f3c3f640
16 changed files with 89 additions and 263 deletions
|
|
@ -1,14 +1,17 @@
|
|||
<?php
|
||||
class TileNavigator implements ITileNavigator
|
||||
class TileNavigator
|
||||
{
|
||||
private $blockedProvider, $neighborProvider, $distanceAlgorithm;
|
||||
|
||||
|
||||
const OFFSETS = [
|
||||
[0, 0, 1],
|
||||
[0, 0, -1],
|
||||
[1, 0, 0],
|
||||
[-1, 0, 0]
|
||||
];
|
||||
|
||||
public static $pathfinderAccessed = 0;
|
||||
|
||||
public function __construct(IBlockedProvider $blockedProvider, INeighborProvider $neighborProvider, IDistanceAlgorithm $distanceAlgorithm){
|
||||
$this->blockedProvider = $blockedProvider;
|
||||
$this->neighborProvider = $neighborProvider;
|
||||
$this->distanceAlgorithm = $distanceAlgorithm;
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public function reconstructPath($path, $current){
|
||||
|
|
@ -23,9 +26,31 @@ class TileNavigator implements ITileNavigator
|
|||
array_pop($totalPath);
|
||||
return array_reverse($totalPath);
|
||||
}
|
||||
|
||||
public function isLocBlocked(PMFLevel $level, $loc){
|
||||
|
||||
$chunkX = $loc >> 20 & 0xf;
|
||||
$chunkZ = $loc >> 12 & 0xf;
|
||||
$chunkY = $loc >> 4 & 0xf;
|
||||
|
||||
$index = $chunkZ << 4 | $chunkX;
|
||||
|
||||
$blockX = $loc >> 16 & 0xf;
|
||||
$blockZ = $loc >> 8 & 0xf;
|
||||
$blockY = $loc & 0xf;
|
||||
|
||||
$id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index);
|
||||
return StaticBlock::getIsSolid($id);
|
||||
}
|
||||
|
||||
public function navigate(PathTile $from, PathTile $to, $maxDist)
|
||||
|
||||
public function navigate(Level $level, $fromX, $fromY, $fromZ, $toX, $toY, $toZ, $maxDist)
|
||||
{
|
||||
$from = $fromX << 16 | $fromZ << 8 | $fromY;
|
||||
$to = $toX << 16 | $toZ << 8 | $toY;
|
||||
|
||||
$pmfLevel = $level->level;
|
||||
|
||||
$open = new SplPriorityQueue();
|
||||
//$open->insert(, 0);
|
||||
$open->insert($from, 0);
|
||||
|
|
@ -33,7 +58,7 @@ class TileNavigator implements ITileNavigator
|
|||
$gScore = [];
|
||||
$gScore[(string) $from] = 0;
|
||||
$has = [(string)$from, true];
|
||||
if($this->blockedProvider->isBlocked($to)){
|
||||
if($this->isLocBlocked($pmfLevel, $to)){
|
||||
return null;
|
||||
}
|
||||
$visited = [];
|
||||
|
|
@ -41,39 +66,61 @@ class TileNavigator implements ITileNavigator
|
|||
while(!$open->isEmpty())
|
||||
{
|
||||
$current = $open->top();
|
||||
$currentX = $current >> 16 & 0xff;
|
||||
$currentY = $current & 0xff;
|
||||
$currentZ = $current >> 8 & 0xff;
|
||||
$open->next();
|
||||
if ($current == $to){
|
||||
return $this->reconstructPath($path, $current);
|
||||
}
|
||||
foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
|
||||
{
|
||||
$dist = ($from->x - $neighbor->x)*($from->x - $neighbor->x) + ($from->y - $neighbor->y)*($from->y - $neighbor->y) + ($from->z - $neighbor->z)*($from->z - $neighbor->z);
|
||||
if($dist < -$maxDist || $dist > $maxDist){ //Utils::distance_noroot($neighbor->asArray(), $from->asArray())
|
||||
continue;
|
||||
}
|
||||
if(isset($visited[(string)$neighbor])){
|
||||
continue;
|
||||
}
|
||||
$points = [];
|
||||
foreach(self::OFFSETS as $offset){
|
||||
$newX = $offset[0] + $currentX;
|
||||
$newY = $offset[1] + $currentY;
|
||||
$newZ = $offset[2] + $currentZ;
|
||||
if($newX < 0 || $newX > 255 || $newY < 0 || $newY > 255 || $newZ < 0 || $newZ > 255) continue;
|
||||
$new = $newX << 16 | $newZ << 8 | $newY;
|
||||
|
||||
$visited[(string)$neighbor] = $neighbor;
|
||||
$distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor);
|
||||
$tentativeG = $gScore[(string) $current] + $distbetweenCost;
|
||||
if (!isset($has[(string)$neighbor]))
|
||||
if(!$this->isLocBlocked($pmfLevel, $new)){
|
||||
|
||||
$dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ);
|
||||
if($dist < -$maxDist || $dist > $maxDist){
|
||||
continue;
|
||||
}
|
||||
|
||||
$points[] = $new;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($points as $neighbor)
|
||||
{
|
||||
if(isset($visited[$neighbor])){
|
||||
continue;
|
||||
}
|
||||
$visited[$neighbor] = true;
|
||||
|
||||
$diffX = ($currentX - ($neighbor >> 16 & 0xff));
|
||||
$diffY = ($currentY - ($neighbor & 0xff));
|
||||
$diffZ = ($currentZ - ($neighbor >> 8 & 0xff));
|
||||
|
||||
$distbetweenCost = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ;
|
||||
$tentativeG = $gScore[$current] + $distbetweenCost;
|
||||
if (!isset($has[$neighbor]))
|
||||
{
|
||||
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
|
||||
//$open[-$tentativeG] = $neighbor;
|
||||
$open->insert($neighbor, -$tentativeG);
|
||||
$has[(string)$neighbor] = true;
|
||||
$has[$neighbor] = true;
|
||||
}
|
||||
elseif ($tentativeG >= $gScore[(string) $neighbor])
|
||||
elseif ($tentativeG >= $gScore[$neighbor])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(!isset($gScore[(string) $neighbor]) || $distbetweenCost < $gScore[(string) $neighbor]){
|
||||
$path[(string) $neighbor] = $current;
|
||||
if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){
|
||||
$path[$neighbor] = $current;
|
||||
}
|
||||
|
||||
$gScore[(string) $neighbor] = $tentativeG;
|
||||
$gScore[$neighbor] = $tentativeG;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue