diff --git a/src/astarnavigator/IElement.php b/src/astarnavigator/IElement.php deleted file mode 100644 index 4357c6ab..00000000 --- a/src/astarnavigator/IElement.php +++ /dev/null @@ -1,7 +0,0 @@ -x = $x; - $this->y = $y; - } - - public function equals(PathTile $t){ - return $t->x === $this->x && $t->y === $this->y; - } - - public function addOffset($offset){ - $this->x += $offset[0]; - $this->y += $offset[1]; - return $this; - } - - public function __toString(){ - return $this->x.":".$this->y; - } - -} - diff --git a/src/astarnavigator/PathTileXYZ.php b/src/astarnavigator/PathTileXYZ.php deleted file mode 100644 index 90ec4c98..00000000 --- a/src/astarnavigator/PathTileXYZ.php +++ /dev/null @@ -1,39 +0,0 @@ -x = floor($x); - $this->y = floor($y); - $this->z = floor($z); - $this->level = $level; - } - - public function equals(PathTile $t){ - return $t->x === $this->x && $t->y === $this->y && $t->z === $this->z; - } - - public function asVector(){ - return new Vector3($this->x, $this->y, $this->z); - } - - public function asArray(){ - return ["x" => $this->x, "y" => $this->y, "z" => $this->z]; - } - - public function addOffset($offset){ - return new PathTileXYZ($this->x + $offset[0], $this->y + $offset[1], $this->z + $offset[2], $this->level); - } - - public function __toString(){ - return $this->x.":".$this->y.":".$this->z; - } -} - diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 4dee05ef..ac0792f1 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -1,14 +1,17 @@ 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; } } diff --git a/src/astarnavigator/algorithms/IDistanceAlgorithm.php b/src/astarnavigator/algorithms/IDistanceAlgorithm.php deleted file mode 100644 index 391c629a..00000000 --- a/src/astarnavigator/algorithms/IDistanceAlgorithm.php +++ /dev/null @@ -1,7 +0,0 @@ -asArray(), $to->asArray()); - } - return 666; - } - -} - diff --git a/src/astarnavigator/algorithms/Pythagoras3D.php b/src/astarnavigator/algorithms/Pythagoras3D.php deleted file mode 100644 index a350bb65..00000000 --- a/src/astarnavigator/algorithms/Pythagoras3D.php +++ /dev/null @@ -1,16 +0,0 @@ -x - $from->x) * ($to->x - $from->x) + ($to->y - $from->y) * ($to->y - $from->y) + ($to->z - $from->z) * ($to->z - $from->z)); - } - return INF; - } - -} - diff --git a/src/astarnavigator/providers/IBlockedProvider.php b/src/astarnavigator/providers/IBlockedProvider.php deleted file mode 100644 index 40adf01b..00000000 --- a/src/astarnavigator/providers/IBlockedProvider.php +++ /dev/null @@ -1,12 +0,0 @@ -level->level->getBlockID($tile->x, $tile->y, $tile->z)); - } - -} - diff --git a/src/astarnavigator/providers/MCDiagonalProvider.php b/src/astarnavigator/providers/MCDiagonalProvider.php deleted file mode 100644 index c6520ad0..00000000 --- a/src/astarnavigator/providers/MCDiagonalProvider.php +++ /dev/null @@ -1,47 +0,0 @@ -addOffset($offset); - - if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){ - $pnts[] = $pnt; - } - - //if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){ - // - //} - - //if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){ - // if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y - 1, $pnt->z))){ - // --$pnt->y; - // } - // $pnts[] = $pnt; - //}else{ - // if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y + 1, $pnt->z))){ - // ++$pnt->y; - // $pnts[] = $pnt; - // } - //} - } - return $pnts; - } - return []; - } - - -} - diff --git a/src/dependencies.php b/src/dependencies.php index 89733a8b..5554945f 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -77,21 +77,6 @@ require_once(FILE_PATH . "/src/math/Vector3.php"); require_once(FILE_PATH . "/src/world/Position.php"); require_once(FILE_PATH . "/src/pmf/PMF.php"); -require_once(FILE_PATH . "/src/astarnavigator/IElement.php"); -require_once(FILE_PATH . "/src/astarnavigator/PHPUtils.php"); -require_once(FILE_PATH . "/src/astarnavigator/PathTile.php"); - -require_once(FILE_PATH . "/src/astarnavigator/algorithms/IDistanceAlgorithm.php"); -require_once(FILE_PATH . "/src/astarnavigator/algorithms/Pythagoras3D.php"); -require_once(FILE_PATH . "/src/astarnavigator/algorithms/ManhattanHeuristic3D.php"); - -require_once(FILE_PATH . "/src/astarnavigator/providers/INeighborProvider.php"); -require_once(FILE_PATH . "/src/astarnavigator/providers/MCDiagonalProvider.php"); - -require_once(FILE_PATH . "/src/astarnavigator/providers/IBlockedProvider.php"); -require_once(FILE_PATH . "/src/astarnavigator/providers/MCBlockedProvider.php"); - -require_once(FILE_PATH . "/src/astarnavigator/ITileNavigator.php"); require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php"); diff --git a/src/entity/Living.php b/src/entity/Living.php index ba7a493f..ed238dd5 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -21,7 +21,7 @@ abstract class Living extends Entity implements Pathfindable{ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; $this->ai = new EntityAI($this); - $this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new Pythagoras3D()); + $this->pathFinder = new TileNavigator(); $this->pathFollower = new PathFollower($this); parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; @@ -121,21 +121,20 @@ abstract class Living extends Entity implements Pathfindable{ $this->ai->updateTasks(); } /*if($this->onGround){ - if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ - $me = new PathTileXYZ($this->x, $this->y, $this->z, $this->level); + if(!$this->hasPath()){ //$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level); - + $pl = null; if(count($this->level->players) > 0){ $pl = array_values($this->level->players)[0]; - $target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level); + //$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level); } - if(self::$pathfind){ - $this->path = $this->pathFinder->navigate($me, $target, 10); + if($pl != null && self::$pathfind){ + $this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10); } if($this->path){ - console("Found path to $target from $me of length ".count($this->path)); + console("Found path of length ".count($this->path)); foreach($this->path as $node){ $eid = self::$lastPathEID++; @@ -144,9 +143,9 @@ abstract class Living extends Entity implements Pathfindable{ $pk = new AddItemEntityPacket(); $pk->eid = $eid; $pk->item = BlockAPI::getItem(GOLD_BLOCK, 0, 1); - $pk->x = $node->x + 0.5; - $pk->y = $node->y; - $pk->z = $node->z + 0.5; + $pk->x = ($node >> 16 & 0xff) + 0.5; + $pk->y = $node & 0xff; + $pk->z = ($node >> 8 & 0xff) + 0.5; $pk->yaw = $pk->pitch = 0; $pk->roll = 0; foreach($this->level->players as $player){ diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php index 48a81ebd..72387d8f 100644 --- a/src/entity/ai/path/PathFollower.php +++ b/src/entity/ai/path/PathFollower.php @@ -18,9 +18,11 @@ class PathFollower{ if($this->entity->currentNode == null){ $this->entity->currentNode = $this->entity->path[$this->entity->currentIndex]; } - - $this->entity->ai->mobController->setMovingTarget($this->entity->currentNode->x + 0.5, $this->entity->currentNode->y, $this->entity->currentNode->z + 0.5, 1.0); - if($this->entity->boundingBox->isXYZInsideNS($this->entity->currentNode->x + 0.5, $this->entity->currentNode->y, $this->entity->currentNode->z + 0.5)){ + $moveX = ($this->entity->currentNode >> 16 & 0xff) + 0.5; + $moveY = $this->entity->currentNode & 0xff; + $moveZ = ($this->entity->currentNode >> 8 & 0xff) + 0.5; + $this->entity->ai->mobController->setMovingTarget($moveX + 0.5, $moveY, $moveZ + 0.5, 1.0); + if($this->entity->boundingBox->isXYZInsideNS($moveX + 0.5, $moveY, $moveZ + 0.5)){ ++$this->entity->currentIndex; console("next"); $this->entity->currentNode = null;