cavegen.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
  18. class GenerateNotifier;
  19. /*
  20. CavesNoiseIntersection is a cave digging algorithm that carves smooth,
  21. web-like, continuous tunnels at points where the density of the intersection
  22. between two separate 3d noises is above a certain value. This value,
  23. cave_width, can be modified to set the effective width of these tunnels.
  24. This algorithm is relatively heavyweight, taking ~80ms to generate an
  25. 80x80x80 chunk of map on a modern processor. Use sparingly!
  26. TODO(hmmmm): Remove dependency on biomes
  27. TODO(hmmmm): Find alternative to overgeneration as solution for sunlight issue
  28. */
  29. class CavesNoiseIntersection
  30. {
  31. public:
  32. CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr,
  33. v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2,
  34. s32 seed, float cave_width);
  35. ~CavesNoiseIntersection();
  36. void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap);
  37. private:
  38. INodeDefManager *m_ndef;
  39. BiomeManager *m_bmgr;
  40. // configurable parameters
  41. v3s16 m_csize;
  42. float m_cave_width;
  43. // intermediate state variables
  44. u16 m_ystride;
  45. u16 m_zstride_1d;
  46. Noise *noise_cave1;
  47. Noise *noise_cave2;
  48. };
  49. /*
  50. CavernsNoise is a cave digging algorithm
  51. */
  52. class CavernsNoise
  53. {
  54. public:
  55. CavernsNoise(INodeDefManager *nodedef, v3s16 chunksize, NoiseParams *np_cavern,
  56. s32 seed, float cavern_limit, float cavern_taper,
  57. float cavern_threshold);
  58. ~CavernsNoise();
  59. bool generateCaverns(MMVManip *vm, v3s16 nmin, v3s16 nmax);
  60. private:
  61. INodeDefManager *m_ndef;
  62. // configurable parameters
  63. v3s16 m_csize;
  64. float m_cavern_limit;
  65. float m_cavern_taper;
  66. float m_cavern_threshold;
  67. // intermediate state variables
  68. u16 m_ystride;
  69. u16 m_zstride_1d;
  70. Noise *noise_cavern;
  71. content_t c_water_source;
  72. content_t c_lava_source;
  73. };
  74. /*
  75. CavesRandomWalk is an implementation of a cave-digging algorithm that
  76. operates on the principle of a "random walk" to approximate the stochiastic
  77. activity of cavern development.
  78. In summary, this algorithm works by carving a randomly sized tunnel in a
  79. random direction a random amount of times, randomly varying in width.
  80. All randomness here is uniformly distributed; alternative distributions have
  81. not yet been implemented.
  82. This algorithm is very fast, executing in less than 1ms on average for an
  83. 80x80x80 chunk of map on a modern processor.
  84. */
  85. class CavesRandomWalk
  86. {
  87. public:
  88. MMVManip *vm;
  89. INodeDefManager *ndef;
  90. GenerateNotifier *gennotify;
  91. s16 *heightmap;
  92. // configurable parameters
  93. s32 seed;
  94. int water_level;
  95. int lava_depth;
  96. NoiseParams *np_caveliquids;
  97. // intermediate state variables
  98. u16 ystride;
  99. s16 min_tunnel_diameter;
  100. s16 max_tunnel_diameter;
  101. u16 tunnel_routepoints;
  102. int part_max_length_rs;
  103. bool large_cave;
  104. bool large_cave_is_flat;
  105. bool flooded;
  106. s16 max_stone_y;
  107. v3s16 node_min;
  108. v3s16 node_max;
  109. v3f orp; // starting point, relative to caved space
  110. v3s16 of; // absolute coordinates of caved space
  111. v3s16 ar; // allowed route area
  112. s16 rs; // tunnel radius size
  113. v3f main_direction;
  114. s16 route_y_min;
  115. s16 route_y_max;
  116. PseudoRandom *ps;
  117. content_t c_water_source;
  118. content_t c_lava_source;
  119. // ndef is a mandatory parameter.
  120. // If gennotify is NULL, generation events are not logged.
  121. CavesRandomWalk(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL,
  122. s32 seed = 0, int water_level = 1,
  123. content_t water_source = CONTENT_IGNORE,
  124. content_t lava_source = CONTENT_IGNORE, int lava_depth = -256);
  125. // vm and ps are mandatory parameters.
  126. // If heightmap is NULL, the surface level at all points is assumed to
  127. // be water_level.
  128. void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
  129. bool is_large_cave, int max_stone_height, s16 *heightmap);
  130. private:
  131. void makeTunnel(bool dirswitch);
  132. void carveRoute(v3f vec, float f, bool randomize_xz);
  133. inline bool isPosAboveSurface(v3s16 p);
  134. };
  135. /*
  136. CavesV6 is the original version of caves used with Mapgen V6.
  137. Though it uses the same fundamental algorithm as CavesRandomWalk, it is made
  138. separate to preserve the exact sequence of PseudoRandom calls - any change
  139. to this ordering results in the output being radically different.
  140. Because caves in Mapgen V6 are responsible for a large portion of the basic
  141. terrain shape, modifying this will break our contract of reverse
  142. compatibility for a 'stable' mapgen such as V6.
  143. tl;dr,
  144. *** DO NOT TOUCH THIS CLASS UNLESS YOU KNOW WHAT YOU ARE DOING ***
  145. */
  146. class CavesV6
  147. {
  148. public:
  149. MMVManip *vm;
  150. INodeDefManager *ndef;
  151. GenerateNotifier *gennotify;
  152. PseudoRandom *ps;
  153. PseudoRandom *ps2;
  154. // configurable parameters
  155. s16 *heightmap;
  156. content_t c_water_source;
  157. content_t c_lava_source;
  158. int water_level;
  159. // intermediate state variables
  160. u16 ystride;
  161. s16 min_tunnel_diameter;
  162. s16 max_tunnel_diameter;
  163. u16 tunnel_routepoints;
  164. int part_max_length_rs;
  165. bool large_cave;
  166. bool large_cave_is_flat;
  167. v3s16 node_min;
  168. v3s16 node_max;
  169. v3f orp; // starting point, relative to caved space
  170. v3s16 of; // absolute coordinates of caved space
  171. v3s16 ar; // allowed route area
  172. s16 rs; // tunnel radius size
  173. v3f main_direction;
  174. s16 route_y_min;
  175. s16 route_y_max;
  176. // ndef is a mandatory parameter.
  177. // If gennotify is NULL, generation events are not logged.
  178. CavesV6(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL,
  179. int water_level = 1, content_t water_source = CONTENT_IGNORE,
  180. content_t lava_source = CONTENT_IGNORE);
  181. // vm, ps, and ps2 are mandatory parameters.
  182. // If heightmap is NULL, the surface level at all points is assumed to
  183. // be water_level.
  184. void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
  185. PseudoRandom *ps2, bool is_large_cave, int max_stone_height,
  186. s16 *heightmap = NULL);
  187. private:
  188. void makeTunnel(bool dirswitch);
  189. void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground);
  190. inline s16 getSurfaceFromHeightmap(v3s16 p);
  191. };