Decoration: Fix schematic probability mess with new MTS file version
[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_extrabloated.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 extern FlagDesc flagdesc_mapgen[];
57 extern FlagDesc flagdesc_ore[];
58 extern FlagDesc flagdesc_deco_schematic[];
59
60 class BiomeDefManager;
61 class Biome;
62 class EmergeManager;
63 class MapBlock;
64 class ManualMapVoxelManipulator;
65 class VoxelManipulator;
66 struct BlockMakeData;
67 class VoxelArea;
68 class Map;
69
70 struct MapgenParams {
71         std::string mg_name;
72         int chunksize;
73         u64 seed;
74         int water_level;
75         u32 flags;
76
77         MapgenParams() {
78                 mg_name     = "v6";
79                 seed        = 0;
80                 water_level = 1;
81                 chunksize   = 5;
82                 flags       = MG_TREES | MG_CAVES | MGV6_BIOME_BLEND;
83         }
84         
85         virtual bool readParams(Settings *settings) { return true; }
86         virtual void writeParams(Settings *settings) {}
87         virtual ~MapgenParams() {}
88 };
89
90 class Mapgen {
91 public:
92         int seed;
93         int water_level;
94         bool generating;
95         int id;
96         ManualMapVoxelManipulator *vm;
97         INodeDefManager *ndef;
98         
99         s16 *heightmap;
100         u8 *biomemap;
101         v3s16 csize;
102
103         Mapgen();
104         virtual ~Mapgen() {}
105
106         s16 findGroundLevelFull(v2s16 p2d);
107         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
108         void updateHeightmap(v3s16 nmin, v3s16 nmax);
109         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
110         void setLighting(v3s16 nmin, v3s16 nmax, u8 light);
111         void lightSpread(VoxelArea &a, v3s16 p, u8 light);
112         void calcLighting(v3s16 nmin, v3s16 nmax);
113         void calcLightingOld(v3s16 nmin, v3s16 nmax);
114
115         virtual void makeChunk(BlockMakeData *data) {}
116         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
117
118         //Legacy functions for Farmesh (pending removal)
119         static bool get_have_beach(u64 seed, v2s16 p2d);
120         static double tree_amount_2d(u64 seed, v2s16 p);
121         static s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision);
122 };
123
124 struct MapgenFactory {
125         virtual Mapgen *createMapgen(int mgid, MapgenParams *params,
126                                                                  EmergeManager *emerge) = 0;
127         virtual MapgenParams *createMapgenParams() = 0;
128         virtual ~MapgenFactory() {}
129 };
130
131 enum MapgenObject {
132         MGOBJ_VMANIP,
133         MGOBJ_HEIGHTMAP,
134         MGOBJ_BIOMEMAP,
135         MGOBJ_HEATMAP,
136         MGOBJ_HUMIDMAP
137 };
138
139 enum OreType {
140         ORE_SCATTER,
141         ORE_SHEET,
142         ORE_CLAYLIKE
143 };
144
145 #define ORE_RANGE_ACTUAL 1
146 #define ORE_RANGE_MIRROR 2
147
148 class Ore {
149 public:
150         std::string ore_name;
151         std::vector<std::string> wherein_names;
152         content_t ore;
153         std::vector<content_t> wherein;  // the node to be replaced
154         u32 clust_scarcity; // ore cluster has a 1-in-clust_scarcity chance of appearing at a node
155         s16 clust_num_ores; // how many ore nodes are in a chunk
156         s16 clust_size;     // how large (in nodes) a chunk of ore is
157         s16 height_min;
158         s16 height_max;
159         u8 ore_param2;          // to set node-specific attributes
160         u32 flags;          // attributes for this ore
161         float nthresh;      // threshhold for noise at which an ore is placed 
162         NoiseParams *np;    // noise for distribution of clusters (NULL for uniform scattering)
163         Noise *noise;
164         
165         Ore() {
166                 ore     = CONTENT_IGNORE;
167                 np      = NULL;
168                 noise   = NULL;
169         }
170         
171         virtual ~Ore();
172         
173         void resolveNodeNames(INodeDefManager *ndef);
174         void placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
175         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
176                                                 u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
177 };
178
179 class OreScatter : public Ore {
180         ~OreScatter() {}
181         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
182                                                 u32 blockseed, v3s16 nmin, v3s16 nmax);
183 };
184
185 class OreSheet : public Ore {
186         ~OreSheet() {}
187         virtual void generate(ManualMapVoxelManipulator *vm, int seed,
188                                                 u32 blockseed, v3s16 nmin, v3s16 nmax);
189 };
190
191 Ore *createOre(OreType type);
192
193
194 enum DecorationType {
195         DECO_SIMPLE,
196         DECO_SCHEMATIC,
197         DECO_LSYSTEM
198 };
199
200 #if 0
201 struct CutoffData {
202         VoxelArea a;
203         Decoration *deco;
204         //v3s16 p;
205         //v3s16 size;
206         //s16 height;
207         
208         CutoffData(s16 x, s16 y, s16 z, s16 h) {
209                 p = v3s16(x, y, z);
210                 height = h;
211         }
212 };
213 #endif
214
215 class Decoration {
216 public:
217         INodeDefManager *ndef;
218         
219         int mapseed;
220         std::string place_on_name;
221         content_t c_place_on;
222         s16 sidelen;
223         float fill_ratio;
224         NoiseParams *np;
225         
226         std::set<u8> biomes;
227         //std::list<CutoffData> cutoffs;
228         //JMutex cutoff_mutex;
229
230         Decoration();
231         virtual ~Decoration();
232         
233         virtual void resolveNodeNames(INodeDefManager *ndef);
234         void placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
235         void placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
236         
237         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) = 0;
238         virtual int getHeight() = 0;
239         virtual std::string getName() = 0;
240 };
241
242 class DecoSimple : public Decoration {
243 public:
244         std::string deco_name;
245         std::string spawnby_name;
246         content_t c_deco;
247         content_t c_spawnby;
248         s16 deco_height;
249         s16 deco_height_max;
250         s16 nspawnby;
251         
252         std::vector<std::string> decolist_names;
253         std::vector<content_t> c_decolist;
254
255         ~DecoSimple() {}
256         
257         void resolveNodeNames(INodeDefManager *ndef);
258         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p);
259         virtual int getHeight();
260         virtual std::string getName();
261 };
262
263 #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
264 #define MTSCHEM_PROB_NEVER  0x00
265 #define MTSCHEM_PROB_ALWAYS 0xFF
266
267 class DecoSchematic : public Decoration {
268 public:
269         std::string filename;
270         
271         std::vector<std::string> *node_names;
272         std::vector<content_t> c_nodes;
273         std::map<std::string, std::string> replacements;
274
275         u32 flags;
276         Rotation rotation;
277         v3s16 size;
278         MapNode *schematic;
279
280         DecoSchematic();
281         ~DecoSchematic();
282         
283         void resolveNodeNames(INodeDefManager *ndef);
284         virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p);
285         virtual int getHeight();
286         virtual std::string getName();
287         
288         void blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
289                                         Rotation rot, bool force_placement);
290         
291         bool loadSchematicFile();
292         void saveSchematicFile(INodeDefManager *ndef);
293         
294         bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
295         void placeStructure(Map *map, v3s16 p);
296         void applyProbabilities(std::vector<std::pair<v3s16, u8> > *plist, v3s16 p0);
297 };
298
299 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
300                                         std::vector<content_t> *usednodes);
301
302 /*
303 class DecoLSystem : public Decoration {
304 public:
305         virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
306 };
307 */
308
309 Decoration *createDecoration(DecorationType type);
310
311 #endif
312