3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
24 #include "network/networkprotocol.h"
26 #include "util/container.h"
27 #include "mapgen/mapgen.h" // for MapgenParams
30 #define BLOCK_EMERGE_ALLOW_GEN (1 << 0)
31 #define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
33 #define EMERGE_DBG_OUT(x) { \
34 if (enable_mapgen_debug_info) \
35 infostream << "EmergeThread: " x << std::endl; \
44 class DecorationManager;
45 class SchematicManager;
49 // Structure containing inputs/outputs for chunk generation
50 struct BlockMakeData {
51 MMVManip *vmanip = nullptr;
55 UniqueQueue<v3s16> transforming_liquid;
56 const NodeDefManager *nodedef = nullptr;
58 BlockMakeData() = default;
60 ~BlockMakeData() { delete vmanip; }
63 // Result from processing an item on the emerge queue
73 typedef void (*EmergeCompletionCallback)(
74 v3s16 blockpos, EmergeAction action, void *param);
78 EmergeCompletionCallback,
83 struct BlockEmergeData {
86 EmergeCallbackList callbacks;
90 friend class EmergeManager;
92 EmergeParams() = delete;
94 DISABLE_CLASS_COPY(EmergeParams);
96 const NodeDefManager *ndef; // shared
97 bool enable_mapgen_debug_info;
100 const std::set<u32> *gen_notify_on_deco_ids; // shared
102 BiomeManager *biomemgr;
104 DecorationManager *decomgr;
105 SchematicManager *schemmgr;
108 EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
109 const OreManager *oremgr, const DecorationManager *decomgr,
110 const SchematicManager *schemmgr);
113 class EmergeManager {
114 /* The mod API needs unchecked access to allow:
115 * - using decomgr or oremgr to place decos/ores
116 * - using schemmgr to load and place schematics
118 friend class ModApiMapgen;
120 const NodeDefManager *ndef;
121 bool enable_mapgen_debug_info;
124 u32 gen_notify_on = 0;
125 std::set<u32> gen_notify_on_deco_ids;
127 // Parameters passed to mapgens owned by ServerMap
128 // TODO(hmmmm): Remove this after mapgen helper methods using them
129 // are moved to ServerMap
130 MapgenParams *mgparams;
132 // Hackish workaround:
133 // For now, EmergeManager must hold onto a ptr to the Map's setting manager
134 // since the Map can only be accessed through the Environment, and the
135 // Environment is not created until after script initialization.
136 MapSettingsManager *map_settings_mgr;
139 EmergeManager(Server *server);
141 DISABLE_CLASS_COPY(EmergeManager);
143 // no usage restrictions
144 const BiomeManager *getBiomeManager() const { return biomemgr; }
145 const OreManager *getOreManager() const { return oremgr; }
146 const DecorationManager *getDecorationManager() const { return decomgr; }
147 const SchematicManager *getSchematicManager() const { return schemmgr; }
148 // only usable before mapgen init
149 BiomeManager *getWritableBiomeManager();
150 OreManager *getWritableOreManager();
151 DecorationManager *getWritableDecorationManager();
152 SchematicManager *getWritableSchematicManager();
154 void initMapgens(MapgenParams *mgparams);
160 bool enqueueBlockEmerge(
164 bool ignore_queue_limits=false);
166 bool enqueueBlockEmergeEx(
170 EmergeCompletionCallback callback,
171 void *callback_param);
173 v3s16 getContainingChunk(v3s16 blockpos);
175 Mapgen *getCurrentMapgen();
177 // Mapgen helpers methods
178 int getSpawnLevelAtPoint(v2s16 p);
179 int getGroundLevelAtPoint(v2s16 p);
180 bool isBlockUnderground(v3s16 blockpos);
182 static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
185 std::vector<Mapgen *> m_mapgens;
186 std::vector<EmergeThread *> m_threads;
187 bool m_threads_active = false;
189 std::mutex m_queue_mutex;
190 std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
191 std::unordered_map<u16, u16> m_peer_queue_count;
194 u16 m_qlimit_diskonly;
195 u16 m_qlimit_generate;
197 // Managers of various map generation-related components
198 // Note that each Mapgen gets a copy(!) of these to work with
199 BiomeManager *biomemgr;
201 DecorationManager *decomgr;
202 SchematicManager *schemmgr;
204 // Requires m_queue_mutex held
205 EmergeThread *getOptimalThread();
207 bool pushBlockEmergeData(
211 EmergeCompletionCallback callback,
212 void *callback_param,
213 bool *entry_already_exists);
215 bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
217 friend class EmergeThread;