Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Improved global entity motion encoding using per-player queues
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Nov 27, 2014
1 parent cd135b3 commit 38089af
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 67 deletions.
28 changes: 28 additions & 0 deletions src/pocketmine/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1053,6 +1058,14 @@ protected function getCreativeBlock(Item $item){
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;
Expand Down Expand Up @@ -1271,6 +1284,21 @@ public function onUpdate($currentTick){
$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;
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Arrow.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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{
Expand Down Expand Up @@ -73,11 +72,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
27 changes: 8 additions & 19 deletions src/pocketmine/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@
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;
Expand Down Expand Up @@ -594,28 +592,23 @@ public function updateMovement(){
$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)){
$this->lastMotionX = $this->motionX;
$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;
Expand Down Expand Up @@ -1152,11 +1145,7 @@ public function setMotion(Vector3 $motion){

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();
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/FallingSand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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{
Expand Down Expand Up @@ -159,11 +158,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Human.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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;

Expand Down Expand Up @@ -175,11 +174,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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{
Expand Down Expand Up @@ -237,11 +236,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/PrimedTNT.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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{
Expand Down Expand Up @@ -147,11 +146,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Snowball.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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{
Expand Down Expand Up @@ -72,11 +71,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Villager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

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{
Expand Down Expand Up @@ -64,11 +63,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/pocketmine/entity/Zombie.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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{
Expand All @@ -52,11 +51,7 @@ public function spawnTo(Player $player){
$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);
}
Expand Down

0 comments on commit 38089af

Please sign in to comment.