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 v3s16 blockpos_requested;
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
74 typedef void (*EmergeCompletionCallback)(
75 v3s16 blockpos, EmergeAction action, void *param);
79 EmergeCompletionCallback,
84 struct BlockEmergeData {
87 EmergeCallbackList callbacks;
91 friend class EmergeManager;
93 EmergeParams() = delete;
95 DISABLE_CLASS_COPY(EmergeParams);
97 const NodeDefManager *ndef; // shared
98 bool enable_mapgen_debug_info;
101 const std::set<u32> *gen_notify_on_deco_ids; // shared
103 BiomeManager *biomemgr;
105 DecorationManager *decomgr;
106 SchematicManager *schemmgr;
109 EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
110 const OreManager *oremgr, const DecorationManager *decomgr,
111 const SchematicManager *schemmgr);
114 class EmergeManager {
115 /* The mod API needs unchecked access to allow:
116 * - using decomgr or oremgr to place decos/ores
117 * - using schemmgr to load and place schematics
119 friend class ModApiMapgen;
121 const NodeDefManager *ndef;
122 bool enable_mapgen_debug_info;
125 u32 gen_notify_on = 0;
126 std::set<u32> gen_notify_on_deco_ids;
128 // Parameters passed to mapgens owned by ServerMap
129 // TODO(hmmmm): Remove this after mapgen helper methods using them
130 // are moved to ServerMap
131 MapgenParams *mgparams;
133 // Hackish workaround:
134 // For now, EmergeManager must hold onto a ptr to the Map's setting manager
135 // since the Map can only be accessed through the Environment, and the
136 // Environment is not created until after script initialization.
137 MapSettingsManager *map_settings_mgr;
140 EmergeManager(Server *server);
142 DISABLE_CLASS_COPY(EmergeManager);
144 // no usage restrictions
145 const BiomeManager *getBiomeManager() const { return biomemgr; }
146 const OreManager *getOreManager() const { return oremgr; }
147 const DecorationManager *getDecorationManager() const { return decomgr; }
148 const SchematicManager *getSchematicManager() const { return schemmgr; }
149 // only usable before mapgen init
150 BiomeManager *getWritableBiomeManager();
151 OreManager *getWritableOreManager();
152 DecorationManager *getWritableDecorationManager();
153 SchematicManager *getWritableSchematicManager();
155 void initMapgens(MapgenParams *mgparams);
161 bool enqueueBlockEmerge(
165 bool ignore_queue_limits=false);
167 bool enqueueBlockEmergeEx(
171 EmergeCompletionCallback callback,
172 void *callback_param);
174 v3s16 getContainingChunk(v3s16 blockpos);
176 Mapgen *getCurrentMapgen();
178 // Mapgen helpers methods
179 int getSpawnLevelAtPoint(v2s16 p);
180 int getGroundLevelAtPoint(v2s16 p);
181 bool isBlockUnderground(v3s16 blockpos);
183 static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
186 std::vector<Mapgen *> m_mapgens;
187 std::vector<EmergeThread *> m_threads;
188 bool m_threads_active = false;
190 std::mutex m_queue_mutex;
191 std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
192 std::unordered_map<u16, u16> m_peer_queue_count;
195 u16 m_qlimit_diskonly;
196 u16 m_qlimit_generate;
198 // Managers of various map generation-related components
199 // Note that each Mapgen gets a copy(!) of these to work with
200 BiomeManager *biomemgr;
202 DecorationManager *decomgr;
203 SchematicManager *schemmgr;
205 // Requires m_queue_mutex held
206 EmergeThread *getOptimalThread();
208 bool pushBlockEmergeData(
212 EmergeCompletionCallback callback,
213 void *callback_param,
214 bool *entry_already_exists);
216 bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
218 friend class EmergeThread;