Improved chunk loading and order refresh times

This commit is contained in:
Shoghi Cervantes 2014-10-16 20:21:06 +02:00
parent 163a37ee23
commit 7760559be1

View File

@ -105,7 +105,6 @@ use pocketmine\scheduler\CallbackTask;
use pocketmine\tile\Sign; use pocketmine\tile\Sign;
use pocketmine\tile\Spawnable; use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile; use pocketmine\tile\Tile;
use pocketmine\utils\ReversePriorityQueue;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
use pocketmine\utils\TextWrapper; use pocketmine\utils\TextWrapper;
@ -168,8 +167,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
public $usedChunks = []; public $usedChunks = [];
protected $loadQueue = []; protected $loadQueue = [];
protected $chunkACK = []; protected $chunkACK = [];
/** @var \pocketmine\scheduler\TaskHandler */ protected $nextChunkOrderRun = 5;
protected $chunkLoadTask;
/** @var Player[] */ /** @var Player[] */
protected $hiddenPlayers = []; protected $hiddenPlayers = [];
@ -594,22 +592,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->chunkACK[$cnt] = Level::chunkHash($x, $z); $this->chunkACK[$cnt] = Level::chunkHash($x, $z);
} }
/** protected function sendNextChunk(){
* Sends, if available, the next ordered chunk to the client if($this->connected === false){
* return false;
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
*/
public function sendNextChunk(){
if($this->connected === false or !isset($this->chunkLoadTask)){
return;
} }
if(count($this->loadQueue) === 0){ $count = 0;
$this->chunkLoadTask->setNextRun($this->chunkLoadTask->getNextRun() + 30);
}else{
$count = 0;;
foreach($this->loadQueue as $index => $distance){ foreach($this->loadQueue as $index => $distance){
if($count >= $this->chunksPerTick){ if($count >= $this->chunksPerTick){
break; break;
@ -633,7 +621,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->level->useChunk($X, $Z, $this); $this->level->useChunk($X, $Z, $this);
$this->level->requestChunk($X, $Z, $this, LevelProvider::ORDER_ZXY); $this->level->requestChunk($X, $Z, $this, LevelProvider::ORDER_ZXY);
} }
}
if(count($this->usedChunks) < 16 and $this->spawned === true){ if(count($this->usedChunks) < 16 and $this->spawned === true){
$this->blocked = true; $this->blocked = true;
@ -650,7 +638,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
if($spawned < 56){ if($spawned < 56){
return; return true;
} }
$this->spawned = true; $this->spawned = true;
@ -686,18 +674,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
} }
/** protected function orderChunks(){
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @return bool
*/
public function orderChunks(){
if($this->connected === false){ if($this->connected === false){
return false; return false;
} }
$this->nextChunkOrderRun = 100;
$radiusSquared = $this->viewDistance; $radiusSquared = $this->viewDistance;
$radius = ceil(sqrt($radiusSquared)); $radius = ceil(sqrt($radiusSquared));
$side = ceil($radius / 2); $side = ceil($radius / 2);
@ -1068,10 +1051,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return -1; return -1;
} }
public function processMovement(){ protected function processMovement($currentTick){
if($this->dead or !$this->spawned or !($this->newPosition instanceof Vector3)){ if($this->dead or !$this->spawned or !($this->newPosition instanceof Vector3)){
return; return;
} }
$oldPos = new Vector3($this->x, $this->y, $this->z); $oldPos = new Vector3($this->x, $this->y, $this->z);
$distance = $oldPos->distance($this->newPosition); $distance = $oldPos->distance($this->newPosition);
@ -1171,6 +1155,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->forceMovement = new Vector3($from->x, $from->y, $from->z); $this->forceMovement = new Vector3($from->x, $from->y, $from->z);
}else{ }else{
$this->forceMovement = null; $this->forceMovement = null;
if($this->nextChunkOrderRun > 4){
$this->nextChunkOrderRun = 4;
}
} }
} }
@ -1179,7 +1166,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
public function onUpdate($currentTick){ public function onUpdate($currentTick){
if($this->spawned === false or $this->dead === true){ if($this->dead === true){
return true; return true;
} }
@ -1187,7 +1174,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->lastUpdate = $currentTick; $this->lastUpdate = $currentTick;
$this->processMovement(); if($this->spawned){
$this->processMovement($currentTick);
$this->entityBaseTick(1); $this->entityBaseTick(1);
@ -1267,6 +1255,15 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
} }
} }
}
if($this->nextChunkOrderRun-- <= 0){
$this->orderChunks();
}
if(count($this->loadQueue) > 0){
$this->sendNextChunk();
}
$this->timings->stopTiming(); $this->timings->stopTiming();
@ -1463,10 +1460,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->orderChunks(); $this->orderChunks();
$this->tasks[] = $this->server->getScheduler()->scheduleDelayedRepeatingTask(new CallbackTask([$this, "orderChunks"]), 10, 40);
$this->sendNextChunk(); $this->sendNextChunk();
$this->tasks[] = $this->chunkLoadTask = $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "sendNextChunk"]), 1);
break; break;
case ProtocolInfo::ROTATE_HEAD_PACKET: case ProtocolInfo::ROTATE_HEAD_PACKET:
if($this->spawned === false or $this->dead === true){ if($this->spawned === false or $this->dead === true){
@ -2525,7 +2519,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->airTicks = 300; $this->airTicks = 300;
$this->fallDistance = 0; $this->fallDistance = 0;
$this->orderChunks(); $this->orderChunks();
$this->chunkLoadTask->setNextRun(0);
$this->forceMovement = $pos; $this->forceMovement = $pos;
$this->newPosition = $pos; $this->newPosition = $pos;