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