1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved World generation

Discussion in 'Development' started by adoswyn, Jan 10, 2019.

  1. adoswyn

    adoswyn Spider

    Messages:
    8
    GitHub:
    adoswyn
    I want to add my populators to biomes without editing the core. Is it possible to do this and how?
     
    Last edited: Jan 10, 2019
    MCPEChicken_ likes this.
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    I have not tested this, i think it is possible by doing
    PHP:
    Biome::getBiome(Biome::FOREST)->addPopulator($populator);
     
    adoswyn likes this.
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    Technically yes, this should work.
     
  4. adoswyn

    adoswyn Spider

    Messages:
    8
    GitHub:
    adoswyn
    The method is not working.
    PHP:
            $flowerPopulator = new FlowerPopulator();
            
    $flowerPopulator->setBaseAmount(5);
            
    $flowerPopulator->addType([Block::DANDELION0]);
            
    $flowerPopulator->addType([Block::RED_FLOWER0]);
            
    $flowerPopulator->addType([Block::RED_FLOWERFlower::TYPE_ALLIUM]);
            
    $flowerPopulator->addType([Block::RED_FLOWER3]);
            
    $flowerPopulator->addType([Block::RED_FLOWERFlower::TYPE_OXEYE_DAISY]);
            
    $flowerPopulator->addType([Block::RED_FLOWERFlower::TYPE_ORANGE_TULIP]);
            
    Biome::getBiome(Biome::PLAINS)->addPopulator($flowerPopulator);
    Plugin startup before world generation. Any other ways?
     
  5. adoswyn

    adoswyn Spider

    Messages:
    8
    GitHub:
    adoswyn
    I checked, the populator is added, but does not work.
    Code:
    array(2) {
      [0]=>
      object(pocketmine\level\generator\populator\TallGrass)#5381 (3) {
        ["level":"pocketmine\level\generator\populator\TallGrass":private]=>
        NULL
        ["randomAmount":"pocketmine\level\generator\populator\TallGrass":private]=>
        NULL
        ["baseAmount":"pocketmine\level\generator\populator\TallGrass":private]=>
        int(12)
      }
      [1]=>
      object(myplugin\world\populator\FlowerPopulator)#10439 (4) {
        ["level":"myplugin\world\populator\FlowerPopulator":private]=>
        NULL
        ["randomAmount":"myplugin\world\populator\FlowerPopulator":private]=>
        NULL
        ["baseAmount":"myplugin\world\populator\FlowerPopulator":private]=>
        int(5)
        ["flowerTypes":"myplugin\world\populator\FlowerPopulator":private]=>
        array(6) {
          [0]=>
          array(2) {
            [0]=>
            int(37)
            [1]=>
            int(0)
          }
          [1]=>
          array(2) {
            [0]=>
            int(38)
            [1]=>
            int(0)
          }
          [2]=>
          array(2) {
            [0]=>
            int(38)
            [1]=>
            int(2)
          }
          [3]=>
          array(2) {
            [0]=>
            int(38)
            [1]=>
            int(3)
          }
          [4]=>
          array(2) {
            [0]=>
            int(38)
            [1]=>
            int(8)
          }
          [5]=>
          array(2) {
            [0]=>
            int(38)
            [1]=>
            int(5)
          }
        }
      }
    }

    PHP:
    class FlowerPopulator extends Populator{

        
    /** @var ChunkManager */
        
    private $level;
        private 
    $randomAmount;
        private 
    $baseAmount 8;
        private 
    $flowerTypes = [];

        
    /**
         * @param $amount
         */
        
    public function setRandomAmount($amount){
            
    $this->randomAmount $amount;
        }

        
    /**
         * @param $amount
         */
        
    public function setBaseAmount($amount){
            
    $this->baseAmount $amount;
        }

        
    /**
         * @param $type
         */
        
    public function addType($type){
            
    $this->flowerTypes[] = $type;
        }

        
    /**
         * @return array
         */
        
    public function getTypes() : array{
            return 
    $this->flowerTypes;
        }

        
    /**
         * @param ChunkManager $level
         * @param              $chunkX
         * @param              $chunkZ
         * @param Random       $random
         *
         * @return mixed|void
         */
        
    public function populate(ChunkManager $level$chunkX$chunkZRandom $random){
            
    $this->level $level;
            
    $amount $random->nextRange(0$this->randomAmount 1) + $this->baseAmount;
            
    $endNum count($this->flowerTypes) - 1;
            for(
    $i 0$i $amount; ++$i){
                
    $x $random->nextRange($chunkX 16$chunkX 16 15);
                
    $z $random->nextRange($chunkZ 16$chunkZ 16 15);
                
    $y $this->getHighestWorkableBlock($x$z);
                if(
    $y !== -and $this->canFlowerStay($x$y$z)){
                    
    $type mt_rand(0$endNum);
                    
    $this->level->setBlockIdAt($x$y$z$this->flowerTypes[$type][0]);
                    
    $this->level->setBlockDataAt($x$y$z$this->flowerTypes[$type][1]);
                }
            }
        }

        
    /**
         * @param $x
         * @param $y
         * @param $z
         *
         * @return bool
         */
        
    private function canFlowerStay($x$y$z) : bool{
            
    $b $this->level->getBlockIdAt($x$y$z);
            return (
    $b === Block::AIR or $b === Block::SNOW_LAYER) and $this->level->getBlockIdAt($x$y 1$z) === Block::GRASS;
        }

        
    /**
         * @param $x
         * @param $z
         *
         * @return int
         */
        
    private function getHighestWorkableBlock($x$z) : int{
            for(
    $y 127$y >= 0; --$y){
                
    $b $this->level->getBlockIdAt($x$y$z);
                if(
    $b !== Block::AIR and $b !== Block::LEAVES and $b !== Block::LEAVES2 and $b !== Block::SNOW_LAYER){
                    break;
                }
            }
            return 
    $y === ? -: ++$y;
        }
    }
     
    Last edited: Jan 10, 2019
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That won't work if you're executing it on the main thread. The generator tasks run on another thread (they are created asynchronously) and the generator
    probably maintain a copy of generators and populators at the time they were registered.
    Adding populators isn't possible without creating a generator class that extends the parent generator class (which is pocketmine's Normal generator if you don't use a custom world generator) and then registering the generator and changing your world's generator type. You can copy that addPopulator code into the constructor of the generator, I think that will work.
     
    Last edited: Jan 10, 2019
    adoswyn likes this.
  7. adoswyn

    adoswyn Spider

    Messages:
    8
    GitHub:
    adoswyn
    Yes, really, has made how you have told also all has earned! :cool: upload_2019-1-11_11-44-54.png
     
    Muqsit likes this.
  8. Primus

    Primus Zombie Pigman

    Messages:
    749
    I could send you my generator which has pretty much all biomes and noise generator from java version. The biome selector isn't perfect. But might be useful anyways. Send me a message I'll drop it there due to licence the Nukkit has (Yea, I translated the generator PHP).
     
  9. Jules

    Jules Witch

    Messages:
    53
    GitHub:
    Julooooos
    I'm very interesting by your generator, can i see it ?
     
  10. adoswyn

    adoswyn Spider

    Messages:
    8
    GitHub:
    adoswyn
    Probably not. Maybe later I open the server with more cool generation.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.