diff --git a/src/Player.php b/src/Player.php index 82f6a0bc..106bfe10 100755 --- a/src/Player.php +++ b/src/Player.php @@ -298,7 +298,14 @@ class Player{ $this->forceMovement = $pos; } } - + + $pk = new SetEntityMotionPacket(); + $pk->eid = 0; + $pk->speedX = 0; + $pk->speedY = 0; + $pk->speedZ = 0; + $this->dataPacket($pk); + $pk = new MovePlayerPacket; $pk->eid = 0; $pk->x = $pos->x; diff --git a/src/entity/Animal.php b/src/entity/Animal.php index a1979353..ac978e70 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -21,21 +21,29 @@ abstract class Animal extends Creature implements Ageable, Breedable{ $this->searchForClosestPlayers = true; } + public function isPlayerValid(Player $player){ + return $player->spawned && $this->isFood($player->getHeldItem()->id) && !$player->entity->dead; + } + public function handlePrePlayerSearcher(){ parent::handlePrePlayerSearcher(); - - if($this->closestPlayerThatCanFeedEID !== false && !isset($this->level->entityList[$this->closestPlayerThatCanFeedEID])){ - $this->closestPlayerThatCanFeedEID = false; - $this->closestPlayerThatCanFeedDist = INF; + if($this->closestPlayerThatCanFeedEID !== false){ + $player = $this->level->entityList[$this->closestPlayerThatCanFeedEID] ?? false; + if($player === false || !$this->isPlayerValid($player->player)){ + $this->closestPlayerThatCanFeedEID = false; + $this->closestPlayerThatCanFeedDist = INF; + }else{ + $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $this->closestPlayerThatCanFeedDist = $dist; + } } - } public function handlePlayerSearcher(Player $player, $dist){ parent::handlePlayerSearcher($player, $dist); if($this->closestPlayerThatCanFeedDist >= $dist){ - if($player->spawned && $this->isFood($player->getHeldItem()->id)){ + if($this->isPlayerValid($player)){ $this->closestPlayerThatCanFeedDist = $dist; $this->closestPlayerThatCanFeedEID = $player->entity->eid; } diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 8ee78114..5a34b3d9 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -21,11 +21,18 @@ abstract class Creature extends Living{ return parent::update($now); } + public function handlePrePlayerSearcher(){ parent::handlePrePlayerSearcher(); - if($this->closestPlayerEID !== false && !isset($this->level->entityList[$this->closestPlayerEID])){ - $this->closestPlayerEID = false; - $this->closestPlayerDist = INF; + if($this->closestPlayerEID !== false){ + $player = $this->level->entityList[$this->closestPlayerEID] ?? false; + if($player === false){ + $this->closestPlayerEID = false; + $this->closestPlayerDist = INF; + }else{ + $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $this->closestPlayerDist = $dist; + } } } diff --git a/src/entity/Monster.php b/src/entity/Monster.php index fdba067f..674515d0 100644 --- a/src/entity/Monster.php +++ b/src/entity/Monster.php @@ -1,8 +1,10 @@ ai->addTask(new TaskAttackPlayer()); } public function getAttackDamage(){ @@ -20,4 +22,34 @@ class Monster extends Creature{ return false; } } + + public function isPlayerValid(Player $player){ + return $player->spawned && !$player->entity->dead; + } + + public function handlePrePlayerSearcher(){ + parent::handlePrePlayerSearcher(); + if($this->closestPlayerToAttackEID !== false){ + $player = $this->level->entityList[$this->closestPlayerToAttackEID] ?? false; + if($player === false || !$this->isPlayerValid($player->player)){ + $this->closestPlayerToAttackEID = false; + $this->closestPlayerToAttackDist = INF; + }else{ + $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $this->closestPlayerToAttackDist = $dist; + } + } + } + + public function handlePlayerSearcher(Player $player, $dist){ + parent::handlePlayerSearcher($player, $dist); + + if($this->closestPlayerToAttackDist >= $dist){ + if($this->isPlayerValid($player)){ + $this->closestPlayerToAttackDist = $dist; + $this->closestPlayerToAttackEID = $player->entity->eid; + } + } + } + } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index d11815fa..7615dbf3 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -50,7 +50,7 @@ class TaskAttackPlayer extends TaskBase public function isTargetValid(EntityAI $ai){ $e = $ai->entity; - if($e->target instanceof Entity && !$e->target->closed){ + if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){ $t = $e->target; $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); @@ -72,7 +72,7 @@ class TaskAttackPlayer extends TaskBase } } - $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null; + $closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null; if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php index 5bcb627f..04d53630 100644 --- a/src/entity/ai/tasks/TaskRangedAttack.php +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -73,26 +73,26 @@ class TaskRangedAttack extends \TaskBase $diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY; $diffZ = $target->z - $selfEntity->z; $v12 = sqrt($diffX * $diffX + $diffZ * $diffZ); - //if($v12 >= 1.0e-7){ - $yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90; - $pitch = -((atan2($diffY, $v12) * 180) / M_PI); - $v16 = $diffX / $v12; - $v18 = $diffZ / $v12; - $arrow->x = $selfEntity->x + $v16; - $arrow->y = $posY; - $arrow->z = $selfEntity->z + $v18; - $arrow->yaw = $yaw; - $arrow->pitch = $pitch; - $v20 = $v12 * 0.2; - $arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12); - $this->server->api->entity->spawnToAll($arrow); - //} + if($v12 >= 0.0000001){ + $yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90; + $pitch = -((atan2($diffY, $v12) * 180) / M_PI); + $v16 = $diffX / $v12; + $v18 = $diffZ / $v12; + $arrow->x = $selfEntity->x + $v16; + $arrow->y = $posY; + $arrow->z = $selfEntity->z + $v18; + $arrow->yaw = $yaw; + $arrow->pitch = $pitch; + $v20 = $v12 * 0.2; + $arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12); + $this->server->api->entity->spawnToAll($arrow); + } } public function isTargetValid(EntityAI $ai){ $e = $ai->entity; - if($e->target instanceof Entity && !$e->target->closed){ + if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){ $t = $e->target; $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); @@ -114,7 +114,7 @@ class TaskRangedAttack extends \TaskBase } } - $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null; + $closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null; if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? diff --git a/src/world/Level.php b/src/world/Level.php index 345ba3fd..ffa7ed81 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -584,7 +584,7 @@ class Level{ $e->handlePrePlayerSearcher(); foreach($this->players as $player){ - $dist = ($e->x - $player->entity->x)*($e->x - $player->entity->x) + ($e->y - $player->entity->y) + ($e->z - $player->entity->z); + $dist = ($e->x - $player->entity->x)*($e->x - $player->entity->x) + ($e->y - $player->entity->y)*($e->y - $player->entity->y) + ($e->z - $player->entity->z)*($e->z - $player->entity->z); $e->handlePlayerSearcher($player, $dist); } }