PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/level/generator/normal/Normal.php

https://gitlab.com/ondraczsk/YouTubeCraftCore
PHP | 305 lines | 225 code | 48 blank | 32 comment | 23 complexity | 3e2957af43c8c2f952156cd281495ade MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * _ _ _ __ __ _
  5. * (_) (_) | | \/ (_)
  6. * _ _ __ ___ __ _ __ _ _ ___ __ _| | \ / |_ _ __ ___
  7. * | | '_ ` _ \ / _` |/ _` | |/ __/ _` | | |\/| | | '_ \ / _ \
  8. * | | | | | | | (_| | (_| | | (_| (_| | | | | | | | | | __/
  9. * |_|_| |_| |_|\__,_|\__, |_|\___\__,_|_|_| |_|_|_| |_|\___|
  10. * __/ |
  11. * |___/
  12. *
  13. * This program is a third party build by ImagicalMine.
  14. *
  15. * PocketMine is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * @author ImagicalMine Team
  21. * @link http://forums.imagicalcorp.ml/
  22. *
  23. *
  24. */
  25. namespace pocketmine\level\generator\normal;
  26. use pocketmine\block\Block;
  27. use pocketmine\block\CoalOre;
  28. use pocketmine\block\DiamondOre;
  29. use pocketmine\block\Dirt;
  30. use pocketmine\block\GoldOre;
  31. use pocketmine\block\Gravel;
  32. use pocketmine\block\IronOre;
  33. use pocketmine\block\LapisOre;
  34. use pocketmine\block\RedstoneOre;
  35. use pocketmine\block\Stone;
  36. use pocketmine\level\ChunkManager;
  37. use pocketmine\level\generator\biome\Biome;
  38. use pocketmine\level\generator\biome\BiomeSelector;
  39. use pocketmine\level\generator\Generator;
  40. use pocketmine\level\generator\noise\Simplex;
  41. use pocketmine\level\generator\object\OreType;
  42. use pocketmine\level\generator\populator\Cave;
  43. use pocketmine\level\generator\populator\GroundCover;
  44. use pocketmine\level\generator\populator\Ore;
  45. use pocketmine\level\generator\populator\Populator;
  46. use pocketmine\level\Level;
  47. use pocketmine\math\Vector3 as Vector3;
  48. use pocketmine\utils\Random;
  49. class Normal extends Generator{
  50. const NAME = "Normal";
  51. /** @var Populator[] */
  52. protected $populators = [];
  53. /** @var ChunkManager */
  54. protected $level;
  55. /** @var Random */
  56. protected $random;
  57. protected $waterHeight = 62;
  58. protected $bedrockDepth = 5;
  59. /** @var Populator[] */
  60. protected $generationPopulators = [];
  61. /** @var Simplex */
  62. protected $noiseBase;
  63. /** @var BiomeSelector */
  64. protected $selector;
  65. private static $GAUSSIAN_KERNEL = null;
  66. private static $SMOOTH_SIZE = 2;
  67. public function __construct(array $options = []){
  68. if(self::$GAUSSIAN_KERNEL === null){
  69. self::generateKernel();
  70. }
  71. }
  72. private static function generateKernel(){
  73. self::$GAUSSIAN_KERNEL = [];
  74. $bellSize = 1 / self::$SMOOTH_SIZE;
  75. $bellHeight = 2 * self::$SMOOTH_SIZE;
  76. for($sx = -self::$SMOOTH_SIZE; $sx <= self::$SMOOTH_SIZE; ++$sx){
  77. self::$GAUSSIAN_KERNEL[$sx + self::$SMOOTH_SIZE] = [];
  78. for($sz = -self::$SMOOTH_SIZE; $sz <= self::$SMOOTH_SIZE; ++$sz){
  79. $bx = $bellSize * $sx;
  80. $bz = $bellSize * $sz;
  81. self::$GAUSSIAN_KERNEL[$sx + self::$SMOOTH_SIZE][$sz + self::$SMOOTH_SIZE] = $bellHeight * exp(-($bx * $bx + $bz * $bz) / 2);
  82. }
  83. }
  84. }
  85. public function getName() : string{
  86. return self::NAME;
  87. }
  88. public function getWaterHeight() : int{
  89. return $this->waterHeight;
  90. }
  91. public function getSettings(){
  92. return [];
  93. }
  94. public function pickBiome($x, $z){
  95. $hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->getSeed();
  96. $hash *= $hash + 223;
  97. $xNoise = $hash >> 20 & 3;
  98. $zNoise = $hash >> 22 & 3;
  99. if($xNoise == 3){
  100. $xNoise = 1;
  101. }
  102. if($zNoise == 3){
  103. $zNoise = 1;
  104. }
  105. return $this->selector->pickBiome($x + $xNoise - 1, $z + $zNoise - 1);
  106. }
  107. public function init(ChunkManager $level, Random $random){
  108. $this->level = $level;
  109. $this->random = $random;
  110. $this->random->setSeed($this->level->getSeed());
  111. $this->noiseBase = new Simplex($this->random, 4, 1 / 4, 1 / 32);
  112. $this->random->setSeed($this->level->getSeed());
  113. $this->selector = new BiomeSelector($this->random, function($temperature, $rainfall){
  114. if($rainfall < 0.25){
  115. if($temperature < 0.7){
  116. return Biome::OCEAN;
  117. }elseif($temperature < 0.85){
  118. return Biome::RIVER;
  119. }else{
  120. return Biome::SWAMP;
  121. }
  122. }elseif($rainfall < 0.60){
  123. if($temperature < 0.25){
  124. return Biome::ICE_PLAINS;
  125. }elseif($temperature < 0.75){
  126. return Biome::PLAINS;
  127. }else{
  128. return Biome::DESERT;
  129. }
  130. }elseif($rainfall < 0.80){
  131. if($temperature < 0.25){
  132. return Biome::TAIGA;
  133. }elseif($temperature < 0.75){
  134. return Biome::FOREST;
  135. }else{
  136. return Biome::BIRCH_FOREST;
  137. }
  138. }else{
  139. if($temperature < 0.25){
  140. return Biome::MOUNTAINS;
  141. }elseif($temperature < 0.70){
  142. return Biome::SMALL_MOUNTAINS;
  143. }else{
  144. return Biome::RIVER;
  145. }
  146. }
  147. }, Biome::getBiome(Biome::OCEAN));
  148. $this->selector->addBiome(Biome::getBiome(Biome::OCEAN));
  149. $this->selector->addBiome(Biome::getBiome(Biome::PLAINS));
  150. $this->selector->addBiome(Biome::getBiome(Biome::DESERT));
  151. $this->selector->addBiome(Biome::getBiome(Biome::MOUNTAINS));
  152. $this->selector->addBiome(Biome::getBiome(Biome::FOREST));
  153. $this->selector->addBiome(Biome::getBiome(Biome::TAIGA));
  154. $this->selector->addBiome(Biome::getBiome(Biome::SWAMP));
  155. $this->selector->addBiome(Biome::getBiome(Biome::RIVER));
  156. $this->selector->addBiome(Biome::getBiome(Biome::ICE_PLAINS));
  157. $this->selector->addBiome(Biome::getBiome(Biome::SMALL_MOUNTAINS));
  158. $this->selector->addBiome(Biome::getBiome(Biome::BIRCH_FOREST));
  159. $this->selector->recalculate();
  160. $cover = new GroundCover();
  161. $this->generationPopulators[] = $cover;
  162. $cave = new Cave();
  163. $this->populators[] = $cave;
  164. $ores = new Ore();
  165. $ores->setOreTypes([
  166. new OreType(new CoalOre(), 20, 16, 0, 128),
  167. new OreType(New IronOre(), 20, 8, 0, 64),
  168. new OreType(new RedstoneOre(), 8, 7, 0, 16),
  169. new OreType(new LapisOre(), 1, 6, 0, 32),
  170. new OreType(new GoldOre(), 2, 8, 0, 32),
  171. new OreType(new DiamondOre(), 1, 7, 0, 16),
  172. new OreType(new Dirt(), 20, 32, 0, 128),
  173. new OreType(new Stone(Stone::GRANITE), 20, 32, 0, 128),
  174. new OreType(new Stone(Stone::DIORITE), 20, 32, 0, 128),
  175. new OreType(new Stone(Stone::ANDESITE), 20, 32, 0, 128),
  176. new OreType(new Gravel(), 10, 16, 0, 128)
  177. ]);
  178. $this->populators[] = $ores;
  179. }
  180. public function generateChunk($chunkX, $chunkZ){
  181. $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
  182. $noise = Generator::getFastNoise3D($this->noiseBase, 16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16);
  183. $chunk = $this->level->getChunk($chunkX, $chunkZ);
  184. $biomeCache = [];
  185. for($x = 0; $x < 16; ++$x){
  186. for($z = 0; $z < 16; ++$z){
  187. $minSum = 0;
  188. $maxSum = 0;
  189. $weightSum = 0;
  190. $biome = $this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z);
  191. $chunk->setBiomeId($x, $z, $biome->getId());
  192. $color = [0, 0, 0];
  193. for($sx = -self::$SMOOTH_SIZE; $sx <= self::$SMOOTH_SIZE; ++$sx){
  194. for($sz = -self::$SMOOTH_SIZE; $sz <= self::$SMOOTH_SIZE; ++$sz){
  195. $weight = self::$GAUSSIAN_KERNEL[$sx + self::$SMOOTH_SIZE][$sz + self::$SMOOTH_SIZE];
  196. if($sx === 0 and $sz === 0){
  197. $adjacent = $biome;
  198. }else{
  199. $index = Level::chunkHash($chunkX * 16 + $x + $sx, $chunkZ * 16 + $z + $sz);
  200. if(isset($biomeCache[$index])){
  201. $adjacent = $biomeCache[$index];
  202. }else{
  203. $biomeCache[$index] = $adjacent = $this->pickBiome($chunkX * 16 + $x + $sx, $chunkZ * 16 + $z + $sz);
  204. }
  205. }
  206. $minSum += ($adjacent->getMinElevation() - 1) * $weight;
  207. $maxSum += $adjacent->getMaxElevation() * $weight;
  208. $bColor = $adjacent->getColor();
  209. $color[0] += (($bColor >> 16) ** 2) * $weight;
  210. $color[1] += ((($bColor >> 8) & 0xff) ** 2) * $weight;
  211. $color[2] += (($bColor & 0xff) ** 2) * $weight;
  212. $weightSum += $weight;
  213. }
  214. }
  215. $minSum /= $weightSum;
  216. $maxSum /= $weightSum;
  217. $chunk->setBiomeColor($x, $z, sqrt($color[0] / $weightSum), sqrt($color[1] / $weightSum), sqrt($color[2] / $weightSum));
  218. $solidLand = false;
  219. for($y = 127; $y >= 0; --$y){
  220. if($y === 0){
  221. $chunk->setBlockId($x, $y, $z, Block::BEDROCK);
  222. continue;
  223. }
  224. // A noiseAdjustment of 1 will guarantee ground, a noiseAdjustment of -1 will guarantee air.
  225. //$effHeight = min($y - $smoothHeight - $minSum,
  226. $noiseAdjustment = 2 * (($maxSum - $y) / ($maxSum - $minSum)) - 1;
  227. // To generate caves, we bring the noiseAdjustment down away from 1.
  228. $caveLevel = $minSum - 10;
  229. $distAboveCaveLevel = max(0, $y - $caveLevel); // must be positive
  230. $noiseAdjustment = min($noiseAdjustment, 0.4 + ($distAboveCaveLevel / 10));
  231. $noiseValue = $noise[$x][$z][$y] + $noiseAdjustment;
  232. if($noiseValue > 0){
  233. $chunk->setBlockId($x, $y, $z, Block::STONE);
  234. $solidLand = true;
  235. }elseif($y <= $this->waterHeight && $solidLand == false){
  236. $chunk->setBlockId($x, $y, $z, Block::STILL_WATER);
  237. }
  238. }
  239. }
  240. }
  241. foreach($this->generationPopulators as $populator){
  242. $populator->populate($this->level, $chunkX, $chunkZ, $this->random);
  243. }
  244. }
  245. public function populateChunk($chunkX, $chunkZ){
  246. $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
  247. foreach($this->populators as $populator){
  248. $populator->populate($this->level, $chunkX, $chunkZ, $this->random);
  249. }
  250. $chunk = $this->level->getChunk($chunkX, $chunkZ);
  251. $biome = Biome::getBiome($chunk->getBiomeId(7, 7));
  252. $biome->populateChunk($this->level, $chunkX, $chunkZ, $this->random);
  253. }
  254. public function getSpawn(){
  255. return new Vector3(127.5, 128, 127.5);
  256. }
  257. }