From 80bdddb8174f9c8fb148a34afade1aabc910ed80 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 24 Dec 2022 22:42:23 +0300 Subject: [PATCH] new ai task, fixes of old ones, php8 fixes --- src/API/EntityAPI.php | 4 +- src/API/TimeAPI.php | 2 +- src/entity/Animal.php | 1 + src/entity/Entity.php | 2887 +++++++++++----------- src/entity/Living.php | 1 - src/entity/ai/EntityAI.php | 6 +- src/entity/ai/tasks/TaskLookAround.php | 4 +- src/entity/ai/tasks/TaskLookAtPlayer.php | 26 +- src/entity/ai/tasks/TaskRandomWalk.php | 3 +- src/entity/ai/tasks/TaskTempt.php | 72 + src/pmf/{Level.php => PMFLevel.php} | 11 +- src/utils/Utils.php | 21 + src/world/MobSpawner.php | 1 + 13 files changed, 1581 insertions(+), 1458 deletions(-) create mode 100644 src/entity/ai/tasks/TaskTempt.php rename src/pmf/{Level.php => PMFLevel.php} (97%) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index b654e603..9787ec65 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -72,8 +72,8 @@ class EntityAPI{ } elseif(is_numeric($args[1])){//summon [amount] $amount = (int) $args[1]; - if($amount > 25){ - $output .= "Cannot spawn > 25 mobs"; + if($amount > 100){ + $output .= "Cannot spawn > 100 mobs"; break; } $isBaby = false; diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index cc31c37a..80999776 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -92,7 +92,7 @@ class TimeAPI{ case "day": case "sunset": case "night": - if(substr($args[1], 0, 2) === "w:"){ + if(count($args) > 1 && substr($args[1], 0, 2) === "w:"){ $levelName = preg_replace("/w:/", "", $args[1]); $level = $this->server->api->level->get($levelName); if($level instanceof Level){ diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 16c2e311..491be870 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -14,6 +14,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{ if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){ $this->setAge(-24000); } + $this->ai->addTask(new TaskTempt()); } public function isBaby(){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 77f18bfd..6efe939f 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -3,1524 +3,1527 @@ class Entity extends Position { - const TYPE = - 1; - const CLASS_TYPE = - 1; + const TYPE = - 1; + const CLASS_TYPE = - 1; - public static $updateOnTick; - public $isCollidable; - public $canBeAttacked; - public $moveTime, $lookTime, $idleTime; - public $needsUpdate; - public $speedModifer; - public $hasGravity; - /** - * @var AxisAlignedBB - */ - public $boundingBox; - public $age; - public $air; - public $spawntime; - public $dmgcounter; - public $eid; - public $type; - public $name; - public $x, $y, $z; - public $speedX, $speedY, $speedZ, $speed; - public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0, $lastSpeedX = 0, $lastSpeedY = 0, $lastSpeedZ = 0; - /** - * 0 = lastX, - * 1 = lastY, - * 2 = lastZ, - * 3 = lastYaw, - * 4 = lastPitch, - * 5 = lastTime. - * - * It is not recommended to use this and it is left as a backwards compability. - * - * @var array - */ - public $last; - public $yaw, $headYaw; - public $pitch; - public $dead; - public $data; - public $class; - public $attach; - public $closed; - /** - * @var Player - */ - public $player; - public $fallY; - public $fallStart; - private $state; - private $tickCounter; - private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0); - public $server; - private $isStatic; - public $level; - public $isRiding = false; - public $lastUpdate; - public $linkedEntity = null; - public $check = true; - public $width = 1; - public $height = 1; - public $random; - public $radius; - public $inAction = false; - public $hasKnockback; + public static $updateOnTick; + public $isCollidable; + public $canBeAttacked; + public $moveTime, $lookTime, $idleTime; + public $needsUpdate; + public $speedModifer; + public $hasGravity; + /** + * @var AxisAlignedBB + */ + public $boundingBox; + public $age; + public $air; + public $spawntime; + public $dmgcounter; + public $eid; + public $type; + public $name; + public $x, $y, $z; + public $speedX, $speedY, $speedZ, $speed; + public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0, $lastSpeedX = 0, $lastSpeedY = 0, $lastSpeedZ = 0; + /** + * 0 = lastX, + * 1 = lastY, + * 2 = lastZ, + * 3 = lastYaw, + * 4 = lastPitch, + * 5 = lastTime. + * + * It is not recommended to use this and it is left as a backwards compability. + * + * @var array + */ + public $last; + public $yaw, $headYaw; + public $pitch; + public $dead; + public $data; + public $class; + public $attach; + public $closed; + /** + * @var Player + */ + public $player; + public $fallY; + public $fallStart; + private $state; + private $tickCounter; + private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0); + public $server; + private $isStatic; + public $level; + public $isRiding = false; + public $lastUpdate; + public $linkedEntity = null; + public $check = true; + public $width = 1; + public $height = 1; + public $random; + public $radius; + public $inAction = false; + public $hasKnockback; - function __construct(Level $level, $eid, $class, $type = 0, $data = array()) - { - $this->random = new Random(); - $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables - $this->canBeAttacked = false; - $this->hasKnockback = false; - $this->level = $level; - $this->speedModifer = 0.7; - $this->fallY = false; - $this->fallStart = false; - $this->server = ServerAPI::request(); - $this->eid = (int) $eid; - $this->type = (int) $type; - $this->class = (int) $class; - $this->player = false; - $this->attach = false; - $this->data = $data; - $this->status = 0; - $this->health = 20; - $this->hasGravity = false; - $this->dmgcounter = array(0, 0, 0); - $this->air = 300; - $this->fire = 0; - $this->crouched = false; - $this->invincible = false; - $this->lastUpdate = $this->spawntime = microtime(true); - $this->dead = false; - $this->closed = false; - $this->isStatic = false; - $this->name = ""; - $this->state = $this->data["State"] = isset($this->data["State"]) ? $this->data["State"] : 0; - $this->tickCounter = 0; - $this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health, hasUpdate) VALUES (" . $this->eid . ", '" . $this->level->getName() . "', " . $this->type . ", " . $this->class . ", " . $this->health . ", 0);"); - $this->x = isset($this->data["x"]) ? (float) $this->data["x"] : 0; - $this->y = isset($this->data["y"]) ? (float) $this->data["y"] : 0; - $this->z = isset($this->data["z"]) ? (float) $this->data["z"] : 0; - $this->speedX = isset($this->data["speedX"]) ? (float) $this->data["speedX"] : 0; - $this->speedY = isset($this->data["speedY"]) ? (float) $this->data["speedY"] : 0; - $this->speedZ = isset($this->data["speedZ"]) ? (float) $this->data["speedZ"] : 0; - $this->speed = 0; - $this->yaw = isset($this->data["yaw"]) ? (float) $this->data["yaw"] : 0; - $this->headYaw = isset($this->data["headYaw"]) ? $this->data["headYaw"] : $this->yaw; - $this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"] : 0; - $this->position = array( - "level" => $this->level, - "x" => &$this->x, - "y" => &$this->y, - "z" => &$this->z, - "yaw" => &$this->yaw, - "pitch" => &$this->pitch - ); - $this->height = 0.98; - $this->moveTime = 0; - $this->lookTime = 0; - $this->onGround = false; - switch($this->class) { - case ENTITY_PLAYER: - $this->player = $this->data["player"]; - $this->setHealth($this->health, "generic"); - $this->speedModifer = 1; - $this->width = 1.2; - $this->height = 1.9; - $this->hasKnockback = true; - $this->hasGravity = true; - $this->canBeAttacked = true; - break; - case ENTITY_ITEM: - if(isset($data["item"]) and ($data["item"] instanceof Item)){ - $this->meta = $this->data["item"]->getMetadata(); - $this->stack = $this->data["item"]->count; - } else{ - $this->meta = (int) $this->data["meta"]; - $this->stack = (int) $this->data["stack"]; - } - $this->hasGravity = true; - $this->setHealth(5, "generic"); - $this->setSize(0.25, 0.25); - break; - case ENTITY_FALLING: - $this->setHealth(PHP_INT_MAX, "generic"); - $this->height = 0.98; - $this->width = 0.98; - $this->hasGravity = true; - break; - case ENTITY_OBJECT: - $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; - $this->y = isset($this->data["TileY"]) ? $this->data["TileY"] : $this->y; - $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"] : $this->z; - $this->setHealth(1, "generic"); - // $this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type)); - $this->width = 1; - $this->height = 1; - if($this->type === OBJECT_SNOWBALL){ - $this->server->schedule(1210, array( - $this, - "update" - )); // Despawn - // $this->update(); - } - break; - } - $this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); - $this->update(); - $this->updateLast(); - $this->updatePosition(); - if($this->isInVoid()){ - $this->outOfWorld(); - } - } + function __construct(Level $level, $eid, $class, $type = 0, $data = array()) + { + $this->random = new Random(); + $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables + $this->canBeAttacked = false; + $this->hasKnockback = false; + $this->level = $level; + $this->speedModifer = 0.7; + $this->fallY = false; + $this->fallStart = false; + $this->server = ServerAPI::request(); + $this->eid = (int) $eid; + $this->type = (int) $type; + $this->class = (int) $class; + $this->player = false; + $this->attach = false; + $this->data = $data; + $this->status = 0; + $this->health = 20; + $this->hasGravity = false; + $this->dmgcounter = array(0, 0, 0); + $this->air = 300; + $this->fire = 0; + $this->crouched = false; + $this->invincible = false; + $this->lastUpdate = $this->spawntime = microtime(true); + $this->dead = false; + $this->closed = false; + $this->isStatic = false; + $this->name = ""; + $this->state = $this->data["State"] = isset($this->data["State"]) ? $this->data["State"] : 0; + $this->tickCounter = 0; + $this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health, hasUpdate) VALUES (" . $this->eid . ", '" . $this->level->getName() . "', " . $this->type . ", " . $this->class . ", " . $this->health . ", 0);"); + $this->x = isset($this->data["x"]) ? (float) $this->data["x"] : 0; + $this->y = isset($this->data["y"]) ? (float) $this->data["y"] : 0; + $this->z = isset($this->data["z"]) ? (float) $this->data["z"] : 0; + $this->speedX = isset($this->data["speedX"]) ? (float) $this->data["speedX"] : 0; + $this->speedY = isset($this->data["speedY"]) ? (float) $this->data["speedY"] : 0; + $this->speedZ = isset($this->data["speedZ"]) ? (float) $this->data["speedZ"] : 0; + $this->speed = 0; + $this->yaw = isset($this->data["yaw"]) ? (float) $this->data["yaw"] : 0; + $this->headYaw = isset($this->data["headYaw"]) ? $this->data["headYaw"] : $this->yaw; + $this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"] : 0; + $this->position = array( + "level" => $this->level, + "x" => &$this->x, + "y" => &$this->y, + "z" => &$this->z, + "yaw" => &$this->yaw, + "pitch" => &$this->pitch + ); + $this->height = 0.98; + $this->moveTime = 0; + $this->lookTime = 0; + $this->onGround = false; + switch($this->class) { + case ENTITY_PLAYER: + $this->player = $this->data["player"]; + $this->setHealth($this->health, "generic"); + $this->speedModifer = 1; + $this->width = 1.2; + $this->height = 1.9; + $this->hasKnockback = true; + $this->hasGravity = true; + $this->canBeAttacked = true; + break; + case ENTITY_ITEM: + if(isset($data["item"]) and ($data["item"] instanceof Item)){ + $this->meta = $this->data["item"]->getMetadata(); + $this->stack = $this->data["item"]->count; + } else{ + $this->meta = (int) $this->data["meta"]; + $this->stack = (int) $this->data["stack"]; + } + $this->hasGravity = true; + $this->setHealth(5, "generic"); + $this->setSize(0.25, 0.25); + break; + case ENTITY_FALLING: + $this->setHealth(PHP_INT_MAX, "generic"); + $this->height = 0.98; + $this->width = 0.98; + $this->hasGravity = true; + break; + case ENTITY_OBJECT: + $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; + $this->y = isset($this->data["TileY"]) ? $this->data["TileY"] : $this->y; + $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"] : $this->z; + $this->setHealth(1, "generic"); + // $this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type)); + $this->width = 1; + $this->height = 1; + if($this->type === OBJECT_SNOWBALL){ + $this->server->schedule(1210, array( + $this, + "update" + )); // Despawn + // $this->update(); + } + break; + } + $this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); + $this->update(); + $this->updateLast(); + $this->updatePosition(); + if($this->isInVoid()){ + $this->outOfWorld(); + } + } public function isType(){ return in_array($this->type, func_get_args()); } + public function addVelocity($vX, $vY = 0, $vZ = 0) + { + if($vX instanceof Vector3){ + return $this->addVelocity($vX->x, $vX->y, $vX->z); + } + $this->speedX += $vX; + $this->speedY += $vY; + $this->speedZ += $vZ; + } + + public function isMoving() + { + return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedY > 0.007 || $this->speedY < - 0.007) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); + } + + public function setVelocity($vX, $vY = 0, $vZ = 0) + { + if($vX instanceof Vector3){ + return $this->setVelocity($vX->x, $vX->y, $vX->z); + } + $this->speedX = $vX; + $this->speedY = $vY; + $this->speedZ = $vZ; + } + + /** + * + * @param mixed $e + * Entity instance or EID + * @return string|false if failed + */ + public static function getNameOf($e) + { + if($e instanceof Entity){ + return $e->getName(); + } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ + return $e->getName(); + } + return false; + } + + /** + * + * @param mixed $e + * Entity instance or EID + * @return number|false if failed + */ + public static function getHeightOf($e) + { + if($e instanceof Entity){ + return $e->getHeight(); + } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ + return $e->getHeight(); + } + return false; + } + + /** + * + * @param mixed $e + * Entity instance or EID + * @return number|false if failed + */ + public static function getWidthOf($e) + { + if($e instanceof Entity){ + return $e->getWidth(); + } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ + return $e->getWidth(); + } + return false; + } + + /** + * Get an entity height + * + * @return number + */ + public function getHeight($a) + { + return $this->height; + } + + /** + * Get an entity width + * + * @return number + */ + public function getWidth() + { + return $this->width; + } + + public function lookOn(Vector3 $target) + { + $horizontal = sqrt(pow(($target->x - $this->x), 2) + pow(($target->z - $this->z), 2)); + $vertical = $target->y - ($this->y + - 0.5); /* 0.5 = $entity->getEyeHeight() */ + $pitch = - asin($horizontal) / M_PI * 180; // negative is up, positive is down + + $xDist = $target->x - $this->x; + $zDist = $target->z - $this->z; + + $yaw = atan2($zDist, $xDist) / M_PI * 180 - 90; + if($yaw < 0){ + $yaw += 360.0; + } + $this->yaw = $yaw; + $this->pitch = $pitch; + $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); + } + + public function getDrops() + { + if($this->class === ENTITY_PLAYER and $this->player instanceof Player and ($this->player->gamemode & 0x01) === 0){ + $inv = []; + for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++ $i){ + $slot = $this->player->getSlot($i); + $this->player->setSlot($i, BlockAPI::getItem(AIR, 0, 0)); + if($slot->getID() !== AIR and $slot->count > 0){ + $inv[] = array( + $slot->getID(), + $slot->getMetadata(), + $slot->count + ); + } + } + for($re = 0; $re < 4; $re ++){ + $slot = $this->player->getArmor($re); + $this->player->setArmor($re, BlockAPI::getItem(AIR, 0, 0)); + if($slot->getID() !== AIR and $slot->count > 0){ + $inv[] = array( + $slot->getID(), + $slot->getMetadata(), + $slot->count + ); + } + } + return $inv; + } + return []; + } + + private function spawnDrops() + { + foreach($this->getDrops() as $drop){ + $this->server->api->entity->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF), true); + } + } + + public function environmentUpdate() + { + $hasUpdate = Entity::$updateOnTick ? $this->class === ENTITY_MOB : false; // force true for mobs + $time = microtime(true); + if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $this->player->blocked !== true && ! $this->dead){ + foreach($this->server->api->entity->getRadius($this, 1, ENTITY_ITEM) as $item){ + if(! $item->closed && $item->spawntime > 0 && ($time - $item->spawntime) >= 0.6){ + if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array( + "eid" => $this->player->eid, + "player" => $this->player, + "entity" => $item, + "block" => $item->type, + "meta" => $item->meta, + "target" => $item->eid + )) !== false){ + $item->close(); + // $item->spawntime = 0; + // $this->server->schedule(15, array($item, "close")); + } + } + } + } elseif($this->class === ENTITY_ITEM){ + if(($time - $this->spawntime) >= 300){ + $this->close(); // Despawn timer + return false; + } + } elseif($this->class === ENTITY_OBJECT and $this->type === OBJECT_SNOWBALL){ + if(($time - $this->spawntime) >= 60){ + $this->close(); + return false; + } + } + + if($this->class !== ENTITY_PLAYER and ($this->x <= 0 or $this->z <= 0 or $this->x >= 256 or $this->z >= 256 or $this->y >= 128 or $this->y <= 0)){ + $this->close(); + return false; + } + + if($this->dead === true){ + $this->fire = 0; + $this->air = 300; + return false; + } + if($this->isInVoid()){ + $this->outOfWorld(); + $hasUpdate = true; + } + + if($this->fire > 0){ + if(($this->fire % 20) === 0){ + $this->harm(1, "burning"); + } + $this->fire -= 10; + if($this->fire <= 0){ + $this->fire = 0; + $this->updateMetadata(); + } else{ + $hasUpdate = true; + } + if(($this->player instanceof Player) and ($this->player->gamemode & 0x01) === CREATIVE){ // Remove fire effects in next tick + $this->fire = 1; + } + } + + $startX = (int) (round($this->x - 0.5) - 1); + $startY = (int) (round($this->y) - 1); + $startZ = (int) (round($this->z - 0.5) - 1); + $endX = $startX + 2; + $endY = $startY + 2; + $endZ = $startZ + 2; + $waterDone = false; + if($this->isPlayer()){ + for($y = $startY; $y <= $endY; ++ $y){ + for($x = $startX; $x <= $endX; ++ $x){ + for($z = $startZ; $z <= $endZ; ++ $z){ + $pos = new Vector3($x, $y, $z); + $b = $this->level->getBlock($pos); + switch($b->getID()) { + case WATER: + case STILL_WATER: // Drowing + if($this->fire > 0 and $this->inBlock($pos)){ + $this->fire = 0; + $this->updateMetadata(); + } + if($this->air <= 0){ + $this->harm(2, "water"); + $hasUpdate = true; + } elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and ! $waterDone){ + $this->air -= 1; + $waterDone = true; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case LAVA: // Lava damage + case STILL_LAVA: + if($this->inBlock($pos)){ + $this->harm(5, "lava"); + $this->fire = 300; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case FIRE: // Fire block damage + if($this->inBlock($pos)){ + $this->harm(1, "fire"); + $this->fire = 300; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case CACTUS: // Cactus damage + if($this->touchingBlock($pos)){ + $this->harm(1, "cactus"); + $hasUpdate = true; + } + break; + default: + if($this->inBlock($pos, 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ + $this->harm(1, "suffocation"); // Suffocation + $hasUpdate = true; + } elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){ + $this->air = 300; // Breathing + } + break; + } + } + } + } + } + + return $hasUpdate; + } - public function addVelocity($vX, $vY = 0, $vZ = 0) - { - if($vX instanceof Vector3){ - return $this->addVelocity($vX->x, $vX->y, $vX->z); - } - $this->speedX += $vX; - $this->speedY += $vY; - $this->speedZ += $vZ; - } - - public function isMoving() - { - return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedY > 0.01 || $this->speedY < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); - } - - public function setVelocity($vX, $vY = 0, $vZ = 0) - { - if($vX instanceof Vector3){ - return $this->setVelocity($vX->x, $vX->y, $vX->z); - } - $this->speedX = $vX; - $this->speedY = $vY; - $this->speedZ = $vZ; - } - - /** - * - * @param mixed $e - * Entity instance or EID - * @return string|false if failed - */ - public static function getNameOf($e) - { - if($e instanceof Entity){ - return $e->getName(); - } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ - return $e->getName(); - } - return false; - } - - /** - * - * @param mixed $e - * Entity instance or EID - * @return number|false if failed - */ - public static function getHeightOf($e) - { - if($e instanceof Entity){ - return $e->getHeight(); - } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ - return $e->getHeight(); - } - return false; - } - - /** - * - * @param mixed $e - * Entity instance or EID - * @return number|false if failed - */ - public static function getWidthOf($e) - { - if($e instanceof Entity){ - return $e->getWidth(); - } elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){ - return $e->getWidth(); - } - return false; - } - - /** - * Get an entity height - * - * @return number - */ - public function getHeight($a) - { - return $this->height; - } - - /** - * Get an entity width - * - * @return number - */ - public function getWidth() - { - return $this->width; - } - - public function lookOn(Vector3 $target) - { - $horizontal = sqrt(pow(($target->x - $this->x), 2) + pow(($target->z - $this->z), 2)); - $vertical = $target->y - ($this->y + - 0.5); /* 0.5 = $entity->getEyeHeight() */ - $pitch = - asin($horizontal) / M_PI * 180; // negative is up, positive is down - - $xDist = $target->x - $this->x; - $zDist = $target->z - $this->z; - - $yaw = atan2($zDist, $xDist) / M_PI * 180 - 90; - if($yaw < 0){ - $yaw += 360.0; - } - $this->yaw = $yaw; - $this->pitch = $pitch; - $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); - } - - public function getDrops() - { - if($this->class === ENTITY_PLAYER and $this->player instanceof Player and ($this->player->gamemode & 0x01) === 0){ - $inv = []; - for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++ $i){ - $slot = $this->player->getSlot($i); - $this->player->setSlot($i, BlockAPI::getItem(AIR, 0, 0)); - if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array( - $slot->getID(), - $slot->getMetadata(), - $slot->count - ); - } - } - for($re = 0; $re < 4; $re ++){ - $slot = $this->player->getArmor($re); - $this->player->setArmor($re, BlockAPI::getItem(AIR, 0, 0)); - if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array( - $slot->getID(), - $slot->getMetadata(), - $slot->count - ); - } - } - return $inv; - } - return []; - } - - private function spawnDrops() - { - foreach($this->getDrops() as $drop){ - $this->server->api->entity->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF), true); - } - } - - public function environmentUpdate() - { - $hasUpdate = Entity::$updateOnTick ? $this->class === ENTITY_MOB : false; // force true for mobs - $time = microtime(true); - if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $this->player->blocked !== true && ! $this->dead){ - foreach($this->server->api->entity->getRadius($this, 1, ENTITY_ITEM) as $item){ - if(! $item->closed && $item->spawntime > 0 && ($time - $item->spawntime) >= 0.6){ - if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array( - "eid" => $this->player->eid, - "player" => $this->player, - "entity" => $item, - "block" => $item->type, - "meta" => $item->meta, - "target" => $item->eid - )) !== false){ - $item->close(); - // $item->spawntime = 0; - // $this->server->schedule(15, array($item, "close")); - } - } - } - } elseif($this->class === ENTITY_ITEM){ - if(($time - $this->spawntime) >= 300){ - $this->close(); // Despawn timer - return false; - } - } elseif($this->class === ENTITY_OBJECT and $this->type === OBJECT_SNOWBALL){ - if(($time - $this->spawntime) >= 60){ - $this->close(); - return false; - } - } - - if($this->class !== ENTITY_PLAYER and ($this->x <= 0 or $this->z <= 0 or $this->x >= 256 or $this->z >= 256 or $this->y >= 128 or $this->y <= 0)){ - $this->close(); - return false; - } - - if($this->dead === true){ - $this->fire = 0; - $this->air = 300; - return false; - } - if($this->isInVoid()){ - $this->outOfWorld(); - $hasUpdate = true; - } - - if($this->fire > 0){ - if(($this->fire % 20) === 0){ - $this->harm(1, "burning"); - } - $this->fire -= 10; - if($this->fire <= 0){ - $this->fire = 0; - $this->updateMetadata(); - } else{ - $hasUpdate = true; - } - if(($this->player instanceof Player) and ($this->player->gamemode & 0x01) === CREATIVE){ // Remove fire effects in next tick - $this->fire = 1; - } - } - - $startX = (int) (round($this->x - 0.5) - 1); - $startY = (int) (round($this->y) - 1); - $startZ = (int) (round($this->z - 0.5) - 1); - $endX = $startX + 2; - $endY = $startY + 2; - $endZ = $startZ + 2; - $waterDone = false; - if($this->isPlayer()){ - for($y = $startY; $y <= $endY; ++ $y){ - for($x = $startX; $x <= $endX; ++ $x){ - for($z = $startZ; $z <= $endZ; ++ $z){ - $pos = new Vector3($x, $y, $z); - $b = $this->level->getBlock($pos); - switch($b->getID()) { - case WATER: - case STILL_WATER: // Drowing - if($this->fire > 0 and $this->inBlock($pos)){ - $this->fire = 0; - $this->updateMetadata(); - } - if($this->air <= 0){ - $this->harm(2, "water"); - $hasUpdate = true; - } elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and ! $waterDone){ - $this->air -= 1; - $waterDone = true; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case LAVA: // Lava damage - case STILL_LAVA: - if($this->inBlock($pos)){ - $this->harm(5, "lava"); - $this->fire = 300; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case FIRE: // Fire block damage - if($this->inBlock($pos)){ - $this->harm(1, "fire"); - $this->fire = 300; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case CACTUS: // Cactus damage - if($this->touchingBlock($pos)){ - $this->harm(1, "cactus"); - $hasUpdate = true; - } - break; - default: - if($this->inBlock($pos, 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ - $this->harm(1, "suffocation"); // Suffocation - $hasUpdate = true; - } elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){ - $this->air = 300; // Breathing - } - break; - } - } - } - } - } - - return $hasUpdate; - } - - public function isInVoid(){ - return $this->y < -1.6; - } + public function isInVoid(){ + return $this->y < -1.6; + } - public function update(){ - if($this->closed === true){ - return false; - } - $now = microtime(true); - if($this->check === false){ - $this->lastUpdate = $now; - return; - } - $tdiff = $now - $this->lastUpdate; + public function update(){ + if($this->closed === true){ + return false; + } + $now = microtime(true); + if($this->check === false){ + $this->lastUpdate = $now; + return; + } + $tdiff = $now - $this->lastUpdate; - if($this->tickCounter === 0){ - $this->tickCounter = 1; - $hasUpdate = $this->environmentUpdate(); - } else{ - $hasUpdate = true; - $this->tickCounter = 0; - } + if($this->tickCounter === 0){ + $this->tickCounter = 1; + $hasUpdate = $this->environmentUpdate(); + } else{ + $hasUpdate = true; + $this->tickCounter = 0; + } - if($this->closed === true){ - return false; - } + if($this->closed === true){ + return false; + } - if($this->isStatic === false){ - $startX = floor($this->x - 0.5 - $this->width - 1); - // prefix for flying when player on fence - $y = floor($this->y - 1); - $yC = ceil($this->y - 1); - $startZ = floor($this->z - 0.5 - $this->width - 1); - $endX = ceil($this->x - 0.5 + $this->width + 1); - $endZ = ceil($this->z - 0.5 + $this->width + 1); - $support = false; - $isFlying = true; - if($this->isPlayer()){ - for($z = $startZ; $z <= $endZ; ++ $z){ - for($x = $startX; $x <= $endX; ++ $x){ - $v = new Vector3($x, $y, $z); - $v1 = new Vector3($x, $yC, $z); - if($this->isSupport($v, $this->width)){ - $b = $this->level->getBlock($v); - if($b->isSolid === true){ - $support = true; - $isFlying = false; - break; - } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ - $isFlying = false; - } - } elseif($this->isSupport($v1, $this->width)){ - $b = $this->level->getBlock($v1); - if($b->isSolid === true){ - $support = true; - $isFlying = false; - break; - } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ - $isFlying = false; - } - } - } - if($support === true){ - break; - } - } - } - - if(!$this->isPlayer()){ - $update = false; - $drag = 0.2; - if(Utils::in_range($this->speedX, - 0.01, 0.01)){ - $this->speedX = 0; - } - if(Utils::in_range($this->speedZ, - 0.01, 0.01)){ - $this->speedZ = 0; - } - if(Utils::in_range($this->speedY, - 0.0007, 0.007)){ - $this->speedY = 0; - } - - if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)){ - $aABB = $this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ); - $x0 = floor($aABB->minX); - $x1 = ceil($aABB->maxX); - $y0 = floor($aABB->minY); - $y1 = round($aABB->maxY); - $z0 = floor($aABB->minZ); - $z1 = ceil($aABB->maxZ); - $x0 = $x0 < 0 ? 0 : $x0; - $y0 = $y0 < 0 ? 0 : $y0; - $z0 = $z0 < 0 ? 0 : $z0; - $x1 = $x1 > 256 ? 256 : $x1; - $y1 = $y1 > 128 ? 128 : $y1; - $z1 = $z1 > 256 ? 256 : $z1; - $waterDone = false; - for($x = $x0; $x < $x1; ++ $x){ - for($y = $y0; $y < $y1; ++ $y){ - for($z = $z0; $z < $z1; ++ $z){ - $pos = new Vector3($x, $y, $z); - $b = $this->level->getBlock($pos); - switch($b->getID()) { - case WATER: - case STILL_WATER: // Drowing - if($this->fire > 0 and $this->inBlock($pos)){ - $this->fire = 0; - $this->updateMetadata(); - } - if($this->air <= 0){ - $this->harm(2, "water"); - $hasUpdate = true; - } elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and ! $waterDone){ - $this->air -= 1; - $waterDone = true; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case LAVA: // Lava damage - case STILL_LAVA: - if($this->inBlock($pos)){ - $this->harm(5, "lava"); - $this->fire = 300; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case FIRE: // Fire block damage - if($this->inBlock($pos)){ - $this->harm(1, "fire"); - $this->fire = 300; - $this->updateMetadata(); - $hasUpdate = true; - } - break; - case CACTUS: // Cactus damage - if($this->touchingBlock($pos)){ - $this->harm(1, "cactus"); - $hasUpdate = true; - } - break; - default: - if($this->inBlock($pos, 0.7) and $y == $y1 and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ - $this->harm(1, "suffocation"); // Suffocation - $hasUpdate = true; - } elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1)){ - $this->air = 300; // Breathing - } - break; - } - if($b != false && $b->boundingBox->intersectsWith($aABB) && $b->isSolid && $this->isMoving()){ - $this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX); - $this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ); - $lastY = $this->speedY; - $this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY); - if($this->speedY == 0 && $lastY > 0){ - $support = true; - } - } - } - } - } - - } - if($this->speedX != 0){ - $this->x += $this->speedX; - $this->speedX -= $this->speedX * $drag; - $update = true; - } - if($this->speedZ != 0){ - $this->z += $this->speedZ; - $this->speedZ -= $this->speedZ * $drag; - $update = true; - } - if($this->speedY != 0){ - $ny = $this->y + $this->speedY; - if($ny <= $this->y){ - $x = (int) ($this->x - 0.5); - $z = (int) ($this->z - 0.5); - $lim = (int) floor($ny); - for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ - - if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){ - $support = true; - if($this->class === ENTITY_FALLING){ - $this->y = $ny; - $fall = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y)), intval($this->z - 0.5))); - $down = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y) - 1), intval($this->z - 0.5))); - if($fall->isFullBlock === false or $down->isFullBlock === false){ - $this->server->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"] & 0xFFFF, 0, 1), true); - } else{ - $this->level->setBlock($fall, BlockAPI::get($this->data["Tile"]), true, false, true); - } - $this->server->api->handle("entity.motion", $this); - $this->close(); - return false; - } - break; - } - } - } - $this->y = $ny; - $this->speedY -= $this->speedY * $drag; - $update = true; - } - $this->onGround = $support; - if($this->hasGravity && $support === false){ - $this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity - $update = true; - } elseif($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ - // $this->speedX = 0; - // $this->speedY = 0; - // $this->speedZ = 0; - $this->server->api->handle("entity.move", $this); - $update = true; - }elseif($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch){ + if($this->isStatic === false){ + $startX = floor($this->x - 0.5 - $this->width - 1); + // prefix for flying when player on fence + $y = floor($this->y - 1); + $yC = ceil($this->y - 1); + $startZ = floor($this->z - 0.5 - $this->width - 1); + $endX = ceil($this->x - 0.5 + $this->width + 1); + $endZ = ceil($this->z - 0.5 + $this->width + 1); + $support = false; + $isFlying = true; + if($this->isPlayer()){ + for($z = $startZ; $z <= $endZ; ++ $z){ + for($x = $startX; $x <= $endX; ++ $x){ + $v = new Vector3($x, $y, $z); + $v1 = new Vector3($x, $yC, $z); + if($this->isSupport($v, $this->width)){ + $b = $this->level->getBlock($v); + if($b->isSolid === true){ + $support = true; + $isFlying = false; + break; + } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ + $isFlying = false; + } + } elseif($this->isSupport($v1, $this->width)){ + $b = $this->level->getBlock($v1); + if($b->isSolid === true){ + $support = true; + $isFlying = false; + break; + } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ + $isFlying = false; + } + } + } + if($support === true){ + break; + } + } + } + + if(!$this->isPlayer()){ + $update = false; + $drag = 0.2; + if(Utils::in_range($this->speedX, -0.01, 0.01)){ + $this->speedX = 0; + } + if(Utils::in_range($this->speedZ, -0.01, 0.01)){ + $this->speedZ = 0; + } + if(Utils::in_range($this->speedY, -0.007, 0.007)){ + $this->speedY = 0; + } + + if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)){ + $aABB = $this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ); + $x0 = floor($aABB->minX); + $x1 = ceil($aABB->maxX); + $y0 = floor($aABB->minY); + $y1 = round($aABB->maxY); + $z0 = floor($aABB->minZ); + $z1 = ceil($aABB->maxZ); + $x0 = $x0 < 0 ? 0 : $x0; + $y0 = $y0 < 0 ? 0 : $y0; + $z0 = $z0 < 0 ? 0 : $z0; + $x1 = $x1 > 256 ? 256 : $x1; + $y1 = $y1 > 128 ? 128 : $y1; + $z1 = $z1 > 256 ? 256 : $z1; + $waterDone = false; + for($x = $x0; $x < $x1; ++ $x){ + for($y = $y0; $y < $y1; ++ $y){ + for($z = $z0; $z < $z1; ++ $z){ + $pos = new Vector3($x, $y, $z); + $b = $this->level->getBlock($pos); + switch($b->getID()) { + case WATER: + case STILL_WATER: // Drowing + if($this->fire > 0 and $this->inBlock($pos)){ + $this->fire = 0; + $this->updateMetadata(); + } + if($this->air <= 0){ + $this->harm(2, "water"); + $hasUpdate = true; + } elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and ! $waterDone){ + $this->air -= 1; + $waterDone = true; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case LAVA: // Lava damage + case STILL_LAVA: + if($this->inBlock($pos)){ + $this->harm(5, "lava"); + $this->fire = 300; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case FIRE: // Fire block damage + if($this->inBlock($pos)){ + $this->harm(1, "fire"); + $this->fire = 300; + $this->updateMetadata(); + $hasUpdate = true; + } + break; + case CACTUS: // Cactus damage + if($this->touchingBlock($pos)){ + $this->harm(1, "cactus"); + $hasUpdate = true; + } + break; + default: + if($this->inBlock($pos, 0.7) and $y == $y1 and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ + $this->harm(1, "suffocation"); // Suffocation + $hasUpdate = true; + } elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1)){ + $this->air = 300; // Breathing + } + break; + } + if($b != false && $b->boundingBox->intersectsWith($aABB) && $b->isSolid && $this->isMoving()){ + $this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX); + $this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ); + $lastY = $this->speedY; + $this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY); + if($this->speedY == 0 && $lastY > 0){ + $support = true; + } + } + } + } + } + + } + if($this->speedX != 0){ + $this->x += $this->speedX; + $this->speedX -= $this->speedX * $drag; + $update = true; + } + if($this->speedZ != 0){ + $this->z += $this->speedZ; + $this->speedZ -= $this->speedZ * $drag; + $update = true; + } + if($this->speedY != 0){ + $ny = $this->y + $this->speedY; + if($ny <= $this->y){ + $x = (int) ($this->x - 0.5); + $z = (int) ($this->z - 0.5); + $lim = (int) floor($ny); + for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ + + if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){ + $support = true; + if($this->class === ENTITY_FALLING){ + $this->y = $ny; + $fall = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y)), intval($this->z - 0.5))); + $down = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y) - 1), intval($this->z - 0.5))); + if($fall->isFullBlock === false or $down->isFullBlock === false){ + $this->server->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"] & 0xFFFF, 0, 1), true); + } else{ + $this->level->setBlock($fall, BlockAPI::get($this->data["Tile"]), true, false, true); + } + $this->server->api->handle("entity.motion", $this); + $this->close(); + return false; + } + break; + } + } + } + $this->y = $ny; + + $this->speedY -= $this->speedY * $drag; + $update = true; + } + if($this->speedY == 0){ + $support = true; + } + $this->onGround = $support; + if($this->hasGravity && $support === false){ + $this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity + $update = true; + } elseif($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ + // $this->speedX = 0; + // $this->speedY = 0; + // $this->speedZ = 0; + $this->server->api->handle("entity.move", $this); + $update = true; + }elseif($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch){ $update = true; } - if($update === true){ - $hasUpdate = true; - if(($this->server->ticks % 4 === 0 && $this->class === ENTITY_ITEM) || $this->class != ENTITY_ITEM){ //update item speed every 4 ticks - $this->server->api->handle("entity.motion", $this); - } - } - } elseif($this->player instanceof Player){ - if($isFlying === true and ($this->player->gamemode & 0x01) === 0x00){ - if($this->fallY === false or $this->fallStart === false){ - $this->fallY = $y; - $this->fallStart = microtime(true); - } elseif($this->class === ENTITY_PLAYER and ($this->fallStart + 5) < microtime(true)){ - if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true and $this->isRiding === false){ - $this->player->close("flying"); - return; - } - } elseif($y > $this->fallY){ - $this->fallY = $y; - } - } elseif($this->fallY !== false){ // Fall damage! - if($y < $this->fallY){ - $blockToFallOn = $this->level->getBlock(new Vector3($x, $y, $z)); - if($blockToFallOn->getID() === FARMLAND){ - $this->level->setBlock($blockToFallOn, new DirtBlock(), true, false, true); - } + if($update === true){ + $hasUpdate = true; + if(($this->server->ticks % 4 === 0 && $this->class === ENTITY_ITEM) || $this->class != ENTITY_ITEM){ //update item speed every 4 ticks + $this->server->api->handle("entity.motion", $this); + } + } + } elseif($this->player instanceof Player){ + if($isFlying === true and ($this->player->gamemode & 0x01) === 0x00){ + if($this->fallY === false or $this->fallStart === false){ + $this->fallY = $y; + $this->fallStart = microtime(true); + } elseif($this->class === ENTITY_PLAYER and ($this->fallStart + 5) < microtime(true)){ + if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true and $this->isRiding === false){ + $this->player->close("flying"); + return; + } + } elseif($y > $this->fallY){ + $this->fallY = $y; + } + } elseif($this->fallY !== false){ // Fall damage! + if($y < $this->fallY){ + $blockToFallOn = $this->level->getBlock(new Vector3($x, $y, $z)); + if($blockToFallOn->getID() === FARMLAND){ + $this->level->setBlock($blockToFallOn, new DirtBlock(), true, false, true); + } - $d = $this->level->getBlock(new Vector3($x, $y + 1, $z)); - $d2 = $this->level->getBlock(new Vector3($x, $y + 2, $z)); - $dmg = ($this->fallY - $y) - 3; - if($dmg > 0 and ! ($d instanceof LiquidBlock) and $d->getID() !== LADDER and $d->getID() !== COBWEB and ! ($d2 instanceof LiquidBlock) and $d2->getID() !== LADDER and $d2->getID() !== COBWEB){ - $this->harm($dmg, "fall"); - } - } - $this->fallY = false; - $this->fallStart = false; - } - $this->calculateVelocity(); - if($this->speed <= 9 or ($this->speed <= 20 and ($this->player->gamemode & 0x01) === 0x01)){ - $this->player->lastCorrect = new Vector3($this->last[0], $this->last[1], $this->last[2]); - } - } - } - if($this->moveTime > 0){ - -- $this->moveTime; - } - if($this->lookTime > 0){ - -- $this->lookTime; - } - if($this->idleTime > 0){ - -- $this->idleTime; - } + $d = $this->level->getBlock(new Vector3($x, $y + 1, $z)); + $d2 = $this->level->getBlock(new Vector3($x, $y + 2, $z)); + $dmg = ($this->fallY - $y) - 3; + if($dmg > 0 and ! ($d instanceof LiquidBlock) and $d->getID() !== LADDER and $d->getID() !== COBWEB and ! ($d2 instanceof LiquidBlock) and $d2->getID() !== LADDER and $d2->getID() !== COBWEB){ + $this->harm($dmg, "fall"); + } + } + $this->fallY = false; + $this->fallStart = false; + } + $this->calculateVelocity(); + if($this->speed <= 9 or ($this->speed <= 20 and ($this->player->gamemode & 0x01) === 0x01)){ + $this->player->lastCorrect = new Vector3($this->last[0], $this->last[1], $this->last[2]); + } + } + } + if($this->moveTime > 0){ + -- $this->moveTime; + } + if($this->lookTime > 0){ + -- $this->lookTime; + } + if($this->idleTime > 0){ + -- $this->idleTime; + } - if($this->class !== ENTITY_PLAYER && $update){ - $this->updateMovement(); - } - $this->needsUpdate = $hasUpdate; - $this->lastUpdate = $now; - } + if($this->class !== ENTITY_PLAYER && $update){ + $this->updateMovement(); + } + $this->needsUpdate = $hasUpdate; + $this->lastUpdate = $now; + } - public function updateMovement() - { - if($this->closed === true){ - return false; - } - $now = microtime(true); - if($this->isStatic === false and ($this->last[0] != $this->x or $this->last[1] != $this->y or $this->last[2] != $this->z or $this->last[3] != $this->yaw or $this->last[4] != $this->pitch)){ - if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){ - if($this->server->api->handle("entity.move", $this) === false){ - if($this->class === ENTITY_PLAYER){ - $this->player->teleport(new Vector3($this->last[0], $this->last[1], $this->last[2]), $this->last[3], $this->last[4]); - } else{ - $this->setPosition($this->last[0], $this->last[1], $this->last[2], $this->last[3], $this->last[4]); - } - } else{ - $this->updateLast(); - $players = $this->server->api->player->getAll($this->level); - if($this->player instanceof Player){ - unset($players[$this->player->CID]); - $pk = new MovePlayerPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->bodyYaw = $this->yaw; - $this->server->api->player->broadcastPacket($players, $pk); - } else{ - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $this->server->api->player->broadcastPacket($players, $pk); - } - } - } else{ - $this->updatePosition(); - if($this->lastHeadYaw != $this->headYaw){ - $this->sendHeadYaw(); - } - $this->updateLast(); - } - } + public function updateMovement() + { + if($this->closed === true){ + return false; + } + $now = microtime(true); + if($this->isStatic === false and ($this->last[0] != $this->x or $this->last[1] != $this->y or $this->last[2] != $this->z or $this->last[3] != $this->yaw or $this->last[4] != $this->pitch)){ + if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){ + if($this->server->api->handle("entity.move", $this) === false){ + if($this->class === ENTITY_PLAYER){ + $this->player->teleport(new Vector3($this->last[0], $this->last[1], $this->last[2]), $this->last[3], $this->last[4]); + } else{ + $this->setPosition($this->last[0], $this->last[1], $this->last[2], $this->last[3], $this->last[4]); + } + } else{ + $this->updateLast(); + $players = $this->server->api->player->getAll($this->level); + if($this->player instanceof Player){ + unset($players[$this->player->CID]); + $pk = new MovePlayerPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->bodyYaw = $this->yaw; + $this->server->api->player->broadcastPacket($players, $pk); + } else{ + $pk = new MoveEntityPacket_PosRot(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $this->server->api->player->broadcastPacket($players, $pk); + } + } + } else{ + $this->updatePosition(); + if($this->lastHeadYaw != $this->headYaw){ + $this->sendHeadYaw(); + } + $this->updateLast(); + } + } - $this->lastUpdate = $now; - } - - /** - * Handle fall out of world - */ - public function outOfWorld(){ - if($this->isPlayer()){ - $this->health = 0; - $this->makeDead("void"); - }else{ - $this->close(); - } - } - - public function getEyeHeight(){ - return $this->isPlayer() ? 1.62 : $this->width; - } - - public function interactWith(Entity $e, $action) - { - if($this->class === ENTITY_PLAYER and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($e->player->gamemode & 0x01) === 0x01)){ - return false; - } + $this->lastUpdate = $now; + } + + /** + * Handle fall out of world + */ + public function outOfWorld(){ + if($this->isPlayer()){ + $this->health = 0; + $this->makeDead("void"); + }else{ + $this->close(); + } + } + + public function getEyeHeight(){ + return $this->isPlayer() ? 1.62 : $this->width; + } + + public function interactWith(Entity $e, $action) + { + if($this->class === ENTITY_PLAYER and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($e->player->gamemode & 0x01) === 0x01)){ + return false; + } - if($action === InteractPacket::ACTION_ATTACK && $e->isPlayer()){ - $slot = $e->player->getHeldItem(); - $damage = $slot->getDamageAgainstOf($e); - $this->harm($damage, $e->eid); - if($slot->isTool() === true and ($e->player->gamemode & 0x01) === 0){ - if($slot->useOn($e) and $slot->getMetadata() >= $slot->getMaxDurability()){ - $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); - } - } - return true; - } - return false; - } + if($action === InteractPacket::ACTION_ATTACK && $e->isPlayer()){ + $slot = $e->player->getHeldItem(); + $damage = $slot->getDamageAgainstOf($e); + $this->harm($damage, $e->eid); + if($slot->isTool() === true and ($e->player->gamemode & 0x01) === 0){ + if($slot->useOn($e) and $slot->getMetadata() >= $slot->getMaxDurability()){ + $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); + } + } + return true; + } + return false; + } - public function getDirection() - { - $rotation = ($this->yaw - 90) % 360; - if($rotation < 0){ - $rotation += 360.0; - } - if((0 <= $rotation and $rotation < 45) or (315 <= $rotation and $rotation < 360)){ - return 2; // North - } elseif(45 <= $rotation and $rotation < 135){ - return 3; // East - } elseif(135 <= $rotation and $rotation < 225){ - return 0; // South - } elseif(225 <= $rotation and $rotation < 315){ - return 1; // West - } else{ - return null; - } - } + public function getDirection() + { + $rotation = ($this->yaw - 90) % 360; + if($rotation < 0){ + $rotation += 360.0; + } + if((0 <= $rotation and $rotation < 45) or (315 <= $rotation and $rotation < 360)){ + return 2; // North + } elseif(45 <= $rotation and $rotation < 135){ + return 3; // East + } elseif(135 <= $rotation and $rotation < 225){ + return 0; // South + } elseif(225 <= $rotation and $rotation < 315){ + return 1; // West + } else{ + return null; + } + } - /* - * METADATA VALUES(EXPEREMENTAL) - * ***************** - * Types: Get input type of - * 0 -> Byte - * 1 -> Short - * 2 -> Integer - * 3 -> Float - * 4 -> Length of , Short - * 5 -> [Short, Byte, Short] - * 6 -> [Integer, Integer, Integer] - * ***************** - * 0 => ["type" => 0, "value" => $flags] --> DATA_FLAGS - * 1 => ["type" => 1, "value" => $this->air] --> Entity Air - * 14 => ["type" => 0, "value" => 1] --> IsBaby, value: 0 => false, 1 => true - * 16 => ["type" => 0, "value" => 0] --> Fuse(TNT), Saddled(Pig), Creeper(29 ticks before explosion) - * 17 => ["type" => 6, "value" => [0, 0, 0]] --> Bed Position - * - * - * DATA FLAGS IDS - * 0 - fire - * 1 - crouching - * 2 - riding - no idea why doesnt it work - * 4 - inAction(ex.: using a bow) - */ - public function getMetadata() - { - $flags = 0; - $flags ^= $this->fire > 0 ? 0b1 : 0; - $flags ^= ($this->crouched ? 0b1 : 0) << 1; - $flags ^= ($this->inAction ? 0b1 : 0) << 4; - $d = [ - 0 => [ - "type" => 0, - "value" => $flags - ], - 1 => [ - "type" => 1, - "value" => $this->air - ], - 14 => [ - "type" => 0, - "value" => 0 - ], - 16 => [ - "type" => 0, - "value" => 0 - ], - 17 => [ - "type" => 6, - "value" => [ - 0, - 0, - 0 - ] - ] - ]; - $d[16]["value"] = $this->data["State"]; - if($this->class === ENTITY_PLAYER){ - if($this->player->isSleeping !== false){ - $d[16]["value"] = 2; - $d[17]["value"] = [ - $this->player->isSleeping->x, - $this->player->isSleeping->y, - $this->player->isSleeping->z - ]; - } - } - return $d; - } + /* + * METADATA VALUES(EXPEREMENTAL) + * ***************** + * Types: Get input type of + * 0 -> Byte + * 1 -> Short + * 2 -> Integer + * 3 -> Float + * 4 -> Length of , Short + * 5 -> [Short, Byte, Short] + * 6 -> [Integer, Integer, Integer] + * ***************** + * 0 => ["type" => 0, "value" => $flags] --> DATA_FLAGS + * 1 => ["type" => 1, "value" => $this->air] --> Entity Air + * 14 => ["type" => 0, "value" => 1] --> IsBaby, value: 0 => false, 1 => true + * 16 => ["type" => 0, "value" => 0] --> Fuse(TNT), Saddled(Pig), Creeper(29 ticks before explosion) + * 17 => ["type" => 6, "value" => [0, 0, 0]] --> Bed Position + * + * + * DATA FLAGS IDS + * 0 - fire + * 1 - crouching + * 2 - riding - no idea why doesnt it work + * 4 - inAction(ex.: using a bow) + */ + public function getMetadata() + { + $flags = 0; + $flags ^= $this->fire > 0 ? 0b1 : 0; + $flags ^= ($this->crouched ? 0b1 : 0) << 1; + $flags ^= ($this->inAction ? 0b1 : 0) << 4; + $d = [ + 0 => [ + "type" => 0, + "value" => $flags + ], + 1 => [ + "type" => 1, + "value" => $this->air + ], + 14 => [ + "type" => 0, + "value" => 0 + ], + 16 => [ + "type" => 0, + "value" => 0 + ], + 17 => [ + "type" => 6, + "value" => [ + 0, + 0, + 0 + ] + ] + ]; + $d[16]["value"] = $this->data["State"]; + if($this->class === ENTITY_PLAYER){ + if($this->player->isSleeping !== false){ + $d[16]["value"] = 2; + $d[17]["value"] = [ + $this->player->isSleeping->x, + $this->player->isSleeping->y, + $this->player->isSleeping->z + ]; + } + } + return $d; + } - public function updateMetadata() - { - $this->server->api->dhandle("entity.metadata", $this); - } + public function updateMetadata() + { + $this->server->api->dhandle("entity.metadata", $this); + } - public function spawn($player) - { - if(! ($player instanceof Player)){ - $player = $this->server->api->player->get($player); - } - if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ - return false; - } - switch($this->class) { - case ENTITY_PLAYER: - if($this->player->connected !== true or $this->player->spawned === false){ - return false; - } - if($this->player->gamemode === SPECTATOR){ - break; - } - $pk = new AddPlayerPacket(); - $pk->clientID = 0; // $this->player->clientID; - $pk->username = $this->player->username; - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = 0; - $pk->pitch = 0; - $pk->itemID = 0; - $pk->itemAuxValue = 0; - $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); + public function spawn($player) + { + if(! ($player instanceof Player)){ + $player = $this->server->api->player->get($player); + } + if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ + return false; + } + switch($this->class) { + case ENTITY_PLAYER: + if($this->player->connected !== true or $this->player->spawned === false){ + return false; + } + if($this->player->gamemode === SPECTATOR){ + break; + } + $pk = new AddPlayerPacket(); + $pk->clientID = 0; // $this->player->clientID; + $pk->username = $this->player->username; + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = 0; + $pk->pitch = 0; + $pk->itemID = 0; + $pk->itemAuxValue = 0; + $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); + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); - $pk = new PlayerEquipmentPacket(); - $pk->eid = $this->eid; - $pk->item = $this->player->getSlot($this->player->slot)->getID(); - $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); - $pk->slot = 0; - $player->dataPacket($pk); - $this->player->sendArmor($player); - break; - case ENTITY_ITEM: - $pk = new AddItemEntityPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); + $pk = new PlayerEquipmentPacket(); + $pk->eid = $this->eid; + $pk->item = $this->player->getSlot($this->player->slot)->getID(); + $pk->meta = $this->player->getSlot($this->player->slot)->getMetadata(); + $pk->slot = 0; + $player->dataPacket($pk); + $this->player->sendArmor($player); + break; + case ENTITY_ITEM: + $pk = new AddItemEntityPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->roll = 0; + $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); + $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); - break; - case ENTITY_FALLING: - $pk = new AddEntityPacket(); - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->did = - $this->data["Tile"]; - $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); + break; + case ENTITY_FALLING: + $pk = new AddEntityPacket(); + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->did = - $this->data["Tile"]; + $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); - break; - } - } + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); + break; + } + } - public function close() - { - if($this->closed === false){ - $this->closed = true; - $this->server->api->entity->remove($this->eid); - } - } + public function close() + { + if($this->closed === false){ + $this->closed = true; + $this->server->api->entity->remove($this->eid); + } + } - public function __destruct() - { - $this->close(); - } + public function __destruct() + { + $this->close(); + } - public function getEID() - { - return $this->eid; - } + public function getEID() + { + return $this->eid; + } - public function getName() - { - return $this->name; - } + public function getName() + { + return $this->name; + } - public function setName($name) - { - $this->name = $name; - // $this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); is this neccessary for database? - } + public function setName($name) + { + $this->name = $name; + // $this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); is this neccessary for database? + } - public function look($pos2) - { - $pos = $this->getPosition(); - $angle = Utils::angle3D($pos2, $pos); - $this->yaw = $angle["yaw"]; - $this->pitch = $angle["pitch"]; - $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); - } + public function look($pos2) + { + $pos = $this->getPosition(); + $angle = Utils::angle3D($pos2, $pos); + $this->yaw = $angle["yaw"]; + $this->pitch = $angle["pitch"]; + $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); + } - public function move(Vector3 $pos, $yaw = 0, $pitch = 0) - { - $this->x += $pos->x; - $this->y += $pos->y; - $this->z += $pos->z; - $this->yaw += $yaw; - $this->yaw %= 360; - $this->pitch += $pitch; - $this->pitch %= 90; - $this->server->query("UPDATE entities SET x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); - $this->updateAABB(); - } + public function move(Vector3 $pos, $yaw = 0, $pitch = 0) + { + $this->x += $pos->x; + $this->y += $pos->y; + $this->z += $pos->z; + $this->yaw += $yaw; + $this->yaw %= 360; + $this->pitch += $pitch; + $this->pitch %= 90; + $this->server->query("UPDATE entities SET x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); + $this->updateAABB(); + } - public function updateAABB() - { - $this->boundingBox->setBounds($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); - } + public function updateAABB() + { + $this->boundingBox->setBounds($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); + } - public function updatePosition() - { - $this->server->query("UPDATE entities SET level = '" . $this->level->getName() . "', x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); - $this->sendMoveUpdate(); - // $this->sendMotion(); - $this->updateAABB(); - } - - public function sendHeadYaw(){ - $pk = new RotateHeadPacket; - $pk->eid = $this->eid; - $pk->yaw = $this->headYaw; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } - - public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) - { - if($pos instanceof Position and $pos->level instanceof Level and $this->level !== $pos->level){ - $this->level = $pos->level; - $this->server->preparedSQL->entity->setLevel->reset(); - $this->server->preparedSQL->entity->setLevel->clear(); - $this->server->preparedSQL->entity->setLevel->bindValue(":level", $this->level->getName(), SQLITE3_TEXT); - $this->server->preparedSQL->entity->setLevel->bindValue(":eid", $this->eid, SQLITE3_INTEGER); - $this->server->preparedSQL->entity->setLevel->execute(); - } - $this->x = $pos->x; - $this->y = $pos->y; - $this->z = $pos->z; - if($yaw !== false){ - $this->yaw = $yaw; - } - if($pitch !== false){ - $this->pitch = $pitch; - } - $this->server->preparedSQL->entity->setPosition->reset(); - $this->server->preparedSQL->entity->setPosition->clear(); - $this->server->preparedSQL->entity->setPosition->bindValue(":x", $this->x, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":y", $this->y, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":z", $this->z, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":pitch", $this->pitch, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":yaw", $this->yaw, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER); - $this->server->preparedSQL->entity->setPosition->execute(); - } + public function updatePosition() + { + $this->server->query("UPDATE entities SET level = '" . $this->level->getName() . "', x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); + $this->sendMoveUpdate(); + // $this->sendMotion(); + $this->updateAABB(); + } + + public function sendHeadYaw(){ + $pk = new RotateHeadPacket; + $pk->eid = $this->eid; + $pk->yaw = $this->headYaw; + $this->server->api->player->broadcastPacket($this->level->players, $pk); + } + + public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) + { + if($pos instanceof Position and $pos->level instanceof Level and $this->level !== $pos->level){ + $this->level = $pos->level; + $this->server->preparedSQL->entity->setLevel->reset(); + $this->server->preparedSQL->entity->setLevel->clear(); + $this->server->preparedSQL->entity->setLevel->bindValue(":level", $this->level->getName(), SQLITE3_TEXT); + $this->server->preparedSQL->entity->setLevel->bindValue(":eid", $this->eid, SQLITE3_INTEGER); + $this->server->preparedSQL->entity->setLevel->execute(); + } + $this->x = $pos->x; + $this->y = $pos->y; + $this->z = $pos->z; + if($yaw !== false){ + $this->yaw = $yaw; + } + if($pitch !== false){ + $this->pitch = $pitch; + } + $this->server->preparedSQL->entity->setPosition->reset(); + $this->server->preparedSQL->entity->setPosition->clear(); + $this->server->preparedSQL->entity->setPosition->bindValue(":x", $this->x, SQLITE3_TEXT); + $this->server->preparedSQL->entity->setPosition->bindValue(":y", $this->y, SQLITE3_TEXT); + $this->server->preparedSQL->entity->setPosition->bindValue(":z", $this->z, SQLITE3_TEXT); + $this->server->preparedSQL->entity->setPosition->bindValue(":pitch", $this->pitch, SQLITE3_TEXT); + $this->server->preparedSQL->entity->setPosition->bindValue(":yaw", $this->yaw, SQLITE3_TEXT); + $this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER); + $this->server->preparedSQL->entity->setPosition->execute(); + } - public function inBlock(Vector3 $block, $radius = 0.8) - { - $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); - if(($block->y == (ceil($this->y)) or $block->y == (ceil($this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ - return true; - } - return false; - } + public function inBlock(Vector3 $block, $radius = 0.8) + { + $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); + if(($block->y == (ceil($this->y)) or $block->y == (ceil($this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ + return true; + } + return false; + } - public function touchingBlock(Vector3 $block, $radius = 0.9) - { - $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); - if(($block->y == (((int) $this->y) - 1) or $block->y == ((int) $this->y) or $block->y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ - return true; - } - return false; - } + public function touchingBlock(Vector3 $block, $radius = 0.9) + { + $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); + if(($block->y == (((int) $this->y) - 1) or $block->y == ((int) $this->y) or $block->y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ + return true; + } + return false; + } - public function isSupport(Vector3 $pos, $radius = 1) - { - $me = new Vector2($this->x - 0.5, $this->z - 0.5); - $diff = $this->y - $pos->y; - if($me->distance(new Vector2($pos->x, $pos->z)) < $radius and $diff > - 0.7 and $diff < 1.6){ - return true; - } - return false; - } + public function isSupport(Vector3 $pos, $radius = 1) + { + $me = new Vector2($this->x - 0.5, $this->z - 0.5); + $diff = $this->y - $pos->y; + if($me->distance(new Vector2($pos->x, $pos->z)) < $radius and $diff > - 0.7 and $diff < 1.6){ + return true; + } + return false; + } - public function resetSpeed() - { - $this->speedMeasure = array( - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ); - } + public function resetSpeed() + { + $this->speedMeasure = array( + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ); + } - public function getSpeed() - { - return $this->speed; - } + public function getSpeed() + { + return $this->speed; + } - public function getSpeedMeasure() - { - return array_sum($this->speedMeasure) / count($this->speedMeasure); - } + public function getSpeedMeasure() + { + return array_sum($this->speedMeasure) / count($this->speedMeasure); + } - public function calculateVelocity() - { - $diffTime = max(0.05, abs(microtime(true) - $this->last[5])); - $origin = new Vector2($this->last[0], $this->last[2]); - $final = new Vector2($this->x, $this->z); - $speedX = ($this->last[0] - $this->x) / $diffTime; - $speedY = ($this->last[1] - $this->y) / $diffTime; - $speedZ = ($this->last[2] - $this->z) / $diffTime; - if($this->speedX != $speedX or $this->speedY != $speedY or $this->speedZ != $speedZ){ - $this->speedX = $speedX; - $this->speedY = $speedY; - $this->speedZ = $speedZ; - $this->server->api->handle("entity.motion", $this); - } - $this->speed = $origin->distance($final) / $diffTime; - unset($this->speedMeasure[key($this->speedMeasure)]); - $this->speedMeasure[] = $this->speed; - } - /** - * @return array - */ - public function createSaveData(){ - $data = [ - "id" => $this->type, - "Health" => $this->health, - "Pos" => [ - 0 => $this->x, - 1 => $this->y, - 2 => $this->z, - ], - "Rotation" => [ - 0 => $this->yaw, - 1 => $this->pitch, - ], - - ]; - if($this->class === ENTITY_OBJECT){ - $data["TileX"] = $this->x; - $data["TileY"] = $this->y; - $data["TileZ"] = $this->z; - } - if($this->class === ENTITY_FALLING){ - $data["Tile"] = $this->data["Tile"]; - } - if($this->class === ENTITY_ITEM){ - $data["Item"] = [ - "id" => $this->type, - "Damage" => $this->meta, - "Count" => $this->stack, - ]; - } - return $data; - } + public function calculateVelocity() + { + $diffTime = max(0.05, abs(microtime(true) - $this->last[5])); + $origin = new Vector2($this->last[0], $this->last[2]); + $final = new Vector2($this->x, $this->z); + $speedX = ($this->last[0] - $this->x) / $diffTime; + $speedY = ($this->last[1] - $this->y) / $diffTime; + $speedZ = ($this->last[2] - $this->z) / $diffTime; + if($this->speedX != $speedX or $this->speedY != $speedY or $this->speedZ != $speedZ){ + $this->speedX = $speedX; + $this->speedY = $speedY; + $this->speedZ = $speedZ; + $this->server->api->handle("entity.motion", $this); + } + $this->speed = $origin->distance($final) / $diffTime; + unset($this->speedMeasure[key($this->speedMeasure)]); + $this->speedMeasure[] = $this->speed; + } + /** + * @return array + */ + public function createSaveData(){ + $data = [ + "id" => $this->type, + "Health" => $this->health, + "Pos" => [ + 0 => $this->x, + 1 => $this->y, + 2 => $this->z, + ], + "Rotation" => [ + 0 => $this->yaw, + 1 => $this->pitch, + ], + + ]; + if($this->class === ENTITY_OBJECT){ + $data["TileX"] = $this->x; + $data["TileY"] = $this->y; + $data["TileZ"] = $this->z; + } + if($this->class === ENTITY_FALLING){ + $data["Tile"] = $this->data["Tile"]; + } + if($this->class === ENTITY_ITEM){ + $data["Item"] = [ + "id" => $this->type, + "Damage" => $this->meta, + "Count" => $this->stack, + ]; + } + return $data; + } - public function updateLast() - { - $this->last[0] = $this->x; - $this->last[1] = $this->y; - $this->last[2] = $this->z; - $this->last[3] = $this->yaw; - $this->last[4] = $this->pitch; - $this->last[5] = microtime(true); - $this->lastHeadYaw = $this->headYaw; - } + public function updateLast() + { + $this->last[0] = $this->x; + $this->last[1] = $this->y; + $this->last[2] = $this->z; + $this->last[3] = $this->yaw; + $this->last[4] = $this->pitch; + $this->last[5] = microtime(true); + $this->lastHeadYaw = $this->headYaw; + } - public function getPosition($round = false) - { - return ! isset($this->position) ? false : ($round === true ? array_map("floor", $this->position) : $this->position); - } + public function getPosition($round = false) + { + return ! isset($this->position) ? false : ($round === true ? array_map("floor", $this->position) : $this->position); + } - public function harm($dmg, $cause = "generic", $force = false) - { - if(!$this->canBeAttacked){ + public function harm($dmg, $cause = "generic", $force = false) + { + if(!$this->canBeAttacked){ return false; } $ret = $this->setHealth(max(- 128, $this->getHealth() - ((int) $dmg)), $cause, $force); if($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){ - $d = $entity->x - $this->x; - - for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01){ - $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; - } + $d = $entity->x - $this->x; + + for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01){ + $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; + } - // attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) > - $this->knockBack($d, $d1); + // attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) > + $this->knockBack($d, $d1); $this->sendMotion(); - } - + } + return $ret; - } + } - public function setState($v) - { - $this->state = $v; - $this->data["State"] = $v; - $this->updateMetadata(); - } + public function setState($v) + { + $this->state = $v; + $this->data["State"] = $v; + $this->updateMetadata(); + } - public function getState() - { - return $this->state; - } + public function getState() + { + return $this->state; + } - public function heal($health, $cause = "generic") - { - return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause); - } + public function heal($health, $cause = "generic") + { + return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause); + } - public function sendMotion() - { - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } + public function sendMotion() + { + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $this->server->api->player->broadcastPacket($this->level->players, $pk); + } - public function linkEntity(Entity $e, $type) - { - if($e->isPlayer()){ - $pk = new SetEntityLinkPacket(); - $pk->rider = $this->eid; - $pk->riding = $type === SetEntityLinkPacket::TYPE_REMOVE ? 0 : $e->eid; - $pk->type = $type; - $e->player->dataPacket($pk); - } - } + public function linkEntity(Entity $e, $type) + { + if($e->isPlayer()){ + $pk = new SetEntityLinkPacket(); + $pk->rider = $this->eid; + $pk->riding = $type === SetEntityLinkPacket::TYPE_REMOVE ? 0 : $e->eid; + $pk->type = $type; + $e->player->dataPacket($pk); + } + } - public function isPlayer() - { - return isset($this->player) && $this->player instanceof Player; - } + public function isPlayer() + { + return isset($this->player) && $this->player instanceof Player; + } - public function sendMoveUpdate() - { - if($this->class === ENTITY_PLAYER){ - $this->player->teleport(new Vector3($this->x, $this->y, $this->z)); - return; - } - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } + public function sendMoveUpdate() + { + if($this->class === ENTITY_PLAYER){ + $this->player->teleport(new Vector3($this->x, $this->y, $this->z)); + return; + } + $pk = new MoveEntityPacket_PosRot(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $this->server->api->player->broadcastPacket($this->level->players, $pk); + } - public function moveEntityWithOffset($oX, $oY, $oZ) - { - $oX = $oX === 0 ? $this->speedX : $this->getSpeedModifer() * $oX * $this->getSpeed(); - $oY = $oY === 0 ? $this->speedY : $this->getSpeedModifer() * $oY * $this->getSpeed(); - $oZ = $oZ === 0 ? $this->speedZ : $this->getSpeedModifer() * $oZ * $this->getSpeed(); - $this->setVelocity($oX, $oY, $oZ); - } + public function moveEntityWithOffset($oX, $oY, $oZ) + { + $oX = $oX === 0 ? $this->speedX : $this->getSpeedModifer() * $oX * $this->getSpeed(); + $oY = $oY === 0 ? $this->speedY : $this->getSpeedModifer() * $oY * $this->getSpeed(); + $oZ = $oZ === 0 ? $this->speedZ : $this->getSpeedModifer() * $oZ * $this->getSpeed(); + $this->setVelocity($oX, $oY, $oZ); + } - public function getSpeedModifer() - { - return $this->speedModifer; - } + public function getSpeedModifer() + { + return $this->speedModifer; + } - public function setHealth($health, $cause = "generic", $force = false) - { - $health = (int) $health; - $harm = false; - if($health < $this->health){ - $harm = true; - $dmg = $this->health - $health; - if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){ - $points = 0; - $values = array( - LEATHER_CAP => 1, - LEATHER_TUNIC => 3, - LEATHER_PANTS => 2, - LEATHER_BOOTS => 1, - CHAIN_HELMET => 1, - CHAIN_CHESTPLATE => 5, - CHAIN_LEGGINGS => 4, - CHAIN_BOOTS => 1, - GOLDEN_HELMET => 1, - GOLDEN_CHESTPLATE => 5, - GOLDEN_LEGGINGS => 3, - GOLDEN_BOOTS => 1, - IRON_HELMET => 2, - IRON_CHESTPLATE => 6, - IRON_LEGGINGS => 5, - IRON_BOOTS => 2, - DIAMOND_HELMET => 3, - DIAMOND_CHESTPLATE => 8, - DIAMOND_LEGGINGS => 6, - DIAMOND_BOOTS => 3 - ); - foreach($this->player->armor as $slot => $part){ - if($part instanceof Item and isset($values[$part->getID()])){ - if(is_numeric($cause)){ // check was entity damage by another entity - $points += $values[$part->getID()]; - $this->player->damageArmorPart($slot, $part); - } - } - } - $this->player->sendArmor($this->player); - $dmg = (int) ($dmg - ($dmg * $points * 0.04)); - $health = $this->health - $dmg; - } - if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and ! $this->dead){ - $this->dmgcounter[0] = microtime(true) + 0.5; - $this->dmgcounter[1] = $dmg; - } else{ - return false; // Entity inmunity - } - } elseif($health === $this->health and ! $this->dead){ - return false; - } - if($this->server->api->dhandle("entity.health.change", array( - "entity" => $this, - "eid" => $this->eid, - "health" => $health, - "cause" => $cause - )) !== false or $force === true){ - $this->health = min(127, max(- 127, $health)); - $this->server->query("UPDATE entities SET health = " . $this->health . " WHERE EID = " . $this->eid . ";"); - if($harm === true){ + public function setHealth($health, $cause = "generic", $force = false) + { + $health = (int) $health; + $harm = false; + if($health < $this->health){ + $harm = true; + $dmg = $this->health - $health; + if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){ + $points = 0; + $values = array( + LEATHER_CAP => 1, + LEATHER_TUNIC => 3, + LEATHER_PANTS => 2, + LEATHER_BOOTS => 1, + CHAIN_HELMET => 1, + CHAIN_CHESTPLATE => 5, + CHAIN_LEGGINGS => 4, + CHAIN_BOOTS => 1, + GOLDEN_HELMET => 1, + GOLDEN_CHESTPLATE => 5, + GOLDEN_LEGGINGS => 3, + GOLDEN_BOOTS => 1, + IRON_HELMET => 2, + IRON_CHESTPLATE => 6, + IRON_LEGGINGS => 5, + IRON_BOOTS => 2, + DIAMOND_HELMET => 3, + DIAMOND_CHESTPLATE => 8, + DIAMOND_LEGGINGS => 6, + DIAMOND_BOOTS => 3 + ); + foreach($this->player->armor as $slot => $part){ + if($part instanceof Item and isset($values[$part->getID()])){ + if(is_numeric($cause)){ // check was entity damage by another entity + $points += $values[$part->getID()]; + $this->player->damageArmorPart($slot, $part); + } + } + } + $this->player->sendArmor($this->player); + $dmg = (int) ($dmg - ($dmg * $points * 0.04)); + $health = $this->health - $dmg; + } + if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and ! $this->dead){ + $this->dmgcounter[0] = microtime(true) + 0.5; + $this->dmgcounter[1] = $dmg; + } else{ + return false; // Entity inmunity + } + } elseif($health === $this->health and ! $this->dead){ + return false; + } + if($this->server->api->dhandle("entity.health.change", array( + "entity" => $this, + "eid" => $this->eid, + "health" => $health, + "cause" => $cause + )) !== false or $force === true){ + $this->health = min(127, max(- 127, $health)); + $this->server->query("UPDATE entities SET health = " . $this->health . " WHERE EID = " . $this->eid . ";"); + if($harm === true){ $pk = new EntityEventPacket; $pk->eid = $this->eid; $pk->event = EntityEventPacket::ENTITY_DAMAGE; $this->server->api->player->broadcastPacket($this->level->players, $pk); - } - if($this->player instanceof Player){ - $pk = new SetHealthPacket(); - $pk->health = $this->health; - $this->player->dataPacket($pk); - } - if($this->health <= 0 and $this->dead === false){ - $this->makeDead($cause); - } elseif($this->health > 0){ - $this->dead = false; - } - return true; - } - return false; - } + } + if($this->player instanceof Player){ + $pk = new SetHealthPacket(); + $pk->health = $this->health; + $this->player->dataPacket($pk); + } + if($this->health <= 0 and $this->dead === false){ + $this->makeDead($cause); + } elseif($this->health > 0){ + $this->dead = false; + } + return true; + } + return false; + } - public function setSize($w, $h) - { - $this->width = $w; - $this->height = $h; - $this->radius = $w / 2; - } - - public function makeDead($cause){ - $this->spawnDrops(); - $this->air = 300; - $this->fire = 0; - $this->crouched = false; - $this->fallY = false; - $this->fallStart = false; - $this->updateMetadata(); - $this->dead = true; - if($this->player instanceof Player){ - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $this->eid; - $pk->x = - 256; - $pk->y = 128; - $pk->z = - 256; - $pk->yaw = 0; - $pk->pitch = 0; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - }else{ - $pk = new EntityEventPacket; - $pk->eid = $this->eid; - $pk->event = 3; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - $this->server->api->dhandle("entity.event", [ - "entity" => $this, - "event" => 3 - ]); // Entity dead - } - if($this->player instanceof Player){ - $this->player->blocked = true; - $this->server->api->dhandle("player.death", [ - "player" => $this->player, - "cause" => $cause - ]); - if($this->server->api->getProperty("hardcore") == 1){ //poor player =< - $this->server->api->ban->ban($this->player->username); - } - } else{ - $this->close(); - } - } - - public function setSpeed($s) - { - $this->speed = $s; - } + public function setSize($w, $h) + { + $this->width = $w; + $this->height = $h; + $this->radius = $w / 2; + } + + public function makeDead($cause){ + $this->spawnDrops(); + $this->air = 300; + $this->fire = 0; + $this->crouched = false; + $this->fallY = false; + $this->fallStart = false; + $this->updateMetadata(); + $this->dead = true; + if($this->player instanceof Player){ + $pk = new MoveEntityPacket_PosRot(); + $pk->eid = $this->eid; + $pk->x = - 256; + $pk->y = 128; + $pk->z = - 256; + $pk->yaw = 0; + $pk->pitch = 0; + $this->server->api->player->broadcastPacket($this->level->players, $pk); + }else{ + $pk = new EntityEventPacket; + $pk->eid = $this->eid; + $pk->event = 3; + $this->server->api->player->broadcastPacket($this->level->players, $pk); + $this->server->api->dhandle("entity.event", [ + "entity" => $this, + "event" => 3 + ]); // Entity dead + } + if($this->player instanceof Player){ + $this->player->blocked = true; + $this->server->api->dhandle("player.death", [ + "player" => $this->player, + "cause" => $cause + ]); + if($this->server->api->getProperty("hardcore") == 1){ //poor player =< + $this->server->api->ban->ban($this->player->username); + } + } else{ + $this->close(); + } + } + + public function setSpeed($s) + { + $this->speed = $s; + } - public function knockBack($d, $d1) - { - $f = sqrt($d * $d + $d1 * $d1); - $f1 = 0.4; - $this->speedX /= 2; - $this->speedZ /= 2; - $this->speedX -= ($d / (double) $f) * (double) $f1; - $this->speedY += 0.40000000596046448; - $this->speedZ -= ($d1 / (double) $f) * (double) $f1; - if($this->speedY > 0.40000000596046448){ - $this->speedY = 0.40000000596046448; - } - $this->speedY /= 2; - } + public function knockBack($d, $d1) + { + $f = sqrt($d * $d + $d1 * $d1); + $f1 = 0.4; + $this->speedX /= 2; + $this->speedZ /= 2; + $this->speedX -= ($d / (double) $f) * (double) $f1; + $this->speedY += 0.40000000596046448; + $this->speedZ -= ($d1 / (double) $f) * (double) $f1; + if($this->speedY > 0.40000000596046448){ + $this->speedY = 0.40000000596046448; + } + $this->speedY /= 2; + } - public function getHealth() - { - return $this->health; - } + public function getHealth() + { + return $this->health; + } - public function __toString() - { - return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; - } - - /** - * Debug - */ - public function printSpeed(){ - ConsoleAPI::debug("{$this->speedX}:{$this->speedY}:{$this->speedZ}"); - } - - /* - * Deprecated methods. - * Those methods were left only for compability with older plugins - */ - /** - * - * @deprecated Use {@link getHeightOf} or {@link getWidthOf} instead - * @throws Exception - */ - public static function getSizeOf($e) - { - throw new Exception("Use getHeightOf or getWidthOf method instead of this."); - } + public function __toString() + { + return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; + } + + /** + * Debug + */ + public function printSpeed($add = ""){ + ConsoleAPI::debug("$add {$this->speedX}:{$this->speedY}:{$this->speedZ}"); + } + + /* + * Deprecated methods. + * Those methods were left only for compability with older plugins + */ + /** + * + * @deprecated Use {@link getHeightOf} or {@link getWidthOf} instead + * @throws Exception + */ + public static function getSizeOf($e) + { + throw new Exception("Use getHeightOf or getWidthOf method instead of this."); + } - /** - * - * @deprecated Use {@link getHeight} or {@link getWidth} instead - * @throws Exception - */ - public function getSize() - { - throw new Exception("Use getHeight or getWidth method instead of this."); - } + /** + * + * @deprecated Use {@link getHeight} or {@link getWidth} instead + * @throws Exception + */ + public function getSize() + { + throw new Exception("Use getHeight or getWidth method instead of this."); + } } diff --git a/src/entity/Living.php b/src/entity/Living.php index c4957579..164fa10d 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -16,7 +16,6 @@ abstract class Living extends Entity implements Damageable{ } public function update(){ - if($this->idleTime <= 0) { $this->ai->updateTasks(); } diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index 4a1fb922..7c9ffca7 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -38,9 +38,13 @@ class EntityAI * @return TaskBase | false */ public function getTask($id){ - return $this->tasks[$id] ?: false; //i never saw this operator before + return isset($this->tasks[$id]) ? $this->tasks[$id] : false; } + public function isStarted($id){ + $task = @$this->getTask($id); + return $task instanceof TaskBase && $task->isStarted; + } public function updateTasks(){ foreach($this->tasks as $t){ diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index d6ee4f65..fa3066d5 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -6,7 +6,7 @@ class TaskLookAround extends TaskBase public function onStart(EntityAI $ai) { $this->selfCounter = 1; - $this->rotation = mt_rand(-90, 90); + $this->rotation = mt_rand(-120, 120); } public function onEnd(EntityAI $ai) @@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase public function canBeExecuted(EntityAI $ai) { - return !$ai->entity->isMoving() && Utils::randomFloat() < 0.04; + return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/ } public function onUpdate(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 6e06e3c0..1c164c3c 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -6,16 +6,32 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; public function canBeExecuted(EntityAI $ai){ - return !$ai->entity->isMoving() && !@$ai->getTask("TaskLookAround")->isStarted && mt_rand(0, 20) == 0; + return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt"); } protected function findTarget($e, $r){ - $ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER); //TODO sort by nearest and select nearest - return count($ents) <= 0 ? false : $ents[array_rand($ents)]; + $svd = null; + $svdDist = -1; + foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){ + if($svdDist === -1){ + $svdDist = Utils::manh_distance($e, $p); + $svd = $p; + continue; + } + if($svd != null && $svdDist === 0){ + $svd = $p; + } + + if(($cd = Utils::manh_distance($e, $p)) < $svdDist){ + $svdDist = $cd; + $svd = $p; + } + } + return $svd; } public function onStart(EntityAI $ai){ - $this->target = $this->findTarget($ai->entity, 5); + $this->target = $this->findTarget($ai->entity, 6); //TODO max distance for different mobs if(!($this->target instanceof Entity) || !$this->target->isPlayer()){ $this->reset(); return; @@ -26,7 +42,7 @@ class TaskLookAtPlayer extends TaskBase{ } public function onUpdate(EntityAI $ai){ - if(!($this->target instanceof Entity)){ + if(!($this->target instanceof Entity) || Utils::distance($ai->entity, $this->target) > 6){ //TODO max distance for different mobs $this->reset(); return; } diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 37a0a2b7..7c34938b 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -17,7 +17,6 @@ class TaskRandomWalk extends TaskBase public function onEnd(EntityAI $ai) { $this->x = $this->z = 0; - $ai->entity->idleTime = mt_rand(20, 120); } public function onUpdate(EntityAI $ai) @@ -34,7 +33,7 @@ class TaskRandomWalk extends TaskBase if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { return false; } - return !@$ai->getTask("TaskLookAround")->isStarted && !@$ai->getTask("TaskLookAtPlayer")->isStarted && mt_rand(0, 40) == 0; + return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; } diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php new file mode 100644 index 00000000..d4883223 --- /dev/null +++ b/src/entity/ai/tasks/TaskTempt.php @@ -0,0 +1,72 @@ +selfCounter = 1; + } + + public function onEnd(EntityAI $ai) + { + unset($this->target); + $ai->entity->pitch = 0; + } + + public function onUpdate(EntityAI $ai) + { + if($this->target instanceof Entity && $this->target->distanceSquared($ai->entity) > 100 || !$ai->entity->isFood($this->target->player->getHeldItem()->getID())){ + $this->reset(); + return; + } + //TODO following + $ai->mobController->lookOn($this->target); + $pk = new RotateHeadPacket(); //TODO headYaw auto update + $pk->eid = $ai->entity->eid; + $pk->yaw = $ai->entity->yaw; + $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); + } + + public function canBeExecuted(EntityAI $ai) + { + if(!($ai->entity instanceof Breedable)){ + return false; + } + $target = $this->findTarget($ai->entity, 10); + if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer() && $ai->entity->isFood($target->player->getHeldItem()->getID())){ + $this->target = $target; + return true; + } + + return false; + } + + protected function findTarget($e, $r){ + $svd = null; + $svdDist = -1; + foreach($e->server->api->entity->getRadius($e, $r * 3, ENTITY_PLAYER) as $p){ + if($svdDist === -1){ + $svdDist = Utils::manh_distance($e, $p); + $svd = $p; + continue; + } + if($svd != null && $svdDist === 0){ + $svd = $p; + } + + if(($cd = Utils::manh_distance($e, $p)) < $svdDist){ + $svdDist = $cd; + $svd = $p; + } + } + + if($svd == null || Utils::distance_noroot($e, $svd) > ($r * $r)){ + return null; + } + + return $svd; + } +} + diff --git a/src/pmf/Level.php b/src/pmf/PMFLevel.php similarity index 97% rename from src/pmf/Level.php rename to src/pmf/PMFLevel.php index 5b06ef54..c3b8bbef 100644 --- a/src/pmf/Level.php +++ b/src/pmf/PMFLevel.php @@ -436,8 +436,15 @@ class PMFLevel extends PMF{ $aZ = $z - ($Z << 4); $aY = $y - ($Y << 4); #Need to fix. But idk how. - $b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); - $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); + if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix + $b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); + $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); + }else{ + console(get_class($this->chunks[$index][$Y])); + $b = 0; + $m = 0; + } + if(($y & 1) === 0){ $m = $m & 0x0F; }else{ diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 6d76d8c0..6aed9834 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -56,6 +56,7 @@ class Utils{ public static function hasEmoji($s){ return preg_match(Utils::emojiRegex, $s); } + public static function getCallableIdentifier(callable $variable){ if(is_array($variable)){ return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1])); @@ -551,6 +552,26 @@ class Utils{ public static function round($number){ return round($number, 0, PHP_ROUND_HALF_DOWN); } + /** + * manhattan distance, less accurate, but still useful sometimes + */ + public static function manh_distance(Vector3 $pos1, Vector3 $pos2){ + return abs($pos2->x - $pos1->x) + abs($pos2->y - $pos1->y) + abs($pos2->z - $pos1->z); + } + + /** + * Euclidian distance, but without square roots + */ + public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){ + return pow($pos2->x - $pos1->x, 2) + pow($pos2->z - $pos1->z, 2) + pow($pos2->z - $pos1->z, 2); + } + + /** + * + * @param $pos1 + * @param $pos2 + * @return number + */ public static function distance($pos1, $pos2){ if($pos1 instanceof Vector3){ $pos1 = $pos1->toArray(); diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index 25f4e42d..af77da8b 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -20,6 +20,7 @@ class MobSpawner{ } public function handle(){ + return false; if($this->countEntities() > self::MOB_LIMIT || count($this->level->players) <= 0){ return false; //not spawning }