*/ private $structure; /** @var int */ private $biome; /** @var string */ private $preset; /** * @param mixed[] $options * @phpstan-param array $options * * @throws InvalidGeneratorOptionsException */ public function __construct(ChunkManager $world, int $seed, array $options = []){ parent::__construct($world, $seed, $options); if(isset($this->options["preset"]) and $this->options["preset"] != ""){ $this->preset = $this->options["preset"]; }else{ $this->preset = "2;bedrock,2xdirt,grass;1;"; //$this->preset = "2;bedrock,59xstone,3xdirt,grass;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)"; } $this->parsePreset(); if(isset($this->options["decoration"])){ $ores = new Ore(); $stone = VanillaBlocks::STONE(); $ores->setOreTypes([ new OreType(VanillaBlocks::COAL_ORE(), $stone, 20, 16, 0, 128), new OreType(VanillaBlocks::IRON_ORE(), $stone, 20, 8, 0, 64), new OreType(VanillaBlocks::REDSTONE_ORE(), $stone, 8, 7, 0, 16), new OreType(VanillaBlocks::LAPIS_LAZULI_ORE(), $stone, 1, 6, 0, 32), new OreType(VanillaBlocks::GOLD_ORE(), $stone, 2, 8, 0, 32), new OreType(VanillaBlocks::DIAMOND_ORE(), $stone, 1, 7, 0, 16), new OreType(VanillaBlocks::DIRT(), $stone, 20, 32, 0, 128), new OreType(VanillaBlocks::GRAVEL(), $stone, 10, 16, 0, 128) ]); $this->populators[] = $ores; } $this->generateBaseChunk(); } /** * @return int[] * @phpstan-return array * * @throws InvalidGeneratorOptionsException */ public static function parseLayers(string $layers) : array{ $result = []; $split = array_map('\trim', explode(',', $layers)); $y = 0; $itemParser = LegacyStringToItemParser::getInstance(); foreach($split as $line){ preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches); if(count($matches) !== 3){ throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\""); } $cnt = $matches[1] !== "" ? (int) $matches[1] : 1; try{ $b = $itemParser->parse($matches[2])->getBlock(); }catch(\InvalidArgumentException $e){ throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e); } for($cY = $y, $y += $cnt; $cY < $y; ++$cY){ $result[$cY] = $b->getFullId(); } } return $result; } protected function parsePreset() : void{ $preset = explode(";", $this->preset); $blocks = $preset[1] ?? ""; $this->biome = (int) ($preset[2] ?? 1); $options = $preset[3] ?? ""; $this->structure = self::parseLayers($blocks); //TODO: more error checking preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $options, $matches); foreach($matches[2] as $i => $option){ $params = true; if($matches[3][$i] !== ""){ $params = []; $p = explode(" ", $matches[3][$i]); foreach($p as $k){ $k = explode("=", $k); if(isset($k[1])){ $params[$k[0]] = $k[1]; } } } $this->options[$option] = $params; } } protected function generateBaseChunk() : void{ $this->chunk = new Chunk(0, 0); $this->chunk->setGenerated(); for($Z = 0; $Z < 16; ++$Z){ for($X = 0; $X < 16; ++$X){ $this->chunk->setBiomeId($X, $Z, $this->biome); } } $count = count($this->structure); for($sy = 0; $sy < $count; $sy += 16){ $subchunk = $this->chunk->getSubChunkChecked($sy >> 4); for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){ $id = $this->structure[$y | $sy]; for($Z = 0; $Z < 16; ++$Z){ for($X = 0; $X < 16; ++$X){ $subchunk->setFullBlock($X, $y, $Z, $id); } } } } } public function generateChunk(int $chunkX, int $chunkZ) : void{ $chunk = clone $this->chunk; $chunk->setX($chunkX); $chunk->setZ($chunkZ); $this->world->setChunk($chunkX, $chunkZ, $chunk); } public function populateChunk(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); } } }