World: use better variable names in orderChunkPopulation()

This commit is contained in:
Dylan K. Taylor 2021-10-31 23:48:00 +00:00
parent 9dec82cdbc
commit f4a3c40b5c
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -2829,46 +2829,46 @@ class World implements ChunkManager{
* *
* @phpstan-return Promise<Chunk> * @phpstan-return Promise<Chunk>
*/ */
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : Promise{ public function orderChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : Promise{
$index = World::chunkHash($x, $z); $chunkHash = World::chunkHash($chunkX, $chunkZ);
$resolver = $this->chunkPopulationRequestMap[$index] ?? null; $resolver = $this->chunkPopulationRequestMap[$chunkHash] ?? null;
if($resolver !== null && isset($this->activeChunkPopulationTasks[$index])){ if($resolver !== null && isset($this->activeChunkPopulationTasks[$chunkHash])){
//generation is already running //generation is already running
return $resolver->getPromise(); return $resolver->getPromise();
} }
for($xx = -1; $xx <= 1; ++$xx){ for($xx = -1; $xx <= 1; ++$xx){
for($zz = -1; $zz <= 1; ++$zz){ for($zz = -1; $zz <= 1; ++$zz){
if($this->isChunkLocked($x + $xx, $z + $zz)){ if($this->isChunkLocked($chunkX + $xx, $chunkZ + $zz)){
//chunk is already in use by another generation request; queue the request for later //chunk is already in use by another generation request; queue the request for later
return $resolver?->getPromise() ?? $this->enqueuePopulationRequest($x, $z, $associatedChunkLoader); return $resolver?->getPromise() ?? $this->enqueuePopulationRequest($chunkX, $chunkZ, $associatedChunkLoader);
} }
} }
} }
$temporaryChunkLoader = new class implements ChunkLoader{}; $temporaryChunkLoader = new class implements ChunkLoader{};
$this->registerChunkLoader($temporaryChunkLoader, $x, $z); $this->registerChunkLoader($temporaryChunkLoader, $chunkX, $chunkZ);
$chunk = $this->loadChunk($x, $z); $chunk = $this->loadChunk($chunkX, $chunkZ);
if($chunk === null || !$chunk->isPopulated()){ if($chunk === null || !$chunk->isPopulated()){
Timings::$population->startTiming(); Timings::$population->startTiming();
$this->activeChunkPopulationTasks[$index] = true; $this->activeChunkPopulationTasks[$chunkHash] = true;
if($resolver === null){ if($resolver === null){
$resolver = new PromiseResolver(); $resolver = new PromiseResolver();
$this->chunkPopulationRequestMap[$index] = $resolver; $this->chunkPopulationRequestMap[$chunkHash] = $resolver;
} }
$chunkPopulationLockId = new ChunkLockId(); $chunkPopulationLockId = new ChunkLockId();
for($xx = -1; $xx <= 1; ++$xx){ for($xx = -1; $xx <= 1; ++$xx){
for($zz = -1; $zz <= 1; ++$zz){ for($zz = -1; $zz <= 1; ++$zz){
$this->lockChunk($x + $xx, $z + $zz, $chunkPopulationLockId); $this->lockChunk($chunkX + $xx, $chunkZ + $zz, $chunkPopulationLockId);
if($xx !== 0 || $zz !== 0){ //avoid registering it twice for the center chunk; we already did that above if($xx !== 0 || $zz !== 0){ //avoid registering it twice for the center chunk; we already did that above
$this->registerChunkLoader($temporaryChunkLoader, $x + $xx, $z + $zz); $this->registerChunkLoader($temporaryChunkLoader, $chunkX + $xx, $chunkZ + $zz);
} }
} }
} }
$task = new PopulationTask($this, $x, $z, $chunk, $temporaryChunkLoader, $chunkPopulationLockId); $task = new PopulationTask($this, $chunkX, $chunkZ, $chunk, $temporaryChunkLoader, $chunkPopulationLockId);
$workerId = $this->workerPool->selectWorker(); $workerId = $this->workerPool->selectWorker();
if(!isset($this->generatorRegisteredWorkers[$workerId])){ if(!isset($this->generatorRegisteredWorkers[$workerId])){
$this->registerGeneratorToWorker($workerId); $this->registerGeneratorToWorker($workerId);
@ -2879,11 +2879,11 @@ class World implements ChunkManager{
return $resolver->getPromise(); return $resolver->getPromise();
} }
$this->unregisterChunkLoader($temporaryChunkLoader, $x, $z); $this->unregisterChunkLoader($temporaryChunkLoader, $chunkX, $chunkZ);
//chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned //chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned
$resolver ??= new PromiseResolver(); $resolver ??= new PromiseResolver();
unset($this->chunkPopulationRequestMap[$index]); unset($this->chunkPopulationRequestMap[$chunkHash]);
$resolver->resolve($chunk); $resolver->resolve($chunk);
return $resolver->getPromise(); return $resolver->getPromise();
} }