Player: clean up chunk selection code

This commit is contained in:
Dylan K. Taylor 2019-02-02 18:49:20 +00:00
parent ce8d9fa9f4
commit 2c0f91ce50

View File

@ -1022,19 +1022,10 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
protected function orderChunks() : void{
if(!$this->isConnected() or $this->viewDistance === -1){
return;
}
Timings::$playerChunkOrderTimer->startTiming();
protected function selectChunks() : \Generator{
$radius = $this->server->getAllowedViewDistance($this->viewDistance);
$radiusSquared = $radius ** 2;
$newOrder = [];
$unloadChunks = $this->usedChunks;
$centerX = $this->getFloorX() >> 4;
$centerZ = $this->getFloorZ() >> 4;
@ -1047,57 +1038,44 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
//If the chunk is in the radius, others at the same offsets in different quadrants are also guaranteed to be.
/* Top right quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $x, $centerZ + $z)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX + $x, $centerZ + $z);
/* Top left quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $x - 1, $centerZ + $z)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX - $x - 1, $centerZ + $z);
/* Bottom right quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $x, $centerZ - $z - 1)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX + $x, $centerZ - $z - 1);
/* Bottom left quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $x - 1, $centerZ - $z - 1)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX - $x - 1, $centerZ - $z - 1);
if($x !== $z){
/* Top right quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $z, $centerZ + $x)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX + $z, $centerZ + $x);
/* Top left quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $z - 1, $centerZ + $x)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX - $z - 1, $centerZ + $x);
/* Bottom right quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $z, $centerZ - $x - 1)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX + $z, $centerZ - $x - 1);
/* Bottom left quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $z - 1, $centerZ - $x - 1)]) or $this->usedChunks[$index] === false){
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
yield Level::chunkHash($centerX - $z - 1, $centerZ - $x - 1);
}
}
}
}
protected function orderChunks() : void{
if(!$this->isConnected() or $this->viewDistance === -1){
return;
}
Timings::$playerChunkOrderTimer->startTiming();
$newOrder = [];
$unloadChunks = $this->usedChunks;
foreach($this->selectChunks() as $hash){
if(!isset($this->usedChunks[$hash]) or $this->usedChunks[$hash] === false){
$newOrder[$hash] = true;
}
unset($unloadChunks[$hash]);
}
foreach($unloadChunks as $index => $bool){
Level::getXZ($index, $X, $Z);