Modernize various files (src/m*) (#6267)
[oweals/minetest.git] / src / mapgen.h
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
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 "mapnode.h"
27 #include "util/string.h"
28 #include "util/container.h"
29
30 #define MAPGEN_DEFAULT MAPGEN_V7
31 #define MAPGEN_DEFAULT_NAME "v7"
32
33 /////////////////// Mapgen flags
34 #define MG_TREES       0x01  // Deprecated. Moved into mgv6 flags
35 #define MG_CAVES       0x02
36 #define MG_DUNGEONS    0x04
37 #define MG_FLAT        0x08  // Deprecated. Moved into mgv6 flags
38 #define MG_LIGHT       0x10
39 #define MG_DECORATIONS 0x20
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 INodeDefManager;
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 enum MgStoneType {
82         MGSTONE_STONE,
83         MGSTONE_DESERT_STONE,
84         MGSTONE_SANDSTONE,
85         MGSTONE_OTHER,
86 };
87
88 struct GenNotifyEvent {
89         GenNotifyType type;
90         v3s16 pos;
91         u32 id;
92 };
93
94 class GenerateNotifier {
95 public:
96         GenerateNotifier();
97         GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
98
99         void setNotifyOn(u32 notify_on);
100         void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
101
102         bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
103         void getEvents(std::map<std::string, std::vector<v3s16> > &event_map,
104                 bool peek_events=false);
105
106 private:
107         u32 m_notify_on = 0;
108         std::set<u32> *m_notify_on_deco_ids;
109         std::list<GenNotifyEvent> m_notify_events;
110 };
111
112 enum MapgenType {
113         MAPGEN_V5,
114         MAPGEN_V6,
115         MAPGEN_V7,
116         MAPGEN_FLAT,
117         MAPGEN_FRACTAL,
118         MAPGEN_VALLEYS,
119         MAPGEN_SINGLENODE,
120         MAPGEN_CARPATHIAN,
121         MAPGEN_INVALID,
122 };
123
124 struct MapgenParams {
125         MapgenParams() = default;
126         virtual ~MapgenParams();
127
128         MapgenType mgtype = MAPGEN_DEFAULT;
129         s16 chunksize = 5;
130         u64 seed = 0;
131         s16 water_level = 1;
132         s16 mapgen_limit = MAX_MAP_GENERATION_LIMIT;
133         u32 flags = MG_CAVES | MG_LIGHT | MG_DECORATIONS;
134
135         BiomeParams *bparams = nullptr;
136
137         s16 mapgen_edge_min = -MAX_MAP_GENERATION_LIMIT;
138         s16 mapgen_edge_max = MAX_MAP_GENERATION_LIMIT;
139
140         virtual void readParams(const Settings *settings);
141         virtual void writeParams(Settings *settings) const;
142
143         bool saoPosOverLimit(const v3f &p);
144         s32 getSpawnRangeMax();
145
146 private:
147         void calcMapgenEdges();
148
149         float m_sao_limit_min = -MAX_MAP_GENERATION_LIMIT * BS;
150         float m_sao_limit_max = MAX_MAP_GENERATION_LIMIT * BS;
151         bool m_mapgen_edges_calculated = false;
152 };
153
154
155 /*
156         Generic interface for map generators.  All mapgens must inherit this class.
157         If a feature exposed by a public member pointer is not supported by a
158         certain mapgen, it must be set to NULL.
159
160         Apart from makeChunk, getGroundLevelAtPoint, and getSpawnLevelAtPoint, all
161         methods can be used by constructing a Mapgen base class and setting the
162         appropriate public members (e.g. vm, ndef, and so on).
163 */
164 class Mapgen {
165 public:
166         s32 seed = 0;
167         int water_level = 0;
168         int mapgen_limit = 0;
169         u32 flags = 0;
170         bool generating = false;
171         int id = -1;
172
173         MMVManip *vm = nullptr;
174         INodeDefManager *ndef = nullptr;
175
176         u32 blockseed;
177         s16 *heightmap = nullptr;
178         biome_t *biomemap = nullptr;
179         v3s16 csize;
180
181         BiomeGen *biomegen = nullptr;
182         GenerateNotifier gennotify;
183
184         Mapgen();
185         Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
186         virtual ~Mapgen();
187         DISABLE_CLASS_COPY(Mapgen);
188
189         virtual MapgenType getType() const { return MAPGEN_INVALID; }
190
191         static u32 getBlockSeed(v3s16 p, s32 seed);
192         static u32 getBlockSeed2(v3s16 p, s32 seed);
193         s16 findGroundLevelFull(v2s16 p2d);
194         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
195         s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
196         void updateHeightmap(v3s16 nmin, v3s16 nmax);
197         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
198
199         void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
200         void lightSpread(VoxelArea &a, v3s16 p, u8 light);
201         void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
202                 bool propagate_shadow = true);
203         void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
204         void spreadLight(v3s16 nmin, v3s16 nmax);
205
206         virtual void makeChunk(BlockMakeData *data) {}
207         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
208
209         // getSpawnLevelAtPoint() is a function within each mapgen that returns a
210         // suitable y co-ordinate for player spawn ('suitable' usually meaning
211         // within 16 nodes of water_level). If a suitable spawn level cannot be
212         // found at the specified (X, Z) 'MAX_MAP_GENERATION_LIMIT' is returned to
213         // signify this and to cause Server::findSpawnPos() to try another (X, Z).
214         virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }
215
216         // Mapgen management functions
217         static MapgenType getMapgenType(const std::string &mgname);
218         static const char *getMapgenName(MapgenType mgtype);
219         static Mapgen *createMapgen(MapgenType mgtype, int mgid,
220                 MapgenParams *params, EmergeManager *emerge);
221         static MapgenParams *createMapgenParams(MapgenType mgtype);
222         static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);
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, EmergeManager *emerge);
248         virtual ~MapgenBasic();
249
250         virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth);
251         virtual bool generateCaverns(s16 max_stone_y);
252         virtual void generateDungeons(s16 max_stone_y,
253                 MgStoneType stone_type, content_t biome_stone);
254         virtual void generateBiomes(MgStoneType *mgstone_type,
255                 content_t *biome_stone, s16 biome_zero_level);
256         virtual void dustTopNodes();
257
258 protected:
259         EmergeManager *m_emerge;
260         BiomeManager *m_bmgr;
261
262         Noise *noise_filler_depth;
263
264         v3s16 node_min;
265         v3s16 node_max;
266         v3s16 full_node_min;
267         v3s16 full_node_max;
268
269         // Content required for generateBiomes
270         content_t c_stone;
271         content_t c_desert_stone;
272         content_t c_sandstone;
273         content_t c_water_source;
274         content_t c_river_water_source;
275         content_t c_lava_source;
276
277         // Content required for generateDungeons
278         content_t c_cobble;
279         content_t c_stair_cobble;
280         content_t c_mossycobble;
281         content_t c_stair_desert_stone;
282         content_t c_sandstonebrick;
283         content_t c_stair_sandstone_block;
284
285         int ystride;
286         int zstride;
287         int zstride_1d;
288         int zstride_1u1d;
289
290         u32 spflags;
291
292         NoiseParams np_cave1;
293         NoiseParams np_cave2;
294         NoiseParams np_cavern;
295         float cave_width;
296         float cavern_limit;
297         float cavern_taper;
298         float cavern_threshold;
299         int lava_depth;
300 };