diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index fb45bd05..22163dfb 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -1,13 +1,14 @@ blockedProvider = $blockedProvider; $this->neighborProvider = $neighborProvider; $this->distanceAlgorithm = $distanceAlgorithm; - $this->heuristicAlgorithm = $heuristicAlgorithm; } public function reconstructPath($path, $current){ @@ -31,15 +32,11 @@ class TileNavigator implements ITileNavigator $gScore = []; $gScore[(string) $from] = 0; $has = [(string)$from, true]; - /** - * @var array[Tile] $fScore - */ - $fScore = []; - $fScore[(string) $from] = $this->heuristicAlgorithm->calculate($from, $to); if($this->blockedProvider->isBlocked($to)){ return null; } $visited = []; + $maxDist*=$maxDist; //no square root while(!$open->isEmpty()) { $current = $open->top(); @@ -50,7 +47,7 @@ class TileNavigator implements ITileNavigator } foreach($this->neighborProvider->getNeighbors($current) as $neighbor) { - if(!Utils::in_range(Utils::distance($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){ + if(!Utils::in_range(Utils::distance_noroot($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){ continue; } if(isset($visited[(string)$neighbor])){ @@ -58,16 +55,11 @@ class TileNavigator implements ITileNavigator } $visited[(string)$neighbor] = $neighbor; - //if ($this->blockedProvider->isBlocked($neighbor)) shouldnt be neccessary now - //{ - // continue; - //} $distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor); $tentativeG = $gScore[(string) $current] + $distbetweenCost; - $tentativeF = $distbetweenCost + $this->heuristicAlgorithm->calculate($neighbor, $to); if (!isset($has[(string)$neighbor])) { - $open->insert($neighbor, -$tentativeF); + $open->insert($neighbor, -$tentativeG); $has[(string)$neighbor] = true; } else if ($tentativeG >= $gScore[(string) $neighbor]) @@ -79,7 +71,6 @@ class TileNavigator implements ITileNavigator } $gScore[(string) $neighbor] = $tentativeG; - $fScore[(string) $neighbor] = $tentativeF; } } diff --git a/src/astarnavigator/algorithms/Pythagoras3D.php b/src/astarnavigator/algorithms/Pythagoras3D.php index e6b1d54b..9c4ae13c 100644 --- a/src/astarnavigator/algorithms/Pythagoras3D.php +++ b/src/astarnavigator/algorithms/Pythagoras3D.php @@ -1,11 +1,13 @@ asArray(), $to->asArray()); + return Utils::distance_noroot($from->asArray(), $to->asArray()); } return INF; } diff --git a/src/astarnavigator/providers/MCDiagonalProvider.php b/src/astarnavigator/providers/MCDiagonalProvider.php index febb5b63..e671b710 100644 --- a/src/astarnavigator/providers/MCDiagonalProvider.php +++ b/src/astarnavigator/providers/MCDiagonalProvider.php @@ -12,14 +12,15 @@ class MCDiagonalProvider implements INeighborProvider $pnts = []; foreach(MCDiagonalProvider::$neighbors as $offset){ $pnt = $tile->addOffset($offset); - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z)->isSolid){ - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $pnt->z)->isSolid){ + if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z, false)->isSolid){ + if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $pnt->z, false)->isSolid){ --$pnt->y; } $pnts[] = $pnt; }else{ - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z)->isSolid){ - $pnts[] = $pnt->addOffset(self::$jumpOffset); + if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z, false)->isSolid){ + ++$pnt->y; + $pnts[] = $pnt; } } } diff --git a/src/entity/Living.php b/src/entity/Living.php index a2216d08..3b849047 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -9,7 +9,7 @@ abstract class Living extends Entity implements Damageable, 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(), new ManhattanHeuristic3D()); + $this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new ManhattanHeuristic3D()); $this->pathFollower = new PathFollower($this); parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; @@ -34,7 +34,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ $this->ai->updateTasks(); } if($this->onGround){ - //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){ + //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // $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 + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); //} $this->pathFollower->followPath(); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 17d507ea..41fa8281 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -333,11 +333,12 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $index = $this->getIndex($X, $Z); - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; + if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ - $b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); }else{ //php8 fix $b = 0; } @@ -357,9 +358,9 @@ class PMFLevel extends PMF{ return false; } $index = $this->getIndex($X, $Z); - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; $this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))] = chr($block); if(!isset($this->chunkChange[$index][$Y])){ $this->chunkChange[$index][$Y] = 1; @@ -378,9 +379,9 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $index = $this->getIndex($X, $Z); - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); }else{ //php8 fix @@ -407,9 +408,9 @@ class PMFLevel extends PMF{ return false; } $index = $this->getIndex($X, $Z); - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; $mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9)); $old_m = ord($this->chunks[$index][$Y][$mindex]); if(($y & 1) === 0){ @@ -446,13 +447,13 @@ class PMFLevel extends PMF{ }elseif($this->chunks[$index][$Y] === false){ return [AIR, 0]; } - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; #Need to fix. But idk how. if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix - $b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); - $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); + $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); }else{ $b = 0; $m = 0; diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 1a28f30e..33261b6c 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -574,8 +574,17 @@ class Utils{ /** * Euclidian distance, but without square roots */ - public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){ - return ($pos2->x - $pos1->x)*($pos2->x - $pos1->x) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z); + public static function distance_noroot($pos1, $pos2){ + if($pos1 instanceof Vector3){ + $pos1 = $pos1->toArray(); + } + if($pos2 instanceof Vector3){ + $pos2 = $pos2->toArray(); + } + $pX = ($pos1["x"] - $pos2["x"]); + $pY = ($pos1["y"] - $pos2["y"]); + $pZ = ($pos1["z"] - $pos2["z"]); + return ($pX*$pX) + ($pY*$pY) + ($pZ*$pZ); } /** diff --git a/src/world/Level.php b/src/world/Level.php index fec9a3a9..ca356d63 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -456,12 +456,13 @@ class Level{ * @param number $x * @param number $y * @param number $z + * @param boolean $positionfy assign coordinates to block or not * @return GenericBlock | false if failed */ - public function getBlockWithoutVector($x, $y, $z){ + public function getBlockWithoutVector($x, $y, $z, $positionfy = true){ $b = $this->level->getBlock($x, $y, $z); - return BlockAPI::get($b[0], $b[1], new Position($x, $y, $z, $this)); + return BlockAPI::get($b[0], $b[1], $positionfy ? new Position($x, $y, $z, $this) : false); } /**