Final Minetest - Please use Linux & Windows build kits at: https://downloads.minetest.org/
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.

cavegen.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2010-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  5. Copyright (C) 2015-2018 paramat
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "util/numeric.h"
  19. #include "map.h"
  20. #include "mapgen.h"
  21. #include "mapgen_v5.h"
  22. #include "mapgen_v6.h"
  23. #include "mapgen_v7.h"
  24. #include "mg_biome.h"
  25. #include "cavegen.h"
  26. static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0);
  27. ////
  28. //// CavesNoiseIntersection
  29. ////
  30. CavesNoiseIntersection::CavesNoiseIntersection(
  31. INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize,
  32. NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width)
  33. {
  34. assert(nodedef);
  35. assert(biomemgr);
  36. m_ndef = nodedef;
  37. m_bmgr = biomemgr;
  38. m_csize = chunksize;
  39. m_cave_width = cave_width;
  40. m_ystride = m_csize.X;
  41. m_zstride_1d = m_csize.X * (m_csize.Y + 1);
  42. // Noises are created using 1-down overgeneration
  43. // A Nx-by-1-by-Nz-sized plane is at the bottom of the desired for
  44. // re-carving the solid overtop placed for blocking sunlight
  45. noise_cave1 = new Noise(np_cave1, seed, m_csize.X, m_csize.Y + 1, m_csize.Z);
  46. noise_cave2 = new Noise(np_cave2, seed, m_csize.X, m_csize.Y + 1, m_csize.Z);
  47. }
  48. CavesNoiseIntersection::~CavesNoiseIntersection()
  49. {
  50. delete noise_cave1;
  51. delete noise_cave2;
  52. }
  53. void CavesNoiseIntersection::generateCaves(MMVManip *vm,
  54. v3s16 nmin, v3s16 nmax, u8 *biomemap)
  55. {
  56. assert(vm);
  57. assert(biomemap);
  58. noise_cave1->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
  59. noise_cave2->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
  60. const v3s16 &em = vm->m_area.getExtent();
  61. u32 index2d = 0; // Biomemap index
  62. for (s16 z = nmin.Z; z <= nmax.Z; z++)
  63. for (s16 x = nmin.X; x <= nmax.X; x++, index2d++) {
  64. bool column_is_open = false; // Is column open to overground
  65. bool is_under_river = false; // Is column under river water
  66. bool is_under_tunnel = false; // Is tunnel or is under tunnel
  67. bool is_top_filler_above = false; // Is top or filler above node
  68. // Indexes at column top
  69. u32 vi = vm->m_area.index(x, nmax.Y, z);
  70. u32 index3d = (z - nmin.Z) * m_zstride_1d + m_csize.Y * m_ystride +
  71. (x - nmin.X); // 3D noise index
  72. // Biome of column
  73. Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index2d]);
  74. u16 depth_top = biome->depth_top;
  75. u16 base_filler = depth_top + biome->depth_filler;
  76. u16 depth_riverbed = biome->depth_riverbed;
  77. u16 nplaced = 0;
  78. // Don't excavate the overgenerated stone at nmax.Y + 1,
  79. // this creates a 'roof' over the tunnel, preventing light in
  80. // tunnels at mapchunk borders when generating mapchunks upwards.
  81. // This 'roof' is removed when the mapchunk above is generated.
  82. for (s16 y = nmax.Y; y >= nmin.Y - 1; y--,
  83. index3d -= m_ystride,
  84. vm->m_area.add_y(em, vi, -1)) {
  85. content_t c = vm->m_data[vi].getContent();
  86. if (c == CONTENT_AIR || c == biome->c_water_top ||
  87. c == biome->c_water) {
  88. column_is_open = true;
  89. is_top_filler_above = false;
  90. continue;
  91. } else if (c == biome->c_river_water) {
  92. column_is_open = true;
  93. is_under_river = true;
  94. is_top_filler_above = false;
  95. continue;
  96. }
  97. // Ground
  98. float d1 = contour(noise_cave1->result[index3d]);
  99. float d2 = contour(noise_cave2->result[index3d]);
  100. if (d1 * d2 > m_cave_width && m_ndef->get(c).is_ground_content) {
  101. // In tunnel and ground content, excavate
  102. vm->m_data[vi] = MapNode(CONTENT_AIR);
  103. is_under_tunnel = true;
  104. // If tunnel roof is top or filler, replace with stone
  105. if (is_top_filler_above)
  106. vm->m_data[vi + em.X] = MapNode(biome->c_stone);
  107. is_top_filler_above = false;
  108. } else if (column_is_open && is_under_tunnel &&
  109. (c == biome->c_stone || c == biome->c_filler)) {
  110. // Tunnel entrance floor, place biome surface nodes
  111. if (is_under_river) {
  112. if (nplaced < depth_riverbed) {
  113. vm->m_data[vi] = MapNode(biome->c_riverbed);
  114. is_top_filler_above = true;
  115. nplaced++;
  116. } else {
  117. // Disable top/filler placement
  118. column_is_open = false;
  119. is_under_river = false;
  120. is_under_tunnel = false;
  121. }
  122. } else if (nplaced < depth_top) {
  123. vm->m_data[vi] = MapNode(biome->c_top);
  124. is_top_filler_above = true;
  125. nplaced++;
  126. } else if (nplaced < base_filler) {
  127. vm->m_data[vi] = MapNode(biome->c_filler);
  128. is_top_filler_above = true;
  129. nplaced++;
  130. } else {
  131. // Disable top/filler placement
  132. column_is_open = false;
  133. is_under_tunnel = false;
  134. }
  135. } else {
  136. // Not tunnel or tunnel entrance floor
  137. // Check node for possible replacing with stone for tunnel roof
  138. if (c == biome->c_top || c == biome->c_filler)
  139. is_top_filler_above = true;
  140. column_is_open = false;
  141. }
  142. }
  143. }
  144. }
  145. ////
  146. //// CavernsNoise
  147. ////
  148. CavernsNoise::CavernsNoise(
  149. INodeDefManager *nodedef, v3s16 chunksize, NoiseParams *np_cavern,
  150. s32 seed, float cavern_limit, float cavern_taper, float cavern_threshold)
  151. {
  152. assert(nodedef);
  153. m_ndef = nodedef;
  154. m_csize = chunksize;
  155. m_cavern_limit = cavern_limit;
  156. m_cavern_taper = cavern_taper;
  157. m_cavern_threshold = cavern_threshold;
  158. m_ystride = m_csize.X;
  159. m_zstride_1d = m_csize.X * (m_csize.Y + 1);
  160. // Noise is created using 1-down overgeneration
  161. // A Nx-by-1-by-Nz-sized plane is at the bottom of the desired for
  162. // re-carving the solid overtop placed for blocking sunlight
  163. noise_cavern = new Noise(np_cavern, seed, m_csize.X, m_csize.Y + 1, m_csize.Z);
  164. c_water_source = m_ndef->getId("mapgen_water_source");
  165. if (c_water_source == CONTENT_IGNORE)
  166. c_water_source = CONTENT_AIR;
  167. c_lava_source = m_ndef->getId("mapgen_lava_source");
  168. if (c_lava_source == CONTENT_IGNORE)
  169. c_lava_source = CONTENT_AIR;
  170. }
  171. CavernsNoise::~CavernsNoise()
  172. {
  173. delete noise_cavern;
  174. }
  175. bool CavernsNoise::generateCaverns(MMVManip *vm, v3s16 nmin, v3s16 nmax)
  176. {
  177. assert(vm);
  178. // Calculate noise
  179. noise_cavern->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
  180. // Cache cavern_amp values
  181. float *cavern_amp = new float[m_csize.Y + 1];
  182. u8 cavern_amp_index = 0; // Index zero at column top
  183. for (s16 y = nmax.Y; y >= nmin.Y - 1; y--, cavern_amp_index++) {
  184. cavern_amp[cavern_amp_index] =
  185. MYMIN((m_cavern_limit - y) / (float)m_cavern_taper, 1.0f);
  186. }
  187. //// Place nodes
  188. bool near_cavern = false;
  189. const v3s16 &em = vm->m_area.getExtent();
  190. u32 index2d = 0;
  191. for (s16 z = nmin.Z; z <= nmax.Z; z++)
  192. for (s16 x = nmin.X; x <= nmax.X; x++, index2d++) {
  193. // Reset cave_amp index to column top
  194. cavern_amp_index = 0;
  195. // Initial voxelmanip index at column top
  196. u32 vi = vm->m_area.index(x, nmax.Y, z);
  197. // Initial 3D noise index at column top
  198. u32 index3d = (z - nmin.Z) * m_zstride_1d + m_csize.Y * m_ystride +
  199. (x - nmin.X);
  200. // Don't excavate the overgenerated stone at node_max.Y + 1,
  201. // this creates a 'roof' over the cavern, preventing light in
  202. // caverns at mapchunk borders when generating mapchunks upwards.
  203. // This 'roof' is excavated when the mapchunk above is generated.
  204. for (s16 y = nmax.Y; y >= nmin.Y - 1; y--,
  205. index3d -= m_ystride,
  206. vm->m_area.add_y(em, vi, -1),
  207. cavern_amp_index++) {
  208. content_t c = vm->m_data[vi].getContent();
  209. float n_absamp_cavern = fabs(noise_cavern->result[index3d]) *
  210. cavern_amp[cavern_amp_index];
  211. // Disable CavesRandomWalk at a safe distance from caverns
  212. // to avoid excessively spreading liquids in caverns.
  213. if (n_absamp_cavern > m_cavern_threshold - 0.1f) {
  214. near_cavern = true;
  215. if (n_absamp_cavern > m_cavern_threshold &&
  216. m_ndef->get(c).is_ground_content)
  217. vm->m_data[vi] = MapNode(CONTENT_AIR);
  218. }
  219. }
  220. }
  221. delete[] cavern_amp;
  222. return near_cavern;
  223. }
  224. ////
  225. //// CavesRandomWalk
  226. ////
  227. CavesRandomWalk::CavesRandomWalk(
  228. INodeDefManager *ndef,
  229. GenerateNotifier *gennotify,
  230. s32 seed,
  231. int water_level,
  232. content_t water_source,
  233. content_t lava_source,
  234. int lava_depth)
  235. {
  236. assert(ndef);
  237. this->ndef = ndef;
  238. this->gennotify = gennotify;
  239. this->seed = seed;
  240. this->water_level = water_level;
  241. this->np_caveliquids = &nparams_caveliquids;
  242. this->lava_depth = lava_depth;
  243. c_water_source = water_source;
  244. if (c_water_source == CONTENT_IGNORE)
  245. c_water_source = ndef->getId("mapgen_water_source");
  246. if (c_water_source == CONTENT_IGNORE)
  247. c_water_source = CONTENT_AIR;
  248. c_lava_source = lava_source;
  249. if (c_lava_source == CONTENT_IGNORE)
  250. c_lava_source = ndef->getId("mapgen_lava_source");
  251. if (c_lava_source == CONTENT_IGNORE)
  252. c_lava_source = CONTENT_AIR;
  253. }
  254. void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
  255. PseudoRandom *ps, bool is_large_cave, int max_stone_height, s16 *heightmap)
  256. {
  257. assert(vm);
  258. assert(ps);
  259. this->vm = vm;
  260. this->ps = ps;
  261. this->node_min = nmin;
  262. this->node_max = nmax;
  263. this->heightmap = heightmap;
  264. this->large_cave = is_large_cave;
  265. this->ystride = nmax.X - nmin.X + 1;
  266. // Set initial parameters from randomness
  267. int dswitchint = ps->range(1, 14);
  268. flooded = ps->range(1, 2) == 2;
  269. if (large_cave) {
  270. part_max_length_rs = ps->range(2, 4);
  271. tunnel_routepoints = ps->range(5, ps->range(15, 30));
  272. min_tunnel_diameter = 5;
  273. max_tunnel_diameter = ps->range(7, ps->range(8, 24));
  274. } else {
  275. part_max_length_rs = ps->range(2, 9);
  276. tunnel_routepoints = ps->range(10, ps->range(15, 30));
  277. min_tunnel_diameter = 2;
  278. max_tunnel_diameter = ps->range(2, 6);
  279. }
  280. large_cave_is_flat = (ps->range(0, 1) == 0);
  281. main_direction = v3f(0, 0, 0);
  282. // Allowed route area size in nodes
  283. ar = node_max - node_min + v3s16(1, 1, 1);
  284. // Area starting point in nodes
  285. of = node_min;
  286. // Allow a bit more (this should be more than the maximum radius of the tunnel)
  287. const s16 insure = 2;
  288. s16 more = MYMAX(MAP_BLOCKSIZE - max_tunnel_diameter / 2 - insure, 1);
  289. ar += v3s16(1, 0, 1) * more * 2;
  290. of -= v3s16(1, 0, 1) * more;
  291. route_y_min = 0;
  292. // Allow half a diameter + 7 over stone surface
  293. route_y_max = -of.Y + max_stone_height + max_tunnel_diameter / 2 + 7;
  294. // Limit maximum to area
  295. route_y_max = rangelim(route_y_max, 0, ar.Y - 1);
  296. if (large_cave) {
  297. s16 minpos = 0;
  298. if (node_min.Y < water_level && node_max.Y > water_level) {
  299. minpos = water_level - max_tunnel_diameter / 3 - of.Y;
  300. route_y_max = water_level + max_tunnel_diameter / 3 - of.Y;
  301. }
  302. route_y_min = ps->range(minpos, minpos + max_tunnel_diameter);
  303. route_y_min = rangelim(route_y_min, 0, route_y_max);
  304. }
  305. s16 route_start_y_min = route_y_min;
  306. s16 route_start_y_max = route_y_max;
  307. route_start_y_min = rangelim(route_start_y_min, 0, ar.Y - 1);
  308. route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1);
  309. // Randomize starting position
  310. orp.Z = (float)(ps->next() % ar.Z) + 0.5f;
  311. orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f;
  312. orp.X = (float)(ps->next() % ar.X) + 0.5f;
  313. // Add generation notify begin event
  314. if (gennotify) {
  315. v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z);
  316. GenNotifyType notifytype = large_cave ?
  317. GENNOTIFY_LARGECAVE_BEGIN : GENNOTIFY_CAVE_BEGIN;
  318. gennotify->addEvent(notifytype, abs_pos);
  319. }
  320. // Generate some tunnel starting from orp
  321. for (u16 j = 0; j < tunnel_routepoints; j++)
  322. makeTunnel(j % dswitchint == 0);
  323. // Add generation notify end event
  324. if (gennotify) {
  325. v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z);
  326. GenNotifyType notifytype = large_cave ?
  327. GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END;
  328. gennotify->addEvent(notifytype, abs_pos);
  329. }
  330. }
  331. void CavesRandomWalk::makeTunnel(bool dirswitch)
  332. {
  333. if (dirswitch && !large_cave) {
  334. main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10;
  335. main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30;
  336. main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10;
  337. main_direction *= (float)ps->range(0, 10) / 10;
  338. }
  339. // Randomize size
  340. s16 min_d = min_tunnel_diameter;
  341. s16 max_d = max_tunnel_diameter;
  342. rs = ps->range(min_d, max_d);
  343. s16 rs_part_max_length_rs = rs * part_max_length_rs;
  344. v3s16 maxlen;
  345. if (large_cave) {
  346. maxlen = v3s16(
  347. rs_part_max_length_rs,
  348. rs_part_max_length_rs / 2,
  349. rs_part_max_length_rs
  350. );
  351. } else {
  352. maxlen = v3s16(
  353. rs_part_max_length_rs,
  354. ps->range(1, rs_part_max_length_rs),
  355. rs_part_max_length_rs
  356. );
  357. }
  358. v3f vec;
  359. // Jump downward sometimes
  360. if (!large_cave && ps->range(0, 12) == 0) {
  361. vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2;
  362. vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y;
  363. vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2;
  364. } else {
  365. vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2;
  366. vec.Y = (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2;
  367. vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2;
  368. }
  369. // Do not make caves that are above ground.
  370. // It is only necessary to check the startpoint and endpoint.
  371. v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2;
  372. v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1;
  373. if (isPosAboveSurface(p1) || isPosAboveSurface(p2))
  374. return;
  375. vec += main_direction;
  376. v3f rp = orp + vec;
  377. if (rp.X < 0)
  378. rp.X = 0;
  379. else if (rp.X >= ar.X)
  380. rp.X = ar.X - 1;
  381. if (rp.Y < route_y_min)
  382. rp.Y = route_y_min;
  383. else if (rp.Y >= route_y_max)
  384. rp.Y = route_y_max - 1;
  385. if (rp.Z < 0)
  386. rp.Z = 0;
  387. else if (rp.Z >= ar.Z)
  388. rp.Z = ar.Z - 1;
  389. vec = rp - orp;
  390. float veclen = vec.getLength();
  391. if (veclen < 0.05f)
  392. veclen = 1.0f;
  393. // Every second section is rough
  394. bool randomize_xz = (ps->range(1, 2) == 1);
  395. // Carve routes
  396. for (float f = 0.f; f < 1.0f; f += 1.0f / veclen)
  397. carveRoute(vec, f, randomize_xz);
  398. orp = rp;
  399. }
  400. void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz)
  401. {
  402. MapNode airnode(CONTENT_AIR);
  403. MapNode waternode(c_water_source);
  404. MapNode lavanode(c_lava_source);
  405. v3s16 startp(orp.X, orp.Y, orp.Z);
  406. startp += of;
  407. float nval = NoisePerlin3D(np_caveliquids, startp.X,
  408. startp.Y, startp.Z, seed);
  409. MapNode liquidnode = (nval < 0.40f && node_max.Y < lava_depth) ?
  410. lavanode : waternode;
  411. v3f fp = orp + vec * f;
  412. fp.X += 0.1f * ps->range(-10, 10);
  413. fp.Z += 0.1f * ps->range(-10, 10);
  414. v3s16 cp(fp.X, fp.Y, fp.Z);
  415. s16 d0 = -rs / 2;
  416. s16 d1 = d0 + rs;
  417. if (randomize_xz) {
  418. d0 += ps->range(-1, 1);
  419. d1 += ps->range(-1, 1);
  420. }
  421. bool flat_cave_floor = !large_cave && ps->range(0, 2) == 2;
  422. for (s16 z0 = d0; z0 <= d1; z0++) {
  423. s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1);
  424. for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) {
  425. s16 maxabsxz = MYMAX(abs(x0), abs(z0));
  426. s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1);
  427. for (s16 y0 = -si2; y0 <= si2; y0++) {
  428. // Make better floors in small caves
  429. if (flat_cave_floor && y0 <= -rs / 2 && rs <= 7)
  430. continue;
  431. if (large_cave_is_flat) {
  432. // Make large caves not so tall
  433. if (rs > 7 && abs(y0) >= rs / 3)
  434. continue;
  435. }
  436. v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0);
  437. p += of;
  438. if (!vm->m_area.contains(p))
  439. continue;
  440. u32 i = vm->m_area.index(p);
  441. content_t c = vm->m_data[i].getContent();
  442. if (!ndef->get(c).is_ground_content)
  443. continue;
  444. if (large_cave) {
  445. int full_ymin = node_min.Y - MAP_BLOCKSIZE;
  446. int full_ymax = node_max.Y + MAP_BLOCKSIZE;
  447. if (flooded && full_ymin < water_level && full_ymax > water_level)
  448. vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode;
  449. else if (flooded && full_ymax < water_level)
  450. vm->m_data[i] = (p.Y < startp.Y - 4) ? liquidnode : airnode;
  451. else
  452. vm->m_data[i] = airnode;
  453. } else {
  454. vm->m_data[i] = airnode;
  455. vm->m_flags[i] |= VMANIP_FLAG_CAVE;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. inline bool CavesRandomWalk::isPosAboveSurface(v3s16 p)
  462. {
  463. if (heightmap != NULL &&
  464. p.Z >= node_min.Z && p.Z <= node_max.Z &&
  465. p.X >= node_min.X && p.X <= node_max.X) {
  466. u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X);
  467. if (heightmap[index] < p.Y)
  468. return true;
  469. } else if (p.Y > water_level) {
  470. return true;
  471. }
  472. return false;
  473. }
  474. ////
  475. //// CavesV6
  476. ////
  477. CavesV6::CavesV6(INodeDefManager *ndef, GenerateNotifier *gennotify,
  478. int water_level, content_t water_source, content_t lava_source)
  479. {
  480. assert(ndef);
  481. this->ndef = ndef;
  482. this->gennotify = gennotify;
  483. this->water_level = water_level;
  484. c_water_source = water_source;
  485. if (c_water_source == CONTENT_IGNORE)
  486. c_water_source = ndef->getId("mapgen_water_source");
  487. if (c_water_source == CONTENT_IGNORE)
  488. c_water_source = CONTENT_AIR;
  489. c_lava_source = lava_source;
  490. if (c_lava_source == CONTENT_IGNORE)
  491. c_lava_source = ndef->getId("mapgen_lava_source");
  492. if (c_lava_source == CONTENT_IGNORE)
  493. c_lava_source = CONTENT_AIR;
  494. }
  495. void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
  496. PseudoRandom *ps, PseudoRandom *ps2,
  497. bool is_large_cave, int max_stone_height, s16 *heightmap)
  498. {
  499. assert(vm);
  500. assert(ps);
  501. assert(ps2);
  502. this->vm = vm;
  503. this->ps = ps;
  504. this->ps2 = ps2;
  505. this->node_min = nmin;
  506. this->node_max = nmax;
  507. this->heightmap = heightmap;
  508. this->large_cave = is_large_cave;
  509. this->ystride = nmax.X - nmin.X + 1;
  510. // Set initial parameters from randomness
  511. min_tunnel_diameter = 2;
  512. max_tunnel_diameter = ps->range(2, 6);
  513. int dswitchint = ps->range(1, 14);
  514. if (large_cave) {
  515. part_max_length_rs = ps->range(2, 4);
  516. tunnel_routepoints = ps->range(5, ps->range(15, 30));
  517. min_tunnel_diameter = 5;
  518. max_tunnel_diameter = ps->range(7, ps->range(8, 24));
  519. } else {
  520. part_max_length_rs = ps->range(2, 9);
  521. tunnel_routepoints = ps->range(10, ps->range(15, 30));
  522. }
  523. large_cave_is_flat = (ps->range(0, 1) == 0);
  524. main_direction = v3f(0, 0, 0);
  525. // Allowed route area size in nodes
  526. ar = node_max - node_min + v3s16(1, 1, 1);
  527. // Area starting point in nodes
  528. of = node_min;
  529. // Allow a bit more
  530. //(this should be more than the maximum radius of the tunnel)
  531. const s16 max_spread_amount = MAP_BLOCKSIZE;
  532. const s16 insure = 10;
  533. s16 more = MYMAX(max_spread_amount - max_tunnel_diameter / 2 - insure, 1);
  534. ar += v3s16(1, 0, 1) * more * 2;
  535. of -= v3s16(1, 0, 1) * more;
  536. route_y_min = 0;
  537. // Allow half a diameter + 7 over stone surface
  538. route_y_max = -of.Y + max_stone_height + max_tunnel_diameter / 2 + 7;
  539. // Limit maximum to area
  540. route_y_max = rangelim(route_y_max, 0, ar.Y - 1);
  541. if (large_cave) {
  542. s16 minpos = 0;
  543. if (node_min.Y < water_level && node_max.Y > water_level) {
  544. minpos = water_level - max_tunnel_diameter / 3 - of.Y;
  545. route_y_max = water_level + max_tunnel_diameter / 3 - of.Y;
  546. }
  547. route_y_min = ps->range(minpos, minpos + max_tunnel_diameter);
  548. route_y_min = rangelim(route_y_min, 0, route_y_max);
  549. }
  550. s16 route_start_y_min = route_y_min;
  551. s16 route_start_y_max = route_y_max;
  552. route_start_y_min = rangelim(route_start_y_min, 0, ar.Y - 1);
  553. route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1);
  554. // Randomize starting position
  555. orp.Z = (float)(ps->next() % ar.Z) + 0.5f;
  556. orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f;
  557. orp.X = (float)(ps->next() % ar.X) + 0.5f;
  558. // Add generation notify begin event
  559. if (gennotify != NULL) {
  560. v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z);
  561. GenNotifyType notifytype = large_cave ?
  562. GENNOTIFY_LARGECAVE_BEGIN : GENNOTIFY_CAVE_BEGIN;
  563. gennotify->addEvent(notifytype, abs_pos);
  564. }
  565. // Generate some tunnel starting from orp
  566. for (u16 j = 0; j < tunnel_routepoints; j++)
  567. makeTunnel(j % dswitchint == 0);
  568. // Add generation notify end event
  569. if (gennotify != NULL) {
  570. v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z);
  571. GenNotifyType notifytype = large_cave ?
  572. GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END;
  573. gennotify->addEvent(notifytype, abs_pos);
  574. }
  575. }
  576. void CavesV6::makeTunnel(bool dirswitch)
  577. {
  578. if (dirswitch && !large_cave) {
  579. main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10;
  580. main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30;
  581. main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10;
  582. main_direction *= (float)ps->range(0, 10) / 10;
  583. }
  584. // Randomize size
  585. s16 min_d = min_tunnel_diameter;
  586. s16 max_d = max_tunnel_diameter;
  587. rs = ps->range(min_d, max_d);
  588. s16 rs_part_max_length_rs = rs * part_max_length_rs;
  589. v3s16 maxlen;
  590. if (large_cave) {
  591. maxlen = v3s16(
  592. rs_part_max_length_rs,
  593. rs_part_max_length_rs / 2,
  594. rs_part_max_length_rs
  595. );
  596. } else {
  597. maxlen = v3s16(
  598. rs_part_max_length_rs,
  599. ps->range(1, rs_part_max_length_rs),
  600. rs_part_max_length_rs
  601. );
  602. }
  603. v3f vec;
  604. vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2;
  605. vec.Y = (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2;
  606. vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2;
  607. // Jump downward sometimes
  608. if (!large_cave && ps->range(0, 12) == 0) {
  609. vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2;
  610. vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y;
  611. vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2;
  612. }
  613. // Do not make caves that are entirely above ground, to fix shadow bugs
  614. // caused by overgenerated large caves.
  615. // It is only necessary to check the startpoint and endpoint.
  616. v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2;
  617. v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1;
  618. // If startpoint and endpoint are above ground, disable placement of nodes
  619. // in carveRoute while still running all PseudoRandom calls to ensure caves
  620. // are consistent with existing worlds.
  621. bool tunnel_above_ground =
  622. p1.Y > getSurfaceFromHeightmap(p1) &&
  623. p2.Y > getSurfaceFromHeightmap(p2);
  624. vec += main_direction;
  625. v3f rp = orp + vec;
  626. if (rp.X < 0)
  627. rp.X = 0;
  628. else if (rp.X >= ar.X)
  629. rp.X = ar.X - 1;
  630. if (rp.Y < route_y_min)
  631. rp.Y = route_y_min;
  632. else if (rp.Y >= route_y_max)
  633. rp.Y = route_y_max - 1;
  634. if (rp.Z < 0)
  635. rp.Z = 0;
  636. else if (rp.Z >= ar.Z)
  637. rp.Z = ar.Z - 1;
  638. vec = rp - orp;
  639. float veclen = vec.getLength();
  640. // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE
  641. if (veclen < 0.05f)
  642. veclen = 1.0f;
  643. // Every second section is rough
  644. bool randomize_xz = (ps2->range(1, 2) == 1);
  645. // Carve routes
  646. for (float f = 0.f; f < 1.0f; f += 1.0f / veclen)
  647. carveRoute(vec, f, randomize_xz, tunnel_above_ground);
  648. orp = rp;
  649. }
  650. void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz,
  651. bool tunnel_above_ground)
  652. {
  653. MapNode airnode(CONTENT_AIR);
  654. MapNode waternode(c_water_source);
  655. MapNode lavanode(c_lava_source);
  656. v3s16 startp(orp.X, orp.Y, orp.Z);
  657. startp += of;
  658. v3f fp = orp + vec * f;
  659. fp.X += 0.1f * ps->range(-10, 10);
  660. fp.Z += 0.1f * ps->range(-10, 10);
  661. v3s16 cp(fp.X, fp.Y, fp.Z);
  662. s16 d0 = -rs / 2;
  663. s16 d1 = d0 + rs;
  664. if (randomize_xz) {
  665. d0 += ps->range(-1, 1);
  666. d1 += ps->range(-1, 1);
  667. }
  668. for (s16 z0 = d0; z0 <= d1; z0++) {
  669. s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1);
  670. for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) {
  671. if (tunnel_above_ground)
  672. continue;
  673. s16 maxabsxz = MYMAX(abs(x0), abs(z0));
  674. s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1);
  675. for (s16 y0 = -si2; y0 <= si2; y0++) {
  676. if (large_cave_is_flat) {
  677. // Make large caves not so tall
  678. if (rs > 7 && abs(y0) >= rs / 3)
  679. continue;
  680. }
  681. v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0);
  682. p += of;
  683. if (!vm->m_area.contains(p))
  684. continue;
  685. u32 i = vm->m_area.index(p);
  686. content_t c = vm->m_data[i].getContent();
  687. if (!ndef->get(c).is_ground_content)
  688. continue;
  689. if (large_cave) {
  690. int full_ymin = node_min.Y - MAP_BLOCKSIZE;
  691. int full_ymax = node_max.Y + MAP_BLOCKSIZE;
  692. if (full_ymin < water_level && full_ymax > water_level) {
  693. vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode;
  694. } else if (full_ymax < water_level) {
  695. vm->m_data[i] = (p.Y < startp.Y - 2) ? lavanode : airnode;
  696. } else {
  697. vm->m_data[i] = airnode;
  698. }
  699. } else {
  700. if (c == CONTENT_AIR)
  701. continue;
  702. vm->m_data[i] = airnode;
  703. vm->m_flags[i] |= VMANIP_FLAG_CAVE;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. inline s16 CavesV6::getSurfaceFromHeightmap(v3s16 p)
  710. {
  711. if (heightmap != NULL &&
  712. p.Z >= node_min.Z && p.Z <= node_max.Z &&
  713. p.X >= node_min.X && p.X <= node_max.X) {
  714. u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X);
  715. return heightmap[index];
  716. }
  717. return water_level;
  718. }