From 38089af098c95da5f0730a1af2c3c252c73e22da Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 27 Nov 2014 14:26:36 +0100 Subject: [PATCH] Improved global entity motion encoding using per-player queues --- src/pocketmine/Player.php | 28 +++++++++++++++++++++++++++ src/pocketmine/entity/Arrow.php | 7 +------ src/pocketmine/entity/Entity.php | 27 ++++++++------------------ src/pocketmine/entity/FallingSand.php | 7 +------ src/pocketmine/entity/Human.php | 7 +------ src/pocketmine/entity/Item.php | 7 +------ src/pocketmine/entity/PrimedTNT.php | 7 +------ src/pocketmine/entity/Snowball.php | 7 +------ src/pocketmine/entity/Villager.php | 7 +------ src/pocketmine/entity/Zombie.php | 7 +------ 10 files changed, 44 insertions(+), 67 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 999ccae86..2b9bd77bd 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -93,8 +93,10 @@ use pocketmine\network\protocol\FullChunkDataPacket; use pocketmine\network\protocol\Info as ProtocolInfo; use pocketmine\network\protocol\LoginStatusPacket; use pocketmine\network\protocol\MessagePacket; +use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\SetDifficultyPacket; +use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\network\protocol\SetHealthPacket; use pocketmine\network\protocol\SetSpawnPositionPacket; use pocketmine\network\protocol\SetTimePacket; @@ -142,6 +144,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ protected $sendIndex = 0; + protected $moveToSend = []; + protected $motionToSend = []; + public $blocked = false; public $achievements = []; public $lastCorrect; @@ -1053,6 +1058,14 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ return -1; } + public function addEntityMotion($entityId, $x, $y, $z){ + $this->motionToSend[$entityId] = [$entityId, $x, $y, $z]; + } + + public function addEntityMovement($entityId, $x, $y, $z, $yaw, $pitch){ + $this->moveToSend[$entityId] = [$entityId, $x, $y, $z, $yaw, $pitch]; + } + protected function processMovement($currentTick){ if($this->dead or !$this->spawned or !($this->newPosition instanceof Vector3)){ return; @@ -1271,6 +1284,21 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->sendNextChunk(); } + if(count($this->moveToSend) > 0){ + $pk = new MoveEntityPacket(); + $pk->entities = $this->moveToSend; + $this->dataPacket($pk); + $this->moveToSend = []; + } + + + if(count($this->motionToSend) > 0){ + $pk = new SetEntityMotionPacket(); + $pk->entities = $this->motionToSend; + $this->dataPacket($pk); + $this->motionToSend = []; + } + $this->timings->stopTiming(); return true; diff --git a/src/pocketmine/entity/Arrow.php b/src/pocketmine/entity/Arrow.php index c2147adaf..734865ea4 100644 --- a/src/pocketmine/entity/Arrow.php +++ b/src/pocketmine/entity/Arrow.php @@ -24,7 +24,6 @@ namespace pocketmine\entity; use pocketmine\level\format\FullChunk; use pocketmine\nbt\tag\Compound; use pocketmine\network\protocol\AddEntityPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class Arrow extends Projectile{ @@ -73,11 +72,7 @@ class Arrow extends Projectile{ $pk->did = 0; //TODO: send motion here $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 8ac019a8d..810f1c282 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -51,11 +51,9 @@ use pocketmine\nbt\tag\Float; use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\String; use pocketmine\Network; -use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\RemoveEntityPacket; use pocketmine\network\protocol\SetEntityDataPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\network\protocol\SetTimePacket; use pocketmine\Player; use pocketmine\plugin\Plugin; @@ -594,15 +592,13 @@ abstract class Entity extends Location implements Metadatable{ $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; $pk->bodyYaw = $this->yaw; + Server::broadcastPacket($this->hasSpawned, $pk); }else{ - //TODO: add to move list - $pk = new MoveEntityPacket(); - $pk->entities = [ - [$this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch] - ]; + foreach($this->hasSpawned as $player){ + $player->addEntityMovement($this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch); + } } - Server::broadcastPacket($this->hasSpawned, $pk); } if(($this->lastMotionX != $this->motionX or $this->lastMotionY != $this->motionY or $this->lastMotionZ != $this->motionZ)){ @@ -610,12 +606,9 @@ abstract class Entity extends Location implements Metadatable{ $this->lastMotionY = $this->motionY; $this->lastMotionZ = $this->motionZ; - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - - Server::broadcastPacket($this->hasSpawned, $pk); + foreach($this->hasSpawned as $player){ + $player->addEntityMotion($this->id, $this->motionX, $this->motionY, $this->motionZ); + } if($this instanceof Player){ $this->motionX = 0; @@ -1152,11 +1145,7 @@ abstract class Entity extends Location implements Metadatable{ if(!$this->justCreated){ if($this instanceof Player){ - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [0, $this->motionX, $this->motionY, $this->motionZ] - ]; - $this->dataPacket($pk); + $this->addEntityMotion(0, $this->motionX, $this->motionY, $this->motionZ); } $this->updateMovement(); } diff --git a/src/pocketmine/entity/FallingSand.php b/src/pocketmine/entity/FallingSand.php index 8c6f5ab27..db5a83955 100644 --- a/src/pocketmine/entity/FallingSand.php +++ b/src/pocketmine/entity/FallingSand.php @@ -31,7 +31,6 @@ use pocketmine\math\Vector3; use pocketmine\nbt\tag\Byte; use pocketmine\nbt\tag\Int; use pocketmine\network\protocol\AddEntityPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class FallingSand extends Entity{ @@ -159,11 +158,7 @@ class FallingSand extends Entity{ $pk->did = -($this->getBlock() | $this->getDamage() << 0x10); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index eab288609..5e2dad80a 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -32,7 +32,6 @@ use pocketmine\nbt\tag\Short; use pocketmine\Network; use pocketmine\network\protocol\AddPlayerPacket; use pocketmine\network\protocol\RemovePlayerPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -175,11 +174,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); $this->inventory->sendArmorContents($player); } diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index c9572d692..aabef1a45 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -32,7 +32,6 @@ use pocketmine\nbt\tag\Compound; use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\String; use pocketmine\network\protocol\AddItemEntityPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class Item extends Entity{ @@ -237,11 +236,7 @@ class Item extends Entity{ $pk->item = $this->getItem(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index 7227bc631..2cce4877b 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -28,7 +28,6 @@ use pocketmine\event\entity\ExplosionPrimeEvent; use pocketmine\level\Explosion; use pocketmine\nbt\tag\Byte; use pocketmine\network\protocol\AddEntityPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class PrimedTNT extends Entity implements Explosive{ @@ -147,11 +146,7 @@ class PrimedTNT extends Entity implements Explosive{ $pk->did = 0; $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/Snowball.php b/src/pocketmine/entity/Snowball.php index db1daba4f..1f735847c 100644 --- a/src/pocketmine/entity/Snowball.php +++ b/src/pocketmine/entity/Snowball.php @@ -25,7 +25,6 @@ namespace pocketmine\entity; use pocketmine\level\format\FullChunk; use pocketmine\nbt\tag\Compound; use pocketmine\network\protocol\AddEntityPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class Snowball extends Projectile{ @@ -72,11 +71,7 @@ class Snowball extends Projectile{ $pk->did = 0; //TODO: send motion here $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/Villager.php b/src/pocketmine/entity/Villager.php index 2233a7281..80dacb6c0 100644 --- a/src/pocketmine/entity/Villager.php +++ b/src/pocketmine/entity/Villager.php @@ -24,7 +24,6 @@ namespace pocketmine\entity; use pocketmine\nbt\tag\Int; use pocketmine\network\protocol\AddMobPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class Villager extends Creature implements NPC, Ageable{ @@ -64,11 +63,7 @@ class Villager extends Creature implements NPC, Ageable{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); } diff --git a/src/pocketmine/entity/Zombie.php b/src/pocketmine/entity/Zombie.php index 2f312d5c3..caf98921b 100644 --- a/src/pocketmine/entity/Zombie.php +++ b/src/pocketmine/entity/Zombie.php @@ -25,7 +25,6 @@ namespace pocketmine\entity; use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\item\Item as ItemItem; use pocketmine\network\protocol\AddMobPacket; -use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; class Zombie extends Monster{ @@ -52,11 +51,7 @@ class Zombie extends Monster{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); - $pk->entities = [ - [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] - ]; - $player->dataPacket($pk); + $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ); parent::spawnTo($player); }