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