mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 11:39:43 +00:00
*forgot to add files*
This commit is contained in:
parent
70a36f8e6b
commit
dda8d5e596
5 changed files with 158 additions and 0 deletions
7
src/entity/Pathfindable.php
Normal file
7
src/entity/Pathfindable.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
/*
|
||||
Illegal features
|
||||
*/
|
||||
interface Pathfindable{
|
||||
function getPathFinder();
|
||||
}
|
||||
0
src/utils/StringUtils.php
Normal file
0
src/utils/StringUtils.php
Normal file
21
src/utils/pathfinder/Path.php
Normal file
21
src/utils/pathfinder/Path.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
class Path{
|
||||
public $points = [];
|
||||
public $currentPointIndex = 0;
|
||||
public function addPoint(Node $point){
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
34
src/utils/pathfinder/PathNode.php
Normal file
34
src/utils/pathfinder/PathNode.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
class Node{
|
||||
public $g, $h, $f, $parent;
|
||||
public $x, $y, $z;
|
||||
public function __construct($x, $y, $z, $parent, $g = "U", $h = "U", $f = "U"){
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
96
src/utils/pathfinder/Pathfinder.php
Normal file
96
src/utils/pathfinder/Pathfinder.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
class Pathfinder{
|
||||
const NO_PATH_FOUND = "NO_PATH";
|
||||
private $entity;
|
||||
private $openList, $closedList;
|
||||
|
||||
public function __construct($entity, $avoidWater = true){ //avoid - WIP
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue