Extract population business logic from PopulationTask

This commit is contained in:
Dylan K. Taylor 2025-04-30 17:38:16 +01:00
parent fe70b31881
commit efaf9311b3
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 83 additions and 31 deletions

View File

@ -27,8 +27,6 @@ use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\format\Chunk;
use pocketmine\world\format\io\FastChunkSerializer;
use pocketmine\world\SimpleChunkManager;
use pocketmine\world\World;
use function array_map;
use function igbinary_serialize;
use function igbinary_unserialize;
@ -71,8 +69,6 @@ class PopulationTask extends AsyncTask{
if($context === null){
throw new AssumptionFailedError("Generator context should have been initialized before any PopulationTask execution");
}
$generator = $context->getGenerator();
$manager = new SimpleChunkManager($context->getWorldMinY(), $context->getWorldMaxY());
$chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
@ -93,21 +89,15 @@ class PopulationTask extends AsyncTask{
$serialChunks
);
self::setOrGenerateChunk($manager, $generator, $this->chunkX, $this->chunkZ, $chunk);
$resultChunks = []; //this is just to keep phpstan's type inference happy
foreach($chunks as $relativeChunkHash => $c){
World::getXZ($relativeChunkHash, $relativeX, $relativeZ);
$resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $this->chunkX + $relativeX, $this->chunkZ + $relativeZ, $c);
}
$chunks = $resultChunks;
$generator->populateChunk($manager, $this->chunkX, $this->chunkZ);
$chunk = $manager->getChunk($this->chunkX, $this->chunkZ);
if($chunk === null){
throw new AssumptionFailedError("We just generated this chunk, so it must exist");
}
$chunk->setPopulated();
[$chunk, $chunks] = PopulationUtils::populateChunkWithAdjacents(
$context->getWorldMinY(),
$context->getWorldMaxY(),
$context->getGenerator(),
$this->chunkX,
$this->chunkZ,
$chunk,
$chunks
);
$this->chunk = FastChunkSerializer::serializeTerrain($chunk);
@ -118,18 +108,6 @@ class PopulationTask extends AsyncTask{
$this->adjacentChunks = igbinary_serialize($serialChunks) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
}
private static function setOrGenerateChunk(SimpleChunkManager $manager, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $chunk) : Chunk{
$manager->setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false));
if($chunk === null){
$generator->generateChunk($manager, $chunkX, $chunkZ);
$chunk = $manager->getChunk($chunkX, $chunkZ);
if($chunk === null){
throw new AssumptionFailedError("We just set this chunk, so it must exist");
}
}
return $chunk;
}
public function onCompletion() : void{
/**
* @var \Closure $onCompletion

View File

@ -0,0 +1,74 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\world\generator;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\format\Chunk;
use pocketmine\world\SimpleChunkManager;
use pocketmine\world\World;
/**
* @internal
*/
final class PopulationUtils{
private static function setOrGenerateChunk(SimpleChunkManager $manager, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $chunk) : Chunk{
$manager->setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false));
if($chunk === null){
$generator->generateChunk($manager, $chunkX, $chunkZ);
$chunk = $manager->getChunk($chunkX, $chunkZ);
if($chunk === null){
throw new AssumptionFailedError("We just set this chunk, so it must exist");
}
}
return $chunk;
}
/**
* @param Chunk[]|null[] $adjacentChunks
* @phpstan-param array<int, Chunk|null> $adjacentChunks
*
* @return Chunk[]|Chunk[][]
* @phpstan-return array{Chunk, array<int, Chunk>}
*/
public static function populateChunkWithAdjacents(int $minY, int $maxY, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $centerChunk, array $adjacentChunks) : array{
$manager = new SimpleChunkManager($minY, $maxY);
self::setOrGenerateChunk($manager, $generator, $chunkX, $chunkZ, $centerChunk);
$resultChunks = []; //this is just to keep phpstan's type inference happy
foreach($adjacentChunks as $relativeChunkHash => $c){
World::getXZ($relativeChunkHash, $relativeX, $relativeZ);
$resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $chunkX + $relativeX, $chunkZ + $relativeZ, $c);
}
$adjacentChunks = $resultChunks;
$generator->populateChunk($manager, $chunkX, $chunkZ);
$centerChunk = $manager->getChunk($chunkX, $chunkZ);
if($centerChunk === null){
throw new AssumptionFailedError("We just generated this chunk, so it must exist");
}
$centerChunk->setPopulated();
return [$centerChunk, $adjacentChunks];
}
}