diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 4138d848..5af74755 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -11,17 +11,16 @@ class TileNavigator implements ITileNavigator } public function reconstructPath($path, $current){ - $totalPath = [$current]; - while (array_key_exists((string)$current, $path)) - { - $current = $path[(string)$current]; - $totalPath[] = $current; - } - - $totalPath = array_reverse($totalPath); - unset($totalPath[0]); - - return array_values($totalPath); + $totalPath = [$current]; + while (isset($path[(string)$current])) + { + $current = $path[(string)$current]; + $totalPath[] = $current; + } + //foreach(array_unique($path) as $k => $p) console($k.":".$p); + //foreach($totalPath as $k => $p) console($k.":".$p); + array_pop($totalPath); + return array_reverse($totalPath); } public function navigate(PathTile $from, PathTile $to) @@ -40,6 +39,7 @@ class TileNavigator implements ITileNavigator if($this->blockedProvider->isBlocked($to)){ return null; } + $visited = []; while($open->countElements() > 0) { //sleep(1); @@ -54,12 +54,14 @@ class TileNavigator implements ITileNavigator $closed->add($current); foreach($this->neighborProvider->getNeighbors($current) as $neighbor) { - if ($closed->has($neighbor) || $this->blockedProvider->isBlocked($neighbor)) { continue; } - + if(in_array($neighbor, $visited)){ + continue; + } + $visited[] = $neighbor; $tentativeG = $gScore[(string) $current] + $this->distanceAlgorithm->calculate($current, $neighbor); if (!$open->has($neighbor)) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index ab11857c..490df76c 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -26,7 +26,7 @@ abstract class Creature extends Living{ public function update(){ if($this->onGround){ - /*if($this->path === null && $this->pathFinder instanceof ITileNavigator && mt_rand(0, 120) === 0){ + /*if($this->path === null && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){ $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y, $this->z + mt_rand(-10, 10), $this->level)); } if($this->path != null && (is_array($this->path) && count($this->path) <= 0 || $this->currentIndex >= count($this->path))){