diff --git a/src/entity/Animal.php b/src/entity/Animal.php index bc803c8f..f5655517 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -17,6 +17,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ $this->ai->addTask(new TaskTempt()); $this->ai->addTask(new TaskPanic()); + $this->ai->addTask(new TaskMate()); } public function harm($dmg, $cause = "generic", $force = false){ @@ -31,11 +32,21 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } public function breed(){ - $c = $this->spawnChild(); - if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){ - $c->parent = $this; + if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){ + $c = $this->spawnChild(); + $c->parent = $this; //TODO save eid to avoid memory leaks? $this->server->api->entity->spawnToAll($c); + return $c; } + return false; + } + + public function resetInLove(){ + $this->inLove = 0; + } + + public function canMate($ent){ + return $ent->eid != $this->eid && $this->type === $ent->type && $this->isInLove() && $ent->isInLove(); } public function update(){ @@ -47,6 +58,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{ }else{ $this->setAge($age); } + if($this->isInLove() && !isset($this->level->entitiesInLove[$this->eid])){ + $this->level->entitiesInLove[$this->eid] = true; + }else if(!$this->isInLove() && isset($this->level->entitiesInLove[$this->eid])){ + unset($this->level->entitiesInLove[$this->eid]); + } } public function getAge(){ @@ -88,8 +104,10 @@ 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())){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1); + if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby()){ //TODO check vanilla + if(($e->player->gamemode & 0x01) === SURVIVAL){ + $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1); + } $this->inLove = 600; //600 ticks, original mehod from mcpe return true; } @@ -103,7 +121,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ --$this->inLove; } } - + public function getDrops(){ if($this->isBaby()){ return []; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index dd1b1cb9..a081c0a1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -687,27 +687,7 @@ class Entity extends Position } - if($this->knockbackTime > 0){ - --$this->knockbackTime; - } - if($this->delayBeforePickup > 0){ - --$this->delayBeforePickup; - } - if($this->moveTime > 0){ - --$this->moveTime; - } - if($this->lookTime > 0){ - --$this->lookTime; - } - if($this->idleTime > 0){ - --$this->idleTime; - } - - - if($this->inAction){ - ++$this->inActionCounter; - } - + $this->counterUpdate(); if($this->lastHeadYaw != $this->headYaw){ $this->sendHeadYaw(); @@ -1025,7 +1005,30 @@ class Entity extends Position break; } } - + + public function counterUpdate(){ + if($this->knockbackTime > 0){ + --$this->knockbackTime; + } + if($this->delayBeforePickup > 0){ + --$this->delayBeforePickup; + } + if($this->moveTime > 0){ + --$this->moveTime; + } + if($this->lookTime > 0){ + --$this->lookTime; + } + if($this->idleTime > 0){ + --$this->idleTime; + } + + + if($this->inAction){ + ++$this->inActionCounter; + } + } + public function close() { if($this->closed === false){ diff --git a/src/entity/Living.php b/src/entity/Living.php index 1d1101f0..7298810f 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -28,12 +28,16 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ return $this->path != null; } public function eatGrass(){} - public function __destruct() - { - parent::__destruct(); + + public function close(){ + parent::close(); unset($this->pathFollower->entity); unset($this->ai->entity); + unset($this->ai->mobController->entity); + unset($this->ai); + unset($this->parent); } + public function canBeShot(){ return true; } diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index af13cff6..78d342bd 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->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ + return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskMate") && !$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 da852d3b..aa8ab776 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; public function canBeExecuted(EntityAI $ai){ - return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath(); + return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath(); } protected function findTarget($e, $r){ diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php new file mode 100644 index 00000000..c0e4842f --- /dev/null +++ b/src/entity/ai/tasks/TaskMate.php @@ -0,0 +1,72 @@ +selfCounter = 60; + } + + public function onEnd(EntityAI $ai) + { + $this->selfCounter = 0; + $this->targetMateID = null; + $ai->entity->pitch = 0; + } + + public function onUpdate(EntityAI $ai) + { + $movTo = $ai->entity->level->entityList[$this->targetMateID] ?? false; + if(!($movTo instanceof Entity) || $movTo->dead || !$movTo->isInLove()){ + $this->reset(); + $this->onEnd($ai); + return; + } + $ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false); + $ai->mobController->lookOn($movTo); + + --$this->selfCounter; + if($this->selfCounter <= 0 && $ai->entity->distanceSquared($movTo) < 9){ + $this->createBaby($ai, $movTo); + } + } + public function createBaby(EntityAI $ai, Entity $entpar2){ + /** + * @var Animal $baby + */ + $baby = $ai->entity->breed(); + //this.theAnimal.setGrowingAge(6000); TODO growing age? + //this.targetMate.setGrowingAge(6000); + + $ai->entity->resetInLove(); + $entpar2->resetInLove(); + $baby->setAge(-24000); + } + + public function findMate(EntityAI $ai){ + $distance = 8.0*8.0; + $minDist = $distance;; //TODO check bb intersection? + $matedEID = null; + + foreach($ai->entity->level->entitiesInLove as $eid => $_){ + $e = $ai->entity->level->entityList[$eid] ?? false; + if($e instanceof Entity){ + $d = $ai->entity->distanceSquared($e); + if($ai->entity->canMate($e) && $d < $minDist){ + $minDist = $d; + $matedEID = $e->eid; + } + } + } + return $matedEID; + } + + public function canBeExecuted(EntityAI $ai) + { + return $ai->entity->isInLove() && ($this->targetMateID = $this->findMate($ai)) != null; + } + +} + diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 82203ef0..e4bae095 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -33,8 +33,8 @@ class TaskRandomWalk extends TaskBase { if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { return false; - } - return !$ai->entity->inPanic && !$ai->isStarted("TaskEatTileGoal")&& !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; + } // 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; } diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index d01fe0e2..9519543a 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -29,7 +29,7 @@ class TaskTempt extends TaskBase public function canBeExecuted(EntityAI $ai) { - if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic){ //TODO Work with path + if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){ //TODO Work with path return false; } $target = $this->findTarget($ai->entity, 10); diff --git a/src/world/Level.php b/src/world/Level.php index 884af3bc..b65a49e3 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -11,6 +11,8 @@ class Level{ */ public $entityList; + public $entitiesInLove = []; + public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0; private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;