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