3 Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 Copyright (C) 2015-2018 paramat
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "util/string.h"
27 #include "util/container.h"
29 #define MAPGEN_DEFAULT MAPGEN_V7
30 #define MAPGEN_DEFAULT_NAME "v7"
32 /////////////////// Mapgen flags
33 #define MG_TREES 0x01 // Obsolete. Moved into mgv6 flags
35 #define MG_DUNGEONS 0x04
36 #define MG_FLAT 0x08 // Obsolete. Moved into mgv6 flags
38 #define MG_DECORATIONS 0x20
39 #define MG_BIOMES 0x40
41 typedef u8 biome_t; // copy from mg_biome.h to avoid an unnecessary include
47 extern FlagDesc flagdesc_mapgen[];
48 extern FlagDesc flagdesc_gennotify[];
56 class VoxelManipulator;
75 GENNOTIFY_LARGECAVE_BEGIN,
76 GENNOTIFY_LARGECAVE_END,
81 struct GenNotifyEvent {
87 class GenerateNotifier {
89 GenerateNotifier() = default;
90 GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
92 void setNotifyOn(u32 notify_on);
93 void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
95 bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
96 void getEvents(std::map<std::string, std::vector<v3s16> > &event_map);
101 std::set<u32> *m_notify_on_deco_ids;
102 std::list<GenNotifyEvent> m_notify_events;
105 // Order must match the order of 'static MapgenDesc g_reg_mapgens[]' in mapgen.cpp
118 struct MapgenParams {
119 MapgenParams() = default;
120 virtual ~MapgenParams();
122 MapgenType mgtype = MAPGEN_DEFAULT;
126 s16 mapgen_limit = MAX_MAP_GENERATION_LIMIT;
127 u32 flags = MG_CAVES | MG_LIGHT | MG_DECORATIONS | MG_BIOMES;
129 BiomeParams *bparams = nullptr;
131 s16 mapgen_edge_min = -MAX_MAP_GENERATION_LIMIT;
132 s16 mapgen_edge_max = MAX_MAP_GENERATION_LIMIT;
134 virtual void readParams(const Settings *settings);
135 virtual void writeParams(Settings *settings) const;
137 s32 getSpawnRangeMax();
140 void calcMapgenEdges();
141 bool m_mapgen_edges_calculated = false;
146 Generic interface for map generators. All mapgens must inherit this class.
147 If a feature exposed by a public member pointer is not supported by a
148 certain mapgen, it must be set to NULL.
150 Apart from makeChunk, getGroundLevelAtPoint, and getSpawnLevelAtPoint, all
151 methods can be used by constructing a Mapgen base class and setting the
152 appropriate public members (e.g. vm, ndef, and so on).
158 int mapgen_limit = 0;
160 bool generating = false;
163 MMVManip *vm = nullptr;
164 const NodeDefManager *ndef = nullptr;
167 s16 *heightmap = nullptr;
168 biome_t *biomemap = nullptr;
171 BiomeGen *biomegen = nullptr;
172 GenerateNotifier gennotify;
175 Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
176 virtual ~Mapgen() = default;
177 DISABLE_CLASS_COPY(Mapgen);
179 virtual MapgenType getType() const { return MAPGEN_INVALID; }
181 static u32 getBlockSeed(v3s16 p, s32 seed);
182 static u32 getBlockSeed2(v3s16 p, s32 seed);
183 s16 findGroundLevelFull(v2s16 p2d);
184 s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
185 s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
186 void updateHeightmap(v3s16 nmin, v3s16 nmax);
187 void getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
188 std::vector<s16> &floors, std::vector<s16> &ceilings);
190 void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
192 void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
193 void lightSpread(VoxelArea &a, std::queue<std::pair<v3s16, u8>> &queue,
194 const v3s16 &p, u8 light);
195 void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
196 bool propagate_shadow = true);
197 void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
198 void spreadLight(const v3s16 &nmin, const v3s16 &nmax);
200 virtual void makeChunk(BlockMakeData *data) {}
201 virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }
203 // getSpawnLevelAtPoint() is a function within each mapgen that returns a
204 // suitable y co-ordinate for player spawn ('suitable' usually meaning
205 // within 16 nodes of water_level). If a suitable spawn level cannot be
206 // found at the specified (X, Z) 'MAX_MAP_GENERATION_LIMIT' is returned to
207 // signify this and to cause Server::findSpawnPos() to try another (X, Z).
208 virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }
210 // Mapgen management functions
211 static MapgenType getMapgenType(const std::string &mgname);
212 static const char *getMapgenName(MapgenType mgtype);
213 static Mapgen *createMapgen(MapgenType mgtype, MapgenParams *params,
214 EmergeManager *emerge);
215 static MapgenParams *createMapgenParams(MapgenType mgtype);
216 static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);
219 // isLiquidHorizontallyFlowable() is a helper function for updateLiquid()
220 // that checks whether there are floodable nodes without liquid beneath
221 // the node at index vi.
222 inline bool isLiquidHorizontallyFlowable(u32 vi, v3s16 em);
226 MapgenBasic is a Mapgen implementation that handles basic functionality
227 the majority of conventional mapgens will probably want to use, but isn't
228 generic enough to be included as part of the base Mapgen class (such as
229 generating biome terrain over terrain node skeletons, generating caves,
232 Inherit MapgenBasic instead of Mapgen to add this basic functionality to
233 your mapgen without having to reimplement it. Feel free to override any of
234 these methods if you desire different or more advanced behavior.
236 Note that you must still create your own generateTerrain implementation when
237 inheriting MapgenBasic.
239 class MapgenBasic : public Mapgen {
241 MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge);
242 virtual ~MapgenBasic();
244 virtual void generateBiomes();
245 virtual void dustTopNodes();
246 virtual void generateCavesNoiseIntersection(s16 max_stone_y);
247 virtual void generateCavesRandomWalk(s16 max_stone_y, s16 large_cave_ymax);
248 virtual bool generateCavernsNoise(s16 max_stone_y);
249 virtual void generateDungeons(s16 max_stone_y);
252 EmergeManager *m_emerge;
253 BiomeManager *m_bmgr;
255 Noise *noise_filler_depth;
263 content_t c_water_source;
264 content_t c_river_water_source;
265 content_t c_lava_source;
275 NoiseParams np_cave1;
276 NoiseParams np_cave2;
277 NoiseParams np_cavern;
278 NoiseParams np_dungeons;
282 float cavern_threshold;
283 int small_cave_num_min;
284 int small_cave_num_max;
285 int large_cave_num_min;
286 int large_cave_num_max;
287 float large_cave_flooded;
288 s16 large_cave_depth;