]> git.lizzy.rs Git - minetest.git/blob - src/emerge.h
Add keybind to swap items between hands
[minetest.git] / src / emerge.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include <map>
23 #include <mutex>
24 #include "network/networkprotocol.h"
25 #include "irr_v3d.h"
26 #include "util/container.h"
27 #include "util/metricsbackend.h"
28 #include "mapgen/mapgen.h" // for MapgenParams
29 #include "map.h"
30
31 #define BLOCK_EMERGE_ALLOW_GEN   (1 << 0)
32 #define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
33
34 #define EMERGE_DBG_OUT(x) {                            \
35         if (enable_mapgen_debug_info)                      \
36                 infostream << "EmergeThread: " x << std::endl; \
37 }
38
39 class EmergeThread;
40 class NodeDefManager;
41 class Settings;
42
43 class BiomeManager;
44 class OreManager;
45 class DecorationManager;
46 class SchematicManager;
47 class Server;
48 class ModApiMapgen;
49
50 // Structure containing inputs/outputs for chunk generation
51 struct BlockMakeData {
52         MMVManip *vmanip = nullptr;
53         u64 seed = 0;
54         v3s16 blockpos_min;
55         v3s16 blockpos_max;
56         UniqueQueue<v3s16> transforming_liquid;
57         const NodeDefManager *nodedef = nullptr;
58
59         BlockMakeData() = default;
60
61         ~BlockMakeData() { delete vmanip; }
62 };
63
64 // Result from processing an item on the emerge queue
65 enum EmergeAction {
66         EMERGE_CANCELLED,
67         EMERGE_ERRORED,
68         EMERGE_FROM_MEMORY,
69         EMERGE_FROM_DISK,
70         EMERGE_GENERATED,
71 };
72
73 const static std::string emergeActionStrs[] = {
74         "cancelled",
75         "errored",
76         "from_memory",
77         "from_disk",
78         "generated",
79 };
80
81 // Callback
82 typedef void (*EmergeCompletionCallback)(
83         v3s16 blockpos, EmergeAction action, void *param);
84
85 typedef std::vector<
86         std::pair<
87                 EmergeCompletionCallback,
88                 void *
89         >
90 > EmergeCallbackList;
91
92 struct BlockEmergeData {
93         u16 peer_requested;
94         u16 flags;
95         EmergeCallbackList callbacks;
96 };
97
98 class EmergeParams {
99         friend class EmergeManager;
100 public:
101         EmergeParams() = delete;
102         ~EmergeParams();
103         DISABLE_CLASS_COPY(EmergeParams);
104
105         const NodeDefManager *ndef; // shared
106         bool enable_mapgen_debug_info;
107
108         u32 gen_notify_on;
109         const std::set<u32> *gen_notify_on_deco_ids; // shared
110
111         BiomeGen *biomegen;
112         BiomeManager *biomemgr;
113         OreManager *oremgr;
114         DecorationManager *decomgr;
115         SchematicManager *schemmgr;
116
117 private:
118         EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
119                 const BiomeManager *biomemgr,
120                 const OreManager *oremgr, const DecorationManager *decomgr,
121                 const SchematicManager *schemmgr);
122 };
123
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
128          */
129         friend class ModApiMapgen;
130 public:
131         const NodeDefManager *ndef;
132         bool enable_mapgen_debug_info;
133
134         // Generation Notify
135         u32 gen_notify_on = 0;
136         std::set<u32> gen_notify_on_deco_ids;
137
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;
142
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;
148
149         // Methods
150         EmergeManager(Server *server, MetricsBackend *mb);
151         ~EmergeManager();
152         DISABLE_CLASS_COPY(EmergeManager);
153
154         const BiomeGen *getBiomeGen() const { return biomegen; }
155
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();
166
167         void initMapgens(MapgenParams *mgparams);
168
169         void startThreads();
170         void stopThreads();
171         bool isRunning();
172
173         bool enqueueBlockEmerge(
174                 session_t peer_id,
175                 v3s16 blockpos,
176                 bool allow_generate,
177                 bool ignore_queue_limits=false);
178
179         bool enqueueBlockEmergeEx(
180                 v3s16 blockpos,
181                 session_t peer_id,
182                 u16 flags,
183                 EmergeCompletionCallback callback,
184                 void *callback_param);
185
186         bool isBlockInQueue(v3s16 pos);
187
188         Mapgen *getCurrentMapgen();
189
190         // Mapgen helpers methods
191         int getSpawnLevelAtPoint(v2s16 p);
192         bool isBlockUnderground(v3s16 blockpos);
193
194         static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
195
196 private:
197         std::vector<Mapgen *> m_mapgens;
198         std::vector<EmergeThread *> m_threads;
199         bool m_threads_active = false;
200
201         std::mutex m_queue_mutex;
202         std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
203         std::unordered_map<u16, u32> m_peer_queue_count;
204
205         u32 m_qlimit_total;
206         u32 m_qlimit_diskonly;
207         u32 m_qlimit_generate;
208
209         // Emerge metrics
210         MetricCounterPtr m_completed_emerge_counter[5];
211
212         // Managers of various map generation-related components
213         // Note that each Mapgen gets a copy(!) of these to work with
214         BiomeGen *biomegen;
215         BiomeManager *biomemgr;
216         OreManager *oremgr;
217         DecorationManager *decomgr;
218         SchematicManager *schemmgr;
219
220         // Requires m_queue_mutex held
221         EmergeThread *getOptimalThread();
222
223         bool pushBlockEmergeData(
224                 v3s16 pos,
225                 u16 peer_requested,
226                 u16 flags,
227                 EmergeCompletionCallback callback,
228                 void *callback_param,
229                 bool *entry_already_exists);
230
231         bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
232
233         void reportCompletedEmerge(EmergeAction action);
234
235         friend class EmergeThread;
236 };