cavegen.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Minetest
  3. Copyright (C) 2015-2020 paramat
  4. Copyright (C) 2010-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. #pragma once
  18. #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
  19. typedef u16 biome_t; // copy from mg_biome.h to avoid an unnecessary include
  20. class GenerateNotifier;
  21. /*
  22. CavesNoiseIntersection is a cave digging algorithm that carves smooth,
  23. web-like, continuous tunnels at points where the density of the intersection
  24. between two separate 3d noises is above a certain value. This value,
  25. cave_width, can be modified to set the effective width of these tunnels.
  26. This algorithm is relatively heavyweight, taking ~80ms to generate an
  27. 80x80x80 chunk of map on a modern processor. Use sparingly!
  28. TODO(hmmmm): Remove dependency on biomes
  29. TODO(hmmmm): Find alternative to overgeneration as solution for sunlight issue
  30. */
  31. class CavesNoiseIntersection
  32. {
  33. public:
  34. CavesNoiseIntersection(const NodeDefManager *nodedef,
  35. BiomeManager *biomemgr, v3s16 chunksize, NoiseParams *np_cave1,
  36. NoiseParams *np_cave2, s32 seed, float cave_width);
  37. ~CavesNoiseIntersection();
  38. void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, biome_t *biomemap);
  39. private:
  40. const NodeDefManager *m_ndef;
  41. BiomeManager *m_bmgr;
  42. // configurable parameters
  43. v3s16 m_csize;
  44. float m_cave_width;
  45. // intermediate state variables
  46. u16 m_ystride;
  47. u16 m_zstride_1d;
  48. Noise *noise_cave1;
  49. Noise *noise_cave2;
  50. };
  51. /*
  52. CavernsNoise is a cave digging algorithm
  53. */
  54. class CavernsNoise
  55. {
  56. public:
  57. CavernsNoise(const NodeDefManager *nodedef, v3s16 chunksize,
  58. NoiseParams *np_cavern, s32 seed, float cavern_limit,
  59. float cavern_taper, float cavern_threshold);
  60. ~CavernsNoise();
  61. bool generateCaverns(MMVManip *vm, v3s16 nmin, v3s16 nmax);
  62. private:
  63. const NodeDefManager *m_ndef;
  64. // configurable parameters
  65. v3s16 m_csize;
  66. float m_cavern_limit;
  67. float m_cavern_taper;
  68. float m_cavern_threshold;
  69. // intermediate state variables
  70. u16 m_ystride;
  71. u16 m_zstride_1d;
  72. Noise *noise_cavern;
  73. content_t c_water_source;
  74. content_t c_lava_source;
  75. };
  76. /*
  77. CavesRandomWalk is an implementation of a cave-digging algorithm that
  78. operates on the principle of a "random walk" to approximate the stochiastic
  79. activity of cavern development.
  80. In summary, this algorithm works by carving a randomly sized tunnel in a
  81. random direction a random amount of times, randomly varying in width.
  82. All randomness here is uniformly distributed; alternative distributions have
  83. not yet been implemented.
  84. This algorithm is very fast, executing in less than 1ms on average for an
  85. 80x80x80 chunk of map on a modern processor.
  86. */
  87. class CavesRandomWalk
  88. {
  89. public:
  90. MMVManip *vm;
  91. const NodeDefManager *ndef;
  92. GenerateNotifier *gennotify;
  93. s16 *heightmap;
  94. BiomeGen *bmgn;
  95. s32 seed;
  96. int water_level;
  97. float large_cave_flooded;
  98. // TODO 'np_caveliquids' is deprecated and should eventually be removed.
  99. // Cave liquids are now defined and located using biome definitions.
  100. NoiseParams *np_caveliquids;
  101. u16 ystride;
  102. s16 min_tunnel_diameter;
  103. s16 max_tunnel_diameter;
  104. u16 tunnel_routepoints;
  105. int part_max_length_rs;
  106. bool large_cave;
  107. bool large_cave_is_flat;
  108. bool flooded;
  109. bool use_biome_liquid;
  110. v3s16 node_min;
  111. v3s16 node_max;
  112. v3f orp; // starting point, relative to caved space
  113. v3s16 of; // absolute coordinates of caved space
  114. v3s16 ar; // allowed route area
  115. s16 rs; // tunnel radius size
  116. v3f main_direction;
  117. s16 route_y_min;
  118. s16 route_y_max;
  119. PseudoRandom *ps;
  120. content_t c_water_source;
  121. content_t c_lava_source;
  122. content_t c_biome_liquid;
  123. // ndef is a mandatory parameter.
  124. // If gennotify is NULL, generation events are not logged.
  125. // If biomegen is NULL, cave liquids have classic behaviour.
  126. CavesRandomWalk(const NodeDefManager *ndef, GenerateNotifier *gennotify =
  127. NULL, s32 seed = 0, int water_level = 1, content_t water_source =
  128. CONTENT_IGNORE, content_t lava_source = CONTENT_IGNORE,
  129. float large_cave_flooded = 0.5f, BiomeGen *biomegen = NULL);
  130. // vm and ps are mandatory parameters.
  131. // If heightmap is NULL, the surface level at all points is assumed to
  132. // be water_level.
  133. void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
  134. bool is_large_cave, int max_stone_height, s16 *heightmap);
  135. private:
  136. void makeTunnel(bool dirswitch);
  137. void carveRoute(v3f vec, float f, bool randomize_xz);
  138. inline bool isPosAboveSurface(v3s16 p);
  139. };
  140. /*
  141. CavesV6 is the original version of caves used with Mapgen V6.
  142. Though it uses the same fundamental algorithm as CavesRandomWalk, it is made
  143. separate to preserve the exact sequence of PseudoRandom calls - any change
  144. to this ordering results in the output being radically different.
  145. Because caves in Mapgen V6 are responsible for a large portion of the basic
  146. terrain shape, modifying this will break our contract of reverse
  147. compatibility for a 'stable' mapgen such as V6.
  148. tl;dr,
  149. *** DO NOT TOUCH THIS CLASS UNLESS YOU KNOW WHAT YOU ARE DOING ***
  150. */
  151. class CavesV6
  152. {
  153. public:
  154. MMVManip *vm;
  155. const NodeDefManager *ndef;
  156. GenerateNotifier *gennotify;
  157. PseudoRandom *ps;
  158. PseudoRandom *ps2;
  159. // configurable parameters
  160. s16 *heightmap;
  161. content_t c_water_source;
  162. content_t c_lava_source;
  163. int water_level;
  164. // intermediate state variables
  165. u16 ystride;
  166. s16 min_tunnel_diameter;
  167. s16 max_tunnel_diameter;
  168. u16 tunnel_routepoints;
  169. int part_max_length_rs;
  170. bool large_cave;
  171. bool large_cave_is_flat;
  172. v3s16 node_min;
  173. v3s16 node_max;
  174. v3f orp; // starting point, relative to caved space
  175. v3s16 of; // absolute coordinates of caved space
  176. v3s16 ar; // allowed route area
  177. s16 rs; // tunnel radius size
  178. v3f main_direction;
  179. s16 route_y_min;
  180. s16 route_y_max;
  181. // ndef is a mandatory parameter.
  182. // If gennotify is NULL, generation events are not logged.
  183. CavesV6(const NodeDefManager *ndef, GenerateNotifier *gennotify = NULL,
  184. int water_level = 1, content_t water_source = CONTENT_IGNORE,
  185. content_t lava_source = CONTENT_IGNORE);
  186. // vm, ps, and ps2 are mandatory parameters.
  187. // If heightmap is NULL, the surface level at all points is assumed to
  188. // be water_level.
  189. void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
  190. PseudoRandom *ps2, bool is_large_cave, int max_stone_height,
  191. s16 *heightmap = NULL);
  192. private:
  193. void makeTunnel(bool dirswitch);
  194. void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground);
  195. inline s16 getSurfaceFromHeightmap(v3s16 p);
  196. };