Use C++11 mutexes only (remove compat code) (#5922)
[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 #ifndef EMERGE_HEADER
21 #define EMERGE_HEADER
22
23 #include <map>
24 #include <mutex>
25 #include "irr_v3d.h"
26 #include "util/container.h"
27 #include "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) do {                         \
34         if (enable_mapgen_debug_info)                      \
35                 infostream << "EmergeThread: " x << std::endl; \
36 } while (0)
37
38 class EmergeThread;
39 class INodeDefManager;
40 class Settings;
41
42 class BiomeManager;
43 class OreManager;
44 class DecorationManager;
45 class SchematicManager;
46 class Server;
47
48 // Structure containing inputs/outputs for chunk generation
49 struct BlockMakeData {
50         MMVManip *vmanip;
51         u64 seed;
52         v3s16 blockpos_min;
53         v3s16 blockpos_max;
54         v3s16 blockpos_requested;
55         UniqueQueue<v3s16> transforming_liquid;
56         INodeDefManager *nodedef;
57
58         BlockMakeData():
59                 vmanip(NULL),
60                 seed(0),
61                 nodedef(NULL)
62         {}
63
64         ~BlockMakeData() { delete vmanip; }
65 };
66
67 // Result from processing an item on the emerge queue
68 enum EmergeAction {
69         EMERGE_CANCELLED,
70         EMERGE_ERRORED,
71         EMERGE_FROM_MEMORY,
72         EMERGE_FROM_DISK,
73         EMERGE_GENERATED,
74 };
75
76 // Callback
77 typedef void (*EmergeCompletionCallback)(
78         v3s16 blockpos, EmergeAction action, void *param);
79
80 typedef std::vector<
81         std::pair<
82                 EmergeCompletionCallback,
83                 void *
84         >
85 > EmergeCallbackList;
86
87 struct BlockEmergeData {
88         u16 peer_requested;
89         u16 flags;
90         EmergeCallbackList callbacks;
91 };
92
93 class EmergeManager {
94 public:
95         INodeDefManager *ndef;
96         bool enable_mapgen_debug_info;
97
98         // Generation Notify
99         u32 gen_notify_on;
100         std::set<u32> gen_notify_on_deco_ids;
101
102         // Parameters passed to mapgens owned by ServerMap
103         // TODO(hmmmm): Remove this after mapgen helper methods using them
104         // are moved to ServerMap
105         MapgenParams *mgparams;
106
107         // Hackish workaround:
108         // For now, EmergeManager must hold onto a ptr to the Map's setting manager
109         // since the Map can only be accessed through the Environment, and the
110         // Environment is not created until after script initialization.
111         MapSettingsManager *map_settings_mgr;
112
113         // Managers of various map generation-related components
114         BiomeManager *biomemgr;
115         OreManager *oremgr;
116         DecorationManager *decomgr;
117         SchematicManager *schemmgr;
118
119         // Methods
120         EmergeManager(Server *server);
121         ~EmergeManager();
122
123         bool initMapgens(MapgenParams *mgparams);
124
125         void startThreads();
126         void stopThreads();
127         bool isRunning();
128
129         bool enqueueBlockEmerge(
130                 u16 peer_id,
131                 v3s16 blockpos,
132                 bool allow_generate,
133                 bool ignore_queue_limits=false);
134
135         bool enqueueBlockEmergeEx(
136                 v3s16 blockpos,
137                 u16 peer_id,
138                 u16 flags,
139                 EmergeCompletionCallback callback,
140                 void *callback_param);
141
142         v3s16 getContainingChunk(v3s16 blockpos);
143
144         Mapgen *getCurrentMapgen();
145
146         // Mapgen helpers methods
147         Biome *getBiomeAtPoint(v3s16 p);
148         int getSpawnLevelAtPoint(v2s16 p);
149         int getGroundLevelAtPoint(v2s16 p);
150         bool isBlockUnderground(v3s16 blockpos);
151
152         static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
153
154 private:
155         std::vector<Mapgen *> m_mapgens;
156         std::vector<EmergeThread *> m_threads;
157         bool m_threads_active;
158
159         std::mutex m_queue_mutex;
160         std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
161         std::unordered_map<u16, u16> m_peer_queue_count;
162
163         u16 m_qlimit_total;
164         u16 m_qlimit_diskonly;
165         u16 m_qlimit_generate;
166
167         // Requires m_queue_mutex held
168         EmergeThread *getOptimalThread();
169
170         bool pushBlockEmergeData(
171                 v3s16 pos,
172                 u16 peer_requested,
173                 u16 flags,
174                 EmergeCompletionCallback callback,
175                 void *callback_param,
176                 bool *entry_already_exists);
177
178         bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
179
180         friend class EmergeThread;
181
182         DISABLE_CLASS_COPY(EmergeManager);
183 };
184
185 #endif