From f991961d9a957df6d054a3dc55c3ad4830936448 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 1 Nov 2020 16:34:56 +0000 Subject: [PATCH] Generator no longer requires a ChunkManager to construct this allows injection of arbitrary ChunkManagers into a single Generator instance. The objective here was to remove the requirement to cache a SimpleChunkManager instance in worker-local storage, because that requires that the chunks it stores be manually removed to avoid memory leaks. However, there are some other obstacles, primarily the worldHeight which is not retained anywhere else. --- src/world/generator/Flat.php | 12 ++++++------ src/world/generator/Generator.php | 11 +++-------- src/world/generator/GeneratorRegisterTask.php | 2 +- src/world/generator/PopulationTask.php | 6 +++--- src/world/generator/hell/Nether.php | 18 +++++++++--------- src/world/generator/normal/Normal.php | 18 +++++++++--------- 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/world/generator/Flat.php b/src/world/generator/Flat.php index 5715ffb66..dfa045820 100644 --- a/src/world/generator/Flat.php +++ b/src/world/generator/Flat.php @@ -57,8 +57,8 @@ class Flat extends Generator{ * * @throws InvalidGeneratorOptionsException */ - public function __construct(ChunkManager $world, int $seed, array $options = []){ - parent::__construct($world, $seed, $options); + public function __construct(int $seed, array $options = []){ + parent::__construct($seed, $options); if(isset($this->options["preset"]) and $this->options["preset"] != ""){ $this->preset = $this->options["preset"]; @@ -169,17 +169,17 @@ class Flat extends Generator{ } } - public function generateChunk(int $chunkX, int $chunkZ) : void{ + public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $chunk = clone $this->chunk; $chunk->setX($chunkX); $chunk->setZ($chunkZ); - $this->world->setChunk($chunkX, $chunkZ, $chunk); + $world->setChunk($chunkX, $chunkZ, $chunk); } - public function populateChunk(int $chunkX, int $chunkZ) : void{ + public function populateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed); foreach($this->populators as $populator){ - $populator->populate($this->world, $chunkX, $chunkZ, $this->random); + $populator->populate($world, $chunkX, $chunkZ, $this->random); } } diff --git a/src/world/generator/Generator.php b/src/world/generator/Generator.php index b3221a692..fa78f6abe 100644 --- a/src/world/generator/Generator.php +++ b/src/world/generator/Generator.php @@ -48,8 +48,6 @@ abstract class Generator{ return $convertedSeed; } - /** @var ChunkManager */ - protected $world; /** @var int */ protected $seed; /** @@ -62,19 +60,16 @@ abstract class Generator{ protected $random; /** - * @throws InvalidGeneratorOptionsException - * * @param mixed[] $options * @phpstan-param array $options */ - public function __construct(ChunkManager $world, int $seed, array $options = []){ - $this->world = $world; + public function __construct(int $seed, array $options = []){ $this->seed = $seed; $this->options = $options; $this->random = new Random($seed); } - abstract public function generateChunk(int $chunkX, int $chunkZ) : void; + abstract public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void; - abstract public function populateChunk(int $chunkX, int $chunkZ) : void; + abstract public function populateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void; } diff --git a/src/world/generator/GeneratorRegisterTask.php b/src/world/generator/GeneratorRegisterTask.php index 831dc7852..e4782eb6c 100644 --- a/src/world/generator/GeneratorRegisterTask.php +++ b/src/world/generator/GeneratorRegisterTask.php @@ -68,7 +68,7 @@ class GeneratorRegisterTask extends AsyncTask{ * @var Generator $generator * @see Generator::__construct() */ - $generator = new $this->generatorClass($manager, $this->seed, igbinary_unserialize($this->settings)); + $generator = new $this->generatorClass($this->seed, igbinary_unserialize($this->settings)); $this->worker->saveToThreadStore("generation.world{$this->worldId}.generator", $generator); } } diff --git a/src/world/generator/PopulationTask.php b/src/world/generator/PopulationTask.php index dd1f152ca..9b5d107ce 100644 --- a/src/world/generator/PopulationTask.php +++ b/src/world/generator/PopulationTask.php @@ -100,7 +100,7 @@ class PopulationTask extends AsyncTask{ $manager->setChunk($chunk->getX(), $chunk->getZ(), $chunk); if(!$chunk->isGenerated()){ - $generator->generateChunk($chunk->getX(), $chunk->getZ()); + $generator->generateChunk($manager, $chunk->getX(), $chunk->getZ()); $chunk = $manager->getChunk($chunk->getX(), $chunk->getZ()); $chunk->setGenerated(); } @@ -108,13 +108,13 @@ class PopulationTask extends AsyncTask{ foreach($chunks as $i => $c){ $manager->setChunk($c->getX(), $c->getZ(), $c); if(!$c->isGenerated()){ - $generator->generateChunk($c->getX(), $c->getZ()); + $generator->generateChunk($manager, $c->getX(), $c->getZ()); $chunks[$i] = $manager->getChunk($c->getX(), $c->getZ()); $chunks[$i]->setGenerated(); } } - $generator->populateChunk($chunk->getX(), $chunk->getZ()); + $generator->populateChunk($manager, $chunk->getX(), $chunk->getZ()); $chunk = $manager->getChunk($chunk->getX(), $chunk->getZ()); $chunk->setPopulated(); diff --git a/src/world/generator/hell/Nether.php b/src/world/generator/hell/Nether.php index cd9d35ae5..4fe82c001 100644 --- a/src/world/generator/hell/Nether.php +++ b/src/world/generator/hell/Nether.php @@ -58,8 +58,8 @@ class Nether extends Generator{ * * @throws InvalidGeneratorOptionsException */ - public function __construct(ChunkManager $world, int $seed, array $options = []){ - parent::__construct($world, $seed, $options); + public function __construct(int $seed, array $options = []){ + parent::__construct($seed, $options); $this->noiseBase = new Simplex($this->random, 4, 1 / 4, 1 / 64); $this->random->setSeed($this->seed); @@ -71,12 +71,12 @@ class Nether extends Generator{ $this->populators[] = $ores; } - public function generateChunk(int $chunkX, int $chunkZ) : void{ + public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed); $noise = $this->noiseBase->getFastNoise3D(16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16); - $chunk = $this->world->getChunk($chunkX, $chunkZ); + $chunk = $world->getChunk($chunkX, $chunkZ); $bedrock = VanillaBlocks::BEDROCK()->getFullId(); $netherrack = VanillaBlocks::NETHERRACK()->getFullId(); @@ -106,18 +106,18 @@ class Nether extends Generator{ } foreach($this->generationPopulators as $populator){ - $populator->populate($this->world, $chunkX, $chunkZ, $this->random); + $populator->populate($world, $chunkX, $chunkZ, $this->random); } } - public function populateChunk(int $chunkX, int $chunkZ) : void{ + public function populateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed); foreach($this->populators as $populator){ - $populator->populate($this->world, $chunkX, $chunkZ, $this->random); + $populator->populate($world, $chunkX, $chunkZ, $this->random); } - $chunk = $this->world->getChunk($chunkX, $chunkZ); + $chunk = $world->getChunk($chunkX, $chunkZ); $biome = Biome::getBiome($chunk->getBiomeId(7, 7)); - $biome->populateChunk($this->world, $chunkX, $chunkZ, $this->random); + $biome->populateChunk($world, $chunkX, $chunkZ, $this->random); } } diff --git a/src/world/generator/normal/Normal.php b/src/world/generator/normal/Normal.php index df0e02898..d677d3eed 100644 --- a/src/world/generator/normal/Normal.php +++ b/src/world/generator/normal/Normal.php @@ -61,8 +61,8 @@ class Normal extends Generator{ * * @throws InvalidGeneratorOptionsException */ - public function __construct(ChunkManager $world, int $seed, array $options = []){ - parent::__construct($world, $seed, $options); + public function __construct(int $seed, array $options = []){ + parent::__construct($seed, $options); $this->gaussian = new Gaussian(2); @@ -145,12 +145,12 @@ class Normal extends Generator{ return $this->selector->pickBiome($x + $xNoise - 1, $z + $zNoise - 1); } - public function generateChunk(int $chunkX, int $chunkZ) : void{ + public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed); $noise = $this->noiseBase->getFastNoise3D(16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16); - $chunk = $this->world->getChunk($chunkX, $chunkZ); + $chunk = $world->getChunk($chunkX, $chunkZ); $biomeCache = []; @@ -216,18 +216,18 @@ class Normal extends Generator{ } foreach($this->generationPopulators as $populator){ - $populator->populate($this->world, $chunkX, $chunkZ, $this->random); + $populator->populate($world, $chunkX, $chunkZ, $this->random); } } - public function populateChunk(int $chunkX, int $chunkZ) : void{ + public function populateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed); foreach($this->populators as $populator){ - $populator->populate($this->world, $chunkX, $chunkZ, $this->random); + $populator->populate($world, $chunkX, $chunkZ, $this->random); } - $chunk = $this->world->getChunk($chunkX, $chunkZ); + $chunk = $world->getChunk($chunkX, $chunkZ); $biome = Biome::getBiome($chunk->getBiomeId(7, 7)); - $biome->populateChunk($this->world, $chunkX, $chunkZ, $this->random); + $biome->populateChunk($world, $chunkX, $chunkZ, $this->random); } }