diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 045dadbf..df63a37f 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -664,7 +664,7 @@ class Entity extends Position // $this->speedZ = 0; $this->server->api->handle("entity.move", $this); $update = true; - }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch) { + }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { $update = true; } @@ -723,7 +723,9 @@ class Entity extends Position if($this->idleTime > 0){ -- $this->idleTime; } - + if($this->lastHeadYaw != $this->headYaw){ + $this->sendHeadYaw(); + } if($this->class !== ENTITY_PLAYER && $update){ $this->updateMovement(); } @@ -769,9 +771,6 @@ class Entity extends Position } } else{ $this->updatePosition(); - if($this->lastHeadYaw != $this->headYaw){ - $this->sendHeadYaw(); - } $this->updateLast(); } diff --git a/src/entity/Living.php b/src/entity/Living.php index faf99cc0..2c103d93 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -33,6 +33,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { $this->ai->updateTasks(); } + $this->ai->mobController->rotateTick(); if($this->onGround){ //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 6a7fde35..32a8db86 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -6,6 +6,9 @@ class MobController * @var Entity */ public $entity; + + public $finalYaw, $finalPitch, $finalHeadYaw; + public function __construct($e){ $this->entity = $e; } @@ -36,16 +39,17 @@ class MobController } } } - + $this->faceEntity($this->entity->add($ox, $oy, $oz)); if($this->entity->knockbackTime <= 0){ $this->entity->moveEntityWithOffset($ox, $oy, $oz); } - - - $this->faceEntity($this->entity->add($ox, $oy, $oz)); return true; } - + public function rotateTick(){ //TODO handle more rotation + $w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); + $w180min = min(abs($w180), 20)*Utils::getSign($w180); + $this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); + } public function moveTo($x, $y, $z){ return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z)); } @@ -58,7 +62,7 @@ class MobController $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; $calcYaw = ($thetaOffset + $tan); - $this->entity->headYaw = $this->entity->yaw = $calcYaw; + $this->finalHeadYaw = $this->entity->yaw = $calcYaw; } public function lookOffset($x, $y, $z, $pitch = true){ @@ -66,7 +70,7 @@ class MobController $thetaOffset = $z < 0 ? 90 : 270; $calcYaw = $tan + $thetaOffset; - $this->entity->yaw = $this->entity->headYaw = $calcYaw; + $this->entity->yaw = $this->finalHeadYaw = $calcYaw; if($pitch){ $diff = sqrt($x * $x + $z * $z); diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 7ddb4b47..d091607f 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -24,7 +24,7 @@ class RotateHeadPacket extends RakNetDataPacket{ $this->putInt(count($this->entities)); foreach($this->entities as $d){ $this->putInt($d[0]); //eid - $this->putByte((int) ($d[1] / 360 / 0.0039062)); + $this->putByte((int) ($d[1] / 360 / 0.0039062)); //wraps 360 angle to 0xff } } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 538324b6..c926d410 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -39,10 +39,16 @@ class Utils{ return $id; } - public static function wrapAngleTo180_float($par0) + public static function wrapAngleTo360($angle) { - $par0 %= 360.0; - return $par0 >= 180 ? $par0 - 360 : (($par0 < -180) ? ($par0 + 360) : $par0); + $angle %= 360; + return $angle < 0 ? $angle + 360 : $angle; + } + + public static function wrapAngleTo180($angle) + { + $angle = fmod($angle+180, 360); + return $angle < 0 ? $angle + 360 : $angle - 180; } public static function getSign($v){