PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
level/generator/populator/TallGrass.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\level\generator\populator;
23 
27 
28 class TallGrass extends Populator{
30  private $level;
31  private $randomAmount;
32  private $baseAmount;
33 
34  public function setRandomAmount($amount){
35  $this->randomAmount = $amount;
36  }
37 
38  public function setBaseAmount($amount){
39  $this->baseAmount = $amount;
40  }
41 
42  public function populate(ChunkManager $level, $chunkX, $chunkZ, Random $random){
43  $this->level = $level;
44  $amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount;
45  for($i = 0; $i < $amount; ++$i){
46  $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
47  $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
48  for($size = 30; $size > 0; --$size){
49  $xx = $x - 7 + $random->nextRange(0, 15);
50  $zz = $z - 7 + $random->nextRange(0, 15);
51  $yy = $this->getHighestWorkableBlock($xx, $zz);
52 
53  if($yy !== -1 and $this->canTallGrassStay($xx, $yy, $zz)){
54  $this->level->setBlockIdAt($xx, $yy, $zz, Block::TALL_GRASS);
55  $this->level->setBlockDataAt($xx, $yy, $zz, 1);
56  }
57  }
58  }
59  }
60 
61  private function canTallGrassStay($x, $y, $z){
62  return $this->level->getBlockIdAt($x, $y, $z) === Block::AIR and $this->level->getBlockIdAt($x, $y - 1, $z) === Block::GRASS;
63  }
64 
65  private function getHighestWorkableBlock($x, $z){
66  for($y = 128; $y > 0; --$y){
67  $b = $this->level->getBlockIdAt($x, $y, $z);
68  if($b === Block::AIR or $b === Block::LEAVES){
69  if(--$y <= 0){
70  return -1;
71  }
72  }else{
73  break;
74  }
75  }
76 
77  return ++$y;
78  }
79 }
nextRange($start=0, $end=0x7fffffff)
Definition: Random.php:112