Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Custom Generator, repeating structure

Comments in 'Plugin Development' started by thebigsmileXD, Jul 27, 2015.

  1. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Not long ago @aliyuli created the empty world generator.
    I also have to create a generator for my plugin.
    Can you give me some basics how to create a custom generator?
    For example, i will have a small structure and this structure will repeat after some blocks.. Is that even possible? What parameters does the generators class have?
    Thanks in advance, Dan :)
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Use $level->setBlockIdAt(new Vector3($x,$y,$z), Block::get($id));
    (it's used for level generators)
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    It will only set one block or what?
  4. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Yes. It'll set only one block ;)
  5. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    not really o_O a generator that set step by step one block ??? ...
    i think this will be very slow...
    Creeperface likes this.
  6. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    btw look into Flat.php under Level->Generator

    PHP:
    <?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/
    *
    *
    */

    namespace pocketmine\level\generator;

    use 
    pocketmine\block\CoalOre;
    use 
    pocketmine\block\DiamondOre;
    use 
    pocketmine\block\Dirt;
    use 
    pocketmine\block\GoldOre;
    use 
    pocketmine\block\Gravel;
    use 
    pocketmine\block\IronOre;
    use 
    pocketmine\block\LapisOre;
    use 
    pocketmine\block\RedstoneOre;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\level\ChunkManager;
    use 
    pocketmine\level\format\FullChunk;
    use 
    pocketmine\level\generator\biome\Biome;
    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 ChunkManager */
        
    private $level;
        
    /** @var FullChunk */
        
    private $chunk;
        
    /** @var Random */
        
    private $random;
        
    /** @var Populator[] */
        
    private $populators = [];
        private 
    $structure$chunks$options$floorLevel$preset;

        public function 
    getSettings(){
            return 
    $this->options;
        }

        public function 
    getName(){
            return 
    "flat";
        }

        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;
            
    $this->chunk null;

            if(isset(
    $this->options["decoration"])){
                
    $ores = new Ore();
                
    $ores->setOreTypes([
                    new 
    object\OreType(new CoalOre(), 20160128),
                    new 
    object\OreType(New IronOre(), 208064),
                    new 
    object\OreType(new RedstoneOre(), 87016),
                    new 
    object\OreType(new LapisOre(), 16032),
                    new 
    object\OreType(new GoldOre(), 28032),
                    new 
    object\OreType(new DiamondOre(), 17016),
                    new 
    object\OreType(new Dirt(), 20320128),
                    new 
    object\OreType(new Gravel(), 10160128),
                ]);
                
    $this->populators[] = $ores;
            }

            
    /*if(isset($this->options["mineshaft"])){
                $this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01);
            }*/
        
    }

        protected function 
    parsePreset($preset$chunkX$chunkZ){
            
    $this->preset $preset;
            
    $preset explode(";"$preset);
            
    $version = (int) $preset[0];
            
    $blocks = isset($preset[1]) ? $preset[1] : "";
            
    $biome = isset($preset[2]) ? $preset[2] : 1;
            
    $options = isset($preset[3]) ? $preset[3] : "";
            
    preg_match_all('#^(([0-9]*x|)([0-9]{1,3})(|:[0-9]{0,2}))$#m'str_replace(",""\n"$blocks), $matches);
            
    $y 0;
            
    $this->structure = [];
            
    $this->chunks = [];
            foreach(
    $matches[3] as $i => $b){
                
    $b Item::fromString($b $matches[4][$i]);
                
    $cnt $matches[2][$i] === "" intval($matches[2][$i]);
                for(
    $cY $y$y += $cnt$cY $y; ++$cY){
                    
    $this->structure[$cY] = [$b->getId(), $b->getDamage()];
                }
            }

            
    $this->floorLevel $y;

            for(; 
    $y 0xFF; ++$y){
                
    $this->structure[$y] = [00];
            }


            
    $this->chunk = clone $this->level->getChunk($chunkX$chunkZ);
            
    $this->chunk->setGenerated();
            
    $c Biome::getBiome($biome)->getColor();
            
    $R $c >> 16;
            
    $G = ($c >> 8) & 0xff;
            
    $B $c 0xff;

            for(
    $Z 0$Z 16; ++$Z){
                for(
    $X 0$X 16; ++$X){
                    
    $this->chunk->setBiomeId($X$Z$biome);
                    
    $this->chunk->setBiomeColor($X$Z$R$G$B);
                    for(
    $y 0$y 128; ++$y){
                        
    $this->chunk->setBlock($X$y$Z, ...$this->structure[$y]);
                    }
                }
            }


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

        public function 
    init(ChunkManager $levelRandom $random){
            
    $this->level $level;
            
    $this->random $random;

            
    /*
              // Commented out : We want to delay this
            if(isset($this->options["preset"]) and $this->options["preset"] != ""){
                $this->parsePreset($this->options["preset"]);
            }else{
                $this->parsePreset($this->preset);
            }
            */
        
    }

        public function 
    generateChunk($chunkX$chunkZ){
            if(
    $this->chunk === null) {
                if(isset(
    $this->options["preset"]) and $this->options["preset"] != ""){
                    
    $this->parsePreset($this->options["preset"], $chunkX$chunkZ);
                }else{
                    
    $this->parsePreset($this->preset$chunkX$chunkZ);
                }
            }
            
    $chunk = clone $this->chunk;
            
    $chunk->setX($chunkX);
            
    $chunk->setZ($chunkZ);
            
    $this->level->setChunk($chunkX$chunkZ$chunk);
        }

        public function 
    populateChunk($chunkX$chunkZ){
            
    $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ $this->level->getSeed());
            foreach(
    $this->populators as $populator){
                
    $populator->populate($this->level$chunkX$chunkZ$this->random);
            }

        }

        public function 
    getSpawn(){
            return new 
    Vector3(128$this->floorLevel128);
        }
    }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  8. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    looks nice... but i need time to understand what uare doing there and how the generator works xD
  9. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Well, i like that code. I'll ask him, if i can modify it. Thanks alot to you all.
  10. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Now, after a while looking into the code i understand what the generator does. I'll try to rewrite the generator to support what i want. Thanks

Share This Page

Advertisement