mapgen_fractal.cpp 13 KB

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