Fix remaining issues with mapgen scriptapi
[oweals/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 "mapgen/mapgen.h" // for MapgenParams
28 #include "map.h"
29
30 #define BLOCK_EMERGE_ALLOW_GEN   (1 << 0)
31 #define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
32
33 #define EMERGE_DBG_OUT(x) {                            \
34         if (enable_mapgen_debug_info)                      \
35                 infostream << "EmergeThread: " x << std::endl; \
36 }
37
38 class EmergeThread;
39 class NodeDefManager;
40 class Settings;
41
42 class BiomeManager;
43 class OreManager;
44 class DecorationManager;
45 class SchematicManager;
46 class Server;
47 class ModApiMapgen;
48
49 // Structure containing inputs/outputs for chunk generation
50 struct BlockMakeData {
51         MMVManip *vmanip = nullptr;
52         u64 seed = 0;
53         v3s16 blockpos_min;
54         v3s16 blockpos_max;
55         v3s16 blockpos_requested;
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 // Callback
74 typedef void (*EmergeCompletionCallback)(
75         v3s16 blockpos, EmergeAction action, void *param);
76
77 typedef std::vector<
78         std::pair<
79                 EmergeCompletionCallback,
80                 void *
81         >
82 > EmergeCallbackList;
83
84 struct BlockEmergeData {
85         u16 peer_requested;
86         u16 flags;
87         EmergeCallbackList callbacks;
88 };
89
90 class EmergeParams {
91         friend class EmergeManager;
92 public:
93         EmergeParams() = delete;
94         ~EmergeParams();
95         DISABLE_CLASS_COPY(EmergeParams);
96
97         const NodeDefManager *ndef; // shared
98         bool enable_mapgen_debug_info;
99
100         u32 gen_notify_on;
101         const std::set<u32> *gen_notify_on_deco_ids; // shared
102
103         BiomeManager *biomemgr;
104         OreManager *oremgr;
105         DecorationManager *decomgr;
106         SchematicManager *schemmgr;
107
108 private:
109         EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
110                 const OreManager *oremgr, const DecorationManager *decomgr,
111                 const SchematicManager *schemmgr);
112 };
113
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
118          */
119         friend class ModApiMapgen;
120 public:
121         const NodeDefManager *ndef;
122         bool enable_mapgen_debug_info;
123
124         // Generation Notify
125         u32 gen_notify_on = 0;
126         std::set<u32> gen_notify_on_deco_ids;
127
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;
132
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;
138
139         // Methods
140         EmergeManager(Server *server);
141         ~EmergeManager();
142         DISABLE_CLASS_COPY(EmergeManager);
143
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();
154
155         void initMapgens(MapgenParams *mgparams);
156
157         void startThreads();
158         void stopThreads();
159         bool isRunning();
160
161         bool enqueueBlockEmerge(
162                 session_t peer_id,
163                 v3s16 blockpos,
164                 bool allow_generate,
165                 bool ignore_queue_limits=false);
166
167         bool enqueueBlockEmergeEx(
168                 v3s16 blockpos,
169                 session_t peer_id,
170                 u16 flags,
171                 EmergeCompletionCallback callback,
172                 void *callback_param);
173
174         v3s16 getContainingChunk(v3s16 blockpos);
175
176         Mapgen *getCurrentMapgen();
177
178         // Mapgen helpers methods
179         int getSpawnLevelAtPoint(v2s16 p);
180         int getGroundLevelAtPoint(v2s16 p);
181         bool isBlockUnderground(v3s16 blockpos);
182
183         static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
184
185 private:
186         std::vector<Mapgen *> m_mapgens;
187         std::vector<EmergeThread *> m_threads;
188         bool m_threads_active = false;
189
190         std::mutex m_queue_mutex;
191         std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
192         std::unordered_map<u16, u16> m_peer_queue_count;
193
194         u16 m_qlimit_total;
195         u16 m_qlimit_diskonly;
196         u16 m_qlimit_generate;
197
198         // Managers of various map generation-related components
199         // Note that each Mapgen gets a copy(!) of these to work with
200         BiomeManager *biomemgr;
201         OreManager *oremgr;
202         DecorationManager *decomgr;
203         SchematicManager *schemmgr;
204
205         // Requires m_queue_mutex held
206         EmergeThread *getOptimalThread();
207
208         bool pushBlockEmergeData(
209                 v3s16 pos,
210                 u16 peer_requested,
211                 u16 flags,
212                 EmergeCompletionCallback callback,
213                 void *callback_param,
214                 bool *entry_already_exists);
215
216         bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
217
218         friend class EmergeThread;
219 };