World: Clean up ticking chunk loader tracking

This commit is contained in:
Dylan K. Taylor 2021-05-17 23:07:48 +01:00
parent 643c3ed14e
commit cdaf734470
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -180,10 +180,10 @@ class World implements ChunkManager{
/** @var int */ /** @var int */
private $maxY; private $maxY;
/** @var ChunkLoader[] */ /** @var TickingChunkLoader[] */
private $loaders = []; private $tickingLoaders = [];
/** @var int[] */ /** @var int[] */
private $loaderCounter = []; private $tickingLoaderCounter = [];
/** @var ChunkLoader[][] */ /** @var ChunkLoader[][] */
private $chunkLoaders = []; private $chunkLoaders = [];
@ -632,11 +632,13 @@ class World implements ChunkManager{
$this->chunkLoaders[$chunkHash][$loaderId] = $loader; $this->chunkLoaders[$chunkHash][$loaderId] = $loader;
if(!isset($this->loaders[$loaderId])){ if($loader instanceof TickingChunkLoader){
$this->loaderCounter[$loaderId] = 1; if(!isset($this->tickingLoaders[$loaderId])){
$this->loaders[$loaderId] = $loader; $this->tickingLoaderCounter[$loaderId] = 1;
$this->tickingLoaders[$loaderId] = $loader;
}else{ }else{
++$this->loaderCounter[$loaderId]; ++$this->tickingLoaderCounter[$loaderId];
}
} }
$this->cancelUnloadChunkRequest($chunkX, $chunkZ); $this->cancelUnloadChunkRequest($chunkX, $chunkZ);
@ -660,9 +662,9 @@ class World implements ChunkManager{
} }
} }
if(--$this->loaderCounter[$loaderId] === 0){ if(isset($this->tickingLoaderCounter[$loaderId]) && --$this->tickingLoaderCounter[$loaderId] === 0){
unset($this->loaderCounter[$loaderId]); unset($this->tickingLoaderCounter[$loaderId]);
unset($this->loaders[$loaderId]); unset($this->tickingLoaders[$loaderId]);
} }
} }
} }
@ -970,22 +972,18 @@ class World implements ChunkManager{
} }
private function tickChunks() : void{ private function tickChunks() : void{
if($this->chunksPerTick <= 0 or count($this->loaders) === 0){ if($this->chunksPerTick <= 0 or count($this->tickingLoaders) === 0){
return; return;
} }
/** @var bool[] $chunkTickList chunkhash => dummy */ /** @var bool[] $chunkTickList chunkhash => dummy */
$chunkTickList = []; $chunkTickList = [];
$chunksPerLoader = min(200, max(1, (int) ((($this->chunksPerTick - count($this->loaders)) / count($this->loaders)) + 0.5))); $chunksPerLoader = min(200, max(1, (int) ((($this->chunksPerTick - count($this->tickingLoaders)) / count($this->tickingLoaders)) + 0.5)));
$randRange = 3 + $chunksPerLoader / 30; $randRange = 3 + $chunksPerLoader / 30;
$randRange = (int) ($randRange > $this->chunkTickRadius ? $this->chunkTickRadius : $randRange); $randRange = (int) ($randRange > $this->chunkTickRadius ? $this->chunkTickRadius : $randRange);
foreach($this->loaders as $loader){ foreach($this->tickingLoaders as $loader){
if(!($loader instanceof TickingChunkLoader)){
//TODO: maybe we should just not track non-ticking chunk loaders here?
continue;
}
$chunkX = (int) floor($loader->getX()) >> 4; $chunkX = (int) floor($loader->getX()) >> 4;
$chunkZ = (int) floor($loader->getZ()) >> 4; $chunkZ = (int) floor($loader->getZ()) >> 4;
@ -1980,13 +1978,6 @@ class World implements ChunkManager{
return $this->players; return $this->players;
} }
/**
* @return ChunkLoader[]
*/
public function getLoaders() : array{
return $this->loaders;
}
/** /**
* Returns the Tile in a position, or null if not found. * Returns the Tile in a position, or null if not found.
* *