From 916a06c1618698b50670652c6d539717fe20fc3a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 13 Nov 2022 23:31:42 +0300 Subject: [PATCH] a slightly better pathnavigator --- src/entity/Creature.php | 11 ++++ src/entity/ai/path/PathNavigator.php | 72 ++++++++++++++++---------- src/utils/pathfinder/MinecraftNode.php | 4 ++ 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index cb3989ff..869cad49 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -19,6 +19,17 @@ abstract class Creature extends Living{ } } + public function update(){ + i/*f($this->path === false || count($this->path) <= 0){ + $this->path = $this->pathFinder->findPath(MinecraftNode::fromArray([$this->x, $this->y, $this->z]), new MinecraftNode($this->x + mt_rand(-7, 7), $this->y, $this->z + mt_rand(-7, 7))); + } + $state = $this->pathNavigator->followPath($this->path); + if($state === PathNavigator::PATH_COMPLETED){ + $this->path = false; + }*/ + parent::update(); + } + public function createSaveData(){ $data = parent::createSaveData(); $data["State"] = @$this->getState(); diff --git a/src/entity/ai/path/PathNavigator.php b/src/entity/ai/path/PathNavigator.php index 0dd078a9..ee0a36e3 100644 --- a/src/entity/ai/path/PathNavigator.php +++ b/src/entity/ai/path/PathNavigator.php @@ -1,46 +1,64 @@ entity = $entity; $this->currentPointIndex = 0; } + public function reset(){ $this->currentPointIndex = 0; } - + /** + * @param Node[] $path + */ public function followPath($path){ //TODO remove local var - if($this->currentPointIndex >= count($path)){ - $this->entity->moveTime = mt_rand(100, 200); - try{ - return true; //path finished - }finally { - $this->reset(); - } + if(!$this->canContinue()){ + console($this->getTo); + console($this->entity); + $diffX = $this->getTo->x - $this->entity->x; + $diffY = 0; //todo ? + $diffZ = $this->getTo->z - $this->entity->z; + + $this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ)); + return self::PATH_CANT_CONTINUE; } - $point = $path[$this->currentPointIndex]; - $eX = ceil($this->entity->x); - $eY = ceil($this->entity->y); - $eZ = ceil($this->entity->z); - //console($point.":::".$eX.":".$eY.":".$eZ."::::".$this->entity->moveTime); - if($this->entity->moveTime <= 0 && $point->x === $eX && $point->y === $eY && $point->z === $eZ){ - ++$this->currentPointIndex; - }elseif($this->entity->moveTime <= 0 && !$this->entity->isMoving()){ - //console($point.":::".$eX.":".$eY.":".$eZ); - $vX = $point->x - $eX; - $vY = 0;//$point->y - $this->entity->y; //vY is always 0 in current pathfinder\ - $vZ = $point->z - $eZ; - //console($vX."::".$vZ); - $this->entity->addVelocity(($vX > 0 ? 0.1 : ($vX < 0 ? -0.1 : 0)), $vY, ($vZ > 0 ? 0.1 : ($vZ < 0 ? -0.1 : 0))); - $this->testLook($point); - //console($this->entity->yaw); - $this->entity->moveTime = 5; //use 20 for debug + $index = $this->currentPointIndex++; + if($index >= count($path)){ + $this->onPathEnd(); + console("COMPLETED"); + return self::PATH_COMPLETED; } - //$this->entity->moveTime = 5* - return false; + $node = $path[$index]; + + $this->getTo = $node; + + $diffX = $node->x - $this->entity->x; + $diffY = 0; //todo ? + $diffZ = $node->z - $this->entity->z; + + $this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ)); + + return self::PATH_CONTINUE; + } + + public function canContinue(){ + return empty($this->getTo) || (floor($this->entity->x) == floor($this->getTo->x) && floor($this->entity->z) == floor($this->getTo->z)); + } + + public function onPathEnd(){ + $this->currentPointIndex = 0; + unset($this->getTo); } public function testLook($pos){ diff --git a/src/utils/pathfinder/MinecraftNode.php b/src/utils/pathfinder/MinecraftNode.php index 36ade307..4d5345af 100644 --- a/src/utils/pathfinder/MinecraftNode.php +++ b/src/utils/pathfinder/MinecraftNode.php @@ -12,6 +12,10 @@ class MinecraftNode extends Node parent::__construct($x, $y, $z); } + public static function fromArray($arr){ + return new MinecraftNode($arr[0], $arr[1], $arr[2]); + } + public function __toString() { return "MinecraftNode({$this->x}, {$this->y}, {$this->z})";