Move biome calculation to BiomeGen
[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 #define MG_DECORATIONS 0x20
38
39 class Settings;
40 class MMVManip;
41 class INodeDefManager;
42
43 extern FlagDesc flagdesc_mapgen[];
44 extern FlagDesc flagdesc_gennotify[];
45
46 class Biome;
47 class BiomeGen;
48 struct BiomeParams;
49 class EmergeManager;
50 class MapBlock;
51 class VoxelManipulator;
52 struct BlockMakeData;
53 class VoxelArea;
54 class Map;
55
56 enum MapgenObject {
57         MGOBJ_VMANIP,
58         MGOBJ_HEIGHTMAP,
59         MGOBJ_BIOMEMAP,
60         MGOBJ_HEATMAP,
61         MGOBJ_HUMIDMAP,
62         MGOBJ_GENNOTIFY
63 };
64
65 enum GenNotifyType {
66         GENNOTIFY_DUNGEON,
67         GENNOTIFY_TEMPLE,
68         GENNOTIFY_CAVE_BEGIN,
69         GENNOTIFY_CAVE_END,
70         GENNOTIFY_LARGECAVE_BEGIN,
71         GENNOTIFY_LARGECAVE_END,
72         GENNOTIFY_DECORATION,
73         NUM_GENNOTIFY_TYPES
74 };
75
76 // TODO(hmmmm/paramat): make stone type selection dynamic
77 enum MgStoneType {
78         STONE,
79         DESERT_STONE,
80         SANDSTONE,
81 };
82
83 struct GenNotifyEvent {
84         GenNotifyType type;
85         v3s16 pos;
86         u32 id;
87 };
88
89 class GenerateNotifier {
90 public:
91         GenerateNotifier();
92         GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
93
94         void setNotifyOn(u32 notify_on);
95         void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
96
97         bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
98         void getEvents(std::map<std::string, std::vector<v3s16> > &event_map,
99                 bool peek_events=false);
100
101 private:
102         u32 m_notify_on;
103         std::set<u32> *m_notify_on_deco_ids;
104         std::list<GenNotifyEvent> m_notify_events;
105 };
106
107 struct MapgenSpecificParams {
108         virtual void readParams(const Settings *settings) = 0;
109         virtual void writeParams(Settings *settings) const = 0;
110         virtual ~MapgenSpecificParams() {}
111 };
112
113 struct MapgenParams {
114         std::string mg_name;
115         s16 chunksize;
116         u64 seed;
117         s16 water_level;
118         u32 flags;
119
120         BiomeParams *bparams;
121         MapgenSpecificParams *sparams;
122
123         MapgenParams() :
124                 mg_name(DEFAULT_MAPGEN),
125                 chunksize(5),
126                 seed(0),
127                 water_level(1),
128                 flags(MG_CAVES | MG_LIGHT | MG_DECORATIONS),
129                 bparams(NULL),
130                 sparams(NULL)
131         {
132         }
133
134         virtual ~MapgenParams();
135
136         void load(const Settings &settings);
137         void save(Settings &settings) const;
138 };
139
140 class Mapgen {
141 public:
142         int seed;
143         int water_level;
144         u32 flags;
145         bool generating;
146         int id;
147
148         MMVManip *vm;
149         INodeDefManager *ndef;
150
151         u32 blockseed;
152         s16 *heightmap;
153         u8 *biomemap;
154         v3s16 csize;
155
156         BiomeGen *biomegen;
157         GenerateNotifier gennotify;
158
159         Mapgen();
160         Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
161         virtual ~Mapgen();
162
163         static u32 getBlockSeed(v3s16 p, int seed);
164         static u32 getBlockSeed2(v3s16 p, int seed);
165         s16 findGroundLevelFull(v2s16 p2d);
166         s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
167         s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
168         void updateHeightmap(v3s16 nmin, v3s16 nmax);
169         void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
170
171         void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
172         void lightSpread(VoxelArea &a, v3s16 p, u8 light);
173         void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
174                 bool propagate_shadow = true);
175         void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
176         void spreadLight(v3s16 nmin, v3s16 nmax);
177
178         virtual void makeChunk(BlockMakeData *data) {}
179         virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
180
181         // getSpawnLevelAtPoint() is a function within each mapgen that returns a
182         // suitable y co-ordinate for player spawn ('suitable' usually meaning
183         // within 16 nodes of water_level). If a suitable spawn level cannot be
184         // found at the specified (X, Z) 'MAX_MAP_GENERATION_LIMIT' is returned to
185         // signify this and to cause Server::findSpawnPos() to try another (X, Z).
186         virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }
187
188 private:
189         DISABLE_CLASS_COPY(Mapgen);
190 };
191
192 struct MapgenFactory {
193         virtual Mapgen *createMapgen(int mgid, MapgenParams *params,
194                 EmergeManager *emerge) = 0;
195         virtual MapgenSpecificParams *createMapgenParams() = 0;
196         virtual ~MapgenFactory() {}
197 };
198
199 #endif