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