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{ protected function selectChunks() : \Generator{
if(!$this->isConnected() or $this->viewDistance === -1){
return;
}
Timings::$playerChunkOrderTimer->startTiming();
$radius = $this->server->getAllowedViewDistance($this->viewDistance); $radius = $this->server->getAllowedViewDistance($this->viewDistance);
$radiusSquared = $radius ** 2; $radiusSquared = $radius ** 2;
$newOrder = [];
$unloadChunks = $this->usedChunks;
$centerX = $this->getFloorX() >> 4; $centerX = $this->getFloorX() >> 4;
$centerZ = $this->getFloorZ() >> 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. //If the chunk is in the radius, others at the same offsets in different quadrants are also guaranteed to be.
/* Top right quadrant */ /* Top right quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $x, $centerZ + $z)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX + $x, $centerZ + $z);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Top left quadrant */ /* Top left quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $x - 1, $centerZ + $z)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX - $x - 1, $centerZ + $z);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Bottom right quadrant */ /* Bottom right quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $x, $centerZ - $z - 1)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX + $x, $centerZ - $z - 1);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Bottom left quadrant */ /* Bottom left quadrant */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $x - 1, $centerZ - $z - 1)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX - $x - 1, $centerZ - $z - 1);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
if($x !== $z){ if($x !== $z){
/* Top right quadrant mirror */ /* Top right quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $z, $centerZ + $x)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX + $z, $centerZ + $x);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Top left quadrant mirror */ /* Top left quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $z - 1, $centerZ + $x)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX - $z - 1, $centerZ + $x);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Bottom right quadrant mirror */ /* Bottom right quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX + $z, $centerZ - $x - 1)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX + $z, $centerZ - $x - 1);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
/* Bottom left quadrant mirror */ /* Bottom left quadrant mirror */
if(!isset($this->usedChunks[$index = Level::chunkHash($centerX - $z - 1, $centerZ - $x - 1)]) or $this->usedChunks[$index] === false){ yield Level::chunkHash($centerX - $z - 1, $centerZ - $x - 1);
$newOrder[$index] = true;
}
unset($unloadChunks[$index]);
} }
} }
} }
}
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){ foreach($unloadChunks as $index => $bool){
Level::getXZ($index, $X, $Z); Level::getXZ($index, $X, $Z);