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