mapgen.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2013-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  5. Copyright (C) 2015-2017 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. #pragma once
  19. #include "noise.h"
  20. #include "nodedef.h"
  21. #include "util/string.h"
  22. #include "util/container.h"
  23. #define MAPGEN_DEFAULT MAPGEN_V7
  24. #define MAPGEN_DEFAULT_NAME "v7"
  25. /////////////////// Mapgen flags
  26. #define MG_TREES 0x01 // Deprecated. Moved into mgv6 flags
  27. #define MG_CAVES 0x02
  28. #define MG_DUNGEONS 0x04
  29. #define MG_FLAT 0x08 // Deprecated. Moved into mgv6 flags
  30. #define MG_LIGHT 0x10
  31. #define MG_DECORATIONS 0x20
  32. typedef u8 biome_t; // copy from mg_biome.h to avoid an unnecessary include
  33. class Settings;
  34. class MMVManip;
  35. class INodeDefManager;
  36. extern FlagDesc flagdesc_mapgen[];
  37. extern FlagDesc flagdesc_gennotify[];
  38. class Biome;
  39. class BiomeGen;
  40. struct BiomeParams;
  41. class BiomeManager;
  42. class EmergeManager;
  43. class MapBlock;
  44. class VoxelManipulator;
  45. struct BlockMakeData;
  46. class VoxelArea;
  47. class Map;
  48. enum MapgenObject {
  49. MGOBJ_VMANIP,
  50. MGOBJ_HEIGHTMAP,
  51. MGOBJ_BIOMEMAP,
  52. MGOBJ_HEATMAP,
  53. MGOBJ_HUMIDMAP,
  54. MGOBJ_GENNOTIFY
  55. };
  56. enum GenNotifyType {
  57. GENNOTIFY_DUNGEON,
  58. GENNOTIFY_TEMPLE,
  59. GENNOTIFY_CAVE_BEGIN,
  60. GENNOTIFY_CAVE_END,
  61. GENNOTIFY_LARGECAVE_BEGIN,
  62. GENNOTIFY_LARGECAVE_END,
  63. GENNOTIFY_DECORATION,
  64. NUM_GENNOTIFY_TYPES
  65. };
  66. enum MgStoneType {
  67. MGSTONE_STONE,
  68. MGSTONE_DESERT_STONE,
  69. MGSTONE_SANDSTONE,
  70. MGSTONE_OTHER,
  71. };
  72. struct GenNotifyEvent {
  73. GenNotifyType type;
  74. v3s16 pos;
  75. u32 id;
  76. };
  77. class GenerateNotifier {
  78. public:
  79. GenerateNotifier() = default;
  80. GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
  81. void setNotifyOn(u32 notify_on);
  82. void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
  83. bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
  84. void getEvents(std::map<std::string, std::vector<v3s16> > &event_map,
  85. bool peek_events=false);
  86. private:
  87. u32 m_notify_on = 0;
  88. std::set<u32> *m_notify_on_deco_ids;
  89. std::list<GenNotifyEvent> m_notify_events;
  90. };
  91. enum MapgenType {
  92. MAPGEN_V5,
  93. MAPGEN_V6,
  94. MAPGEN_V7,
  95. MAPGEN_FLAT,
  96. MAPGEN_FRACTAL,
  97. MAPGEN_VALLEYS,
  98. MAPGEN_SINGLENODE,
  99. MAPGEN_CARPATHIAN,
  100. MAPGEN_INVALID,
  101. };
  102. struct MapgenParams {
  103. MapgenParams() = default;
  104. virtual ~MapgenParams();
  105. MapgenType mgtype = MAPGEN_DEFAULT;
  106. s16 chunksize = 5;
  107. u64 seed = 0;
  108. s16 water_level = 1;
  109. s16 mapgen_limit = MAX_MAP_GENERATION_LIMIT;
  110. u32 flags = MG_CAVES | MG_LIGHT | MG_DECORATIONS;
  111. BiomeParams *bparams = nullptr;
  112. s16 mapgen_edge_min = -MAX_MAP_GENERATION_LIMIT;
  113. s16 mapgen_edge_max = MAX_MAP_GENERATION_LIMIT;
  114. virtual void readParams(const Settings *settings);
  115. virtual void writeParams(Settings *settings) const;
  116. bool saoPosOverLimit(const v3f &p);
  117. s32 getSpawnRangeMax();
  118. private:
  119. void calcMapgenEdges();
  120. float m_sao_limit_min = -MAX_MAP_GENERATION_LIMIT * BS;
  121. float m_sao_limit_max = MAX_MAP_GENERATION_LIMIT * BS;
  122. bool m_mapgen_edges_calculated = false;
  123. };
  124. /*
  125. Generic interface for map generators. All mapgens must inherit this class.
  126. If a feature exposed by a public member pointer is not supported by a
  127. certain mapgen, it must be set to NULL.
  128. Apart from makeChunk, getGroundLevelAtPoint, and getSpawnLevelAtPoint, all
  129. methods can be used by constructing a Mapgen base class and setting the
  130. appropriate public members (e.g. vm, ndef, and so on).
  131. */
  132. class Mapgen {
  133. public:
  134. s32 seed = 0;
  135. int water_level = 0;
  136. int mapgen_limit = 0;
  137. u32 flags = 0;
  138. bool generating = false;
  139. int id = -1;
  140. MMVManip *vm = nullptr;
  141. INodeDefManager *ndef = nullptr;
  142. u32 blockseed;
  143. s16 *heightmap = nullptr;
  144. biome_t *biomemap = nullptr;
  145. v3s16 csize;
  146. BiomeGen *biomegen = nullptr;
  147. GenerateNotifier gennotify;
  148. Mapgen() = default;
  149. Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
  150. virtual ~Mapgen() = default;
  151. DISABLE_CLASS_COPY(Mapgen);
  152. virtual MapgenType getType() const { return MAPGEN_INVALID; }
  153. static u32 getBlockSeed(v3s16 p, s32 seed);
  154. static u32 getBlockSeed2(v3s16 p, s32 seed);
  155. s16 findGroundLevelFull(v2s16 p2d);
  156. s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
  157. s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
  158. void updateHeightmap(v3s16 nmin, v3s16 nmax);
  159. void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
  160. void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
  161. void lightSpread(VoxelArea &a, v3s16 p, u8 light);
  162. void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
  163. bool propagate_shadow = true);
  164. void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
  165. void spreadLight(v3s16 nmin, v3s16 nmax);
  166. virtual void makeChunk(BlockMakeData *data) {}
  167. virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
  168. // getSpawnLevelAtPoint() is a function within each mapgen that returns a
  169. // suitable y co-ordinate for player spawn ('suitable' usually meaning
  170. // within 16 nodes of water_level). If a suitable spawn level cannot be
  171. // found at the specified (X, Z) 'MAX_MAP_GENERATION_LIMIT' is returned to
  172. // signify this and to cause Server::findSpawnPos() to try another (X, Z).
  173. virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }
  174. // Mapgen management functions
  175. static MapgenType getMapgenType(const std::string &mgname);
  176. static const char *getMapgenName(MapgenType mgtype);
  177. static Mapgen *createMapgen(MapgenType mgtype, int mgid,
  178. MapgenParams *params, EmergeManager *emerge);
  179. static MapgenParams *createMapgenParams(MapgenType mgtype);
  180. static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);
  181. private:
  182. // isLiquidHorizontallyFlowable() is a helper function for updateLiquid()
  183. // that checks whether there are floodable nodes without liquid beneath
  184. // the node at index vi.
  185. inline bool isLiquidHorizontallyFlowable(u32 vi, v3s16 em);
  186. };
  187. /*
  188. MapgenBasic is a Mapgen implementation that handles basic functionality
  189. the majority of conventional mapgens will probably want to use, but isn't
  190. generic enough to be included as part of the base Mapgen class (such as
  191. generating biome terrain over terrain node skeletons, generating caves,
  192. dungeons, etc.)
  193. Inherit MapgenBasic instead of Mapgen to add this basic functionality to
  194. your mapgen without having to reimplement it. Feel free to override any of
  195. these methods if you desire different or more advanced behavior.
  196. Note that you must still create your own generateTerrain implementation when
  197. inheriting MapgenBasic.
  198. */
  199. class MapgenBasic : public Mapgen {
  200. public:
  201. MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge);
  202. virtual ~MapgenBasic();
  203. virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth);
  204. virtual bool generateCaverns(s16 max_stone_y);
  205. virtual void generateDungeons(s16 max_stone_y,
  206. MgStoneType stone_type, content_t biome_stone);
  207. virtual void generateBiomes(MgStoneType *mgstone_type,
  208. content_t *biome_stone);
  209. virtual void dustTopNodes();
  210. protected:
  211. EmergeManager *m_emerge;
  212. BiomeManager *m_bmgr;
  213. Noise *noise_filler_depth;
  214. v3s16 node_min;
  215. v3s16 node_max;
  216. v3s16 full_node_min;
  217. v3s16 full_node_max;
  218. // Content required for generateBiomes
  219. content_t c_stone;
  220. content_t c_desert_stone;
  221. content_t c_sandstone;
  222. content_t c_water_source;
  223. content_t c_river_water_source;
  224. content_t c_lava_source;
  225. // Content required for generateDungeons
  226. content_t c_cobble;
  227. content_t c_stair_cobble;
  228. content_t c_mossycobble;
  229. content_t c_stair_desert_stone;
  230. content_t c_sandstonebrick;
  231. content_t c_stair_sandstone_block;
  232. int ystride;
  233. int zstride;
  234. int zstride_1d;
  235. int zstride_1u1d;
  236. u32 spflags;
  237. NoiseParams np_cave1;
  238. NoiseParams np_cave2;
  239. NoiseParams np_cavern;
  240. float cave_width;
  241. float cavern_limit;
  242. float cavern_taper;
  243. float cavern_threshold;
  244. int lava_depth;
  245. };