From dda8d5e5969618bb9b88a6520cea07bf5b7a08af Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 28 Aug 2022 18:43:33 +0300 Subject: [PATCH] *forgot to add files* --- src/entity/Pathfindable.php | 7 +++ src/utils/StringUtils.php | 0 src/utils/pathfinder/Path.php | 21 +++++++ src/utils/pathfinder/PathNode.php | 34 ++++++++++ src/utils/pathfinder/Pathfinder.php | 96 +++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 src/entity/Pathfindable.php create mode 100644 src/utils/StringUtils.php create mode 100644 src/utils/pathfinder/Path.php create mode 100644 src/utils/pathfinder/PathNode.php create mode 100644 src/utils/pathfinder/Pathfinder.php diff --git a/src/entity/Pathfindable.php b/src/entity/Pathfindable.php new file mode 100644 index 00000000..46b35445 --- /dev/null +++ b/src/entity/Pathfindable.php @@ -0,0 +1,7 @@ +points[] = $point; + } + + public function getPointAndIncreaseIndex(){ + return $this->points[$this->currentPointIndex++]; + } + + public function increaseIndexAndGetPoint(){ + return isset($this->points[$this->currentPointIndex + 1]) ? $this->points[++$this->currentPointIndex] : null; + } + + public function finish(){ + $this->points = array_reverse($this->points); + } +} \ No newline at end of file diff --git a/src/utils/pathfinder/PathNode.php b/src/utils/pathfinder/PathNode.php new file mode 100644 index 00000000..77e0cf2e --- /dev/null +++ b/src/utils/pathfinder/PathNode.php @@ -0,0 +1,34 @@ +x = $x; + $this->y = $y; + $this->z = $z; + $this->parent = $parent; + $this->g = $g; + $this->h = $h; + $this->f = $f; + } + public function __toString(){ + return "PathNode(x={$this->x}, y={$this->y}, z ={$this->z}, g={$this->g}, h={$this->h}, f={$this->f})"; + } + public function calcH($endNode){ + return pow(($endNode->x - $this->x), 2) + pow(($endNode->y - $this->y), 2) + pow(($endNode->z - $this->z), 2); + } + + public function calcF(){ + return $this->h + $this->g; + } + + public static function loadFromVector3($v3, $parent = null){ + return new Node($v3->x, $v3->y, $v3->z, $parent, 0, 0, 0); + } + + public function equals($n){ + return $n->x == $this->x && $n->y == $this->y && $n->z == $this->z; + } + +} \ No newline at end of file diff --git a/src/utils/pathfinder/Pathfinder.php b/src/utils/pathfinder/Pathfinder.php new file mode 100644 index 00000000..9b159644 --- /dev/null +++ b/src/utils/pathfinder/Pathfinder.php @@ -0,0 +1,96 @@ +entity = $entity; + } + + public function updateEntityPos($x, $y, $z){ + $this->entity->x = $x; + $this->entity->y = $y; + $this->entity->z = $z; + } + + public function buildTo($end){ + $goal = Node::loadFromVector3($end); //247 68 215 + $this->openList[] = new Node(ceil($this->entity->x), ceil($this->entity->y), ceil($this->entity->z), 0, 0, 0); + $this->closedList = []; + $cnt = 0; + while(!empty($this->openList)){ + $currentIndex = 0; + $currentNode = $this->openList[$currentIndex]; + foreach($this->openList as $index => $node){ + if($node->f < $currentNode->f){ + $currentIndex = $index; + $currentNode = $node; + } + } + array_splice($this->openList, $currentIndex, 1); + $this->closedList[] = $currentNode; + if($currentNode->equals($goal)){ + $path = new Path(); + $current = $currentNode; + while($current != null){ + $path->addPoint($current); + $current = $current->parent; + } + $path->finish(); + return $path; + } + $children = []; + //y was removed + for($x = -1; $x <= 1; $x++){ + for($z = -1; $z <= 1; $z++){ + $nodePosition = new Vector3($currentNode->x + $x, $currentNode->y, $currentNode->z + $z); + if(!$this->isPositionValid($nodePosition)){ + continue; + } + if($currentNode->equals($nodePosition)){ + continue; + } + $node = Node::loadFromVector3($nodePosition, $currentNode); + $children[] = $node; + } + } + + foreach($children as $child){ + foreach($this->closedList as $closed){ + if($child->equals($closed)){ + goto exitLoopLbl; + } + } + exitLoopLbl: + $child->g = $currentNode->g + 1; + $child->h = $child->calcH($end); + $child->f = $child->g + $child->h; + + $this->openList[] = $child; + + } + + $cnt++; + if($cnt === 6){ + break; + } + } + + return Pathfinder::NO_PATH_FOUND; + + } + + public function isPositionValid($nodePosition){ + return $nodePosition->x >= 0 && $nodePosition->x < 256 && $nodePosition->y > 0 && $nodePosition->y < 128 && $nodePosition->z >= 0 && $nodePosition->z < 256 && $this->isEmpty($nodePosition); + } + public function distanceBetween($p1, $p2){ + return sqrt(pow(($p1->x - $p2->x),2) + pow(($p1->y - $p2->y),2) + pow(($p1->z - $p2->z),2)); + } + + public function isEmpty($v3){ + return !$this->entity->level->getBlock($v3)->isSolid; + } + +} \ No newline at end of file