A spheres world generator. A new survival challenge.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
7.8 KiB

  1. <?php
  2. /***
  3. * ____ _ _ _
  4. * / ___| _ __ | |__ ___ _ __ __ _ | |__ | | ___
  5. * \___ \ | '_ \ | '_ \ / _ \| '__|/ _` || '_ \ | | / _ \
  6. * ___) || |_) || | | || __/| | | (_| || |_) || || __/
  7. * |____/ | .__/ |_| |_| \___||_| \__,_||_.__/ |_| \___|
  8. * |_|
  9. *
  10. * Spheres world generator. A new survival challenge.
  11. * @author Ad5001 <mail@ad5001.eu>
  12. * @copyright (C) 2017 Ad5001
  13. * @license NTOSL (View LICENSE.md)
  14. * @package Spherical
  15. * @version 1.0.0
  16. * @link https://download.ad5001.eu/en/view.php?name=Spherable&src=github
  17. */
  18. declare(strict_types = 1);
  19. namespace Ad5001\Spherable\generators\spheres;
  20. use pocketmine\level\generator\Generator;
  21. use pocketmine\level\generator\biome\BiomeSelector;
  22. use pocketmine\level\generator\biome\Biome;
  23. use pocketmine\level\generator\object\OreType;
  24. use pocketmine\level\generator\populator\GroundCover;
  25. use pocketmine\level\generator\populator\Ore;
  26. use pocketmine\level\generator\populator\Populator;
  27. use pocketmine\block\Block;
  28. try {
  29. if(!class_exists("pocketmine\\block\\BlockFactory")) {
  30. class_alias("pocketmine\\block\\Block", "pocketmine\\block\\BlockFactory");
  31. }
  32. } catch(Throwable $e){
  33. class_alias("pocketmine\\block\\Block", "pocketmine\\block\\BlockFactory");
  34. }
  35. use pocketmine\level\ChunkManager;
  36. use pocketmine\level\Level;
  37. use pocketmine\math\Vector3;
  38. use pocketmine\utils\Random;
  39. class SpheresGenerator extends Generator {
  40. /** @var Level */
  41. protected $level;
  42. /** @var Random */
  43. protected $random;
  44. /**
  45. * @var array[]
  46. *
  47. * An array of planets made of different blocks.
  48. **/
  49. protected $spheresBlocks = [
  50. [
  51. [Block::IRON_ORE, 0, 25],
  52. [Block::STONE, 0, 75],
  53. ],
  54. [
  55. [Block::COAL_ORE, 0, 25],
  56. [Block::STONE, 0, 75],
  57. ],
  58. [
  59. [Block::GOLD_ORE, 0, 25],
  60. [Block::STONE, 0, 75],
  61. ],
  62. [
  63. [Block::DIAMOND_ORE, 0, 7],
  64. [Block::LAPIS_BLOCK, 0, 93],
  65. ],
  66. [
  67. [Block::DIAMOND_ORE, 0, 5],
  68. [Block::REDSTONE_BLOCK, 0, 95],
  69. ],
  70. [
  71. [Block::DIAMOND_ORE, 0, 5],
  72. [Block::WOOD, 12, 95],
  73. ],
  74. [
  75. [Block::DIAMOND_ORE, 0, 5],
  76. [Block::WOOD, 13, 95],
  77. ],
  78. [
  79. [Block::DIAMOND_ORE, 0, 5],
  80. [Block::WOOD, 14, 95],
  81. ],
  82. [
  83. [Block::DIAMOND_ORE, 0, 5],
  84. [Block::WOOD, 15, 95],
  85. ],
  86. [
  87. [Block::DIAMOND_ORE, 0, 5],
  88. [Block::WOOD2, 12, 95],
  89. ],
  90. [
  91. [Block::DIAMOND_ORE, 0, 5],
  92. [Block::WOOD2, 13, 95],
  93. ],
  94. [
  95. [Block::DIAMOND_ORE, 0, 5],
  96. [Block::PLANKS, 0, 95],
  97. ],
  98. [
  99. [Block::DIAMOND_ORE, 0, 5],
  100. [Block::LEAVES, 4, 95],
  101. ],
  102. [
  103. [Block::DIAMOND_ORE, 0, 7],
  104. [Block::NOTEBLOCK, 0, 93],
  105. ],
  106. [
  107. [Block::DIAMOND_ORE, 0, 3],
  108. [Block::SNOW_BLOCK, 0, 97],
  109. ],
  110. [
  111. [Block::DIAMOND_ORE, 0, 5],
  112. [Block::COBWEB, 0, 95],
  113. ],
  114. [
  115. [Block::DIAMOND_ORE, 0, 5],
  116. [Block::WOOL, 0, 95],
  117. ],
  118. [
  119. [Block::DIAMOND_ORE, 0, 5],
  120. [Block::WOOL, 1, 95],
  121. ],
  122. [
  123. [Block::DIAMOND_ORE, 0, 5],
  124. [Block::WOOL, 3, 95],
  125. ],
  126. [
  127. [Block::DIAMOND_ORE, 0, 5],
  128. [Block::WOOL, 4, 95],
  129. ],
  130. [
  131. [Block::DIAMOND_ORE, 0, 5],
  132. [Block::WOOL, 0, 95],
  133. ],
  134. [
  135. [Block::DIAMOND_ORE, 0, 7],
  136. [Block::BOOKSHELF, 0, 90],
  137. ],
  138. [
  139. [Block::DIAMOND_ORE, 0, 30],
  140. [Block::OBSIDIAN, 0, 70],
  141. ],
  142. [
  143. [Block::DIAMOND_ORE, 0, 7],
  144. [Block::STONE_BRICK, 0, 90],
  145. ],
  146. [
  147. [Block::DIAMOND_ORE, 0, 7],
  148. [Block::GRAVEL, 0, 40],
  149. [Block::STONE, 0, 50],
  150. ],
  151. [
  152. [Block::DIAMOND_ORE, 0, 7],
  153. [Block::SAND, 0, 40],
  154. [Block::SANDSTONE, 0, 53],
  155. ],
  156. [
  157. [Block::DIAMOND_ORE, 0, 7],
  158. [Block::PACKED_ICE, 0, 93],
  159. ],
  160. [
  161. [Block::DIAMOND_ORE, 0, 3],
  162. [Block::SLIME_BLOCK, 0, 97],
  163. ],
  164. [
  165. [Block::DIAMOND_ORE, 0, 5],
  166. [Block::QUARTZ_BLOCK, 0, 95],
  167. ],
  168. [
  169. [Block::DIAMOND_ORE, 0, 2],
  170. [Block::NETHERRACK, 0, 98],
  171. ],
  172. [
  173. [Block::DIAMOND_ORE, 0, 5],
  174. [Block::EMERALD_ORE, 0, 95],
  175. ],
  176. [
  177. [Block::DIAMOND_ORE, 0, 7],
  178. [Block::REDSTONE_LAMP, 0, 90],
  179. ],
  180. [
  181. [Block::DIAMOND_ORE, 0, 10],
  182. [Block::END_STONE, 0, 90],
  183. ],
  184. [
  185. [Block::DIAMOND_ORE, 0, 10],
  186. [Block::NETHER_BRICK_BLOCK, 0, 90],
  187. ],
  188. [
  189. [Block::DIAMOND_ORE, 0, 10],
  190. [Block::MELON_BLOCK, 0, 90],
  191. ],
  192. [
  193. [Block::DIAMOND_ORE, 0, 7],
  194. [Block::GLOWSTONE, 0, 93],
  195. ],
  196. [
  197. [Block::DIAMOND_ORE, 0, 7],
  198. [Block::PUMPKIN, 0, 93],
  199. ],
  200. [
  201. [Block::DIAMOND_ORE, 0, 10],
  202. [Block::SOUL_SAND, 0, 90],
  203. ],
  204. [
  205. [Block::DIAMOND_ORE, 0, 10],
  206. [Block::SPONGE, 0, 90],
  207. ],
  208. [
  209. [Block::DIAMOND_ORE, 0, 10],
  210. [Block::PRISMARINE, 0, 90],
  211. ],
  212. [
  213. [Block::DIAMOND_ORE, 0, 10],
  214. [Block::SEA_LANTERN, 0, 90],
  215. ],
  216. [
  217. [Block::DIAMOND_ORE, 0, 10],
  218. [Block::NETHER_REACTOR, 0, 90],
  219. ],
  220. ];
  221. public function __construct(array $options = []){}
  222. /**
  223. * Inits the class for the var
  224. * @param ChunkManager $level
  225. * @param Random $random
  226. * @return void
  227. */
  228. public function init(ChunkManager $level, Random $random) {
  229. $this->level = $level;
  230. $this->random = $random;
  231. }
  232. /***
  233. * Returns the name of the generator
  234. *
  235. * @return string
  236. */
  237. public function getName() : string{
  238. return "spheres";
  239. }
  240. /**
  241. * Returns the settings of the generator
  242. *
  243. * @return array
  244. */
  245. public function getSettings() : array{
  246. return [];
  247. }
  248. /**
  249. * Generates a chunk
  250. *
  251. * @param int $chunkX
  252. * @param int $chunkZ
  253. * @return void
  254. */
  255. public function generateChunk(int $chunkX, int $chunkZ){
  256. // Leave blank, planets will be generated later
  257. $chunk = $this->level->getChunk($chunkX, $chunkZ);
  258. for($x = 0; $x < 16; $x++) {
  259. for($z = 0; $z < 16; $z++) {
  260. $chunk->setBiomeId($x, $z, 1);
  261. if($chunkX == 16 && $chunkZ == 16) $chunk->setBlockId($x, 254, $z, 2);
  262. }
  263. }
  264. $chunk->setGenerated();
  265. }
  266. /**
  267. * Populates the chunk with planets
  268. *
  269. * @param int $chunkX
  270. * @param int $chunkZ
  271. * @return void
  272. */
  273. public function populateChunk(int $chunkX, int $chunkZ){
  274. $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
  275. $chunk = $this->level->getChunk($chunkX, $chunkZ);
  276. $count = $this->random->nextRange(1, 4);
  277. for($i = 0; $i <= $count; $i++){
  278. $y = $this->random->nextRange(17, Level::Y_MAX - 25);
  279. $maxRadius = $y % 10;
  280. if($maxRadius < 6) $maxRadius = 6;
  281. // $maxRadius is situated between 12 and 20 depending on Y choosen
  282. // Let's add a little bit more random
  283. $radius = $this->random->nextRange(5, (int) round($maxRadius));
  284. // Generating planet
  285. $x = $chunkX * 16 + $this->random->nextRange(0, 15);
  286. $z = $chunkZ * 16 + $this->random->nextRange(0, 15);
  287. $center = new Vector3($x, $y, $z);
  288. $this->generatePlanet($center, $radius);
  289. }
  290. }
  291. /**
  292. * Returns the dafault spawn
  293. *
  294. * @return void
  295. */
  296. public function getSpawn() : Vector3{
  297. return new Vector3(264, 255, 264);
  298. }
  299. /**
  300. * Generates a planet
  301. * psmcoreactplugin createlevel4psm Welp spheres 9247603569486
  302. *
  303. * @param Vector3 $center
  304. * @param int $radius
  305. * @return void
  306. */
  307. public function generatePlanet(Vector3 $center, int $radius){
  308. $radiusSquared = $radius ** 2;
  309. $currentSphereBlocks = $this->spheresBlocks[array_rand($this->spheresBlocks)];
  310. for ($x = $center->x - $radius; $x <= $center->x + $radius; $x++) {
  311. $xsquared = ($center->x - $x) * ($center->x - $x);
  312. for ($y = $center->y - $radius; $y <= $center->y + $radius; $y++) {
  313. $ysquared = ($center->y - $y) * ($center->y - $y);
  314. for ($z = $center->z - $radius; $z <= $center->z + $radius; $z++) {
  315. $zsquared = ($center->z - $z) * ($center->z - $z);
  316. if($xsquared + $ysquared + $zsquared < $radiusSquared) {
  317. // Choosing a random block to place
  318. $rand = $this->random->nextBoundedInt(100) + 1;
  319. foreach($currentSphereBlocks as $block){
  320. if($rand > $block[2]) {
  321. $rand = $block[2];
  322. continue;
  323. } else {
  324. $this->level->setBlockIdAt($x, $y, $z, $block[0], false, false);
  325. $this->level->setBlockDataAt($x, $y, $z, $block[1], false, false);
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }