Flat: separate logic of parsing preset and generating base chunk

now always generates the base chunk on init, because the generator won't be created unless there is generation to be done.
This commit is contained in:
Dylan K. Taylor 2018-07-14 11:51:49 +01:00
parent 53068caf3c
commit 478a131aa5

View File

@ -26,11 +26,13 @@ namespace pocketmine\level\generator;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\item\ItemFactory;
use pocketmine\level\ChunkManager;
use pocketmine\level\format\Chunk;
use pocketmine\level\generator\object\OreType;
use pocketmine\level\generator\populator\Ore;
use pocketmine\level\generator\populator\Populator;
use pocketmine\math\Vector3;
use pocketmine\utils\Random;
class Flat extends Generator{
/** @var Chunk */
@ -41,6 +43,8 @@ class Flat extends Generator{
private $structure;
/** @var int */
private $floorLevel;
/** @var int */
private $biome;
/** @var mixed[] */
private $options;
/** @var string */
@ -55,9 +59,15 @@ class Flat extends Generator{
}
public function __construct(array $options = []){
$this->preset = "2;7,2x3,2;1;";
//$this->preset = "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)";
$this->options = $options;
if(isset($this->options["preset"]) and $this->options["preset"] != ""){
$this->preset = $this->options["preset"];
}else{
$this->preset = "2;7,2x3,2;1;";
//$this->preset = "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)";
}
$this->parsePreset();
if(isset($this->options["decoration"])){
$ores = new Ore();
@ -90,39 +100,14 @@ class Flat extends Generator{
return $result;
}
protected function generateBaseChunk(string $preset) : void{
$this->preset = $preset;
$preset = explode(";", $preset);
$version = (int) $preset[0];
protected function parsePreset() : void{
$preset = explode(";", $this->preset);
$blocks = (string) ($preset[1] ?? "");
$biome = (int) ($preset[2] ?? 1);
$this->biome = (int) ($preset[2] ?? 1);
$options = (string) ($preset[3] ?? "");
$this->structure = self::parseLayers($blocks);
$this->floorLevel = $y = count($this->structure);
$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, $biome);
}
}
$count = count($this->structure);
for($sy = 0; $sy < $count; $sy += 16){
$subchunk = $this->chunk->getSubChunk($sy >> 4, true);
for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){
list($id, $meta) = $this->structure[$y | $sy];
for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
$subchunk->setBlock($X, $y, $Z, $id, $meta);
}
}
}
}
$this->floorLevel = count($this->structure);
preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $options, $matches);
foreach($matches[2] as $i => $option){
@ -141,14 +126,37 @@ class Flat extends Generator{
}
}
public function generateChunk(int $chunkX, int $chunkZ) : void{
if($this->chunk === null){
if(isset($this->options["preset"]) and $this->options["preset"] != ""){
$this->generateBaseChunk($this->options["preset"]);
}else{
$this->generateBaseChunk($this->preset);
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->getSubChunk($sy >> 4, true);
for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){
list($id, $meta) = $this->structure[$y | $sy];
for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
$subchunk->setBlock($X, $y, $Z, $id, $meta);
}
}
}
}
}
public function init(ChunkManager $level, Random $random) : void{
parent::init($level, $random);
$this->generateBaseChunk();
}
public function generateChunk(int $chunkX, int $chunkZ) : void{
$chunk = clone $this->chunk;
$chunk->setX($chunkX);
$chunk->setZ($chunkZ);