mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 19:49:44 +00:00
changes to skeleton ai, theoretical performance increase with huge amount of mobs and players
This commit is contained in:
parent
c9da91378d
commit
da28b3b0ff
9 changed files with 89 additions and 96 deletions
|
|
@ -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){
|
||||
|
|
|
|||
|
|
@ -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(){
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ?
|
||||
|
|
|
|||
|
|
@ -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){
|
||||
|
|
|
|||
|
|
@ -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 ?
|
||||
|
|
|
|||
|
|
@ -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 ?
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue