mapgen_fractal.cpp 12 KB

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