feec60e19b09220ced3890a61ca7f5a210ca02a3
[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 "irrlichttypes_bloated.h"
24 #include "util/container.h" // UniqueQueue
25 #include "gamedef.h"
26 #include "nodedef.h"
27 #include "mapnode.h"
28 #include "noise.h"
29 #include "settings.h"
30
31 /////////////////// Mapgen flags
32 #define MG_TREES         0x01
33 #define MG_CAVES         0x02
34 #define MG_DUNGEONS      0x04
35 #define MGV6_JUNGLES     0x08
36 #define MGV6_BIOME_BLEND 0x10
37 #define MG_FLAT          0x20
38 #define MG_NOLIGHT       0x40
39 #define MGV7_MOUNTAINS   0x80
40 #define MGV7_RIDGES      0x100
41
42 /////////////////// Ore generation flags
43 // Use absolute value of height to determine ore placement
44 #define OREFLAG_ABSHEIGHT 0x01
45 // Use 3d noise to get density of ore placement, instead of just the position
46 #define OREFLAG_DENSITY   0x02 // not yet implemented
47 // For claylike ore types, place ore if the number of surrounding
48 // nodes isn't the specified node
49 #define OREFLAG_NODEISNT  0x04 // not yet implemented
50
51 /////////////////// Decoration flags
52 #define DECO_PLACE_CENTER_X 1
53 #define DECO_PLACE_CENTER_Y 2
54 #define DECO_PLACE_CENTER_Z 4
55
56 #define ORE_RANGE_ACTUAL 1
57 #define ORE_RANGE_MIRROR 2
58
59 #define NUM_GEN_NOTIFY 6
60
61
62 extern FlagDesc flagdesc_mapgen[];
63 extern FlagDesc flagdesc_ore[];
64 extern FlagDesc flagdesc_deco_schematic[];
65 extern FlagDesc flagdesc_gennotify[];
66
67 class BiomeDefManager;
68 class Biome;
69 class EmergeManager;
70 class MapBlock;
71 class ManualMapVoxelManipulator;
72 class VoxelManipulator;
73 struct BlockMakeData;
74 class VoxelArea;
75 class Map;
76
77
78 enum MapgenObject {
79         MGOBJ_VMANIP,
80         MGOBJ_HEIGHTMAP,
81         MGOBJ_BIOMEMAP,
82         MGOBJ_HEATMAP,
83         MGOBJ_HUMIDMAP,
84         MGOBJ_GENNOTIFY
85 };
86
87 enum GenNotify {
88         GENNOTIFY_DUNGEON,
89         GENNOTIFY_TEMPLE,
90         GENNOTIFY_CAVE_BEGIN,
91         GENNOTIFY_CAVE_END,
92         GENNOTIFY_LARGECAVE_BEGIN,
93         GENNOTIFY_LARGECAVE_END
94 };
95
96 enum OreType {
97         ORE_SCATTER,
98         ORE_SHEET,
99         ORE_CLAYLIKE
100 };
101
102
103 struct MapgenParams {
104         std::string mg_name;
105         int chunksize;
106         u64 seed;
107         int water_level;
108         u32 flags;
109
110         MapgenParams() {
111                 mg_name     = "v6";
112                 seed        = 0;
113                 water_level = 1;
114                 chunksize   = 5;
115                 flags       = MG_TREES | MG_CAVES | MGV6_BIOME_BLEND;
116         }
117
118         virtual bool readParams(Settings *settings) { return true; }
119         virtual void writeParams(Settings *settings) {}
120         virtual ~MapgenParams() {}
121 };
122
123 class Mapgen {
124 public:
125         int seed;
126         int water_level;
127         bool generating;
128         int id;
129         ManualMapVoxelManipulator *vm;
130         INodeDefManager *ndef;
131
132         s16 *heightmap;
133         u8 *biomemap;
134         v3s16 csize;
135
136         u32 gennotify;
137         std::vector<v3s16> *gen_notifications[NUM_GEN_NOTIFY];
138
139         Mapgen();
140         virtual ~Mapgen();
141
142         s16 findGroundLevelFull(v2s16 p2d);
143         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
144         void updateHeightmap(v3s16 nmin, v3s16 nmax);
145         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
146         void setLighting(v3s16 nmin, v3s16 nmax, u8 light);
147         void lightSpread(VoxelArea &a, v3s16 p, u8 light);
148         void calcLighting(v3s16 nmin, v3s16 nmax);
149         void calcLightingOld(v3s16 nmin, v3s16 nmax);
150
151         virtual void makeChunk(BlockMakeData *data) {}
152         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
153 };
154
155 struct MapgenFactory {
156         virtual Mapgen *createMapgen(int mgid, MapgenParams *params,
157                                                                  EmergeManager *emerge) = 0;
158         virtual MapgenParams *createMapgenParams() = 0;
159         virtual ~MapgenFactory() {}
160 };
161
162 class Ore {
163 public:
164         std::string ore_name;
165         std::vector<std::string> wherein_names;
166         content_t ore;
167         std::vector<content_t> wherein;  // the node to be replaced
168         u32 clust_scarcity; // ore cluster has a 1-in-clust_scarcity chance of appearing at a node
169         s16 clust_num_ores; // how many ore nodes are in a chunk
170         s16 clust_size;     // how large (in nodes) a chunk of ore is
171         s16 height_min;
172         s16 height_max;
173         u8 ore_param2;          // to set node-specific attributes
174         u32 flags;          // attributes for this ore
175         float nthresh;      // threshhold for noise at which an ore is placed
176         NoiseParams *np;    // noise for distribution of clusters (NULL for uniform scattering)
177         Noise *noise;
178
179         Ore() {
180                 ore     = CONTENT_IGNORE;
181                 np      = NULL;
182                 noise   = NULL;
183         }
184
185         virtual ~Ore();
186
187         void resolveNodeNames(INodeDefManager *ndef);
188         void placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
189         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
190                                                 u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
191 };
192
193 class OreScatter : public Ore {
194         ~OreScatter() {}
195         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
196                                                 u32 blockseed, v3s16 nmin, v3s16 nmax);
197 };
198
199 class OreSheet : public Ore {
200         ~OreSheet() {}
201         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
202                                                 u32 blockseed, v3s16 nmin, v3s16 nmax);
203 };
204
205 Ore *createOre(OreType type);
206
207
208 enum DecorationType {
209         DECO_SIMPLE,
210         DECO_SCHEMATIC,
211         DECO_LSYSTEM
212 };
213
214 #if 0
215 struct CutoffData {
216         VoxelArea a;
217         Decoration *deco;
218         //v3s16 p;
219         //v3s16 size;
220         //s16 height;
221
222         CutoffData(s16 x, s16 y, s16 z, s16 h) {
223                 p = v3s16(x, y, z);
224                 height = h;
225         }
226 };
227 #endif
228
229 class Decoration {
230 public:
231         INodeDefManager *ndef;
232
233         int mapseed;
234         std::string place_on_name;
235         content_t c_place_on;
236         s16 sidelen;
237         float fill_ratio;
238         NoiseParams *np;
239
240         std::set<u8> biomes;
241         //std::list<CutoffData> cutoffs;
242         //JMutex cutoff_mutex;
243
244         Decoration();
245         virtual ~Decoration();
246
247         virtual void resolveNodeNames(INodeDefManager *ndef);
248         void placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
249         void placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
250
251         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) = 0;
252         virtual int getHeight() = 0;
253         virtual std::string getName() = 0;
254 };
255
256 class DecoSimple : public Decoration {
257 public:
258         std::string deco_name;
259         std::string spawnby_name;
260         content_t c_deco;
261         content_t c_spawnby;
262         s16 deco_height;
263         s16 deco_height_max;
264         s16 nspawnby;
265
266         std::vector<std::string> decolist_names;
267         std::vector<content_t> c_decolist;
268
269         ~DecoSimple() {}
270
271         void resolveNodeNames(INodeDefManager *ndef);
272         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p);
273         virtual int getHeight();
274         virtual std::string getName();
275 };
276
277 #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
278 #define MTSCHEM_FILE_VER_HIGHEST_READ  3
279 #define MTSCHEM_FILE_VER_HIGHEST_WRITE 3
280
281 #define MTSCHEM_PROB_NEVER  0x00
282 #define MTSCHEM_PROB_ALWAYS 0xFF
283
284 class DecoSchematic : public Decoration {
285 public:
286         std::string filename;
287
288         std::vector<std::string> *node_names;
289         std::vector<content_t> c_nodes;
290         std::map<std::string, std::string> replacements;
291
292         u32 flags;
293         Rotation rotation;
294         v3s16 size;
295         MapNode *schematic;
296         u8 *slice_probs;
297
298         DecoSchematic();
299         ~DecoSchematic();
300
301         void resolveNodeNames(INodeDefManager *ndef);
302         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p);
303         virtual int getHeight();
304         virtual std::string getName();
305
306         void blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
307                                         Rotation rot, bool force_placement);
308
309         bool loadSchematicFile();
310         void saveSchematicFile(INodeDefManager *ndef);
311
312         bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
313         void placeStructure(Map *map, v3s16 p);
314         void applyProbabilities(v3s16 p0,
315                 std::vector<std::pair<v3s16, u8> > *plist,
316                 std::vector<std::pair<s16, u8> > *splist);
317 };
318
319 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
320                                         std::vector<content_t> *usednodes);
321
322 /*
323 class DecoLSystem : public Decoration {
324 public:
325         virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
326 };
327 */
328
329 Decoration *createDecoration(DecorationType type);
330
331 #endif
332