diff --git a/src/dependencies.php b/src/dependencies.php index c10c553f..517001e9 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -103,6 +103,7 @@ require_once(FILE_PATH . "/src/entity/Attachable.php"); require_once(FILE_PATH . "/src/entity/Damageable.php"); require_once(FILE_PATH . "/src/entity/Breedable.php"); require_once(FILE_PATH . "/src/entity/Damageable.php"); +require_once(FILE_PATH . "/src/entity/Pathfindable.php"); require_once(FILE_PATH . "/src/entity/Entity.php"); require_once(FILE_PATH . "/src/entity/Living.php"); require_once(FILE_PATH . "/src/entity/Creature.php"); diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 0619a9e5..b7ec2975 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -6,13 +6,10 @@ move methods abstract class Creature extends Living{ const CLASS_TYPE = ENTITY_MOB; - public $pathFinder, $path = null, $currentIndex = 0, $currentNode; - public $inPanic; public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->inPanic = false; //force for now - $this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new Pythagoras3D(), new ManhattanHeuristic3D()); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 1, "generic"); //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); @@ -24,23 +21,6 @@ abstract class Creature extends Living{ } - public function update(){ - if($this->onGround){ - /*if($this->path === null && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){ - $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, $this->z + mt_rand(-10, 10), $this->level)); - } - if($this->path != null && (is_array($this->path) && count($this->path) <= 0 || $this->currentIndex >= count($this->path))){ - $this->path = null; - $this->currentIndex = 0; - $this->currentNode = false; - }elseif($this->path != null && ($this->currentNode == false || $this->ai->mobController->moveTo($this->currentNode->x, $this->currentNode->y, $this->currentNode->z) === false)){ - $this->currentNode = $this->path[$this->currentIndex++]; - }*/ - } - parent::update(); - - } - public function createSaveData(){ $data = parent::createSaveData(); $data["State"] = @$this->getState(); diff --git a/src/entity/Living.php b/src/entity/Living.php index d4fc352a..bda9945c 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -1,20 +1,38 @@ target = false; $this->ai = new EntityAI($this); + $this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new ManhattanHeuristic3D(), new ManhattanHeuristic3D()); + $this->pathFollower = new PathFollower($this); parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; $this->hasGravity = true; $this->hasKnockback = true; } + public function hasPath(){ + return $this->path != null; + } + + public function __destruct() + { + parent::__destruct(); + unset($this->pathFollower->entity); + unset($this->ai->entity); + } + public function update(){ if($this->idleTime <= 0) { $this->ai->updateTasks(); } + + $this->pathFollower->followPath(); + parent::update(); } diff --git a/src/entity/Pathfindable.php b/src/entity/Pathfindable.php index 46b35445..6a21af69 100644 --- a/src/entity/Pathfindable.php +++ b/src/entity/Pathfindable.php @@ -3,5 +3,5 @@ Illegal features */ interface Pathfindable{ - function getPathFinder(); + public function hasPath(); } \ No newline at end of file diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php new file mode 100644 index 00000000..85d0ddb9 --- /dev/null +++ b/src/entity/ai/path/PathFollower.php @@ -0,0 +1,24 @@ +entity = $entity; + } + + public function followPath(){ + if($this->entity->path != null && (is_array($this->entity->path) && count($this->entity->path) <= 0 || $this->entity->currentIndex >= count($this->entity->path))){ + $this->entity->path = null; + $this->entity->currentIndex = 0; + $this->entity->currentNode = false; + }elseif($this->entity->path != null && ($this->entity->currentNode == false || $this->entity->ai->mobController->moveTo($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z) === false)){ + $this->entity->currentNode = $this->entity->path[$this->entity->currentIndex++]; + } + } + +} + diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index 9ebfbb37..3e56ddc6 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase public function canBeExecuted(EntityAI $ai) { - return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/ + return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ } public function onUpdate(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 1c164c3c..f120f98c 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -6,7 +6,7 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; public function canBeExecuted(EntityAI $ai){ - return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt"); + return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); } protected function findTarget($e, $r){ @@ -42,7 +42,7 @@ class TaskLookAtPlayer extends TaskBase{ } public function onUpdate(EntityAI $ai){ - if(!($this->target instanceof Entity) || Utils::distance($ai->entity, $this->target) > 6){ //TODO max distance for different mobs + if(!($this->target instanceof Entity) || Utils::distance($ai->entity, $this->target) > 6 || $this->target->level->getName() != $ai->entity->level->getName()){ //TODO max distance for different mobs $this->reset(); return; } diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 03b88661..19d33eef 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -21,7 +21,7 @@ class TaskRandomWalk extends TaskBase public function onUpdate(EntityAI $ai) { - if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { + if($ai->entity instanceof Creeper && $ai->entity->isIgnited() || $ai->isStarted("TaskTempt")) { return false; //TODO Better way: block movement } --$this->selfCounter; @@ -30,7 +30,7 @@ class TaskRandomWalk extends TaskBase public function canBeExecuted(EntityAI $ai) { - if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { + if($ai->entity instanceof Creeper && $ai->entity->isIgnited() || $ai->entity->hasPath() || $ai->isStarted("TaskTempt")) { return false; } return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index e155c0d5..5a1b4ec2 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -17,7 +17,7 @@ class TaskTempt extends TaskBase public function onUpdate(EntityAI $ai) { - if($this->target instanceof Entity && $this->target->isPlayer() && $this->target->distanceSquared($ai->entity) > 100 || !$ai->entity->isFood($this->target->player->getHeldItem()->getID())){ + if(($this->target instanceof Entity && !$this->target->isPlayer()) || $this->target->distanceSquared($ai->entity) > 100 || !$ai->entity->isFood($this->target->player->getHeldItem()->getID()) || $this->target->level->getName() != $ai->entity->level->getName()){ $this->reset(); return; } @@ -31,7 +31,7 @@ class TaskTempt extends TaskBase public function canBeExecuted(EntityAI $ai) { - if(!($ai->entity instanceof Breedable)){ + if(!($ai->entity instanceof Breedable)){ //TODO Work with path return false; } $target = $this->findTarget($ai->entity, 10);