From b8589c9dd45ffb07a9945594ecf6793688eb7dd7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jan 2023 00:19:50 +0300 Subject: [PATCH] mobs will attack now, fixed division by 0(i hate php) --- src/API/PlayerAPI.php | 2 +- src/Player.php | 1 + src/dependencies.php | 1 + src/entity/Entity.php | 5 +++ src/entity/Monster.php | 9 +++++- src/entity/ai/EntityAI.php | 1 + src/entity/ai/MobController.php | 2 +- src/entity/ai/tasks/TaskAttackPlayer.php | 39 ++++++++++++++++++++++++ src/entity/ai/tasks/TaskTempt.php | 1 - src/entity/monster/Creeper.php | 3 ++ src/entity/monster/PigZombie.php | 4 ++- src/entity/monster/Skeleton.php | 4 +++ src/entity/monster/Zombie.php | 4 +++ 13 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/entity/ai/tasks/TaskAttackPlayer.php diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f797b7c9..63dfb710 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -66,7 +66,7 @@ class PlayerAPI{ $message = " was killed by " . $e->name; break; default: - $message = " was killed"; + $message = " was killed by {$e->getName()}"; break; } } diff --git a/src/Player.php b/src/Player.php index b24ca053..d7be1339 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1511,6 +1511,7 @@ class Player{ }else{ $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch); } + $this->entity->updateAABB(); } break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: diff --git a/src/dependencies.php b/src/dependencies.php index 46829248..b7af7702 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -96,6 +96,7 @@ require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php"); require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); +require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php"); require_once(FILE_PATH . "/src/entity/Rideable.php"); require_once(FILE_PATH . "/src/entity/Attachable.php"); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4ac442ac..865d827b 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -175,6 +175,7 @@ class Entity extends Position } break; } + $this->radius = $this->width / 2; $this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); $this->update(); $this->updateLast(); @@ -1482,6 +1483,10 @@ class Entity extends Position } } + public function getAttackDamage(){ + return 0; + } + public function setSpeed($s) { $this->speed = $s; diff --git a/src/entity/Monster.php b/src/entity/Monster.php index 510f2b83..30615f02 100644 --- a/src/entity/Monster.php +++ b/src/entity/Monster.php @@ -1,4 +1,11 @@ ai->addTask(new TaskAttackPlayer()); + } + + public function getAttackDamage(){ + return 2; + } } diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index ccf7ba13..5a08915c 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -47,6 +47,7 @@ class EntityAI } public function updateTasks(){ + if(!($this->entity instanceof Entity)) return; foreach($this->tasks as $t){ if(!$t->isStarted && $t->canBeExecuted($this)){ $t->isStarted = true; diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 55495dd7..a9585dd3 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -64,7 +64,7 @@ class MobController if($pitch){ $diff = sqrt($x * $x + $z * $z); - $calcPitch = rad2deg(atan($y / $diff)); + $calcPitch = $diff == 0 ? ($y < 0 ? -90 : 90) : rad2deg(atan($y / $diff)); $this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch; } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php new file mode 100644 index 00000000..b6acb81a --- /dev/null +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -0,0 +1,39 @@ +selfCounter = 1; + } + public function onUpdate(EntityAI $ai) + { + if(($this->target instanceof Entity && !$this->target->isPlayer()) || $this->target->distanceSquared($ai->entity) > 100 || $this->target->level->getName() != $ai->entity->level->getName()){ + $this->reset(); + return; + } + + $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z); + $ai->mobController->lookOn($this->target); + if($ai->entity->boundingBox->intersectsWith($this->target->boundingBox) && $this->attackTime <= 0){ + $this->attackTime = 20; + $this->target->harm($ai->entity->getAttackDamage(), $ai->entity->eid); + } + --$this->attackTime; + + } + public function canBeExecuted(EntityAI $ai) + { + $target = $this->findTarget($ai->entity, 10); + if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ + $this->target = $target; + return true; + } + + return false; + } +} + diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index 2f989359..3d766ccb 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -23,7 +23,6 @@ class TaskTempt extends TaskBase } $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z); - $ai->mobController->lookOn($this->target); } diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 796146d9..a89f687b 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -14,6 +14,9 @@ class Creeper extends Monster{ $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; } + public function getAttackDamage(){ + return 0; //TODO special attack + } public function setIgnited($v = null){ $this->setState($v === null ? !$this->getState() : $v); diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index f0851f76..d409d089 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -9,7 +9,9 @@ class PigZombie extends Monster{ $this->setSpeed(0.25); $this->update(); } - + public function getAttackDamage(){ + return 5; + } public function getDrops(){ return [ [COOKED_PORKCHOP, 0, mt_rand(0,2)], diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 5765f34a..6ed4a269 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -10,6 +10,10 @@ class Skeleton extends Monster{ $this->update(); } + public function getAttackDamage(){ + return 0; //TODO special attack + } + public function updateBurning(){ if($this->fire > 0 or !$this->level->isDay()){ return false; diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 0bed4521..a91f7b96 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -30,6 +30,10 @@ class Zombie extends Monster{ } } + public function getAttackDamage(){ + return 4; //TODO vanillafy(zombies might be able to hold items) + } + public function update(){ $this->updateBurning(); parent::update();