diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 17bd5b0f..c21cb93c 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -134,7 +134,8 @@ class ServerAPI{ if(ADVANCED_CACHE == true){ console("[INFO] Advanced cache enabled"); } - + MobSpawner::$spawnAnimals = $this->getProperty("spawn-animals"); + MobSpawner::$spawnMobs = $this->getProperty("spawn-mobs"); if($this->getProperty("upnp-forwarding") == true){ console("[INFO] [UPnP] Trying to port forward..."); UPnP_PortForward($this->getProperty("server-port")); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 8b79b14d..b97284ce 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -197,10 +197,13 @@ class Entity extends Position $this->speedY += $vY; $this->speedZ += $vZ; } - + public function isMovingHorizontally() + { + return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); + } 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); + return $this->isMovingHorizontally() || ($this->speedY > 0.007 || $this->speedY < - 0.007); } public function setVelocity($vX, $vY = 0, $vZ = 0) @@ -756,14 +759,6 @@ class Entity extends Position $this->updateMovement(); } - if($this->headYaw != $this->lastHeadYaw) { - $this->lastHeadYaw = $this->headYaw; - $pk = new RotateHeadPacket(); - $pk->eid = $this->eid; - $pk->yaw = $this->headYaw; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } - $this->needsUpdate = $hasUpdate; $this->lastUpdate = $now; } @@ -774,13 +769,13 @@ class Entity extends Position 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->isStatic === false and ($this->lastX != $this->x or $this->lastY != $this->y or $this->lastZ != $this->z or $this->lastYaw != $this->yaw or $this->lastPitch != $this->pitch or $this->lastHeadYaw != $this->headYaw)){ 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]); + //TODO fix $this->setPosition($this->last[0], $this->last[1], $this->last[2], $this->last[3], $this->last[4]); } } else{ $this->updateLast(); @@ -797,6 +792,7 @@ class Entity extends Position $pk->bodyYaw = $this->yaw; $this->server->api->player->broadcastPacket($players, $pk); } else{ + $pk = new MoveEntityPacket_PosRot(); $pk->eid = $this->eid; $pk->x = $this->x; @@ -814,6 +810,8 @@ class Entity extends Position } $this->updateLast(); } + + } $this->lastUpdate = $now; diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 9b857112..70eff8eb 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -35,14 +35,17 @@ class MobController $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; - $this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan); + $calcYaw = ($thetaOffset + $tan); + $this->entity->yaw = $this->entity->headYaw = $calcYaw; } public function lookOffset($x, $y, $z, $pitch = true){ $tan = $z == 0 ? ($x < 0 ? 180 : 0) : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/ $thetaOffset = $z < 0 ? 90 : 270; $calcYaw = $tan + $thetaOffset; - $this->entity->headYaw = $calcYaw; + + $this->entity->yaw = $this->entity->headYaw = $calcYaw; + if($pitch){ $diff = sqrt($x * $x + $z * $z); $calcPitch = rad2deg(atan($y / $diff)); diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index c28b9588..be16d0aa 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -6,7 +6,7 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; public function canBeExecuted(EntityAI $ai){ - return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); + return Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); } protected function findTarget($e, $r){ diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index c2e69d8c..c1567d7f 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -11,7 +11,7 @@ class Skeleton extends Monster{ } public function updateBurning(){ - if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){ + if($this->fire > 0 or !$this->level->isDay()){ return false; } diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index d802118f..4c6894c8 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -11,7 +11,7 @@ class Zombie extends Monster{ } public function updateBurning(){ - if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){ + if($this->fire > 0 or !$this->level->isDay()){ return false; } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 4a9fc024..1a28f30e 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -42,7 +42,7 @@ class Utils{ public static function wrapAngleTo180_float($par0) { $par0 %= 360.0; - return $par0 >= 180 ? $par0 - 360 : ($par0 + 360); + return $par0 >= 180 ? $par0 - 360 : (($par0 < -180) ? ($par0 + 360) : $par0); } public static function getSign($v){ diff --git a/src/world/Level.php b/src/world/Level.php index 19d9b6f3..fec9a3a9 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -81,6 +81,13 @@ class Level{ unset($this->mobSpawner->level); } + public function isDay(){ + return $this->getTime() % 19200 < TimeAPI::$phases["sunset"]; + } + public function isNight(){ + $t = $this->getTime() % 19200; + return $t < TimeAPI::$phases["sunrise"] && $t > TimeAPI::$phases["sunset"]; + } public function save($force = false, $extra = true){ if(!isset($this->level)){ return false; diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index f76ed5c0..e99b3c67 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -1,6 +1,7 @@ level->entityList as $e){ //TODO no for loop - if(!$e->isPlayer() && $e->class === ENTITY_MOB){ - ++$ents; - } + foreach($this->level->entityList as $e){ + if(!$e->isPlayer() && $e->class === ENTITY_MOB) ++$ents; } return $ents; } @@ -28,12 +27,10 @@ class MobSpawner{ public function spawnMobs(){ - $t = $this->level->getTime(); - if($this->server->api->getProperty("spawn-animals") && $t <= 9500){ //Animal + if(self::$spawnAnimals && $this->level->isDay()){ //Animal $type = mt_rand(10, 13); $baby = false; //TODO baby - }else - if($this->server->api->getProperty("spawn-mobs") && $t >= 10000){ //Monster + }elseif(self::$spawnMobs && $this->level->isNight()){ //Monster $type = mt_rand(32, 35); $baby = 2; }else{ @@ -47,7 +44,9 @@ class MobSpawner{ } $data = $this->genPosData($x, $y + 1, $z); if($baby != 2) $data["IsBaby"] = $baby; + $e = $this->server->api->entity->add($this->level, 2, $type, $data); + if($e instanceof Entity){ $this->server->api->entity->spawnToAll($e); //console("[DEBUG] $type spawned at $x, $y, $z");