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

@ -1,17 +1,15 @@
<?php
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($data["Age"] ?? 0);
@ -20,11 +18,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
$this->searchForClosestPlayers = true;
}
public function isPlayerValid(Player $player){
return $player->spawned && $this->isFood($player->getHeldItem()->id) && !$player->entity->dead;
}
public function handlePrePlayerSearcher(){
parent::handlePrePlayerSearcher();
if($this->closestPlayerThatCanFeedEID !== false){
@ -33,15 +31,15 @@ abstract class Animal extends Creature implements Ageable, Breedable{
$this->closestPlayerThatCanFeedEID = false;
$this->closestPlayerThatCanFeedDist = INF;
}else{
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
$dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z);
$this->closestPlayerThatCanFeedDist = $dist;
}
}
}
public function handlePlayerSearcher(Player $player, $dist){
parent::handlePlayerSearcher($player, $dist);
if($this->closestPlayerThatCanFeedDist >= $dist){
if($this->isPlayerValid($player)){
$this->closestPlayerThatCanFeedDist = $dist;
@ -49,18 +47,18 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
}
}
public function harm($dmg, $cause = "generic", $force = false){
$ret = parent::harm($dmg, $cause, $force);
$this->inPanic |= ($ret && is_numeric($cause));
$this->inLove = false;
return $ret;
}
public function isBaby(){
return $this->getAge() < 0;
}
public function breed(){
if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){
$c = $this->spawnChild();
@ -70,22 +68,22 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
return false;
}
public function resetInLove(){
$this->inLove = 0;
}
public function canMate($ent){
return $ent->eid != $this->eid && $this->type === $ent->type && $this->isInLove() && $ent->isInLove();
}
public function update($now){
parent::update($now);
if($this->loveTimeout > 0){
--$this->loveTimeout;
}
$age = $this->getAge() + 1; //100 - fast. debug, 1 - normal
if($age >= 0 && $this->isBaby()){
$this->setAge($age);
@ -99,15 +97,15 @@ abstract class Animal extends Creature implements Ageable, Breedable{
unset($this->level->entitiesInLove[$this->eid]);
}
}
public function getAge(){
return $this->age;
}
public function setAge($i){
$this->age = $i;
}
public function spawnChild()
{
return $this->server->api->entity->add($this->level, $this->class, $this->type, [
@ -118,24 +116,24 @@ abstract class Animal extends Creature implements Ageable, Breedable{
"Age" => -24000,
]);
}
public function getMetadata(){
$d = parent::getMetadata();
$d[14]["value"] = $this->isBaby();
return $d;
}
public function createSaveData(){
$data = parent::createSaveData();
$data["IsBaby"] = $this->isBaby();
$data["Age"] = $this->getAge();
return $data;
}
public function isInLove(){
return $this->inLove > 0;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
@ -149,14 +147,14 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
parent::interactWith($e, $action);
}
public function counterUpdate(){
parent::counterUpdate();
if($this->isInLove()){
--$this->inLove;
}
}
public function getDrops(){
if($this->isBaby()){
return [];

View file

@ -2,8 +2,8 @@
interface Breedable{
public function isFood($id); //original name
public function spawnChild();
public function isInLove();
}

View file

@ -2,12 +2,11 @@
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);
@ -15,13 +14,12 @@ abstract class Creature extends Living{
$this->enableAutojump = true;
$this->searchForClosestPlayers = true;
}
public function update($now){
$this->handlePrePlayerSearcher();
return parent::update($now);
}
public function handlePrePlayerSearcher(){
parent::handlePrePlayerSearcher();
if($this->closestPlayerEID !== false){
@ -30,12 +28,12 @@ abstract class Creature extends Living{
$this->closestPlayerEID = false;
$this->closestPlayerDist = INF;
}else{
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
$dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z);
$this->closestPlayerDist = $dist;
}
}
}
public function handlePlayerSearcher(Player $player, $dist){
parent::handlePlayerSearcher($player, $dist);
if($this->closestPlayerDist >= $dist){
@ -43,13 +41,13 @@ abstract class Creature extends Living{
$this->closestPlayerDist = $dist;
}
}
public function createSaveData(){
$data = parent::createSaveData();
$data["State"] = @$this->getState();
return $data;
}
public function getSpeedModifer(){
return 0.7;
}
@ -71,19 +69,19 @@ abstract class Creature extends Living{
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->getMetadata();
$pk->metadata = $this->getMetadata();
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
if($this->linkedEntity != 0 && $this->isRider){
$player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,11 @@
<?php
abstract class Living extends Entity implements Pathfindable{
public static $despawnMobs, $despawnTimer, $entityPushing = false;
public static $pathfinderTest = false;
/**
* @var Entity $target
* @var EntityAI $ai
@ -14,14 +13,14 @@ abstract class Living extends Entity implements Pathfindable{
public $target, $ai;
public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower;
public $ticksExisted = 0;
public $landMovementFactor = 0.1;
public $jumpMovementFactor = 0.02;
public $aiMoveSpeed;
public $renderYawOffset = 0.0; //used by head rotation, TODO better name
public $jumping, $jumpTicks;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->target = false;
$this->ai = new EntityAI($this);
$this->pathFinder = new TileNavigator($this);
@ -30,43 +29,43 @@ 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
}
public function isPickable(){
return !$this->dead;
}
public function fall(){
$dmg = ceil($this->fallDistance - 3);
if($dmg > 0){
$this->harm($dmg, "fall");
}
}
public function handlePlayerSearcher(Player $player, $dist){
}
public function getAIMoveSpeed(){
return $this->aiMoveSpeed;
}
public function getVerticalFaceSpeed(){
return 40; //unused in 0.8 but may be useful in 0.9/0.10
}
public function setAIMoveSpeed($speed){
$this->aiMoveSpeed = $speed;
$this->moveForward = $speed;
}
public function hasPath(){
return $this->path != null;
}
public function eatGrass(){}
public function close(){
parent::close();
unset($this->pathFollower->entity);
@ -76,22 +75,22 @@ abstract class Living extends Entity implements Pathfindable{
unset($this->parent);
unset($this->pathFinder->entity);
}
public function canBeShot(){
return true;
}
public function isPushable(){
return !$this->dead;
}
public function collideHandler(){
$bb = $this->boundingBox->expand(0.2, 0, 0.2);
$minChunkX = ((int)($bb->minX)) >> 4;
$minChunkZ = ((int)($bb->minZ)) >> 4;
$maxChunkX = ((int)($bb->minX)) >> 4;
$maxChunkZ = ((int)($bb->minZ)) >> 4;
$minChunkX = ((int) ($bb->minX)) >> 4;
$minChunkZ = ((int) ($bb->minZ)) >> 4;
$maxChunkX = ((int) ($bb->minX)) >> 4;
$maxChunkZ = ((int) ($bb->minZ)) >> 4;
//TODO also index by chunkY?
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
@ -106,14 +105,14 @@ abstract class Living extends Entity implements Pathfindable{
}
}
}
public function update($now){
if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){
$this->close();
return;
}
if($this->closed) return;
$check = $this->level->mobSpawner->checkDespawn($this);
if($check){
$this->close();
@ -121,7 +120,7 @@ abstract class Living extends Entity implements Pathfindable{
}
parent::update($now);
}
public $pathEIDS = [];
private static $lastPathEID = 10000000;
public static $pathfind = true;
@ -139,22 +138,21 @@ abstract class Living extends Entity implements Pathfindable{
}
if(self::$pathfind){
$this->path = $this->pathFinder->navigate(
$this->level,
(int)$this->x, (int)$this->y, (int)$this->z,
//(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z,
(int)$this->x + mt_rand(-16, 16), (int)$this->y, (int)$this->z + mt_rand(-16, 16),
$this->level,
(int) $this->x, (int) $this->y, (int) $this->z,
//(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z,
(int) $this->x + mt_rand(-16, 16), (int) $this->y, (int) $this->z + mt_rand(-16, 16),
16
);
}
/*if($this->path){
console("Found path of length ".count($this->path));
foreach($this->path as $node){
$eid = self::$lastPathEID++;
$this->pathEIDS[] = $eid;
$pk = new AddItemEntityPacket();
$pk->eid = $eid;
$pk->item = BlockAPI::getItem(GOLD_BLOCK, 0, 1);
@ -171,8 +169,7 @@ abstract class Living extends Entity implements Pathfindable{
}
$this->pathFollower->followPath();
}
//not exactly here
if($this->jumping){
if(!$this->inWater && !$this->inLava){
@ -186,12 +183,11 @@ abstract class Living extends Entity implements Pathfindable{
}else{
$this->jumpTicks = 0;
}
$this->ai->mobController->movementTick();
$this->ai->mobController->rotateTick();
$this->ai->mobController->jumpTick();
if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0;
if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0;
if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0;
@ -203,24 +199,23 @@ abstract class Living extends Entity implements Pathfindable{
$this->landMovementFactor = $savedLandFactor;
//Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too
$this->ai->mobController->updateHeadYaw();
if(self::$entityPushing){
$this->collideHandler();
}
}
public function jump(){
$this->speedY = 0.42;
//TODO also set AirBorne?
}
public function counterUpdate(){
parent::counterUpdate();
if($this->jumpTicks > 0) --$this->jumpTicks;
}
public function moveEntityWithHeading($strafe, $forward){
if($this->inWater){ //also check is player, and can it fly or not
$prevPosY = $this->y;
@ -230,8 +225,7 @@ abstract class Living extends Entity implements Pathfindable{
$this->speedY *= 0.8;
$this->speedZ *= 0.8;
$this->speedY -= 0.02;
if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){
$this->speedY = 0.3;
}
@ -243,69 +237,69 @@ abstract class Living extends Entity implements Pathfindable{
$this->speedY *= 0.5;
$this->speedZ *= 0.5;
$this->speedY -= 0.02;
if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){
$this->speedY = 0.3;
}
}else{
$friction = 0.91;
if($this->onGround){
$friction = 0.546;
$blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z));
if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91;
}
$var8 = 0.16277 / ($friction*$friction*$friction);
$var8 = 0.16277 / ($friction * $friction * $friction);
if($this->onGround){
$var5 = $this->getAIMoveSpeed(); //in vanilla it returns either this.seed or calls getBaseSpeed(depending on useNewAi)
$var5 *= $var8; //in vanilla it also multiplies by speedModifer
}else{
$var5 = $this->jumpMovementFactor;
}
$this->moveFlying($strafe, $forward, $var5);
//recalculating friction, might be not neccessary
$friction = 0.91;
if($this->onGround){
$friction = 0.546;
$blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z));
if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91;
}
if($this->isOnLadder()){
$speedY = $this->speedY;
$this->fallDistance = 0;
if($speedY < -0.15) $this->speedY = -0.15;
}
$this->move($this->speedX, $this->speedY, $this->speedZ);
if($this->isOnLadder() && $this->isCollidedHorizontally){
$this->speedY = 0.2;
}
$this->speedY -= 0.08; //gravity
$this->speedY *= 0.98;
$this->speedX *= $friction;
$this->speedZ *= $friction;
if($this->isCollidedHorizontally && $this->enableAutojump){
$this->ai->mobController->setJumping(true); //non vanilla
}
}
}
public function sendMoveUpdate(){
if($this->counter % 3 != 0){
return;
}
parent::sendMoveUpdate();
}
}

View file

@ -1,17 +1,18 @@
<?php
class Monster extends Creature{
public $closestPlayerToAttackEID = false;
public $closestPlayerToAttackDist = INF;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
}
public function getAttackDamage(){
return 2;
}
/**
*
*
* @see Entity::attackEntity()
*/
public function attackEntity($entity, $distance){
@ -22,11 +23,11 @@ class Monster extends Creature{
return false;
}
}
public function isPlayerValid(Player $player){
return $player->spawned && !$player->entity->dead;
}
public function handlePrePlayerSearcher(){
parent::handlePrePlayerSearcher();
if($this->closestPlayerToAttackEID !== false){
@ -35,15 +36,15 @@ class Monster extends Creature{
$this->closestPlayerToAttackEID = false;
$this->closestPlayerToAttackDist = INF;
}else{
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
$dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z);
$this->closestPlayerToAttackDist = $dist;
}
}
}
public function handlePlayerSearcher(Player $player, $dist){
parent::handlePlayerSearcher($player, $dist);
if($this->closestPlayerToAttackDist >= $dist){
if($this->isPlayerValid($player)){
$this->closestPlayerToAttackDist = $dist;
@ -51,5 +52,5 @@ class Monster extends Creature{
}
}
}
}

View file

@ -1,4 +1,5 @@
<?php
abstract class Projectile extends Entity{
const CLASS_TYPE = ENTITY_OBJECT;
public $shooterEID;
@ -7,13 +8,13 @@ abstract class Projectile extends Entity{
public $ticksInAir = 0, $ticksInGround = 0;
public $xTile = 0, $yTile = 0, $zTile = 0;
public $inTile = false;
public function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{
parent::__construct($level, $eid, $class, $type, $data);
$this->gravity = 0.03;
$this->setSize(0.25, 0.25);
$this->x -= cos($this->yaw / 180 * M_PI) * 0.16;
$this->y -= 0.1;
$this->z -= cos($this->yaw / 180 * M_PI) * 0.16;
@ -25,7 +26,7 @@ abstract class Projectile extends Entity{
$this->xTile = $data["xTile"] ?? $this->xTile;
$this->yTile = $data["yTile"] ?? $this->yTile;
$this->zTile = $data["zTile"] ?? $this->zTile;
if($shooter instanceof Entity){
$this->shooterEID = $shooter->eid;
if($shooter->isPlayer()){
@ -44,9 +45,9 @@ abstract class Projectile extends Entity{
$this->shoot(, , , $throwPower, 1.0);*/
}
}
}
public function shoot($d, $d1, $d2, $f, $f1){ //unchecked, may be not same as arrow
$f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2);
$d /= $f2;
@ -66,7 +67,7 @@ abstract class Projectile extends Entity{
$this->pitch = (atan2($d1, $f3) * 180) / M_PI;
$this->updatePosition();
}
public function update($now){
$this->lastX = $this->x;
$this->lastY = $this->y;
@ -88,32 +89,32 @@ abstract class Projectile extends Entity{
$this->fire -= 4;
if($this->fire <= 0) $this->fire = 0;
}
if($this->fire <= 0){
$this->updateMetadata();
}
}
if($this->handleLavaMovement()){
if(!$this->isImmuneToFire){
$this->harm(4, "fire");
$oldOnFire = $this->fire;
if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds
if($oldOnFire < 20 * 30) $this->fire = 20 * 30; //30 seconds
}
}
if($this->isInVoid()){
$this->outOfWorld();
}
//Entity::update end
if($this->shake > 0) --$this->shake;
if(!$this->inGround){
++$this->ticksInAir;
goto CHECK_COLLISIONS; //TODO dont use gotos
}
$blockID = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile);
if($this->inTile != $blockID){
$this->speedX += (lcg_value() * 0.2);
@ -122,7 +123,7 @@ abstract class Projectile extends Entity{
$this->inGround = false;
$this->ticksInAir = $this->ticksInGround = 0;
CHECK_COLLISIONS:
$start = new Vector3($this->x, $this->y, $this->z);
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
/**
@ -135,44 +136,44 @@ abstract class Projectile extends Entity{
}else{
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
}
$entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1));
$bestDist = 0;
$bestEnt = null;
foreach($entities as $eid => $ent){
if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->ticksInAir >= 5)){
$v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3);
$v13 = $v12->calculateIntercept($start, $end);
if($v13 != null){
$dist = $start->distance($v13->hitVector);
if($dist < $bestDist || $bestDist == 0){
$bestEnt = $ent;
}
}
}
}
if($bestEnt != null){
$v4 = MovingObjectPosition::fromEntity($bestEnt);
}
if($v4 != null){
$this->onHit($v4);
}
$this->x += $this->speedX;
$this->y += $this->speedY;
$this->z += $this->speedZ;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$v21 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ);
$this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->pitch = atan2($this->speedY, $v21) * 180 / M_PI;
$this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2;
$this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2;
$v24 = $this->inWater ? 0.8 : 0.99;
$this->speedX *= $v24;
$this->speedY *= $v24;
@ -183,19 +184,19 @@ abstract class Projectile extends Entity{
$this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7);
}
}
public function onHit(MovingObjectPosition $hitResult){
}
public function createSaveData(){
$data = parent::createSaveData();
$data["inTile"] = $this->inTile;
$data["inGround"] = $this->inGround;
$data["xTile"] = $this->xTile;
$data["yTile"] = $this->yTile;
$data["zTile"] = $this->zTile;
return $data;
}
public function spawn($player)
@ -212,5 +213,5 @@ abstract class Projectile extends Entity{
$pk->did = 0;
$player->dataPacket($pk);
}
}

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;

View file

@ -10,7 +10,7 @@ class Chicken extends Animal{
$this->setHealth($this->data["Health"] ?? 4, "generic");
$this->setName('Chicken');
$this->setSpeed(0.25);
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAtPlayer(6));
$this->ai->addTask(new TaskPanic(1.5));
@ -23,27 +23,27 @@ class Chicken extends Animal{
public function isFood($id){
return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS;
}
public function updateEntityMovement(){
parent::updateEntityMovement();
if(!$this->onGround && $this->speedY < 0) $this->speedY *= 0.6;
if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){
$this->dropAnEgg();
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
}
}
public function fall(){} //chickens have no fall dmg?
public function dropAnEgg(){
if($this->closed){
return;
}
ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(EGG, 0, 1));
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : [
[FEATHER, 0, mt_rand(0,2)],

View file

@ -17,11 +17,11 @@ class Cow extends Animal{
$this->ai->addTask(new TaskMate(1.0));
$this->ai->addTask(new TaskFollowParent(1.1));
}
public function isFood($id){
return $id === WHEAT;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
@ -33,9 +33,9 @@ class Cow extends Animal{
}
parent::interactWith($e, $action);
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : [
return $this->isBaby() ? parent::getDrops() : [
[LEATHER, 0, mt_rand(0,2)],
[($this->fire > 0 ? STEAK : BEEF), 0, mt_rand(0, 3)]
];

View file

@ -1,7 +1,8 @@
<?php
class Pig extends Animal implements Rideable{
const TYPE = MOB_PIG;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->setSize(0.9, 0.9);
parent::__construct($level, $eid, $class, $type, $data);
@ -9,7 +10,7 @@ class Pig extends Animal implements Rideable{
$this->server = ServerAPI::request();
$this->setName("Pig");
$this->setSpeed(0.25);
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAtPlayer(6));
$this->ai->addTask(new TaskPanic(1.5));
@ -25,18 +26,18 @@ class Pig extends Animal implements Rideable{
public function isSaddled(){
return (boolean) $this->getState();
}
/**
* @param boolean $value
*/
public function setSaddled($value = null){
$this->setState($value === null ? !$this->getState() : $value);
}
public function canRide($e){
return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0;
}
public function updateEntityMovement(){
/*if($this->linkedEntity != 0){
$e = $this->level->entityList[$this->linkedEntity] ?? false;
@ -47,10 +48,10 @@ class Pig extends Animal implements Rideable{
$this->yaw = $e->headYaw;
}
}*/
parent::updateEntityMovement();
}
public function interactWith(Entity $e, $action)
{
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
@ -59,7 +60,7 @@ class Pig extends Animal implements Rideable{
$e->setRiding($this);
return true;
}
if($slot->getID() === SADDLE){
if(!$this->isSaddled()){
$e->player->removeItem($slot->getID(), 0, 1);
@ -67,15 +68,15 @@ class Pig extends Animal implements Rideable{
}
return true; //avoid further interactions
}
}
return parent::interactWith($e, $action);
}
public function isFood($id){
return $id === POTATO || $id === CARROT || $id === BEETROOT;
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : ($this->isSaddled() ? [
[($this->fire > 0 ? COOKED_PORKCHOP : RAW_PORKCHOP), 0, mt_rand(0,2)],

View file

@ -3,7 +3,7 @@
class Sheep extends Animal{
public $color;
const TYPE = MOB_SHEEP;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->setSize(0.9, 1.3);
parent::__construct($level, $eid, $class, $type, $data);
@ -12,7 +12,7 @@ class Sheep extends Animal{
$this->data["Sheared"] = $this->data["Sheared"] ?? 0;
$this->data["Color"] = $this->data["Color"] ?? $this->sheepColor();
$this->setSpeed(0.25);
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAtPlayer(6));
$this->ai->addTask(new TaskPanic(1.5));
@ -23,14 +23,14 @@ class Sheep extends Animal{
$this->ai->addTask(new TaskMate(1.0));
$this->ai->addTask(new TaskFollowParent(1.0));
}
public function createSaveData(){
$data = parent::createSaveData();
$data["Color"] = @$this->data["Color"];
$data["Sheared"] = @$this->data["Sheared"];
return $data;
}
public function eatGrass(){
$this->setSheared(0);
if($this->isBaby()){
@ -39,24 +39,24 @@ class Sheep extends Animal{
$this->setAge($age);
}
}
public function setSheared($v = null){
$this->data["Sheared"] = $v === null ? !$this->isSheared() : $v;
$this->updateMetadata();
}
public function isSheared(){
return $this->data["Sheared"];
}
public function getColor(){
return $this->data["Color"]; //color === 16 -> color = 0, color === 17 -> color = 1 ...
}
public function switchColorMeta($meta){
return abs($meta - 15);
}
public function setColor($meta){
$this->data["Color"] = $meta;
}
@ -65,11 +65,11 @@ class Sheep extends Animal{
[WOOL, $this->getColor(), 1]
];
}
public function isFood($id){
return $id === WHEAT;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
@ -79,7 +79,7 @@ class Sheep extends Animal{
$this->setSheared(1);
$speedX = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1;
$speedZ = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1;
$speedY = 0.2 + (lcg_value()) * 0.05;
$speedY = 0.2 + (lcg_value()) * 0.05;
$this->server->api->entity->dropRawPos($this->level, $this->x, $this->y + 1, $this->z, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)), $speedX, $speedY, $speedZ);
if($slot->getMetadata() >= $slot->getMaxDurability()){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
@ -90,7 +90,7 @@ class Sheep extends Animal{
return true;
}elseif($slot->getID() === DYE){
$this->setColor($this->switchColorMeta($slot->getMetadata()));
if(($e->player->gamemode & 0x01) === SURVIVAL){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true);
}
@ -98,7 +98,7 @@ class Sheep extends Animal{
}
return parent::interactWith($e, $action);
}
public function getMetadata(){
$d = parent::getMetadata();
if(!isset($this->data["Sheared"])){
@ -107,7 +107,7 @@ class Sheep extends Animal{
$d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->getColor() & 0x0F);
return $d;
}
public function sheepColor(){ //a method from 0.8.1
$c = mt_rand(0,100);
if($c <= 4){

View file

@ -1,4 +1,5 @@
<?php
class Creeper extends Monster{
const TYPE = MOB_CREEPER;
const EXPL_TIME = 30;
@ -12,30 +13,28 @@ class Creeper extends Monster{
$this->ignited = 0;
$this->setSpeed(0.25);
$this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0;
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskAttackPlayer(1.25, 16));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
}
public function getAttackDamage(){
return 0;
}
public function setIgnited($v = null){
$this->setState($v === null ? !$this->getState() : $v);
$this->setState($v === null ? !$this->getState() : $v);
}
/**
* @return boolean
*/
public function isIgnited(){
return $this->getState() > 0;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
@ -51,12 +50,12 @@ class Creeper extends Monster{
}
return parent::interactWith($e, $action);
}
public function ignite(){
$this->setIgnited(1);
$this->timeUntilExplode = self::EXPL_TIME;
}
public function attackEntity($entity, $distance){
if(Utils::distance_noroot($entity, $this) <= 49 && !$this->isIgnited() && $this->ai->canSee($entity)){
$this->ignite();
@ -88,7 +87,7 @@ class Creeper extends Monster{
$this->close();
$explosion->explode();
}
public function getDrops(){
return [
[GUNPOWDER, 0, mt_rand(0,2)]

View file

@ -1,4 +1,5 @@
<?php
class PigZombie extends Monster{ //TODO extends zombie
const TYPE = MOB_PIGMAN;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){

View file

@ -1,4 +1,5 @@
<?php
class Skeleton extends Monster{
const TYPE = MOB_SKELETON;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
@ -8,31 +9,31 @@ class Skeleton extends Monster{
$this->setName("Skeleton");
$this->ai->removeTask("TaskAttackPlayer");
$this->setSpeed(0.25);
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskRangedAttack(1.0, 16));
}
public function getAttackDamage(){
return 0;
}
public function updateBurning(){
if($this->fire > 0 || !$this->level->isDay() || $this->inWater){
return false;
}
//bad fix for burning in water
[$block, $meta] = $this->level->level->getBlock((int)$this->x, (int)$this->y, (int)$this->z);
[$block, $meta] = $this->level->level->getBlock((int) $this->x, (int) $this->y, (int) $this->z);
if($block == WATER || $block == STILL_WATER){
$v16 = ((int)$this->y + 1) - LiquidBlock::getPercentAir($meta);
if($this->boundingBox->intersectsWith(new AxisAlignedBB((int)$this->x, (int)$this->y, (int)$this->z, (int)$this->x + 1, $v16, (int)$this->z + 1))){
$v16 = ((int) $this->y + 1) - LiquidBlock::getPercentAir($meta);
if($this->boundingBox->intersectsWith(new AxisAlignedBB((int) $this->x, (int) $this->y, (int) $this->z, (int) $this->x + 1, $v16, (int) $this->z + 1))){
return false;
}
}
for($y = (int)$this->y; $y < 129; $y++){
for($y = (int) $this->y; $y < 129; $y++){
$block = $this->level->level->getBlockID($this->x, $y, $this->z);
if(StaticBlock::getIsSolid($block)){
return false;
@ -49,12 +50,12 @@ class Skeleton extends Monster{
return false;
}
}
public function updateEntityMovement(){
$this->updateBurning();
return parent::updateEntityMovement();
}
public function getDrops(){
return [
[ARROW, 0, mt_rand(0,2)],

View file

@ -1,8 +1,9 @@
<?php
class Spider extends Monster{
const TYPE = MOB_SPIDER;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->setSize(1.4, 0.9);
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth($this->data["Health"] ?? 8, "generic");
@ -11,35 +12,35 @@ class Spider extends Monster{
$this->ai->addTask(new TaskRandomWalk(1.2));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskAttackPlayer(1.2, 16));
$this->ai->addTask(new TaskAttackPlayer(1.2, 16));
}
public function getAttackDamage(){
return 2;
}
public function attackEntity($entity, $distance){
if($distance > 2.0 && $distance < 6.0){
if($this->onGround){
$diffX = $entity->x - $this->x;
$diffZ = $entity->z - $this->z;
$dist = sqrt($diffX*$diffX + $diffZ*$diffZ);
$this->speedX = $diffX / $dist*0.5*0.8 + $this->speedX*0.2;
$this->speedZ = $diffZ / $dist*0.5*0.8 + $this->speedZ*0.2;
$dist = sqrt($diffX * $diffX + $diffZ * $diffZ);
$this->speedX = $diffX / $dist * 0.5 * 0.8 + $this->speedX * 0.2;
$this->speedZ = $diffZ / $dist * 0.5 * 0.8 + $this->speedZ * 0.2;
$this->speedY = 0.4;
}
return false;
}else{
return parent::attackEntity($entity, $distance);
}
}
public function isOnLadder(){
return $this->isCollidedHorizontally;
}
public function getDrops(){
return [
[STRING, 0, mt_rand(0,2)]

View file

@ -1,37 +1,38 @@
<?php
class Zombie extends Monster{
const TYPE = MOB_ZOMBIE;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->setSize(0.6, 1.85);
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth($this->data["Health"] ?? 12, "generic");
$this->setName("Zombie");
$this->setSpeed(0.23);
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskAttackPlayer(1.0, 16));
}
public function getArmorValue(){
return 2;
}
public function updateBurning(){
if($this->fire > 0 || !$this->level->isDay() || $this->inWater){
return false;
}
//bad fix for burning in water
[$block, $meta] = $this->level->level->getBlock((int)$this->x, (int)$this->y, (int)$this->z);
[$block, $meta] = $this->level->level->getBlock((int) $this->x, (int) $this->y, (int) $this->z);
if($block == WATER || $block == STILL_WATER){
$v16 = ((int)$this->y + 1) - LiquidBlock::getPercentAir($meta);
if($this->boundingBox->intersectsWith(new AxisAlignedBB((int)$this->x, (int)$this->y, (int)$this->z, (int)$this->x + 1, $v16, (int)$this->z + 1))){
$v16 = ((int) $this->y + 1) - LiquidBlock::getPercentAir($meta);
if($this->boundingBox->intersectsWith(new AxisAlignedBB((int) $this->x, (int) $this->y, (int) $this->z, (int) $this->x + 1, $v16, (int) $this->z + 1))){
return false;
}
}
for($y = (int)$this->y; $y < 129; $y++){
for($y = (int) $this->y; $y < 129; $y++){
$block = $this->level->level->getBlockID($this->x, $y, $this->z);
if(StaticBlock::getIsSolid($block)){
return false;
@ -48,7 +49,7 @@ class Zombie extends Monster{
return false;
}
}
public function getAttackDamage(){
return 4;
}
@ -57,7 +58,7 @@ class Zombie extends Monster{
$this->updateBurning();
return parent::updateEntityMovement();
}
public function getDrops(){
return [
[CARROT, 0, Utils::chance(0.83) ? 1 : 0],

View file

@ -3,7 +3,7 @@
class Arrow extends Entity{
const TYPE = OBJECT_ARROW;
const CLASS_TYPE = ENTITY_OBJECT;
public $shooterEID = 0;
public $xTile, $yTile, $zTile;
public $inTile, $inData;
@ -13,7 +13,7 @@ class Arrow extends Entity{
public $criticial = false;
public $groundTicks = 0;
public $shotByPlayer = false;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->gravity = 0.05;
@ -32,12 +32,12 @@ class Arrow extends Entity{
$this->yTile = $data["yTile"] ?? $this->yTile;
$this->zTile = $data["zTile"] ?? $this->zTile;
$this->shotByPlayer = $data["shotByPlayer"] ?? $this->shotByPlayer;
//$this->server->schedule(1210, array($this, "update")); //Despawn
}
public function createSaveData(){
$data = parent::createSaveData();
$data["inTile"] = $this->inTile;
$data["inData"] = $this->inData;
$data["inGround"] = $this->inGround;
@ -57,7 +57,7 @@ class Arrow extends Entity{
$pk->pitch = $this->pitch;
$this->server->api->player->broadcastPacket($this->level->players, $pk);
}
public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3
$f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2);
$d /= $f2;
@ -79,7 +79,7 @@ class Arrow extends Entity{
//$this->update();
$this->groundTicks = 0;
}
public function update($now){
$this->lastX = $this->x;
$this->lastY = $this->y;
@ -101,40 +101,39 @@ class Arrow extends Entity{
$this->fire -= 4;
if($this->fire <= 0) $this->fire = 0;
}
if($this->fire <= 0){
$this->updateMetadata();
}
}
if($this->handleLavaMovement()){
if(!$this->isImmuneToFire){
$this->harm(4, "fire");
$oldOnFire = $this->fire;
if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds
if($oldOnFire < 20 * 30) $this->fire = 20 * 30; //30 seconds
}
}
if($this->isInVoid()){
$this->outOfWorld();
}
//Entity::update end
if($this->lastPitch == $this->lastYaw && $this->lastYaw == 0){
$v1 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$v1 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ);
$this->lastYaw = $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI;
}
if($this->shake > 0){
--$this->shake;
}
if($this->inGround){
[$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile);
$this->speedZ = $this->speedX = $this->speedY = 0;
if($blockID == $this->inTile && $blockMeta == $this->inData){
++$this->groundTicks;
if($this->groundTicks >= 1200){ //TODO customizeable?
@ -147,7 +146,7 @@ class Arrow extends Entity{
$this->speedZ *= lcg_value() * 0.2;
$this->groundTicks = $this->airTicks = 0;
}
}else{
++$this->airTicks;
$start = new Vector3($this->x, $this->y, $this->z);
@ -162,42 +161,40 @@ class Arrow extends Entity{
}else{
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
}
$entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1));
$bestDist = 0;
$bestEnt = null;
foreach($entities as $eid => $ent){
if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){
$v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3);
$v13 = $v12->calculateIntercept($start, $end);
if($v13 != null){
$dist = $start->distance($v13->hitVector);
if($dist < $bestDist || $bestDist == 0){
$bestEnt = $ent;
}
}
}
}
if($bestEnt != null){
$v4 = MovingObjectPosition::fromEntity($bestEnt);
}
if($v4 != null){
if($v4->entityHit != null){
$v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$damage = ceil($v49+$v49);
$v49 = sqrt($this->speedY * $this->speedY + $this->speedX * $this->speedX + $this->speedZ * $this->speedZ);
$damage = ceil($v49 + $v49);
if($this->criticial){
$damage += mt_rand(0, $damage / 2 + 1);
}
if($v4->entityHit->harm($damage, $this->eid)){
//vanilla seems to increase arrow count if $v4->entity is mob
$this->close();
@ -213,36 +210,36 @@ class Arrow extends Entity{
$this->xTile = $v4->blockX;
$this->yTile = $v4->blockY;
$this->zTile = $v4->blockZ;
[$this->inTile, $this->inData] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile);
$this->speedX = $v4->hitVector->x - $this->x;
$this->speedY = $v4->hitVector->y - $this->y;
$this->speedZ = $v4->hitVector->z - $this->z;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedY*$this->speedY + $this->speedZ*$this->speedZ);
$v21 = sqrt($this->speedX * $this->speedX + $this->speedY * $this->speedY + $this->speedZ * $this->speedZ);
if($v21 != 0){
$this->x -= $this->speedX / $v21 * 0.05;
$this->y -= $this->speedY / $v21 * 0.05;
$this->z -= $this->speedZ / $v21 * 0.05;
}
$this->inGround = true;
$this->shake = 7;
$this->criticial = false;
}
}
$this->x += $this->speedX;
$this->y += $this->speedY;
$this->z += $this->speedZ;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$v21 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ);
$this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->pitch = atan2($this->speedY, $v21) * 180 / M_PI;
$this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2;
$this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2;
$v24 = $this->inWater ? 0.8 : 0.99;
$this->speedX *= $v24;
$this->speedY *= $v24;
@ -252,14 +249,12 @@ class Arrow extends Entity{
$v7 = $this->width / 2;
$v8 = $this->height;
$this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7);
$this->doBlocksCollision();
}
}
public function spawn($player){
if($this->type === OBJECT_ARROW){
$pk = new AddEntityPacket;
@ -268,7 +263,7 @@ class Arrow extends Entity{
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->did = 1;
$pk->did = 1;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;

View file

@ -3,9 +3,9 @@
class FallingSand extends Entity{
const TYPE = FALLING_SAND;
const CLASS_TYPE = ENTITY_FALLING;
public $fallTime = 0;
public function __construct($level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(PHP_INT_MAX, "generic");
@ -15,14 +15,14 @@ class FallingSand extends Entity{
$this->hasGravity = true;
$this->gravity = 0.04;
}
public function isPickable(){
return !$this->dead;
}
public function update($now){
if($this->closed) return;
if( $this->data["Tile"] == AIR) {
$this->close();
return;
@ -33,15 +33,15 @@ class FallingSand extends Entity{
++$this->fallTime;
$this->speedY -= 0.04;
$this->move($this->speedX, $this->speedY, $this->speedZ);
$this->speedX *= 0.98;
$this->speedY *= 0.98;
$this->speedZ *= 0.98;
$x = floor($this->x);
$y = floor($this->y);
$z = floor($this->z);
if($this->onGround){
$this->speedX *= 0.7;
$this->speedZ *= 0.7;

View file

@ -4,10 +4,10 @@ class ItemEntity extends Entity{
const TYPE = ENTITY_ITEM_TYPE;
const CLASS_TYPE = ENTITY_ITEM;
public static $searchRadiusX = 0.5, $searchRadiusY = 0.0, $searchRadiusZ = 0.5;
public $meta, $stack, $itemID;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array())
public function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{
parent::__construct($level, $eid, $class, $type, $data);
$this->setSize(0.25, 0.25);
@ -27,21 +27,21 @@ class ItemEntity extends Entity{
$this->delayBeforePickup = 40; //in vanilla it is 0 by default
$this->stepHeight = 0;
}
public function counterUpdate(){
parent::counterUpdate();
if($this->delayBeforePickup > 0) --$this->delayBeforePickup;
}
public function searchForOtherItemsNearby(){
$ents = $this->level->getEntitiesInAABBOfType($this->boundingBox->expand(self::$searchRadiusX, self::$searchRadiusY, self::$searchRadiusZ), ENTITY_ITEM);
foreach($ents as $e){
$this->tryCombining($e);
}
}
public function spawn($player)
{
$pk = new AddItemEntityPacket();
@ -55,7 +55,7 @@ class ItemEntity extends Entity{
$pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack);
$pk->metadata = $this->getMetadata();
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket();
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
@ -63,16 +63,15 @@ class ItemEntity extends Entity{
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
}
public function tryCombining(Entity $another){
if($another->eid == $this->eid) return false;
if(!$another->closed && !$this->closed){
if($another->itemID == $this->itemID && $another->meta == $this->meta){
if(($another->stack + $this->stack) > 64) return false; //TODO dynamic stack size
$another->stack += $this->stack;
$another->age = min($this->age, $another->age);
$this->close();
@ -84,29 +83,29 @@ class ItemEntity extends Entity{
$xFloor = floor($x);
$yFloor = floor($y);
$zFloor = floor($z);
$id = $this->level->level->getBlockID($xFloor, $yFloor, $zFloor);
if(StaticBlock::getIsSolid($id)){
$xDiff = $x - $xFloor;
$id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor);
$xNeg = StaticBlock::getIsSolid($id);
$id = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor);
$xPos = StaticBlock::getIsSolid($id);
$id = $this->level->level->getBlockID($xFloor, $yFloor - 1, $zFloor);
$yNeg = StaticBlock::getIsSolid($id);
$id = $this->level->level->getBlockID($xFloor, $yFloor + 1, $zFloor);
$yPos = StaticBlock::getIsSolid($id);
$id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor - 1);
$zNeg = StaticBlock::getIsSolid($id);
$zPos = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor + 1);
if($xNeg || $xDiff >= 9999.0){ //TODO not needed check?
$v15 = 9999.0;
$v16 = -1;
@ -114,22 +113,22 @@ class ItemEntity extends Entity{
$v15 = $xDiff;
$v16 = 0;
}
if(!$xPos){
$v17 = 1 - $xDiff;
if($v17 < $v15){
$v15 = $v17;
$v16 = 1;
}
}
$yDiff = $y - $yFloor;
if(!$yNeg && $yDiff < $v15){
$v15 = $yDiff;
$v16 = 2;
}
if(!$yPos){
$v19 = 1.0 - $yDiff;
if($v19 < $v15){
@ -137,17 +136,17 @@ class ItemEntity extends Entity{
$v16 = 3;
}
}
$zDiff = $z - $zFloor;
if(!$zNeg && $zDiff < $v15){
$v15 = $zDiff;
$v16 = 4;
}
if(!StaticBlock::getIsSolid($zPos) && ((1.0 - $zDiff) < $v15)){
$v16 = 5;
}
$v21 = lcg_value() * 0.2 + 0.1;
switch($v16){
case 0:
@ -166,24 +165,24 @@ class ItemEntity extends Entity{
$this->speedZ = $v21;
return 0;
}
return 0;
}
}
public function handleWaterMovement(){
return $this->level->handleMaterialAcceleration($this->boundingBox, 0, $this);
}
public function updateEntityMovement(){
//TODO custom update( method
$this->speedY -= 0.04;
//$this->noClip = false;
$this->checkInTile($this->x, $this->y, $this->z);
$this->move($this->speedX, $this->speedY, $this->speedZ);
$var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ;
$var1 = (int) $this->x != (int) $this->lastX || (int) $this->y != (int) $this->lastY || (int) $this->z != (int) $this->lastZ;
if($var1 || $this->counter % 25 == 0){
$blockIDAt = $this->level->level->getBlockID(floor($this->x), floor($this->y), floor($this->z));
if($blockIDAt == LAVA || $blockIDAt == STILL_LAVA){
@ -191,30 +190,30 @@ class ItemEntity extends Entity{
$this->speedX = (lcg_value() - lcg_value()) * 0.2;
$this->speedZ = (lcg_value() - lcg_value()) * 0.2;
}
//$this->searchForOtherItemsNearby(); //not in vanilla 0.8.1
}
if($this->closed) return;
$friction = 0.98;
if($this->onGround){
$friction = 0.588;
$v3 = $this->level->level->getBlockID($this->x, floor($this->boundingBox->minY) - 1, $this->z);
if($v3 > 0) $friction = StaticBlock::getSlipperiness($v3);
}
$this->speedX *= $friction;
$this->speedY *= 0.98;
$this->speedZ *= $friction;
if($this->onGround) $this->speedY *= -0.5;
if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0;
if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0;
if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0;
++$this->age;
//TODO despawn after age >= 6000 ?;
//TODO despawn after age >= 6000 ?;
}
}

View file

@ -4,7 +4,7 @@ class Minecart extends Vehicle{
const TYPE = OBJECT_MINECART;
/**
* A minecart rotation matrix
* @var int[][][]
* @var int[][][]
*/
private static $matrix = [
[
@ -48,14 +48,14 @@ class Minecart extends Vehicle{
[1, 0, 0]
]
];
private $hurtTime = 0; //syncentdata 17 int
private $damage = 0; //syncentdata 19 float
private $minecartX = 0, $minecartY = 0, $minecartZ = 0;
private $turnProgress = 0;
public $isInReverse = false;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
@ -67,76 +67,76 @@ class Minecart extends Vehicle{
$this->yOffset = $this->height / 2;
$this->stepHeight = 0;
}
public function isPickable(){
return !$this->dead;
}
public function getDrops(){
return [
[MINECART, 0, 1]
];
}
public function getPos($x, $y, $z){
$blockX = floor($x);
$blockY = floor($y);
$blockZ = floor($z);
if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)) --$blockY;
[$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ);
if(RailBaseBlock::isRailID($id)){
if($id == POWERED_RAIL) $meta &= 7;
$mat = self::$matrix[$meta];
$v13 = 0;
$v15 = $blockX + 0.5 + $mat[0][0] * 0.5;
$v17 = $blockY + 0.5 + $mat[0][1] * 0.5;
$v19 = $blockZ + 0.5 + $mat[0][2] * 0.5;
$v21 = $blockX + 0.5 + $mat[1][0] * 0.5;
$v23 = $blockY + 0.5 + $mat[1][1] * 0.5;
$v25 = $blockZ + 0.5 + $mat[1][2] * 0.5;
$v27 = $v21 - $v15;
$v29 = ($v23 - $v17) * 2;
$v31 = $v25 - $v19;
if($v27 == 0){
$x = $blockX + 0.5;
$v13 = $z - $blockZ;
}else if($v31 == 0){
}elseif($v31 == 0){
$z = $blockZ + 0.5;
$v13 = $x - $blockX;
}else{
$v33 = $x - $v15;
$v35 = $z - $v19;
$v13 = ($v33*$v27 + $v35*$v31) * 2;
$v13 = ($v33 * $v27 + $v35 * $v31) * 2;
}
$x = $v15 + $v27 * $v13;
$y = $v17 + $v29 * $v13;
$z = $v19 + $v31 * $v13;
if($v29 < 0) ++$y;
if($v29 > 0) $y += 0.5;
return [$x, $y, $z];
}
return false;
}
public function moveAlongTrack($x, $y, $z, $maxSpeed, $boost, $id, $meta){
$this->fallDistance = 0;
$vec = $this->getPos($this->x, $this->y, $this->z);
$this->y = $y;
if($id == POWERED_RAIL) $meta &= 7;
if($meta >= 2 && $meta <= 5) $this->y = $y + 1;
switch($meta){
case 2:
$this->speedX -= $boost;
@ -151,53 +151,53 @@ class Minecart extends Vehicle{
$this->speedZ -= $boost;
break;
}
$mat = self::$matrix[$meta];
$matXDiff = $mat[1][0] - $mat[0][0];
$matZDiff = $mat[1][2] - $mat[0][2];
$matDiffTotal = sqrt($matXDiff*$matXDiff + $matZDiff*$matZDiff);
$matDiffTotal = sqrt($matXDiff * $matXDiff + $matZDiff * $matZDiff);
if(($this->speedZ * $matZDiff + $this->speedX * $matXDiff) < 0){
$matXDiff = -$matXDiff;
$matZDiff = -$matZDiff;
}
$speedTotal = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX);
$speedTotal = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX);
if($speedTotal > 2) $speedTotal = 2;
$this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal;
$this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal;
if($this->linkedEntity != 0 && !$this->isRider){
$rider = $this->level->entityList[$this->linkedEntity] ?? false;
if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){
if($rider->moveForward > 0){
$v32 = sin($this->yaw * M_PI / 180);
$v33 = cos($this->yaw * M_PI / 180);
if($this->speedZ*$this->speedZ + $this->speedX*$this->speedX < 0.01){
$this->speedX -= $v32*0.1;
$this->speedZ += $v33*0.1;
if($this->speedZ * $this->speedZ + $this->speedX * $this->speedX < 0.01){
$this->speedX -= $v32 * 0.1;
$this->speedZ += $v33 * 0.1;
}
}
}
}
$v38 = ($x + 0.5) + ($mat[0][0] * 0.5);
$v39 = ($z + 0.5) + ($mat[0][2] * 0.5);
$v40 = (($x + 0.5) + ($mat[1][0] * 0.5)) - $v38;
$v41 = (($z + 0.5) + ($mat[1][2] * 0.5)) - $v39;
if($v40 == 0){
$v42 = $this->z - $z;
}else if($v41 == 0){
}elseif($v41 == 0){
$v42 = $this->x - $x;
}else{
$v44 = (($this->z - $v39) * $v41) + (($this->x - $v38) * $v40);
$v42 = $v44 + $v44;
}
$this->x = $v38 + ($v40 * $v42);
$this->z = $v39 + ($v41 * $v42);
$this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z);
$dx = $this->speedX;
$dz = $this->speedZ;
@ -205,55 +205,55 @@ class Minecart extends Vehicle{
$dx *= 0.75;
$dz *= 0.75;
}
if($dx < -$maxSpeed) $dx = -$maxSpeed;
else if($dx > $maxSpeed) $dx = $maxSpeed;
elseif($dx > $maxSpeed) $dx = $maxSpeed;
if($dz < -$maxSpeed) $dz = -$maxSpeed;
else if($dz > $maxSpeed) $dz = $maxSpeed;
elseif($dz > $maxSpeed) $dz = $maxSpeed;
$this->move($dx, 0, $dz);
if($mat[0][1]){
if((floor($this->x) - $x) == $mat[0][0] && (floor($this->z) - $z) == $mat[0][2]){
$this->setPos($this->x, $mat[0][1] + $this->y, $this->z);
}else if($mat[1][1]){
}elseif($mat[1][1]){
goto mat_1_1;
}
}else if($mat[1][1]){
}elseif($mat[1][1]){
mat_1_1:
if((floor($this->x) - $x) == $mat[1][0] && (floor($this->z) - $z) == $mat[1][2]){
$this->setPos($this->x, $mat[1][1] + $this->y, $this->z);
}
}
$this->applyNaturalSlowdown();
$vec2 = $this->getPos($this->x, $this->y, $this->z);
if($vec2 !== false && $vec !== false){
$yDiff = ($vec[1] - $vec2[1]) * 0.05;
$totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX);
$totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX);
if($totalSpeed > 0){
$this->speedX = ($this->speedX / $totalSpeed) * ($totalSpeed + $yDiff);
$this->speedZ = ($this->speedZ / $totalSpeed) * ($totalSpeed + $yDiff);
$this->speedZ = ($this->speedZ / $totalSpeed) * ($totalSpeed + $yDiff);
}
$this->setPos($this->x, $vec2[1], $this->z);
}
if(floor($this->x) != $x || floor($this->z) != $z){ //this breaks everything
$totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX);
$totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX);
$this->speedX = (floor($this->x) - $x) * $totalSpeed;
$this->speedZ = (floor($this->z) - $z) * $totalSpeed;
}
if($id == POWERED_RAIL){
$totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX);
$totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX);
if($totalSpeed > 0.01){
$this->speedX += (($this->speedX / $totalSpeed) * 0.06);
$this->speedZ += (($this->speedZ / $totalSpeed) * 0.06);
return;
}
if($meta == 1){
//TODO Level::isSolidBlockingTile
if(StaticBlock::getIsSolid($this->level->level->getBlockID($x - 1, $y, $z))){
@ -265,7 +265,7 @@ class Minecart extends Vehicle{
$v63 = -0.02;
}
$this->speedX = $v63;
}else if($meta == 0){
}elseif($meta == 0){
//TODO Level::isSolidBlockingTile
if(StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z - 1))){
return;
@ -278,58 +278,57 @@ class Minecart extends Vehicle{
$this->speedZ = $v64;
}
}
}
public function applyNaturalSlowdown(){
$mult = $this->linkedEntity != 0 && !$this->isRider ? 0.997 : 0.96;
$this->speedX *= $mult;
$this->speedY = 0;
$this->speedZ *= $mult;
}
public function comeOffTrack($topSpeed){
if($this->speedX < -$topSpeed) $this->speedX = -$topSpeed;
else if($this->speedX > $topSpeed) $this->speedX = $topSpeed;
elseif($this->speedX > $topSpeed) $this->speedX = $topSpeed;
if($this->speedZ < -$topSpeed) $this->speedZ = -$topSpeed;
else if($this->speedZ > $topSpeed) $this->speedZ = $topSpeed;
elseif($this->speedZ > $topSpeed) $this->speedZ = $topSpeed;
if($this->onGround){
$this->speedX *= 0.5;
$this->speedY *= 0.5;
$this->speedZ *= 0.5;
}
$this->move($this->speedX, $this->speedY, $this->speedZ);
if(!$this->onGround){
$this->speedX *= 0.95;
$this->speedY *= 0.95;
$this->speedZ *= 0.95;
}
}
public function applyCollision(Entity $collided){
$diffX = $collided->x - $this->x;
$diffZ = $collided->z - $this->z;
$dist = $diffX*$diffX + $diffZ*$diffZ;
$dist = $diffX * $diffX + $diffZ * $diffZ;
if($dist >= 0.0001){
$sqrtMax = sqrt($dist);
$diffX /= $sqrtMax;
$diffZ /= $sqrtMax;
$col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v);
$diffX *= $col;
$diffZ *= $col;
$diffX *= 0.1;
$diffZ *= 0.1;
$diffX *= 0.5;
$diffZ *= 0.5;
$this->addVelocity(-$diffX, 0, -$diffZ);
$collided->addVelocity($diffX / 4, 0, $diffZ / 4);
}
@ -341,18 +340,18 @@ class Minecart extends Vehicle{
}
$this->updateLast();
//$this->updatePosition();
$this->speedY -= 0.04;
//TODO port stuff
$blockX = floor($this->x);
$blockY = floor($this->y);
$blockZ = floor($this->z);
if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)){
--$blockY;
}
[$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ);
if(RailBaseBlock::isRailID($id)){
$this->moveAlongTrack($blockX, $blockY, $blockZ, 0.4, 0.0078125, $id, $meta);
@ -362,32 +361,32 @@ class Minecart extends Vehicle{
}
$this->doBlocksCollision();
$this->pitch = 0;
$diffX = $this->lastX - $this->x;
$diffZ = $this->lastZ - $this->z;
if($diffX*$diffX + $diffZ*$diffZ > 0.001){
if($diffX * $diffX + $diffZ * $diffZ > 0.001){
$this->yaw = atan2($diffZ, $diffX) * 180 / M_PI;
if($this->isInReverse) $this->yaw += 180;
}
$yw = fmod($this->yaw - $this->lastYaw, 360);
if($yw >= 180) $yw -= 360;
if($yw < 180) $yw += 360;
if($yw < -170 || $yw >= 170){
$this->isInReverse = !$this->isInReverse;
$this->yaw = $this->yaw + 180;
}
$bb = $this->boundingBox->expand(0.2, 0, 0.2);
$minChunkX = ((int)($bb->minX - 2)) >> 4;
$minChunkZ = ((int)($bb->minZ - 2)) >> 4;
$maxChunkX = ((int)($bb->minX + 2)) >> 4;
$maxChunkZ = ((int)($bb->minZ + 2)) >> 4;
$minChunkX = ((int) ($bb->minX - 2)) >> 4;
$minChunkZ = ((int) ($bb->minZ - 2)) >> 4;
$maxChunkX = ((int) ($bb->minX + 2)) >> 4;
$maxChunkZ = ((int) ($bb->minZ + 2)) >> 4;
//TODO also index by chunkY?
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
@ -401,14 +400,14 @@ class Minecart extends Vehicle{
}else{
$e->applyCollision($this);
}
}
}
}
}
}
}
public function close()
{
parent::close();
@ -421,11 +420,11 @@ class Minecart extends Vehicle{
}
}
}
public function isPushable(){
return false; //TODO replace with true
}
public function spawn($player){
$pk = new AddEntityPacket;
$pk->eid = $this->eid;
@ -436,7 +435,7 @@ class Minecart extends Vehicle{
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
@ -444,7 +443,7 @@ class Minecart extends Vehicle{
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
}
public function interactWith(Entity $e, $action){
if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){
$e->setRiding($this);

View file

@ -3,23 +3,23 @@
class Painting extends Entity{
const CLASS_TYPE = ENTITY_OBJECT;
const TYPE = OBJECT_PAINTING;
public $motive;
public $direction;
public $xPos, $yPos, $zPos;
public $isValid = true;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->x = $this->data["TileX"] ?? $this->x;
$this->y = $this->data["TileY"] ?? $this->y;
$this->z = $this->data["TileZ"] ?? $this->z;
$this->xPos = $this->data["xPos"] ?? (int)$this->x;
$this->zPos = $this->data["zPos"] ?? (int)$this->z;
$this->yPos = $this->data["yPos"] ?? (int)$this->y;
$this->xPos = $this->data["xPos"] ?? (int) $this->x;
$this->zPos = $this->data["zPos"] ?? (int) $this->z;
$this->yPos = $this->data["yPos"] ?? (int) $this->y;
$this->health = 1;
$this->canBeAttacked = true;
$this->width = 1;
@ -30,30 +30,30 @@ class Painting extends Entity{
if($dir === false){
$dir = floor($this->yaw / 90);
}
if($mot === false){
$this->setRandomMotive($dir);
}else{
$this->motive = $mot;
$this->direction = $dir;
$this->setDirection($dir);
}
}
public $counter = 0;
public function update($now){
if($this->closed === true){
return false;
}
if($this->closed === true){
return false;
}
if(++$this->counter >= 100){
$this->counter = 0;
if(!$this->survives() && !$this->dead){ //maybe dont check entity collision?
$this->makeDead("nosurvive");
}
}
return true;
}
@ -62,34 +62,34 @@ class Painting extends Entity{
foreach(PaintingItem::$motives as $name => $motive){
$this->motive = $name;
$this->setDirection($dir);
if($this->survives()){
$tochoose[] = $name;
}
}
if(count($tochoose) > 0){
$ind = mt_rand(0, count($tochoose)-1);
$ind = mt_rand(0, count($tochoose) - 1);
}else{
$this->isValid = false;
return;
}
$this->motive = $tochoose[$ind];
$this->setDirection($dir);
}
public static function offs($n){
if($n == 32 || $n == 64) return 0.5;
return 0;
}
const DIRECTION_OPPOSITE = [2, 3, 0, 1];
public function setDirection($dir){
$this->direction = $dir;
$this->lastYaw = $this->yaw = $dir * 90;
$size = PaintingItem::$motives[$this->motive] ?? array(1, 1);
$width = $size[0]*16;
$height = $size[1]*16;
$size = PaintingItem::$motives[$this->motive] ?? [1, 1];
$width = $size[0] * 16;
$height = $size[1] * 16;
if($dir == 2 || $dir == 0){
$v10 = 2;
$v9 = $width;
@ -98,28 +98,28 @@ class Painting extends Entity{
$v9 = 2;
$v10 = $width;
}
$v12 = $v9 * 0.03125;
$v13 = $height * 0.03125;
$v14 = $v10 * 0.03125;
$v15 = $this->xPos + 0.5;
$v16 = $this->yPos + 0.5;
$v17 = $this->zPos + 0.5; //XXX check
if($dir == 2){
$v17 = $v17 - 0.5625;
$v15 = $v15 - self::offs($width);
}else if($dir == 1){
}elseif($dir == 1){
$v15 = $v15 - 0.5625;
$v17 = $v17 + self::offs($width);
}else if($dir == 3){
}elseif($dir == 3){
$v15 = $v15 + 0.5625;
$v17 = $v17 - self::offs($width);
}else{
$v17 = $v17 + 0.5625;
$v15 = $v15 + self::offs($width);
}
$v23 = $v16 + self::offs($height);
$this->setPos($v15, $v23, $v17);
$this->boundingBox->minX = ($v15 - $v12) + 0.03125;
@ -129,35 +129,35 @@ class Painting extends Entity{
$this->boundingBox->maxY = ($v23 + $v13) - 0.03125;
$this->boundingBox->maxZ = ($v17 + $v14) - 0.03125;
}
public function getDrops(){
return [
[PAINTING, 0, 1]
];
}
public function isPickable(){
return true;
}
public function survives(){
$cubes = $this->level->getCubes($this, $this->boundingBox);
if(count($cubes) == 0){
$size = PaintingItem::$motives[$this->motive] ?? array(1, 1);
$size = PaintingItem::$motives[$this->motive] ?? [1, 1];
$width = $size[0];
$width2 = $width*16/32;
$width2 = $width * 16 / 32;
$height = $size[1];
$height2 = $height*16/32;
$height2 = $height * 16 / 32;
if($width < 1) $width = 1;
if($height < 1) $height = 1;
$positionX = $this->xPos;
$positionZ = $this->zPos;
if($this->direction == 2 || $this->direction == 0) $positionX = floor($this->x - $width2);
if($this->direction == 1 || $this->direction == 3) $positionZ = floor($this->z - $width2);
$minY = floor($this->y - $height2);
for($off = 0; $off < $width; ++$off){
for($yoff = 0; $yoff < $height; ++$yoff){
$y = $minY + $yoff;
@ -166,13 +166,13 @@ class Painting extends Entity{
}else{
$id = $this->level->level->getBlockId($positionX + $off, $y, $this->zPos);
}
if(!StaticBlock::getIsSolid($id)){
return false;
}
}
}
$ents = $this->level->getEntitiesInAABBOfType($this->boundingBox, ENTITY_OBJECT);
foreach($ents as $e){
if($e->eid == $this->eid) continue;
@ -182,13 +182,13 @@ class Painting extends Entity{
}
return true;
}
return false;
}
public function createSaveData(){
$data = parent::createSaveData();
$data["Motive"] = $this->motive;
$data["Direction"] = $this->direction;
$data["xPos"] = $this->xPos;

View file

@ -14,11 +14,11 @@ class PrimedTNT extends Entity{
$this->yOffset = $this->height / 2;
$this->setHealth(10000000, "generic");
}
public function isPickable(){
return !$this->dead;
}
public function getMetadata(){
$d = parent::getMetadata();
$d[16]["value"] = (int) $this->data["fuse"];
@ -26,34 +26,34 @@ class PrimedTNT extends Entity{
}
public function createSaveData(){
$data = parent::createSaveData();
$data["fuse"] = $this->data["fuse"];
$data["power"] = $this->data["power"];
return $data;
}
public function update($now){
if($this->closed) return;
$this->lastX = $this->x;
$this->lastY = $this->y;
$this->lastZ = $this->z;
$this->speedY -= 0.04;
$this->move($this->speedX, $this->speedY, $this->speedZ);
$this->speedX *- 0.98;
$this->speedX * -0.98;
$this->speedY *= 0.98;
$this->speedZ *= 0.98;
if($this->onGround){
$this->speedX *= 0.7;
$this->speedZ *= 0.7;
$this->speedY *= -0.5;
}
$tickDiff = ($now - $this->lastUpdate) / 0.05;
$this->data["fuse"] -= $tickDiff;
$this->updateMetadata();
if($this->data["fuse"] <= 0){
@ -61,10 +61,10 @@ class PrimedTNT extends Entity{
$explosion = new Explosion($this, $this->data["power"]);
$explosion->explode();
}
$this->lastUpdate = $now;
}
public function spawn($player){
$pk = new AddEntityPacket;
$pk->eid = $this->eid;

View file

@ -1,32 +1,32 @@
<?php
class ThrownEgg extends Projectile{
const TYPE = OBJECT_EGG;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->typeOfHit == 1){
$hitResult->entityHit->harm(0, $this->eid);
}
$rand = mt_rand(0, 7);
if($rand == 0){
$count = mt_rand(0, 31) == 0 ? 4 : 1;
$data = [
"x" => $this->x,
"y" => $this->y,
"z" => $this->z,
"z" => $this->z,
"yaw" => $this->yaw,
"pitch" => 0,
"IsBaby" => true
];
for($i = 0; $i < $count; ++$i){
$chicken = $this->server->api->entity->add($this->level, ENTITY_MOB, MOB_CHICKEN, $data);
$this->server->api->entity->spawnToAll($chicken);
}
}
$this->close();
}
}
}

View file

@ -1,11 +1,12 @@
<?php
class ThrownSnowball extends Projectile{
const TYPE = OBJECT_SNOWBALL;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->typeOfHit == 1){
$hitResult->entityHit->harm(0, $this->eid);
}
$this->close();
}
}

View file

@ -2,16 +2,16 @@
class TripodCamera extends Entity{
const TYPE = OBJECT_TRIPOD_CAMERA;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(PHP_INT_MAX, "generic");
}
public function isPickable(){
return !$this->dead;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){
//todo

View file

@ -1,8 +1,8 @@
<?php
class EntityRegistry{
class EntityRegistry{
public static $entityList;
/*Register all entities*/
public static function registerEntities(){
ConsoleAPI::info("Registering entities...");
@ -25,13 +25,13 @@ class EntityRegistry{
self::registerEntity(ThrownEgg::class);
self::registerEntity(ThrownSnowball::class);
}
/*Register an Entity*/
public static function registerEntity($className){
$class = new \ReflectionClass($className);
if(is_a($className, Entity::class, true) and !$class->isAbstract()){
//self::$entityList[$className::TYPE] = $className;
self::$entityList->addEntity(new PropertyEntity($className::CLASS_TYPE, $className::TYPE, $className));
//self::$shortNames[$className] = $class->getShortName(); what is this even supposed to do?
//console("[INFO] Registered a ".$className);

View file

@ -2,13 +2,13 @@
class PropertyEntity{
private $class,$type,$name;
function __construct($class, $type, $name){
$this->class = $class;
$this->type = $type;
$this->name = $name;
}
public function getEntityClass(){
return $this->class;
}