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

@ -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;