diff --git a/src/entity/Animal.php b/src/entity/Animal.php index fd9678e7..5adf6ba8 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -5,6 +5,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public $parent; public $inLove; //do NOT add it into metadata, it doesnt send it to player public $age; + + public $loveTimeout; + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->setAge(isset($data["Age"]) ? $data["Age"] : 0); @@ -48,6 +51,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public function update($now){ parent::update($now); + + if($this->loveTimeout > 0){ + --$this->loveTimeout; + } + $age = $this->getAge() + 1; //100 - fast. debug, 1 - normal if($age >= 0 && $this->isBaby()){ $this->setAge($age); @@ -101,7 +109,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public function interactWith(Entity $e, $action){ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); - if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby()){ + if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby() && $this->loveTimeout <= 0){ if(($e->player->gamemode & 0x01) === SURVIVAL){ $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1); } diff --git a/src/entity/ai/tasks/TaskFollowParent.php b/src/entity/ai/tasks/TaskFollowParent.php new file mode 100644 index 00000000..3c71c031 --- /dev/null +++ b/src/entity/ai/tasks/TaskFollowParent.php @@ -0,0 +1,44 @@ +speedMultiplier = $speed; + } + + public function onStart(EntityAI $ai) + { + $this->selfCounter = 1; + } + + public function onEnd(EntityAI $ai) + { + $this->selfCounter = 0; + } + + public function onUpdate(EntityAI $ai) + { + if(!$ai->entity->isBaby() || !($ai->entity->parent instanceof Entity) && $ai->entity->parent->dead){ + $this->reset(); + $ai->entity->parent = null; + return false; + } + + $dist = $ai->entity->distanceSquared($ai->entity->parent); + + if($dist > 256 || $dist < 9){ //3-16 blocks + $this->reset(); + return false; + } + $parent = $ai->entity->parent; + + $ai->mobController->setLookPosition($parent->x, $parent->y + $parent->getEyeHeight(), $parent->z, 30, $ai->entity->getVerticalFaceSpeed()); + $ai->mobController->setMovingTarget($parent->x, $parent->y, $parent->z, $this->speedMultiplier); + } + + public function canBeExecuted(EntityAI $ai) + { + return $ai->entity->isBaby() && !$ai->entity->parent->dead; //TODO find a new parent if the current one is dead + } +} + diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php index 28ebe40b..5bfdd26b 100644 --- a/src/entity/ai/tasks/TaskMate.php +++ b/src/entity/ai/tasks/TaskMate.php @@ -46,6 +46,9 @@ class TaskMate extends TaskBase //this.theAnimal.setGrowingAge(6000); TODO growing age? //this.targetMate.setGrowingAge(6000); + $ai->entity->loveTimeout = 6000; + $entpar2->loveTimeout = 6000; + $ai->entity->resetInLove(); $entpar2->resetInLove(); $baby->setAge(-24000); diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index cb9d38ee..dacf4f67 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -26,7 +26,7 @@ class TaskRandomWalk extends TaskBase public function onUpdate(EntityAI $ai) { - if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt")) { + if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskFollowParent") ) { $this->reset(); return false; //TODO Better way: block movement } @@ -40,7 +40,7 @@ class TaskRandomWalk extends TaskBase if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { return false; } // i really need mutexBits - return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; + return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$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 6d7d2b53..4fd664ca 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->isTargetValid($ai)){ + if(!$this->isTargetValid($ai) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){ $this->reset(); return false; } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 5cf752b6..054fd647 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -18,7 +18,7 @@ class Chicken extends Animal{ $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskTempt(1.0)); $this->ai->addTask(new TaskMate(1.0)); - + $this->ai->addTask(new TaskFollowParent(1.0)); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index 564bf77f..81fed1f7 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -15,6 +15,7 @@ class Cow extends Animal{ $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskTempt(1.0)); $this->ai->addTask(new TaskMate(1.0)); + $this->ai->addTask(new TaskFollowParent(1.1)); } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 5cee7186..49fb18c1 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -17,7 +17,7 @@ class Pig extends Animal implements Rideable{ $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskTempt(1.0)); $this->ai->addTask(new TaskMate(1.0)); - + $this->ai->addTask(new TaskFollowParent(1.1)); } /** diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 94bff624..c03ee938 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -21,6 +21,7 @@ class Sheep extends Animal{ $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskTempt(1.0)); $this->ai->addTask(new TaskMate(1.0)); + $this->ai->addTask(new TaskFollowParent(1.0)); } public function createSaveData(){ diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 7c1cc7a3..9e8c9955 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -132,7 +132,7 @@ class Vector3{ } public function distanceSquared($x = 0, $y = 0, $z = 0){ - if(($x instanceof Vector3) === true){ + if($x instanceof Vector3){ return $this->distanceSquared($x->x, $x->y, $x->z); }else{ return ($this->x - $x)*($this->x - $x) + ($this->y - $y)*($this->y - $y) + ($this->z - $z)*($this->z - $z);