mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-08 14:59:46 +00:00
Extract population business logic from PopulationTask
This commit is contained in:
parent
fe70b31881
commit
efaf9311b3
@ -27,8 +27,6 @@ use pocketmine\scheduler\AsyncTask;
|
|||||||
use pocketmine\utils\AssumptionFailedError;
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
use pocketmine\world\format\io\FastChunkSerializer;
|
use pocketmine\world\format\io\FastChunkSerializer;
|
||||||
use pocketmine\world\SimpleChunkManager;
|
|
||||||
use pocketmine\world\World;
|
|
||||||
use function array_map;
|
use function array_map;
|
||||||
use function igbinary_serialize;
|
use function igbinary_serialize;
|
||||||
use function igbinary_unserialize;
|
use function igbinary_unserialize;
|
||||||
@ -71,8 +69,6 @@ class PopulationTask extends AsyncTask{
|
|||||||
if($context === null){
|
if($context === null){
|
||||||
throw new AssumptionFailedError("Generator context should have been initialized before any PopulationTask execution");
|
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;
|
$chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
|
||||||
|
|
||||||
@ -93,21 +89,15 @@ class PopulationTask extends AsyncTask{
|
|||||||
$serialChunks
|
$serialChunks
|
||||||
);
|
);
|
||||||
|
|
||||||
self::setOrGenerateChunk($manager, $generator, $this->chunkX, $this->chunkZ, $chunk);
|
[$chunk, $chunks] = PopulationUtils::populateChunkWithAdjacents(
|
||||||
|
$context->getWorldMinY(),
|
||||||
$resultChunks = []; //this is just to keep phpstan's type inference happy
|
$context->getWorldMaxY(),
|
||||||
foreach($chunks as $relativeChunkHash => $c){
|
$context->getGenerator(),
|
||||||
World::getXZ($relativeChunkHash, $relativeX, $relativeZ);
|
$this->chunkX,
|
||||||
$resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $this->chunkX + $relativeX, $this->chunkZ + $relativeZ, $c);
|
$this->chunkZ,
|
||||||
}
|
$chunk,
|
||||||
$chunks = $resultChunks;
|
$chunks
|
||||||
|
);
|
||||||
$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();
|
|
||||||
|
|
||||||
$this->chunk = FastChunkSerializer::serializeTerrain($chunk);
|
$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");
|
$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{
|
public function onCompletion() : void{
|
||||||
/**
|
/**
|
||||||
* @var \Closure $onCompletion
|
* @var \Closure $onCompletion
|
||||||
|
74
src/world/generator/PopulationUtils.php
Normal file
74
src/world/generator/PopulationUtils.php
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user