changes to skeleton ai, theoretical performance increase with huge amount of mobs and players

This commit is contained in:
GameHerobrine 2024-04-12 06:06:59 +03:00
parent c9da91378d
commit da28b3b0ff
9 changed files with 89 additions and 96 deletions

View file

@ -5,19 +5,41 @@ abstract class Animal extends Creature implements Ageable, Breedable{
public $parent; public $parent;
public $inLove; //do NOT add it into metadata, it doesnt send it to player public $inLove; //do NOT add it into metadata, it doesnt send it to player
public $age; public $age;
public $loveTimeout; public $loveTimeout;
public $closestPlayerThatCanFeedEID = false;
public $closestPlayerThatCanFeedDist = INF;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->setAge(isset($data["Age"]) ? $data["Age"] : 0); $this->setAge(isset($data["Age"]) ? $data["Age"] : 0);
if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){ if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){
$this->setAge(-24000); $this->setAge(-24000);
} }
$this->searchForClosestPlayers = true;
}
//$this->ai->addTask(new TaskTempt()); public function handlePrePlayerSearcher(){
//$this->ai->addTask(new TaskPanic()); parent::handlePrePlayerSearcher();
//$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){ public function harm($dmg, $cause = "generic", $force = false){

View file

@ -4,17 +4,32 @@ abstract class Creature extends Living{
const CLASS_TYPE = ENTITY_MOB; const CLASS_TYPE = ENTITY_MOB;
public $inPanic; public $inPanic;
public $closestPlayerEID = false;
public $closestPlayerDist = INF;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->inPanic = false; //force for now $this->inPanic = false; //force for now
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 1, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 1, "generic");
$this->enableAutojump = true; $this->enableAutojump = true;
//$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); $this->searchForClosestPlayers = true;
//$this->ai->addTask(new TaskLookAround()); }
//$this->ai->addTask(new TaskRandomWalk());
//$this->ai->addTask(new TaskLookAtPlayer()); public function handlePrePlayerSearcher(){
//$this->ai->addTask(new TaskSwimming()); 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(){ public function createSaveData(){

View file

@ -7,6 +7,8 @@ class Entity extends Position
const CLASS_TYPE = - 1; const CLASS_TYPE = - 1;
const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player
public $searchForClosestPlayers = false;
public $counter = 0; public $counter = 0;
public $fallDistance = 0; public $fallDistance = 0;
public static $updateOnTick, $allowedAI; public static $updateOnTick, $allowedAI;
@ -186,7 +188,9 @@ class Entity extends Position
$this->outOfWorld(); $this->outOfWorld();
} }
} }
public function handlePrePlayerSearcher(){
}
public function isType(){ public function isType(){
return in_array($this->type, func_get_args()); return in_array($this->type, func_get_args());
} }
@ -715,6 +719,10 @@ class Entity extends Position
return $this->y < -1.6; return $this->y < -1.6;
} }
public function handlePlayerSearcher(Player $player, $dist){
}
public function update($now){ public function update($now){
if($this->closed === true){ if($this->closed === true){
return false; return false;

View file

@ -27,6 +27,7 @@ abstract class Living extends Entity implements Pathfindable{
$this->canBeAttacked = true; $this->canBeAttacked = true;
$this->hasGravity = true; $this->hasGravity = true;
$this->hasKnockback = true; $this->hasKnockback = true;
//if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 //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(){ public function getAIMoveSpeed(){
return $this->aiMoveSpeed; return $this->aiMoveSpeed;
} }

View file

@ -69,23 +69,8 @@ class TaskAttackPlayer extends TaskBase
return true; return true;
} }
} }
$bestTargetDistance = INF;
$closestTarget = null; $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : 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;
}
}
}
}
if($closestTarget != null){ if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ? $e->target = $closestTarget; //TODO dont save entity object ?

View file

@ -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(); 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){ protected function findTarget(Creature $e, $r){
$svd = null; return $e->closestPlayerDist > $r*$r ? null : $e->level->entityList[$e->closestPlayerEID];
$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;
} }
public function onStart(EntityAI $ai){ public function onStart(EntityAI $ai){

View file

@ -4,7 +4,7 @@ class TaskRangedAttack extends \TaskBase
{ {
public $server; public $server;
public $attackCounter = 0; public $attackCounter = 0;
public $ticksNoSeen = 0; public $seenTicks = 0;
public function __construct($speed, $range){ public function __construct($speed, $range){
$this->speedMultiplier = $speed; $this->speedMultiplier = $speed;
$this->rangeSquared = $range*$range; $this->rangeSquared = $range*$range;
@ -31,15 +31,16 @@ class TaskRangedAttack extends \TaskBase
return false; return false;
} }
--$this->attackCounter; --$this->attackCounter;
//if(!$ai->canSee($ai->entity->target)){ useless without pathfinder =< if($ai->canSee($ai->entity->target)){
// ++$this->ticksNoSeen; ++$this->seenTicks;
//}else{ }else{
// $this->ticksNoSeen = 0; $this->seenTicks = 0;
//} }
$dist = $ai->entity->distanceSquared($ai->entity->target); $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); $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier);
}else{ }else{
$ai->mobController->headYawIsYaw = true; $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()); $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->rangedAttack($ai->entity, $ai->entity->target);
$this->attackCounter = 60; $this->attackCounter = 60;
} }
@ -112,23 +113,8 @@ class TaskRangedAttack extends \TaskBase
return true; return true;
} }
} }
$bestTargetDistance = INF;
$closestTarget = null; $closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : 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;
}
}
}
}
if($closestTarget != null){ if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ? $e->target = $closestTarget; //TODO dont save entity object ?

View file

@ -68,23 +68,8 @@ class TaskTempt extends TaskBase
return true; return true;
} }
} }
$bestTargetDistance = INF;
$closestTarget = null; $closestTarget = $e->closestPlayerThatCanFeedDist <= $this->targetDistance ? $e->level->entityList[$e->closestPlayerThatCanFeedEID] : 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;
}
}
}
}
if($closestTarget != null){ if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ? $e->target = $closestTarget; //TODO dont save entity object ?

View file

@ -553,6 +553,18 @@ class Level{
unset($this->entityListPositioned[$index][$e->eid]); unset($this->entityListPositioned[$index][$e->eid]);
$this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks $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"])){ }elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){
unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]);
} }