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