code style fix

This commit is contained in:
Sunch 2025-04-27 20:36:24 +08:00
parent fc5c5625cb
commit 5e8e9330e2
361 changed files with 4766 additions and 4650 deletions

View file

@ -10,29 +10,27 @@ class EntityAI
* @var Living
*/
public $entity;
/**
* @var TaskBase[]
*/
protected $tasks;
public $lastTask;
public function __construct($entity){
$this->entity = $entity;
$this->tasks = [];
$this->mobController = new MobController($entity);
}
public function canSee(Entity $entity){
//TODO caches
return $this->entity->canSee($entity);
}
/**
* Add a task for entity
* @param TaskBase $task
*/
public function addTask(TaskBase $task){
$this->tasks[$task->__toString()] = $task;
@ -44,16 +42,16 @@ class EntityAI
}
return false;
}
/**
*
*
* @param mixed $id classname
* @return TaskBase | false
*/
public function getTask($id){
return $this->tasks[$id] ?? false;
}
public function isStarted($id){
$task = $this->getTask($id);
return $task instanceof TaskBase && $task->isStarted;
@ -75,10 +73,10 @@ class EntityAI
}
}
}
public function __destruct(){
unset($this->entity);
}
}

View file

@ -14,24 +14,24 @@ class MobController
* @var Living
*/
public $entity;
public $finalYaw, $finalPitch, $finalHeadYaw;
public $headYaw;
public $someTicker = 0;
protected $jumping;
public $moveToX, $moveToY, $moveToZ;
public $speedMultiplier;
public $updateMove = false;
public $lookX, $lookY, $lookZ;
public $deltaLookYaw, $deltaLookPitch;
public $isLooking = false;
public $headYawIsYaw = false;
public function __construct($e){
$this->entity = $e;
}
@ -41,11 +41,11 @@ class MobController
public function isJumping(){
return $this->jumping;
}
public function setJumping($b){
$this->jumping = $b;
}
public function isRotationCompleted(){
return $this->finalHeadYaw === $this->entity->headYaw;
}
@ -56,7 +56,7 @@ class MobController
$this->speedMultiplier = $speed;
$this->updateMove = true;
}
public function setMovingOffset($x, $y, $z, $speed){
$this->moveToX = $this->entity->x + ($x);
$this->moveToY = $this->entity->y + ($y);
@ -64,50 +64,50 @@ class MobController
$this->speedMultiplier = $speed;
$this->updateMove = true;
}
public function canJump(){
return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround;
}
public static function limitAngle2($old, $newA, $limit){
$v4 = Utils::wrapAngleTo180($old - $newA);
if($v4 < -$limit) $v4 = -$limit;
if($v4 >= $limit) $v4 = $limit;
return $old - $v4;
}
public static function limitAngle($old, $newA, $limit){
$v4 = Utils::wrapAngleTo180($newA - $old);
if($v4 > $limit) $v4 = $limit;
if($v4 < -$limit) $v4 = -$limit;
return $old + $v4;
}
public function jumpTick(){
$this->entity->jumping = $this->jumping;
$this->jumping = false;
}
public function movementTick(){
$this->entity->moveForward = 0;
if($this->updateMove){
$this->updateMove = false;
$v1 = floor($this->entity->boundingBox->minY + 0.5);
$diffX = $this->moveToX - $this->entity->x;
$diffZ = $this->moveToZ - $this->entity->z;
$diffY = $this->moveToY - $v1;
$v8 = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ;
$v8 = $diffX * $diffX + $diffY * $diffY + $diffZ * $diffZ;
if($v8 >= 0.00000025){
$v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90;
$this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30);
$this->entity->setAIMoveSpeed($this->speedMultiplier * $this->entity->getSpeed() * $this->entity->getSpeedModifer());
if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true);
if($diffY > 0 && $diffX * $diffX + $diffZ * $diffZ < 1) $this->setJumping(true);
}
}
//TODO handle jumps somewhere
@ -117,11 +117,11 @@ class MobController
//}
//if($this->jumpTimeout > 0) --$this->jumpTimeout;
}
public function updateHeadYaw(){
$diffX = $this->entity->x - $this->entity->lastX;
$diffZ = $this->entity->z - $this->entity->lastZ;
if(($diffX*$diffX + $diffZ*$diffZ) > 0.00000025){
if(($diffX * $diffX + $diffZ * $diffZ) > 0.00000025){
$this->entity->renderYawOffset = $this->entity->yaw;
$this->entity->headYaw = self::limitAngle2($this->entity->renderYawOffset, $this->entity->headYaw, 75);
$this->headYaw = $this->entity->headYaw;
@ -133,7 +133,7 @@ class MobController
$this->headYaw = $this->entity->headYaw;
}else{
++$this->someTicker;
if($this->someTicker > 10){
$v5 = max(1 - ($this->someTicker - 10) / 10, 0) * 75;
}
@ -141,53 +141,53 @@ class MobController
$this->entity->renderYawOffset = self::limitAngle2($this->entity->headYaw, $this->entity->renderYawOffset, $v5);
}
}
public function rotateTick(){ //TODO handle more rotation
$this->entity->pitch = 0;
if($this->isLooking){
$this->isLooking = false;
$diffX = $this->lookX - $this->entity->x;
$diffY = $this->lookY - ($this->entity->y + $this->entity->getEyeHeight());
$diffZ = $this->lookZ - $this->entity->z;
$distance = sqrt($diffX*$diffX + $diffZ*$diffZ);
$v9 = (atan2($diffZ, $diffX)*180 / M_PI) - 90;
$v10 = -(atan2($diffY, $distance)*180 / M_PI);
$distance = sqrt($diffX * $diffX + $diffZ * $diffZ);
$v9 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90;
$v10 = -(atan2($diffY, $distance) * 180 / M_PI);
$this->entity->pitch = self::limitAngle($this->entity->pitch, $v10, $this->deltaLookPitch);
$this->entity->headYaw = self::limitAngle($this->entity->headYaw, $v9, $this->deltaLookYaw);
}else{
$this->entity->headYaw = self::limitAngle($this->entity->headYaw, $this->entity->renderYawOffset, 10);
}
if($this->headYawIsYaw) $this->entity->yaw = $this->entity->headYaw;
$this->headYawIsYaw = false;
/* Some stuff for pathfinder - nc doesnt have it now
* float var11 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset);
if (!this.entity.getNavigator().noPath())
{
if (var11 < -75.0F)
{
this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F;
}
if (!this.entity.getNavigator().noPath())
{
if (var11 < -75.0F)
{
this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F;
}
if (var11 > 75.0F)
{
this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F;
}
}
if (var11 > 75.0F)
{
this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F;
}
}
*/
//$this->entity->lastHeadYaw = $this->entity->headYaw;
//$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw);
//$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw);
//$w180min = min(abs($w180), 20)*Utils::getSign($w180);
//$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min);
}
public function setLookPosition($posX, $posY, $posZ, $lookYaw, $lookPitch){
$this->lookX = $posX;
$this->lookY = $posY;
@ -196,9 +196,9 @@ class MobController
$this->deltaLookPitch = $lookPitch;
$this->isLooking = true;
}
public function faceEntity($x, $y, $z){
$len = sqrt($x*$x + $z*$z + $y*$y);
$len = sqrt($x * $x + $z * $z + $y * $y);
//$d = $len == 0 ?//$v->subtract($this->entity)->normalize();
if($len == 0){
$dx = 0;
@ -207,38 +207,37 @@ class MobController
$dx = $x / $len;
$dz = $z / $len;
}
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$thetaOffset = $dz < 0 ? 90 : 270;
$calcYaw = ($thetaOffset + $tan);
$this->finalHeadYaw = $this->entity->yaw = Utils::wrapAngleTo360($calcYaw);
}
public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? ($x < 0 ? 180 : 0) : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $this->finalHeadYaw = Utils::wrapAngleTo360($calcYaw);
if($pitch){
$diff = sqrt($x * $x + $z * $z);
$calcPitch = $diff == 0 ? ($y < 0 ? -90 : 90) : rad2deg(atan($y / $diff));
$this->entity->pitch = $calcPitch;
}
//$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
return true;
}
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
if($x instanceof Vector3){
return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch);
}
return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch);
}
public function __destruct(){
unset($this->entity);
}

