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