]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mapgen.h
Update to minetest 5.4.0-dev
[dragonfireclient.git] / src / mapgen / mapgen.h
1 /*
2 Minetest
3 Copyright (C) 2010-2020 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2015-2020 paramat
5 Copyright (C) 2013-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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  // Obsolete. Moved into mgv6 flags
34 #define MG_CAVES       0x02
35 #define MG_DUNGEONS    0x04
36 #define MG_FLAT        0x08  // Obsolete. Moved into mgv6 flags
37 #define MG_LIGHT       0x10
38 #define MG_DECORATIONS 0x20
39 #define MG_BIOMES      0x40
40
41 typedef u16 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 EmergeParams;
55 class EmergeManager;
56 class MapBlock;
57 class VoxelManipulator;
58 struct BlockMakeData;
59 class VoxelArea;
60 class Map;
61
62 enum MapgenObject {
63         MGOBJ_VMANIP,
64         MGOBJ_HEIGHTMAP,
65         MGOBJ_BIOMEMAP,
66         MGOBJ_HEATMAP,
67         MGOBJ_HUMIDMAP,
68         MGOBJ_GENNOTIFY
69 };
70
71 enum GenNotifyType {
72         GENNOTIFY_DUNGEON,
73         GENNOTIFY_TEMPLE,
74         GENNOTIFY_CAVE_BEGIN,
75         GENNOTIFY_CAVE_END,
76         GENNOTIFY_LARGECAVE_BEGIN,
77         GENNOTIFY_LARGECAVE_END,
78         GENNOTIFY_DECORATION,
79         NUM_GENNOTIFY_TYPES
80 };
81
82 struct GenNotifyEvent {
83         GenNotifyType type;
84         v3s16 pos;
85         u32 id;
86 };
87
88 class GenerateNotifier {
89 public:
90         GenerateNotifier() = default;
91         GenerateNotifier(u32 notify_on, const std::set<u32> *notify_on_deco_ids);
92
93         void setNotifyOn(u32 notify_on);
94         void setNotifyOnDecoIds(const std::set<u32> *notify_on_deco_ids);
95
96         bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
97         void getEvents(std::map<std::string, std::vector<v3s16> > &event_map);
98         void clearEvents();
99
100 private:
101         u32 m_notify_on = 0;
102         const std::set<u32> *m_notify_on_deco_ids;
103         std::list<GenNotifyEvent> m_notify_events;
104 };
105
106 // Order must match the order of 'static MapgenDesc g_reg_mapgens[]' in mapgen.cpp
107 enum MapgenType {
108         MAPGEN_V7,
109         MAPGEN_VALLEYS,
110         MAPGEN_CARPATHIAN,
111         MAPGEN_V5,
112         MAPGEN_FLAT,
113         MAPGEN_FRACTAL,
114         MAPGEN_SINGLENODE,
115         MAPGEN_V6,
116         MAPGEN_INVALID,
117 };
118
119 struct MapgenParams {
120         MapgenParams() = default;
121         virtual ~MapgenParams();
122
123         MapgenType mgtype = MAPGEN_DEFAULT;
124         s16 chunksize = 5;
125         u64 seed = 0;
126         s16 water_level = 1;
127         s16 mapgen_limit = MAX_MAP_GENERATION_LIMIT;
128         // Flags set in readParams
129         u32 flags = 0;
130         u32 spflags = 0;
131
132         BiomeParams *bparams = nullptr;
133
134         s16 mapgen_edge_min = -MAX_MAP_GENERATION_LIMIT;
135         s16 mapgen_edge_max = MAX_MAP_GENERATION_LIMIT;
136
137         virtual void readParams(const Settings *settings);
138         virtual void writeParams(Settings *settings) const;
139         // Default settings for g_settings such as flags
140         virtual void setDefaultSettings(Settings *settings) {};
141
142         s32 getSpawnRangeMax();
143
144 private:
145         void calcMapgenEdges();
146         bool m_mapgen_edges_calculated = false;
147 };
148
149
150 /*
151         Generic interface for map generators.  All mapgens must inherit this class.
152         If a feature exposed by a public member pointer is not supported by a
153         certain mapgen, it must be set to NULL.
154
155         Apart from makeChunk, getGroundLevelAtPoint, and getSpawnLevelAtPoint, all
156         methods can be used by constructing a Mapgen base class and setting the
157         appropriate public members (e.g. vm, ndef, and so on).
158 */
159 class Mapgen {
160 public:
161         s32 seed = 0;
162         int water_level = 0;
163         int mapgen_limit = 0;
164         u32 flags = 0;
165         bool generating = false;
166         int id = -1;
167
168         MMVManip *vm = nullptr;
169         const NodeDefManager *ndef = nullptr;
170
171         u32 blockseed;
172         s16 *heightmap = nullptr;
173         biome_t *biomemap = nullptr;
174         v3s16 csize;
175
176         BiomeGen *biomegen = nullptr;
177         GenerateNotifier gennotify;
178
179         Mapgen() = default;
180         Mapgen(int mapgenid, MapgenParams *params, EmergeParams *emerge);
181         virtual ~Mapgen() = default;
182         DISABLE_CLASS_COPY(Mapgen);
183
184         virtual MapgenType getType() const { return MAPGEN_INVALID; }
185
186         static u32 getBlockSeed(v3s16 p, s32 seed);
187         static u32 getBlockSeed2(v3s16 p, s32 seed);
188         s16 findGroundLevelFull(v2s16 p2d);
189         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
190         s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
191         void updateHeightmap(v3s16 nmin, v3s16 nmax);
192         void getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
193                 std::vector<s16> &floors, std::vector<s16> &ceilings);
194
195         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
196
197         void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
198         void lightSpread(VoxelArea &a, std::queue<std::pair<v3s16, u8>> &queue,
199                 const v3s16 &p, u8 light);
200         void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
201                 bool propagate_shadow = true);
202         void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
203         void spreadLight(const v3s16 &nmin, const v3s16 &nmax);
204
205         virtual void makeChunk(BlockMakeData *data) {}
206         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
207
208         // getSpawnLevelAtPoint() is a function within each mapgen that returns a
209         // suitable y co-ordinate for player spawn ('suitable' usually meaning
210         // within 16 nodes of water_level). If a suitable spawn level cannot be
211         // found at the specified (X, Z) 'MAX_MAP_GENERATION_LIMIT' is returned to
212         // signify this and to cause Server::findSpawnPos() to try another (X, Z).
213         virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }
214
215         // Mapgen management functions
216         static MapgenType getMapgenType(const std::string &mgname);
217         static const char *getMapgenName(MapgenType mgtype);
218         static Mapgen *createMapgen(MapgenType mgtype, MapgenParams *params,
219                 EmergeParams *emerge);
220         static MapgenParams *createMapgenParams(MapgenType mgtype);
221         static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);
222         static void setDefaultSettings(Settings *settings);
223
224 private:
225         // isLiquidHorizontallyFlowable() is a helper function for updateLiquid()
226         // that checks whether there are floodable nodes without liquid beneath
227         // the node at index vi.
228         inline bool isLiquidHorizontallyFlowable(u32 vi, v3s16 em);
229 };
230
231 /*
232         MapgenBasic is a Mapgen implementation that handles basic functionality
233         the majority of conventional mapgens will probably want to use, but isn't
234         generic enough to be included as part of the base Mapgen class (such as
235         generating biome terrain over terrain node skeletons, generating caves,
236         dungeons, etc.)
237
238         Inherit MapgenBasic instead of Mapgen to add this basic functionality to
239         your mapgen without having to reimplement it.  Feel free to override any of
240         these methods if you desire different or more advanced behavior.
241
242         Note that you must still create your own generateTerrain implementation when
243         inheriting MapgenBasic.
244 */
245 class MapgenBasic : public Mapgen {
246 public:
247         MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerge);
248         virtual ~MapgenBasic();
249
250         virtual void generateBiomes();
251         virtual void dustTopNodes();
252         virtual void generateCavesNoiseIntersection(s16 max_stone_y);
253         virtual void generateCavesRandomWalk(s16 max_stone_y, s16 large_cave_ymax);
254         virtual bool generateCavernsNoise(s16 max_stone_y);
255         virtual void generateDungeons(s16 max_stone_y);
256
257 protected:
258         EmergeParams *m_emerge;
259         BiomeManager *m_bmgr;
260
261         Noise *noise_filler_depth;
262
263         v3s16 node_min;
264         v3s16 node_max;
265         v3s16 full_node_min;
266         v3s16 full_node_max;
267
268         content_t c_stone;
269         content_t c_water_source;
270         content_t c_river_water_source;
271         content_t c_lava_source;
272         content_t c_cobble;
273
274         int ystride;
275         int zstride;
276         int zstride_1d;
277         int zstride_1u1d;
278
279         u32 spflags;
280
281         NoiseParams np_cave1;
282         NoiseParams np_cave2;
283         NoiseParams np_cavern;
284         NoiseParams np_dungeons;
285         float cave_width;
286         float cavern_limit;
287         float cavern_taper;
288         float cavern_threshold;
289         int small_cave_num_min;
290         int small_cave_num_max;
291         int large_cave_num_min;
292         int large_cave_num_max;
293         float large_cave_flooded;
294         s16 large_cave_depth;
295         s16 dungeon_ymin;
296         s16 dungeon_ymax;
297 };