View file

@ -5,16 +5,16 @@ class PathFollower{
* @var Living
*/
public $entity;
public function __construct(Living $entity){
$this->entity = $entity;
}
public function followPath(){
if(!isset($this->entity) || !($this->entity instanceof Living)) return;
if(!$this->entity->hasPath()) return;
if($this->entity->currentNode == null){
$this->entity->currentNode = $this->entity->path[$this->entity->currentIndex];
}
@ -27,14 +27,13 @@ class PathFollower{
//console("next");
$this->entity->currentNode = null;
}
if($this->entity->currentIndex >= count($this->entity->path)){
console("path finished.");
$this->entity->currentNode = null;
$this->entity->path = null;
$this->entity->currentIndex = 0;
/*foreach($this->entity->pathEIDS as $eid){
$pk = new RemoveEntityPacket();
$pk->eid = $eid;
@ -42,9 +41,9 @@ class PathFollower{
$player->dataPacket($pk);
}
}*/
}
}
}

View file

@ -4,20 +4,19 @@ class TaskAttackPlayer extends TaskBase
{
public $speedMultiplier;
public $rangeSquared;
public $attackCounter;
public function __construct($speed, $range){
$this->speedMultiplier = $speed;
$this->rangeSquared = $range*$range;
$this->rangeSquared = $range * $range;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onUpdate(EntityAI $ai)
{
if(!$this->isTargetValid($ai)){
@ -27,7 +26,7 @@ class TaskAttackPlayer extends TaskBase
}
$ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier);
$ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed());
--$this->attackCounter;
$v1 = $ai->entity->width * $ai->entity->width * 4;
if($ai->entity instanceof Creeper){
@ -38,16 +37,15 @@ class TaskAttackPlayer extends TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
$dist = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff);
$dist = ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff);
if($dist <= $v1){
if($this->attackCounter <= 0){
$this->attackCounter = $e->attackEntity($t, sqrt($dist)) ? 20 : 0;
}
}
}
public function isTargetValid(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){
@ -55,11 +53,11 @@ class TaskAttackPlayer extends TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared;
return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared;
}
return false;
}
public function tryTargeting(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity){
@ -67,25 +65,25 @@ class TaskAttackPlayer extends TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){
if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared){
return true;
}
}
$closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null;
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return $this->tryTargeting($ai);
}
public function onEnd(EntityAI $ai)
{
$ai->entity->target = false;

View file

@ -3,45 +3,41 @@
abstract class TaskBase
{
public $isStarted = false, $selfCounter = 0;
public function __construct(){}
/**
* Executed when entity starts task. It is recommended to set counter here.
* @param EntityAI $ai
*/
abstract function onStart(EntityAI $ai);
/**
* Every tick until counter will be not equal to 0. It is recommended to update value of counter here.
* @param EntityAI $ai
*/
abstract function onUpdate(EntityAI $ai);
/**
* On task end.
* @param EntityAI $ai
*/
abstract function onEnd(EntityAI $ai);
/**
* Can start the task
* @param EntityAI $ai
*/
abstract function canBeExecuted(EntityAI $ai);
/**
* @return number ticks
*/
function getIdleTimeAfterEnd(){
return 0;
}
function reset(){
$this->isStarted = false;
$this->selfCounter = 0;
}
final function __toString(){
return get_class($this);
}

View file

@ -10,7 +10,7 @@ class TaskEatTileGoal extends TaskBase
public function onEnd(EntityAI $ai)
{
}
public function onUpdate(EntityAI $ai)
@ -18,7 +18,7 @@ class TaskEatTileGoal extends TaskBase
if($ai->isStarted("TaskPanic")){
$this->reset();
return false;
}
}
if (--$this->selfCounter == 4)
{
@ -27,7 +27,7 @@ class TaskEatTileGoal extends TaskBase
if($id === TALL_GRASS){
$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y, $ai->entity->z, AIR, 0);
$ai->entity->eatGrass();
}elseif($idb === GRASS){
$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y - 1, $ai->entity->z, DIRT, 0);
$ai->entity->eatGrass();

View file

@ -5,7 +5,7 @@ class TaskFollowParent extends TaskBase
public function __construct($speed){
$this->speedMultiplier = $speed;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
@ -23,19 +23,19 @@ class TaskFollowParent extends TaskBase
$ai->entity->parent = null;
return false;
}
$dist = $ai->entity->distanceSquared($ai->entity->parent);
if($dist > 256 || $dist < 9){ //3-16 blocks
$this->reset();
return false;
}
$parent = $ai->entity->parent;
$ai->mobController->setLookPosition($parent->x, $parent->y + $parent->getEyeHeight(), $parent->z, 10, $ai->entity->getVerticalFaceSpeed());
$ai->mobController->setMovingTarget($parent->x, $parent->y, $parent->z, $this->speedMultiplier);
}
public function canBeExecuted(EntityAI $ai)
{
return $ai->entity->isBaby() && ($ai->entity->parent instanceof Entity) && !$ai->entity->parent->dead; //TODO find a new parent if the current one is dead

View file

@ -3,7 +3,7 @@
class TaskLookAround extends TaskBase
{
public $lookOffsetX, $lookOffsetZ;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 20 + lcg_value();
@ -19,9 +19,9 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)
{
--$this->selfCounter;

View file

@ -6,13 +6,13 @@ class TaskLookAtPlayer extends TaskBase{
public function __construct($distance){
$this->distance = $distance;
}
public function canBeExecuted(EntityAI $ai){
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(Creature $e, $r){
return $e->closestPlayerDist > $r*$r ? null : $e->level->entityList[$e->closestPlayerEID];
return $e->closestPlayerDist > $r * $r ? null : $e->level->entityList[$e->closestPlayerEID];
}
public function onStart(EntityAI $ai){
@ -22,7 +22,7 @@ class TaskLookAtPlayer extends TaskBase{
$this->onEnd($ai);
return;
}
$this->selfCounter = mt_rand(20, 60);
}
@ -35,7 +35,7 @@ class TaskLookAtPlayer extends TaskBase{
$ai->mobController->setLookPosition($this->target->x, $this->target->y + $this->target->getEyeHeight(), $this->target->z, 10, $ai->entity->getVerticalFaceSpeed());
$this->selfCounter--;
}
public function onEnd(EntityAI $ai){
unset($this->target);
$ai->entity->pitch = 0;

View file

@ -5,9 +5,9 @@ class TaskMate extends TaskBase
public function __construct($speed){
$this->speedMultiplier = $speed;
}
public $targetMateID;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 60;
@ -27,17 +27,16 @@ class TaskMate extends TaskBase
$this->onEnd($ai);
return;
}
$ai->mobController->setLookPosition($movTo->x, $movTo->y + $movTo->getEyeHeight(), $movTo->z, 10, $ai->entity->getVerticalFaceSpeed());
$ai->mobController->setMovingTarget($movTo->x, $movTo->y, $movTo->z, $this->speedMultiplier);
--$this->selfCounter;
if($this->selfCounter <= 0 && $ai->entity->distanceSquared($movTo) < 9){
$this->createBaby($ai, $movTo);
}
}
public function createBaby(EntityAI $ai, Entity $entpar2){
/**
* @var Animal $baby
@ -45,20 +44,20 @@ class TaskMate extends TaskBase
$baby = $ai->entity->breed();
//this.theAnimal.setGrowingAge(6000); TODO growing age?
//this.targetMate.setGrowingAge(6000);
$ai->entity->loveTimeout = 6000;
$entpar2->loveTimeout = 6000;
$ai->entity->resetInLove();
$entpar2->resetInLove();
$baby->setAge(-24000);
}
public function findMate(EntityAI $ai){
$distance = 8.0*8.0;
$distance = 8.0 * 8.0;
$minDist = $distance; //TODO check bb intersection?
$matedEID = null;
foreach($ai->entity->level->entitiesInLove as $eid => $_){
$e = $ai->entity->level->entityList[$eid] ?? false;
if($e instanceof Entity){
@ -71,7 +70,7 @@ class TaskMate extends TaskBase
}
return $matedEID;
}
public function canBeExecuted(EntityAI $ai)
{
return $ai->entity->isInLove() && ($this->targetMateID = $this->findMate($ai)) != null;

View file

@ -3,12 +3,12 @@
class TaskPanic extends TaskBase
{
protected $randX = 2, $randZ = 2;
public $speedMultiplier;
public function __construct($speedMultiplier){
$this->speedMultiplier = $speedMultiplier;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 60;
@ -20,16 +20,16 @@ class TaskPanic extends TaskBase
$ai->entity->inPanic = false;
$this->reset();
}
public function regenerateRandXZ(){
$this->randX = (mt_rand(0, 1) ? -1 : 1);
$this->randZ = (mt_rand(0, 1) ? -1 : 1);
}
public function reset(){
$this->randX = $this->randZ = 2;
}
public function onUpdate(EntityAI $ai)
{
--$this->selfCounter;
@ -42,6 +42,5 @@ class TaskPanic extends TaskBase
return $ai->entity instanceof Animal && $ai->entity->inPanic && $ai->entity->knockbackTime <= 0;
}
}
}

View file

@ -7,7 +7,7 @@ class TaskRandomWalk extends TaskBase
public function __construct($speedMultiplier){
$this->speedMultiplier = $speedMultiplier;
}
public function onStart(EntityAI $ai)
{
$this->x = mt_rand(-1, 1);
@ -30,7 +30,7 @@ class TaskRandomWalk extends TaskBase
$this->reset();
return false; //TODO Better way: block movement
}
--$this->selfCounter;
$ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->speedMultiplier);
}
@ -40,8 +40,7 @@ class TaskRandomWalk extends TaskBase
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskAttackPlayer") || $ai->isStarted("TaskRangedAttack")) {
return false;
} // i really need mutexBits
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}
}
}

View file

@ -7,10 +7,10 @@ class TaskRangedAttack extends \TaskBase
public $seenTicks = 0;
public function __construct($speed, $range){
$this->speedMultiplier = $speed;
$this->rangeSquared = $range*$range;
$this->rangeSquared = $range * $range;
$this->server = ServerAPI::request();
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
@ -20,7 +20,7 @@ class TaskRangedAttack extends \TaskBase
public function onEnd(EntityAI $ai)
{
$ai->entity->target = false;
}
public function onUpdate(EntityAI $ai)
@ -36,24 +36,23 @@ class TaskRangedAttack extends \TaskBase
}else{
$this->seenTicks = 0;
}
$dist = $ai->entity->distanceSquared($ai->entity->target);
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;
}
$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 && $this->seenTicks > 0){
$this->rangedAttack($ai->entity, $ai->entity->target);
$this->attackCounter = 60;
}
}
public function rangedAttack($selfEntity, $target){
$d = [
"x" => $selfEntity->x,
@ -88,8 +87,7 @@ class TaskRangedAttack extends \TaskBase
$this->server->api->entity->spawnToAll($arrow);
}
}
public function isTargetValid(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){
@ -97,11 +95,11 @@ class TaskRangedAttack extends \TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared;
return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared;
}
return false;
}
public function tryTargeting(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity){
@ -109,21 +107,20 @@ class TaskRangedAttack extends \TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){
if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared){
return true;
}
}
$closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null;
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return $this->tryTargeting($ai);

View file

@ -9,7 +9,7 @@ class TaskSwimming extends TaskBase
public function onEnd(EntityAI $ai)
{
}
public function onUpdate(EntityAI $ai)
@ -18,12 +18,12 @@ class TaskSwimming extends TaskBase
$this->reset();
return;
}
if(lcg_value() < 0.8){ #1.5.2 method
$ai->mobController->setJumping(true);
}
}
public function canBeExecuted(EntityAI $ai)
{
return $ai->entity->inWater || $ai->entity->inLava;

View file

@ -4,19 +4,19 @@ class TaskTempt extends TaskBase
{
public function __construct($speed){
$this->speedMultiplier = $speed;
$this->targetDistance = 10*10;
$this->targetDistance = 10 * 10;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onEnd(EntityAI $ai)
{
$ai->entity->target = false;
}
public function onUpdate(EntityAI $ai)
{
if(!$this->isTargetValid($ai) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){
@ -24,22 +24,21 @@ class TaskTempt extends TaskBase
$this->onEnd($ai);
return false;
}
$target = $ai->entity->target;
$xdiff = ($target->x - $ai->entity->x);
$ydiff = ($target->y - $ai->entity->y);
$zdiff = ($target->z - $ai->entity->z);
$dist = $xdiff*$xdiff + $ydiff*$ydiff + $zdiff*$zdiff;
$dist = $xdiff * $xdiff + $ydiff * $ydiff + $zdiff * $zdiff;
$ai->mobController->setLookPosition($target->x, $target->y + $target->getEyeHeight(), $target->z, 30, $ai->entity->getVerticalFaceSpeed());
if($dist >= 6.25){
$ai->mobController->setMovingTarget($target->x, $target->y, $target->z, $this->speedMultiplier);
}else{
$ai->mobController->headYawIsYaw = true;
}
}
public function isTargetValid(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity && !$e->target->closed && $e->isFood($e->target->player->getHeldItem()->id)){
@ -47,16 +46,16 @@ class TaskTempt extends TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->targetDistance;
return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->targetDistance;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && $this->tryTargeting($ai);
}
public function tryTargeting(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity){
@ -64,13 +63,13 @@ class TaskTempt extends TaskBase
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->targetDistance){
if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->targetDistance){
return true;
}
}
$closestTarget = $e->closestPlayerThatCanFeedDist <= $this->targetDistance ? $e->level->entityList[$e->closestPlayerThatCanFeedEID] : null;
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;