From da28b3b0ff5a8a0b3b091f7c5362385b560d8493 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 12 Apr 2024 06:06:59 +0300 Subject: [PATCH] changes to skeleton ai, theoretical performance increase with huge amount of mobs and players --- src/entity/Animal.php | 30 +++++++++++++++++--- src/entity/Creature.php | 25 ++++++++++++---- src/entity/Entity.php | 10 ++++++- src/entity/Living.php | 5 ++++ src/entity/ai/tasks/TaskAttackPlayer.php | 19 ++----------- src/entity/ai/tasks/TaskLookAtPlayer.php | 29 ++----------------- src/entity/ai/tasks/TaskRangedAttack.php | 36 ++++++++---------------- src/entity/ai/tasks/TaskTempt.php | 19 ++----------- src/world/Level.php | 12 ++++++++ 9 files changed, 89 insertions(+), 96 deletions(-) diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 89fa2e44..a1979353 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -5,19 +5,41 @@ 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 $closestPlayerThatCanFeedEID = false; + public $closestPlayerThatCanFeedDist = INF; + 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); if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){ $this->setAge(-24000); } + $this->searchForClosestPlayers = true; + } + + public function handlePrePlayerSearcher(){ + parent::handlePrePlayerSearcher(); - //$this->ai->addTask(new TaskTempt()); - //$this->ai->addTask(new TaskPanic()); - //$this->ai->addTask(new TaskMate()); + if($this->closestPlayerThatCanFeedEID !== false && !isset($this->level->entityList[$this->closestPlayerThatCanFeedEID])){ + $this->closestPlayerThatCanFeedEID = false; + $this->closestPlayerThatCanFeedDist = INF; + } + + } + + public function handlePlayerSearcher(Player $player, $dist){ + parent::handlePlayerSearcher($player, $dist); + + if($this->closestPlayerThatCanFeedDist >= $dist){ + if($player->spawned && $this->isFood($player->getHeldItem()->id)){ + $this->closestPlayerThatCanFeedDist = $dist; + $this->closestPlayerThatCanFeedEID = $player->entity->eid; + } + } } public function harm($dmg, $cause = "generic", $force = false){ diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 5aacff56..656a8589 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -4,17 +4,32 @@ abstract class Creature extends Living{ const CLASS_TYPE = ENTITY_MOB; public $inPanic; + public $closestPlayerEID = false; + public $closestPlayerDist = INF; + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->inPanic = false; //force for now parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 1, "generic"); $this->enableAutojump = true; - //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); - //$this->ai->addTask(new TaskLookAround()); - //$this->ai->addTask(new TaskRandomWalk()); - //$this->ai->addTask(new TaskLookAtPlayer()); - //$this->ai->addTask(new TaskSwimming()); + $this->searchForClosestPlayers = true; + } + + public function handlePrePlayerSearcher(){ + parent::handlePrePlayerSearcher(); + if($this->closestPlayerEID !== false && !isset($this->level->entityList[$this->closestPlayerEID])){ + $this->closestPlayerEID = false; + $this->closestPlayerDist = INF; + } + } + + public function handlePlayerSearcher(Player $player, $dist){ + parent::handlePlayerSearcher($player, $dist); + if($this->closestPlayerDist >= $dist){ + $this->closestPlayerEID = $player->entity->eid; + $this->closestPlayerDist = $dist; + } } public function createSaveData(){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index adf8b069..44dc2311 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -7,6 +7,8 @@ class Entity extends Position const CLASS_TYPE = - 1; const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player + public $searchForClosestPlayers = false; + public $counter = 0; public $fallDistance = 0; public static $updateOnTick, $allowedAI; @@ -186,7 +188,9 @@ class Entity extends Position $this->outOfWorld(); } } - + public function handlePrePlayerSearcher(){ + + } public function isType(){ return in_array($this->type, func_get_args()); } @@ -715,6 +719,10 @@ class Entity extends Position return $this->y < -1.6; } + public function handlePlayerSearcher(Player $player, $dist){ + + } + public function update($now){ if($this->closed === true){ return false; diff --git a/src/entity/Living.php b/src/entity/Living.php index c912c351..fafcc09d 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -27,6 +27,7 @@ abstract class Living extends Entity implements Pathfindable{ $this->canBeAttacked = true; $this->hasGravity = true; $this->hasKnockback = true; + //if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 } @@ -41,6 +42,10 @@ abstract class Living extends Entity implements Pathfindable{ } } + public function handlePlayerSearcher(Player $player, $dist){ + + } + public function getAIMoveSpeed(){ return $this->aiMoveSpeed; } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index 28b8cd39..4b757431 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -69,23 +69,8 @@ class TaskAttackPlayer extends TaskBase return true; } } - $bestTargetDistance = INF; - $closestTarget = null; - foreach($e->level->players as $p){ - if($p->spawned){ - $pt = $p->entity; - $xDiff = $pt->x - $e->x; - $yDiff = $pt->y - $e->y; - $zDiff = $pt->z - $e->z; - $d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); - if($d <= $this->rangeSquared){ - if($bestTargetDistance >= $d){ - $closestTarget = $pt; - $bestTargetDistance = $d; - } - } - } - } + + $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null; if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 522bcc8b..65a9f1e3 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -11,33 +11,8 @@ class TaskLookAtPlayer extends TaskBase{ 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){ - $svd = null; - $svdDist = -1; - foreach($e->level->players as $p){ - $p = $p->entity; - if(Utils::distance_noroot($e, $p) > $r*$r){ - continue; - } - if($svdDist === -1){ - $svdDist = Utils::distance_noroot($e, $p); - $svd = $p; - continue; - } - if($svd != null && $svdDist === 0){ - $svd = $p; - } - if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){ - $svdDist = $cd; - $svd = $p; - } - } - - if($svd == null){ - return null; - } - - return $svd; + protected function findTarget(Creature $e, $r){ + return $e->closestPlayerDist > $r*$r ? null : $e->level->entityList[$e->closestPlayerEID]; } public function onStart(EntityAI $ai){ diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php index 3e9a1cec..c0e93386 100644 --- a/src/entity/ai/tasks/TaskRangedAttack.php +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -4,7 +4,7 @@ class TaskRangedAttack extends \TaskBase { public $server; public $attackCounter = 0; - public $ticksNoSeen = 0; + public $seenTicks = 0; public function __construct($speed, $range){ $this->speedMultiplier = $speed; $this->rangeSquared = $range*$range; @@ -31,15 +31,16 @@ class TaskRangedAttack extends \TaskBase return false; } --$this->attackCounter; - //if(!$ai->canSee($ai->entity->target)){ useless without pathfinder =< - // ++$this->ticksNoSeen; - //}else{ - // $this->ticksNoSeen = 0; - //} + if($ai->canSee($ai->entity->target)){ + ++$this->seenTicks; + }else{ + $this->seenTicks = 0; + } + $dist = $ai->entity->distanceSquared($ai->entity->target); - if($dist > 100){ + if($dist > 100 || $this->seenTicks < 20){ $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier); }else{ $ai->mobController->headYawIsYaw = true; @@ -47,7 +48,7 @@ class TaskRangedAttack extends \TaskBase $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); - if($this->attackCounter <= 0){ + if($this->attackCounter <= 0 && $this->seenTicks > 0){ $this->rangedAttack($ai->entity, $ai->entity->target); $this->attackCounter = 60; } @@ -112,23 +113,8 @@ class TaskRangedAttack extends \TaskBase return true; } } - $bestTargetDistance = INF; - $closestTarget = null; - foreach($e->level->players as $p){ - if($p->spawned){ - $pt = $p->entity; - $xDiff = $pt->x - $e->x; - $yDiff = $pt->y - $e->y; - $zDiff = $pt->z - $e->z; - $d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); - if($d <= $this->rangeSquared){ - if($bestTargetDistance >= $d){ - $closestTarget = $pt; - $bestTargetDistance = $d; - } - } - } - } + + $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null; if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index c8f84c7d..eee2f39a 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -68,23 +68,8 @@ class TaskTempt extends TaskBase return true; } } - $bestTargetDistance = INF; - $closestTarget = null; - foreach($e->level->players as $p){ - if($p->spawned && $e->isFood($p->getHeldItem()->id)){ - $pt = $p->entity; - $xDiff = $pt->x - $e->x; - $yDiff = $pt->y - $e->y; - $zDiff = $pt->z - $e->z; - $d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); - if($d <= $this->targetDistance){ - if($bestTargetDistance >= $d){ - $closestTarget = $pt; - $bestTargetDistance = $d; - } - } - } - } + + $closestTarget = $e->closestPlayerThatCanFeedDist <= $this->targetDistance ? $e->level->entityList[$e->closestPlayerThatCanFeedEID] : 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 3087e603..e9a4d1cd 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -553,6 +553,18 @@ class Level{ unset($this->entityListPositioned[$index][$e->eid]); $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks } + + if($e->searchForClosestPlayers){ + $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); + $e->handlePlayerSearcher($player, $dist); + } + } + + + }elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); }