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 "util/metricsbackend.h"
28 #include "mapgen/mapgen.h" // for MapgenParams
31 #define BLOCK_EMERGE_ALLOW_GEN (1 << 0)
32 #define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
34 #define EMERGE_DBG_OUT(x) { \
35 if (enable_mapgen_debug_info) \
36 infostream << "EmergeThread: " x << std::endl; \
45 class DecorationManager;
46 class SchematicManager;
50 // Structure containing inputs/outputs for chunk generation
51 struct BlockMakeData {
52 MMVManip *vmanip = nullptr;
56 UniqueQueue<v3s16> transforming_liquid;
57 const NodeDefManager *nodedef = nullptr;
59 BlockMakeData() = default;
61 ~BlockMakeData() { delete vmanip; }
64 // Result from processing an item on the emerge queue
73 const static std::string emergeActionStrs[] = {
82 typedef void (*EmergeCompletionCallback)(
83 v3s16 blockpos, EmergeAction action, void *param);
87 EmergeCompletionCallback,
92 struct BlockEmergeData {
95 EmergeCallbackList callbacks;
99 friend class EmergeManager;
101 EmergeParams() = delete;
103 DISABLE_CLASS_COPY(EmergeParams);
105 const NodeDefManager *ndef; // shared
106 bool enable_mapgen_debug_info;
109 const std::set<u32> *gen_notify_on_deco_ids; // shared
112 BiomeManager *biomemgr;
114 DecorationManager *decomgr;
115 SchematicManager *schemmgr;
118 EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
119 const BiomeManager *biomemgr,
120 const OreManager *oremgr, const DecorationManager *decomgr,
121 const SchematicManager *schemmgr);
124 class EmergeManager {
125 /* The mod API needs unchecked access to allow:
126 * - using decomgr or oremgr to place decos/ores
127 * - using schemmgr to load and place schematics
129 friend class ModApiMapgen;
131 const NodeDefManager *ndef;
132 bool enable_mapgen_debug_info;
135 u32 gen_notify_on = 0;
136 std::set<u32> gen_notify_on_deco_ids;
138 // Parameters passed to mapgens owned by ServerMap
139 // TODO(hmmmm): Remove this after mapgen helper methods using them
140 // are moved to ServerMap
141 MapgenParams *mgparams;
143 // Hackish workaround:
144 // For now, EmergeManager must hold onto a ptr to the Map's setting manager
145 // since the Map can only be accessed through the Environment, and the
146 // Environment is not created until after script initialization.
147 MapSettingsManager *map_settings_mgr;
150 EmergeManager(Server *server, MetricsBackend *mb);
152 DISABLE_CLASS_COPY(EmergeManager);
154 const BiomeGen *getBiomeGen() const { return biomegen; }
156 // no usage restrictions
157 const BiomeManager *getBiomeManager() const { return biomemgr; }
158 const OreManager *getOreManager() const { return oremgr; }
159 const DecorationManager *getDecorationManager() const { return decomgr; }
160 const SchematicManager *getSchematicManager() const { return schemmgr; }
161 // only usable before mapgen init
162 BiomeManager *getWritableBiomeManager();
163 OreManager *getWritableOreManager();
164 DecorationManager *getWritableDecorationManager();
165 SchematicManager *getWritableSchematicManager();
167 void initMapgens(MapgenParams *mgparams);
173 bool enqueueBlockEmerge(
177 bool ignore_queue_limits=false);
179 bool enqueueBlockEmergeEx(
183 EmergeCompletionCallback callback,
184 void *callback_param);
186 bool isBlockInQueue(v3s16 pos);
188 Mapgen *getCurrentMapgen();
190 // Mapgen helpers methods
191 int getSpawnLevelAtPoint(v2s16 p);
192 bool isBlockUnderground(v3s16 blockpos);
194 static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
197 std::vector<Mapgen *> m_mapgens;
198 std::vector<EmergeThread *> m_threads;
199 bool m_threads_active = false;
201 std::mutex m_queue_mutex;
202 std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
203 std::unordered_map<u16, u32> m_peer_queue_count;
206 u32 m_qlimit_diskonly;
207 u32 m_qlimit_generate;
210 MetricCounterPtr m_completed_emerge_counter[5];
212 // Managers of various map generation-related components
213 // Note that each Mapgen gets a copy(!) of these to work with
215 BiomeManager *biomemgr;
217 DecorationManager *decomgr;
218 SchematicManager *schemmgr;
220 // Requires m_queue_mutex held
221 EmergeThread *getOptimalThread();
223 bool pushBlockEmergeData(
227 EmergeCompletionCallback callback,
228 void *callback_param,
229 bool *entry_already_exists);
231 bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
233 void reportCompletedEmerge(EmergeAction action);
235 friend class EmergeThread;