PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Flat.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;
23 
38 
39 class Flat extends Generator{
41  private $level;
43  private $chunk;
45  private $random;
47  private $populators = [];
48  private $structure, $chunks, $options, $floorLevel, $preset;
49 
50  public function getSettings(){
51  return $this->options;
52  }
53 
54  public function getName(){
55  return "flat";
56  }
57 
58  public function __construct(array $options = []){
59  $this->preset = "2;7,2x3,2;1;";
60  //$this->preset = "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)";
61  $this->options = $options;
62 
63  if(isset($this->options["decoration"])){
64  $ores = new Ore();
65  $ores->setOreTypes([
66  new object\OreType(new CoalOre(), 20, 16, 0, 128),
67  new object\OreType(New IronOre(), 20, 8, 0, 64),
68  new object\OreType(new RedstoneOre(), 8, 7, 0, 16),
69  new object\OreType(new LapisOre(), 1, 6, 0, 32),
70  new object\OreType(new GoldOre(), 2, 8, 0, 32),
71  new object\OreType(new DiamondOre(), 1, 7, 0, 16),
72  new object\OreType(new Dirt(), 20, 32, 0, 128),
73  new object\OreType(new Gravel(), 10, 16, 0, 128),
74  ]);
75  $this->populators[] = $ores;
76  }
77 
78  /*if(isset($this->options["mineshaft"])){
79  $this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01);
80  }*/
81  }
82 
83  protected function parsePreset($preset){
84  $this->preset = $preset;
85  $preset = explode(";", $preset);
86  $version = (int) $preset[0];
87  $blocks = @$preset[1];
88  $biome = isset($preset[2]) ? $preset[2] : 1;
89  $options = isset($preset[3]) ? $preset[3] : "";
90  preg_match_all('#(([0-9]{0,})x?([0-9]{1,3}:?[0-9]{0,2})),?#', $blocks, $matches);
91  $y = 0;
92  $this->structure = [];
93  $this->chunks = [];
94  foreach($matches[3] as $i => $b){
95  $b = Item::fromString($b);
96  $cnt = $matches[2][$i] === "" ? 1 : intval($matches[2][$i]);
97  for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
98  $this->structure[$cY] = [$b->getId(), $b->getDamage()];
99  }
100  }
101 
102  $this->floorLevel = $y;
103 
104  for(; $y < 0xFF; ++$y){
105  $this->structure[$y] = [0, 0];
106  }
107 
108 
109  $this->chunk = $this->level->getChunk(0, 0);
110  $this->chunk->setGenerated();
111 
112  for($Z = 0; $Z < 16; ++$Z){
113  for($X = 0; $X < 16; ++$X){
114  for($y = 0; $y < 128; ++$y){
115  if($this->structure[$y][0] !== 0){
116  $this->chunk->setBlockId($X, $y, $Z, $this->structure[$y][0]);
117  }
118  if($this->structure[$y][0] !== 0){
119  $this->chunk->setBlockData($X, $y, $Z, $this->structure[$y][1]);
120  }
121  }
122  }
123  }
124 
125 
126  preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $options, $matches);
127  foreach($matches[2] as $i => $option){
128  $params = true;
129  if($matches[3][$i] !== ""){
130  $params = [];
131  $p = explode(" ", $matches[3][$i]);
132  foreach($p as $k){
133  $k = explode("=", $k);
134  if(isset($k[1])){
135  $params[$k[0]] = $k[1];
136  }
137  }
138  }
139  $this->options[$option] = $params;
140  }
141  }
142 
143  public function init(GenerationChunkManager $level, Random $random){
144  $this->level = $level;
145  $this->random = $random;
146 
147  if(isset($this->options["preset"]) and $this->options["preset"] != ""){
148  $this->parsePreset($this->options["preset"]);
149  }else{
150  $this->parsePreset($this->preset);
151  }
152 
153  }
154 
155  public function generateChunk($chunkX, $chunkZ){
156  $chunk = clone $this->chunk;
157  $chunk->setX($chunkX);
158  $chunk->setZ($chunkZ);
159  $this->level->setChunk($chunkX, $chunkZ, $chunk);
160  }
161 
162  public function populateChunk($chunkX, $chunkZ){
163  $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
164  foreach($this->populators as $populator){
165  $populator->populate($this->level, $chunkX, $chunkZ, $this->random);
166  }
167 
168  }
169 
170  public function getSpawn(){
171  return new Vector3(128, $this->floorLevel, 128);
172  }
173 }