Mapgen v5/v7: Detect sandstone, enable sandstone brick dungeons
[oweals/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
38 class Settings;
39 class MMVManip;
40 class INodeDefManager;
41
42 extern FlagDesc flagdesc_mapgen[];
43 extern FlagDesc flagdesc_gennotify[];
44
45 class Biome;
46 class EmergeManager;
47 class MapBlock;
48 class VoxelManipulator;
49 struct BlockMakeData;
50 class VoxelArea;
51 class Map;
52
53 enum MapgenObject {
54         MGOBJ_VMANIP,
55         MGOBJ_HEIGHTMAP,
56         MGOBJ_BIOMEMAP,
57         MGOBJ_HEATMAP,
58         MGOBJ_HUMIDMAP,
59         MGOBJ_GENNOTIFY
60 };
61
62 enum GenNotifyType {
63         GENNOTIFY_DUNGEON,
64         GENNOTIFY_TEMPLE,
65         GENNOTIFY_CAVE_BEGIN,
66         GENNOTIFY_CAVE_END,
67         GENNOTIFY_LARGECAVE_BEGIN,
68         GENNOTIFY_LARGECAVE_END,
69         GENNOTIFY_DECORATION,
70         NUM_GENNOTIFY_TYPES
71 };
72
73 // TODO(hmmmm/paramat): make stone type selection dynamic
74 enum MgStoneType {
75         STONE,
76         DESERT_STONE,
77         SANDSTONE,
78 };
79
80 struct GenNotifyEvent {
81         GenNotifyType type;
82         v3s16 pos;
83         u32 id;
84 };
85
86 class GenerateNotifier {
87 public:
88         GenerateNotifier();
89         GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
90
91         void setNotifyOn(u32 notify_on);
92         void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
93
94         bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
95         void getEvents(std::map<std::string, std::vector<v3s16> > &event_map,
96                 bool peek_events=false);
97
98 private:
99         u32 m_notify_on;
100         std::set<u32> *m_notify_on_deco_ids;
101         std::list<GenNotifyEvent> m_notify_events;
102 };
103
104 struct MapgenSpecificParams {
105         virtual void readParams(const Settings *settings) = 0;
106         virtual void writeParams(Settings *settings) const = 0;
107         virtual ~MapgenSpecificParams() {}
108 };
109
110 struct MapgenParams {
111         std::string mg_name;
112         s16 chunksize;
113         u64 seed;
114         s16 water_level;
115         u32 flags;
116
117         NoiseParams np_biome_heat;
118         NoiseParams np_biome_humidity;
119
120         MapgenSpecificParams *sparams;
121
122         MapgenParams() :
123                 mg_name(DEFAULT_MAPGEN),
124                 chunksize(5),
125                 seed(0),
126                 water_level(1),
127                 flags(MG_TREES | MG_CAVES | MG_LIGHT),
128                 np_biome_heat(NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.5, 2.0)),
129                 np_biome_humidity(NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.5, 2.0)),
130                 sparams(NULL)
131         {}
132
133         void load(const Settings &settings);
134         void save(Settings &settings) const;
135 };
136
137 class Mapgen {
138 public:
139         int seed;
140         int water_level;
141         u32 flags;
142         bool generating;
143         int id;
144
145         MMVManip *vm;
146         INodeDefManager *ndef;
147
148         u32 blockseed;
149         s16 *heightmap;
150         u8 *biomemap;
151         v3s16 csize;
152
153         GenerateNotifier gennotify;
154
155         Mapgen();
156         Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
157         virtual ~Mapgen();
158
159         static u32 getBlockSeed(v3s16 p, int seed);
160         static u32 getBlockSeed2(v3s16 p, int seed);
161         s16 findGroundLevelFull(v2s16 p2d);
162         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
163         void updateHeightmap(v3s16 nmin, v3s16 nmax);
164         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
165
166         void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
167         void lightSpread(VoxelArea &a, v3s16 p, u8 light);
168
169         void calcLighting(v3s16 nmin, v3s16 nmax);
170         void calcLighting(v3s16 nmin, v3s16 nmax,
171                 v3s16 full_nmin, v3s16 full_nmax);
172
173         void propagateSunlight(v3s16 nmin, v3s16 nmax);
174         void spreadLight(v3s16 nmin, v3s16 nmax);
175
176         void calcLightingOld(v3s16 nmin, v3s16 nmax);
177
178         virtual void makeChunk(BlockMakeData *data) {}
179         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
180 };
181
182 struct MapgenFactory {
183         virtual Mapgen *createMapgen(int mgid, MapgenParams *params,
184                 EmergeManager *emerge) = 0;
185         virtual MapgenSpecificParams *createMapgenParams() = 0;
186         virtual ~MapgenFactory() {}
187 };
188
189 typedef std::map<std::string, std::string> StringMap;
190 typedef u32 ObjDefHandle;
191
192 #define OBJDEF_INVALID_INDEX ((u32)(-1))
193 #define OBJDEF_INVALID_HANDLE 0
194 #define OBJDEF_HANDLE_SALT 0x00585e6fu
195 #define OBJDEF_MAX_ITEMS (1 << 18)
196 #define OBJDEF_UID_MASK ((1 << 7) - 1)
197
198 enum ObjDefType {
199         OBJDEF_GENERIC,
200         OBJDEF_BIOME,
201         OBJDEF_ORE,
202         OBJDEF_DECORATION,
203         OBJDEF_SCHEMATIC,
204 };
205
206 class ObjDef {
207 public:
208         virtual ~ObjDef() {}
209
210         u32 index;
211         u32 uid;
212         ObjDefHandle handle;
213         std::string name;
214 };
215
216 // WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
217 // added/set to.  Note that ObjDefs managed by ObjDefManager are NOT refcounted,
218 // so the same ObjDef instance must not be referenced multiple
219 class ObjDefManager {
220 public:
221         ObjDefManager(IGameDef *gamedef, ObjDefType type);
222         virtual ~ObjDefManager();
223
224         virtual const char *getObjectTitle() const { return "ObjDef"; }
225
226         virtual void clear();
227         virtual ObjDef *getByName(const std::string &name) const;
228
229         //// Add new/get/set object definitions by handle
230         virtual ObjDefHandle add(ObjDef *obj);
231         virtual ObjDef *get(ObjDefHandle handle) const;
232         virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
233
234         //// Raw variants that work on indexes
235         virtual u32 addRaw(ObjDef *obj);
236
237         // It is generally assumed that getRaw() will always return a valid object
238         // This won't be true if people do odd things such as call setRaw() with NULL
239         virtual ObjDef *getRaw(u32 index) const;
240         virtual ObjDef *setRaw(u32 index, ObjDef *obj);
241
242         size_t getNumObjects() const { return m_objects.size(); }
243         ObjDefType getType() const { return m_objtype; }
244         INodeDefManager *getNodeDef() const { return m_ndef; }
245
246         u32 validateHandle(ObjDefHandle handle) const;
247         static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
248         static bool decodeHandle(ObjDefHandle handle, u32 *index,
249                 ObjDefType *type, u32 *uid);
250
251 protected:
252         INodeDefManager *m_ndef;
253         std::vector<ObjDef *> m_objects;
254         ObjDefType m_objtype;
255 };
256
257 #endif