From 6c08b743943180a3a019e4c80c81c37ad5601f71 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 30 Oct 2023 07:14:18 +0300 Subject: [PATCH] small rewrite of how movement packets are sent to players --- src/Player.php | 74 +++++++++++++++++++++++++++++++++++++++++-- src/entity/Entity.php | 23 +++++++------- src/world/Level.php | 13 ++++++++ 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/Player.php b/src/Player.php index d0b3cda5..eddb1ea1 100755 --- a/src/Player.php +++ b/src/Player.php @@ -68,7 +68,10 @@ class Player{ private $packetStats; private $chunkCount = []; private $received = []; - + + + public $entityMovementQueue; + public $entityMovementQueueLength = 0; /** * @param integer $clientID * @param string $ip @@ -94,6 +97,8 @@ class Player{ $this->hotbar = [0, -1, -1, -1, -1, -1, -1, -1, -1]; $this->packetStats = [0, 0]; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityMovementQueue->data = []; $this->buffer->data = []; $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); @@ -1136,7 +1141,72 @@ class Player{ } } } - + public function sendEntityMovementUpdateQueue(){ + if($this->entityMovementQueueLength > 0 and $this->entityMovementQueue instanceof RakNetPacket){ + $this->entityMovementQueue->seqNumber = $this->counter[0]++; + $this->send($this->entityMovementQueue); + } + $this->entityMovementQueueLength = 0; + $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->entityMovementQueue->data = []; + } + public function addEntityMovementUpdateToQueue(Entity $e){ + $len = 0; + $packets = 1; + $motionSent = false; + $moveSent = false; + if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0){ + $motion = new SetEntityMotionPacket(); + $motion->eid = $e->eid; + $motion->speedX = $e->speedX; + $motion->speedY = $e->speedY; + $motion->speedZ = $e->speedZ; + $motion->encode(); + $len += 1 + strlen($motion->buffer); + ++$packets; + $motionSent = true; + } + if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ + $move = new MoveEntityPacket_PosRot(); + $move->eid = $e->eid; + $move->x = $e->x; + $move->y = $e->y; + $move->z = $e->z; + $move->yaw = $e->yaw; + $move->pitch = $e->pitch; + $move->encode(); + $len += strlen($move->buffer) + 1; + ++$packets; + $moveSent = true; + } + + $headyaw = new RotateHeadPacket(); + $headyaw->eid = $e->eid; + $headyaw->yaw = $e->headYaw; + $headyaw->encode(); + + $len = strlen($headyaw->buffer) + 1; + $MTU = $this->MTU - 24; + if(($this->entityMovementQueueLength + $len) >= $MTU){ + $this->sendEntityMovementUpdateQueue(); + } + if($motionSent){ + $motion->messageIndex = $this->counter[3]++; + $motion->reliability = 2; + $this->entityMovementQueue->data[] = $motion; + } + if($moveSent){ + $move->messageIndex = $this->counter[3]++; + $move->reliability = 2; + $this->entityMovementQueue->data[] = $move; + } + $headyaw->messageIndex = $this->counter[3]++; + $headyaw->reliability = 2; + $this->entityMovementQueue->data[] = $headyaw; + + $this->entityMovementQueueLength += 6*$packets + $len; + } + /** * @param string $reason Reason for closing connection * @param boolean $msg Set to false to silently disconnect player. No broadcast. diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4f0470eb..481e4f48 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -531,6 +531,7 @@ class Entity extends Position ++$this->counter; if($this->isStatic === false){ if(!$this->isPlayer()){ + $this->updateLast(); $update = false; if($this->speedX >= -0.001 && $this->speedX <= 0.001){ $this->speedX = 0; @@ -636,7 +637,7 @@ class Entity extends Position 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); + //$this->server->api->handle("entity.motion", $this); $this->lastSpeedZ = $this->speedZ; $this->lastSpeedY = $this->speedY; $this->lastSpeedX = $this->speedX; @@ -778,9 +779,9 @@ class Entity extends Position //TODO fix $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){ + $this->updateLast(); unset($players[$this->player->CID]); $pk = new MovePlayerPacket(); $pk->eid = $this->eid; @@ -792,20 +793,18 @@ class Entity extends Position $pk->bodyYaw = $this->yaw; $this->server->api->player->broadcastPacket($players, $pk); } else{ - - $pk = new MoveEntityPacket_PosRot(); + /*$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); + $this->server->api->player->broadcastPacket($players, $pk);*/ } } } else{ $this->updatePosition(); - $this->updateLast(); } } @@ -1095,10 +1094,10 @@ class Entity extends Position } public function sendHeadYaw(){ - $pk = new RotateHeadPacket; + /*$pk = new RotateHeadPacket; $pk->eid = $this->eid; $pk->yaw = $this->headYaw; - $this->server->api->player->broadcastPacket($this->level->players, $pk); + $this->server->api->player->broadcastPacket($this->level->players, $pk);*/ } public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) @@ -1290,12 +1289,12 @@ class Entity extends Position public function sendMotion() { - $pk = new SetEntityMotionPacket(); + /*$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); + $this->server->api->player->broadcastPacket($this->level->players, $pk);*/ } public function linkEntity(Entity $e, $type) @@ -1318,14 +1317,14 @@ class Entity extends Position $this->player->teleport(new Vector3($this->x, $this->y, $this->z)); return; } - $pk = new MoveEntityPacket_PosRot(); + /*$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); + $this->server->api->player->broadcastPacket($this->level->players, $pk);*/ } public function moveEntityWithOffset($oX, $oY, $oZ) diff --git a/src/world/Level.php b/src/world/Level.php index ef73bfae..884af3bc 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -362,6 +362,7 @@ class Level{ } } $this->totalMobsAmount = 0; + $post = []; foreach($this->entityList as $k => $e){ if(!($e instanceof Entity)){ unset($this->entityList[$k]); @@ -373,8 +374,20 @@ class Level{ } if($e->isPlayer() || $e->needsUpdate){ $e->update(); + if(!$e->isPlayer()) $post[] = $k; } } + foreach($this->players as $player){ + foreach($post as $eid){ + $e = $this->entityList[$eid] ?? false; + if(!($e instanceof Entity)){ + continue; + } + $player->addEntityMovementUpdateToQueue($e); + } + $player->sendEntityMovementUpdateQueue(); + } + $this->checkSleep();