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.
This commit is contained in:
Dylan K. Taylor 2020-11-01 16:34:56 +00:00
parent 315962c12c
commit f991961d9a
6 changed files with 31 additions and 36 deletions

View File

@ -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);
}
}

View File

@ -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<string, mixed> $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;
}

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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);
}
}