mapgen_fractal.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. Minetest
  3. Copyright (C) 2015-2018 paramat
  4. Copyright (C) 2015-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "mapgen.h"
  18. #include <cmath>
  19. #include "voxel.h"
  20. #include "noise.h"
  21. #include "mapblock.h"
  22. #include "mapnode.h"
  23. #include "map.h"
  24. #include "content_sao.h"
  25. #include "nodedef.h"
  26. #include "voxelalgorithms.h"
  27. //#include "profiler.h" // For TimeTaker
  28. #include "settings.h" // For g_settings
  29. #include "emerge.h"
  30. #include "dungeongen.h"
  31. #include "cavegen.h"
  32. #include "mg_biome.h"
  33. #include "mg_ore.h"
  34. #include "mg_decoration.h"
  35. #include "mapgen_fractal.h"
  36. FlagDesc flagdesc_mapgen_fractal[] = {
  37. {NULL, 0}
  38. };
  39. ///////////////////////////////////////////////////////////////////////////////////////
  40. MapgenFractal::MapgenFractal(int mapgenid, MapgenFractalParams *params, EmergeManager *emerge)
  41. : MapgenBasic(mapgenid, params, emerge)
  42. {
  43. spflags = params->spflags;
  44. cave_width = params->cave_width;
  45. large_cave_depth = params->large_cave_depth;
  46. lava_depth = params->lava_depth;
  47. dungeon_ymin = params->dungeon_ymin;
  48. dungeon_ymax = params->dungeon_ymax;
  49. fractal = params->fractal;
  50. iterations = params->iterations;
  51. scale = params->scale;
  52. offset = params->offset;
  53. slice_w = params->slice_w;
  54. julia_x = params->julia_x;
  55. julia_y = params->julia_y;
  56. julia_z = params->julia_z;
  57. julia_w = params->julia_w;
  58. //// 2D terrain noise
  59. noise_seabed = new Noise(&params->np_seabed, seed, csize.X, csize.Z);
  60. noise_filler_depth = new Noise(&params->np_filler_depth, seed, csize.X, csize.Z);
  61. MapgenBasic::np_cave1 = params->np_cave1;
  62. MapgenBasic::np_cave2 = params->np_cave2;
  63. formula = fractal / 2 + fractal % 2;
  64. julia = fractal % 2 == 0;
  65. }
  66. MapgenFractal::~MapgenFractal()
  67. {
  68. delete noise_seabed;
  69. delete noise_filler_depth;
  70. }
  71. MapgenFractalParams::MapgenFractalParams():
  72. np_seabed (-14, 9, v3f(600, 600, 600), 41900, 5, 0.6, 2.0),
  73. np_filler_depth (0, 1.2, v3f(150, 150, 150), 261, 3, 0.7, 2.0),
  74. np_cave1 (0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
  75. np_cave2 (0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0)
  76. {
  77. }
  78. void MapgenFractalParams::readParams(const Settings *settings)
  79. {
  80. settings->getFlagStrNoEx("mgfractal_spflags", spflags, flagdesc_mapgen_fractal);
  81. settings->getFloatNoEx("mgfractal_cave_width", cave_width);
  82. settings->getS16NoEx("mgfractal_large_cave_depth", large_cave_depth);
  83. settings->getS16NoEx("mgfractal_lava_depth", lava_depth);
  84. settings->getS16NoEx("mgfractal_dungeon_ymin", dungeon_ymin);
  85. settings->getS16NoEx("mgfractal_dungeon_ymax", dungeon_ymax);
  86. settings->getU16NoEx("mgfractal_fractal", fractal);
  87. settings->getU16NoEx("mgfractal_iterations", iterations);
  88. settings->getV3FNoEx("mgfractal_scale", scale);
  89. settings->getV3FNoEx("mgfractal_offset", offset);
  90. settings->getFloatNoEx("mgfractal_slice_w", slice_w);
  91. settings->getFloatNoEx("mgfractal_julia_x", julia_x);
  92. settings->getFloatNoEx("mgfractal_julia_y", julia_y);
  93. settings->getFloatNoEx("mgfractal_julia_z", julia_z);
  94. settings->getFloatNoEx("mgfractal_julia_w", julia_w);
  95. settings->getNoiseParams("mgfractal_np_seabed", np_seabed);
  96. settings->getNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
  97. settings->getNoiseParams("mgfractal_np_cave1", np_cave1);
  98. settings->getNoiseParams("mgfractal_np_cave2", np_cave2);
  99. }
  100. void MapgenFractalParams::writeParams(Settings *settings) const
  101. {
  102. settings->setFlagStr("mgfractal_spflags", spflags, flagdesc_mapgen_fractal, U32_MAX);
  103. settings->setFloat("mgfractal_cave_width", cave_width);
  104. settings->setS16("mgfractal_large_cave_depth", large_cave_depth);
  105. settings->setS16("mgfractal_lava_depth", lava_depth);
  106. settings->setS16("mgfractal_dungeon_ymin", dungeon_ymin);
  107. settings->setS16("mgfractal_dungeon_ymax", dungeon_ymax);
  108. settings->setU16("mgfractal_fractal", fractal);
  109. settings->setU16("mgfractal_iterations", iterations);
  110. settings->setV3F("mgfractal_scale", scale);
  111. settings->setV3F("mgfractal_offset", offset);
  112. settings->setFloat("mgfractal_slice_w", slice_w);
  113. settings->setFloat("mgfractal_julia_x", julia_x);
  114. settings->setFloat("mgfractal_julia_y", julia_y);
  115. settings->setFloat("mgfractal_julia_z", julia_z);
  116. settings->setFloat("mgfractal_julia_w", julia_w);
  117. settings->setNoiseParams("mgfractal_np_seabed", np_seabed);
  118. settings->setNoiseParams("mgfractal_np_filler_depth", np_filler_depth);
  119. settings->setNoiseParams("mgfractal_np_cave1", np_cave1);
  120. settings->setNoiseParams("mgfractal_np_cave2", np_cave2);
  121. }
  122. /////////////////////////////////////////////////////////////////
  123. int MapgenFractal::getSpawnLevelAtPoint(v2s16 p)
  124. {
  125. bool solid_below = false; // Dry solid node is present below to spawn on
  126. u8 air_count = 0; // Consecutive air nodes above the dry solid node
  127. s16 seabed_level = NoisePerlin2D(&noise_seabed->np, p.X, p.Y, seed);
  128. // Seabed can rise above water_level or might be raised to create dry land
  129. s16 search_start = MYMAX(seabed_level, water_level + 1);
  130. if (seabed_level > water_level)
  131. solid_below = true;
  132. for (s16 y = search_start; y <= search_start + 128; y++) {
  133. if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node
  134. solid_below = true;
  135. air_count = 0;
  136. } else if (solid_below) { // Air above solid node
  137. air_count++;
  138. // 3 to account for snowblock dust
  139. if (air_count == 3)
  140. return y - 2;
  141. }
  142. }
  143. return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
  144. }
  145. void MapgenFractal::makeChunk(BlockMakeData *data)
  146. {
  147. // Pre-conditions
  148. assert(data->vmanip);
  149. assert(data->nodedef);
  150. assert(data->blockpos_requested.X >= data->blockpos_min.X &&
  151. data->blockpos_requested.Y >= data->blockpos_min.Y &&
  152. data->blockpos_requested.Z >= data->blockpos_min.Z);
  153. assert(data->blockpos_requested.X <= data->blockpos_max.X &&
  154. data->blockpos_requested.Y <= data->blockpos_max.Y &&
  155. data->blockpos_requested.Z <= data->blockpos_max.Z);
  156. this->generating = true;
  157. this->vm = data->vmanip;
  158. this->ndef = data->nodedef;
  159. //TimeTaker t("makeChunk");
  160. v3s16 blockpos_min = data->blockpos_min;
  161. v3s16 blockpos_max = data->blockpos_max;
  162. node_min = blockpos_min * MAP_BLOCKSIZE;
  163. node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
  164. full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
  165. full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
  166. blockseed = getBlockSeed2(full_node_min, seed);
  167. // Generate base terrain, mountains, and ridges with initial heightmaps
  168. s16 stone_surface_max_y = generateTerrain();
  169. // Create heightmap
  170. updateHeightmap(node_min, node_max);
  171. // Init biome generator, place biome-specific nodes, and build biomemap
  172. biomegen->calcBiomeNoise(node_min);
  173. generateBiomes();
  174. if (flags & MG_CAVES) {
  175. // Generate tunnels
  176. generateCavesNoiseIntersection(stone_surface_max_y);
  177. // Generate large randomwalk caves
  178. generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
  179. }
  180. if ((flags & MG_DUNGEONS) && full_node_min.Y >= dungeon_ymin &&
  181. full_node_max.Y <= dungeon_ymax)
  182. generateDungeons(stone_surface_max_y);
  183. // Generate the registered decorations
  184. if (flags & MG_DECORATIONS)
  185. m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
  186. // Generate the registered ores
  187. m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
  188. // Sprinkle some dust on top after everything else was generated
  189. dustTopNodes();
  190. //printf("makeChunk: %dms\n", t.stop());
  191. updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
  192. if (flags & MG_LIGHT)
  193. calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
  194. full_node_min, full_node_max);
  195. //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
  196. // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
  197. this->generating = false;
  198. }
  199. bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
  200. {
  201. float cx, cy, cz, cw, ox, oy, oz, ow;
  202. if (julia) { // Julia set
  203. cx = julia_x;
  204. cy = julia_y;
  205. cz = julia_z;
  206. cw = julia_w;
  207. ox = (float)x / scale.X - offset.X;
  208. oy = (float)y / scale.Y - offset.Y;
  209. oz = (float)z / scale.Z - offset.Z;
  210. ow = slice_w;
  211. } else { // Mandelbrot set
  212. cx = (float)x / scale.X - offset.X;
  213. cy = (float)y / scale.Y - offset.Y;
  214. cz = (float)z / scale.Z - offset.Z;
  215. cw = slice_w;
  216. ox = 0.0f;
  217. oy = 0.0f;
  218. oz = 0.0f;
  219. ow = 0.0f;
  220. }
  221. float nx = 0.0f;
  222. float ny = 0.0f;
  223. float nz = 0.0f;
  224. float nw = 0.0f;
  225. for (u16 iter = 0; iter < iterations; iter++) {
  226. switch (formula) {
  227. default:
  228. case 1: // 4D "Roundy"
  229. nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
  230. ny = 2.0f * (ox * oy + oz * ow) + cy;
  231. nz = 2.0f * (ox * oz + oy * ow) + cz;
  232. nw = 2.0f * (ox * ow + oy * oz) + cw;
  233. break;
  234. case 2: // 4D "Squarry"
  235. nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
  236. ny = 2.0f * (ox * oy + oz * ow) + cy;
  237. nz = 2.0f * (ox * oz + oy * ow) + cz;
  238. nw = 2.0f * (ox * ow - oy * oz) + cw;
  239. break;
  240. case 3: // 4D "Mandy Cousin"
  241. nx = ox * ox - oy * oy - oz * oz + ow * ow + cx;
  242. ny = 2.0f * (ox * oy + oz * ow) + cy;
  243. nz = 2.0f * (ox * oz + oy * ow) + cz;
  244. nw = 2.0f * (ox * ow + oy * oz) + cw;
  245. break;
  246. case 4: // 4D "Variation"
  247. nx = ox * ox - oy * oy - oz * oz - ow * ow + cx;
  248. ny = 2.0f * (ox * oy + oz * ow) + cy;
  249. nz = 2.0f * (ox * oz - oy * ow) + cz;
  250. nw = 2.0f * (ox * ow + oy * oz) + cw;
  251. break;
  252. case 5: // 3D "Mandelbrot/Mandelbar"
  253. nx = ox * ox - oy * oy - oz * oz + cx;
  254. ny = 2.0f * ox * oy + cy;
  255. nz = -2.0f * ox * oz + cz;
  256. break;
  257. case 6: // 3D "Christmas Tree"
  258. // Altering the formula here is necessary to avoid division by zero
  259. if (std::fabs(oz) < 0.000000001f) {
  260. nx = ox * ox - oy * oy - oz * oz + cx;
  261. ny = 2.0f * oy * ox + cy;
  262. nz = 4.0f * oz * ox + cz;
  263. } else {
  264. float a = (2.0f * ox) / (std::sqrt(oy * oy + oz * oz));
  265. nx = ox * ox - oy * oy - oz * oz + cx;
  266. ny = a * (oy * oy - oz * oz) + cy;
  267. nz = a * 2.0f * oy * oz + cz;
  268. }
  269. break;
  270. case 7: // 3D "Mandelbulb"
  271. if (std::fabs(oy) < 0.000000001f) {
  272. nx = ox * ox - oz * oz + cx;
  273. ny = cy;
  274. nz = -2.0f * oz * std::sqrt(ox * ox) + cz;
  275. } else {
  276. float a = 1.0f - (oz * oz) / (ox * ox + oy * oy);
  277. nx = (ox * ox - oy * oy) * a + cx;
  278. ny = 2.0f * ox * oy * a + cy;
  279. nz = -2.0f * oz * std::sqrt(ox * ox + oy * oy) + cz;
  280. }
  281. break;
  282. case 8: // 3D "Cosine Mandelbulb"
  283. if (std::fabs(oy) < 0.000000001f) {
  284. nx = 2.0f * ox * oz + cx;
  285. ny = 4.0f * oy * oz + cy;
  286. nz = oz * oz - ox * ox - oy * oy + cz;
  287. } else {
  288. float a = (2.0f * oz) / std::sqrt(ox * ox + oy * oy);
  289. nx = (ox * ox - oy * oy) * a + cx;
  290. ny = 2.0f * ox * oy * a + cy;
  291. nz = oz * oz - ox * ox - oy * oy + cz;
  292. }
  293. break;
  294. case 9: // 4D "Mandelbulb"
  295. float rxy = std::sqrt(ox * ox + oy * oy);
  296. float rxyz = std::sqrt(ox * ox + oy * oy + oz * oz);
  297. if (std::fabs(ow) < 0.000000001f && std::fabs(oz) < 0.000000001f) {
  298. nx = (ox * ox - oy * oy) + cx;
  299. ny = 2.0f * ox * oy + cy;
  300. nz = -2.0f * rxy * oz + cz;
  301. nw = 2.0f * rxyz * ow + cw;
  302. } else {
  303. float a = 1.0f - (ow * ow) / (rxyz * rxyz);
  304. float b = a * (1.0f - (oz * oz) / (rxy * rxy));
  305. nx = (ox * ox - oy * oy) * b + cx;
  306. ny = 2.0f * ox * oy * b + cy;
  307. nz = -2.0f * rxy * oz * a + cz;
  308. nw = 2.0f * rxyz * ow + cw;
  309. }
  310. break;
  311. }
  312. if (nx * nx + ny * ny + nz * nz + nw * nw > 4.0f)
  313. return false;
  314. ox = nx;
  315. oy = ny;
  316. oz = nz;
  317. ow = nw;
  318. }
  319. return true;
  320. }
  321. s16 MapgenFractal::generateTerrain()
  322. {
  323. MapNode n_air(CONTENT_AIR);
  324. MapNode n_stone(c_stone);
  325. MapNode n_water(c_water_source);
  326. s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
  327. u32 index2d = 0;
  328. noise_seabed->perlinMap2D(node_min.X, node_min.Z);
  329. for (s16 z = node_min.Z; z <= node_max.Z; z++) {
  330. for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
  331. u32 vi = vm->m_area.index(node_min.X, y, z);
  332. for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index2d++) {
  333. if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
  334. s16 seabed_height = noise_seabed->result[index2d];
  335. if (y <= seabed_height || getFractalAtPoint(x, y, z)) {
  336. vm->m_data[vi] = n_stone;
  337. if (y > stone_surface_max_y)
  338. stone_surface_max_y = y;
  339. } else if (y <= water_level) {
  340. vm->m_data[vi] = n_water;
  341. } else {
  342. vm->m_data[vi] = n_air;
  343. }
  344. }
  345. }
  346. index2d -= ystride;
  347. }
  348. index2d += ystride;
  349. }
  350. return stone_surface_max_y;
  351. }