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; \
39 class INodeDefManager;
44 class DecorationManager;
45 class SchematicManager;
48 // Structure containing inputs/outputs for chunk generation
49 struct BlockMakeData {
50 MMVManip *vmanip = nullptr;
54 v3s16 blockpos_requested;
55 UniqueQueue<v3s16> transforming_liquid;
56 INodeDefManager *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;
91 INodeDefManager *ndef;
92 bool enable_mapgen_debug_info;
95 u32 gen_notify_on = 0;
96 std::set<u32> gen_notify_on_deco_ids;
98 // Parameters passed to mapgens owned by ServerMap
99 // TODO(hmmmm): Remove this after mapgen helper methods using them
100 // are moved to ServerMap
101 MapgenParams *mgparams;
103 // Hackish workaround:
104 // For now, EmergeManager must hold onto a ptr to the Map's setting manager
105 // since the Map can only be accessed through the Environment, and the
106 // Environment is not created until after script initialization.
107 MapSettingsManager *map_settings_mgr;
109 // Managers of various map generation-related components
110 BiomeManager *biomemgr;
112 DecorationManager *decomgr;
113 SchematicManager *schemmgr;
116 EmergeManager(Server *server);
118 DISABLE_CLASS_COPY(EmergeManager);
120 bool initMapgens(MapgenParams *mgparams);
126 bool enqueueBlockEmerge(
130 bool ignore_queue_limits=false);
132 bool enqueueBlockEmergeEx(
136 EmergeCompletionCallback callback,
137 void *callback_param);
139 v3s16 getContainingChunk(v3s16 blockpos);
141 Mapgen *getCurrentMapgen();
143 // Mapgen helpers methods
144 int getSpawnLevelAtPoint(v2s16 p);
145 int getGroundLevelAtPoint(v2s16 p);
146 bool isBlockUnderground(v3s16 blockpos);
148 static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
151 std::vector<Mapgen *> m_mapgens;
152 std::vector<EmergeThread *> m_threads;
153 bool m_threads_active = false;
155 std::mutex m_queue_mutex;
156 std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
157 std::unordered_map<u16, u16> m_peer_queue_count;
160 u16 m_qlimit_diskonly;
161 u16 m_qlimit_generate;
163 // Requires m_queue_mutex held
164 EmergeThread *getOptimalThread();
166 bool pushBlockEmergeData(
170 EmergeCompletionCallback callback,
171 void *callback_param,
172 bool *entry_already_exists);
174 bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
176 friend class EmergeThread;