From d7561ed1ba6d520cf591e9e57e66cdfef591270e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 18 Feb 2023 19:27:47 +0300 Subject: [PATCH] fixed bug with flying detection, added panic task --- src/entity/Animal.php | 7 ++++ src/entity/Creature.php | 2 +- src/entity/Entity.php | 6 ++-- src/entity/ai/EntityAI.php | 1 - src/entity/ai/MobController.php | 4 +-- src/entity/ai/tasks/TaskLookAround.php | 2 +- src/entity/ai/tasks/TaskLookAtPlayer.php | 20 ++++++++---- src/entity/ai/tasks/TaskPanic.php | 41 ++++++++++++++++++++++++ src/entity/ai/tasks/TaskRandomWalk.php | 2 +- src/entity/ai/tasks/TaskTempt.php | 2 +- 10 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 src/entity/ai/tasks/TaskPanic.php diff --git a/src/entity/Animal.php b/src/entity/Animal.php index a205b0b2..0f5b2e3c 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -16,6 +16,13 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } $this->ai->addTask(new TaskTempt()); + $this->ai->addTask(new TaskPanic()); + } + + public function harm($dmg, $cause = "generic", $force = false){ + $ret = parent::harm($dmg, $cause, $force); + $this->inPanic |= ($ret && is_numeric($cause)); + return $ret; } public function isBaby(){ diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 3fd81cc6..586e369f 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -26,7 +26,7 @@ abstract class Creature extends Living{ } public function getSpeedModifer(){ - return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7); + return $this->speedModifer * ($this->inPanic ? 1.0 : 0.7); } public function getArmorValue(){ return 2; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 1d6d3bf6..ed8ef5d4 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -133,7 +133,7 @@ class Entity extends Position $this->player = $this->data["player"]; $this->setHealth($this->health, "generic"); $this->speedModifer = 1; - $this->width = 0.6; + $this->width = 1.2; $this->height = 1.8; $this->hasKnockback = true; $this->hasGravity = true; @@ -523,7 +523,7 @@ class Entity extends Position for($x = $startX; $x <= $endX; ++ $x){ $v = new Vector3($x, $y, $z); $v1 = new Vector3($x, $yC, $z); - if($this->isSupport($v, $this->radius)){ + if($this->isSupport($v, $this->width)){ $b = $this->level->getBlock($v); if($b->isSolid === true){ $support = true; @@ -532,7 +532,7 @@ class Entity extends Position } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ $isFlying = false; } - } elseif($this->isSupport($v1, $this->radius)){ + } elseif($this->isSupport($v1, $this->width)){ $b = $this->level->getBlock($v1); if($b->isSolid === true){ $support = true; diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index 5bdea803..7edce1f0 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -32,7 +32,6 @@ class EntityAI console("[DEBUG] Adding new task...",true, true, 2); $this->tasks[$task->__toString()] = $task; } - public function removeTask($name){ if(isset($this->tasks[$name])){ unset($this->tasks[$name]); diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index bf31572c..6a7fde35 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -12,7 +12,7 @@ class MobController public function moveNonInstant($x, $y, $z){ if($x == 0 && $y == 0 && $z == 0){ - return false; //failed + return false; } $ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0)); @@ -58,7 +58,7 @@ class MobController $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; $calcYaw = ($thetaOffset + $tan); - $this->entity->yaw = $this->entity->headYaw = $calcYaw; + $this->entity->headYaw = $this->entity->yaw = $calcYaw; } public function lookOffset($x, $y, $z, $pitch = true){ diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index 188854de..2a010ddc 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") && !$ai->entity->hasPath(); /*Vanilla value*/ + return !$ai->entity->inPanic && !$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 be16d0aa..17fb49b0 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -1,32 +1,38 @@ entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); + return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); } protected function findTarget($e, $r){ $svd = null; $svdDist = -1; - foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){ + foreach($e->level->players as $p){ + $p = $p->entity; + if(Utils::distance_noroot($e, $p) > $r*$r){ + continue; + } if($svdDist === -1){ - $svdDist = Utils::manh_distance($e, $p); + $svdDist = Utils::distance_noroot($e, $p); $svd = $p; continue; } if($svd != null && $svdDist === 0){ $svd = $p; } - - if(($cd = Utils::manh_distance($e, $p)) < $svdDist){ + if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){ $svdDist = $cd; $svd = $p; } } + + if($svd == null){ + return null; + } + return $svd; } diff --git a/src/entity/ai/tasks/TaskPanic.php b/src/entity/ai/tasks/TaskPanic.php new file mode 100644 index 00000000..dcedcf8e --- /dev/null +++ b/src/entity/ai/tasks/TaskPanic.php @@ -0,0 +1,41 @@ +selfCounter = mt_rand(60, 120); + $this->regenerateRandXZ(); + } + + public function onEnd(EntityAI $ai) + { + $ai->entity->inPanic = false; + $this->reset(); + } + + public function regenerateRandXZ(){ + $this->randX = (mt_rand(0, 1) ? -1 : 1) * 2; + $this->randZ = (mt_rand(0, 1) ? -1 : 1) * 2; + } + + public function reset(){ + $this->randX = $this->randZ = 2; + } + + public function onUpdate(EntityAI $ai) + { + --$this->selfCounter; + if($this->selfCounter % 20 === 0) $this->regenerateRandXZ(); + $ai->mobController->moveNonInstant($this->randX, 0, $this->randZ); + } + + public function canBeExecuted(EntityAI $ai) + { + return $ai->entity instanceof Animal && $ai->entity->inPanic && $ai->entity->knockbackTime <= 0; + } + + +} + diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 3d613326..429dbf6a 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -34,7 +34,7 @@ class TaskRandomWalk extends TaskBase 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; + return !$ai->entity->inPanic && !$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 5c03b5a1..1e2bfc21 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)){ //TODO Work with path + if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic){ //TODO Work with path return false; } $target = $this->findTarget($ai->entity, 10);