Hide mapgens from main menu not intended for end users
[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         // Map generation parameters
101         MapgenParams params;
102
103         // Managers of various map generation-related components
104         BiomeManager *biomemgr;
105         OreManager *oremgr;
106         DecorationManager *decomgr;
107         SchematicManager *schemmgr;
108
109         // Methods
110         EmergeManager(IGameDef *gamedef);
111         ~EmergeManager();
112
113         void loadMapgenParams();
114         void initMapgens();
115
116         void startThreads();
117         void stopThreads();
118
119         bool enqueueBlockEmerge(
120                 u16 peer_id,
121                 v3s16 blockpos,
122                 bool allow_generate,
123                 bool ignore_queue_limits=false);
124
125         bool enqueueBlockEmergeEx(
126                 v3s16 blockpos,
127                 u16 peer_id,
128                 u16 flags,
129                 EmergeCompletionCallback callback,
130                 void *callback_param);
131
132         v3s16 getContainingChunk(v3s16 blockpos);
133
134         Mapgen *getCurrentMapgen();
135
136         // Mapgen helpers methods
137         Biome *getBiomeAtPoint(v3s16 p);
138         int getGroundLevelAtPoint(v2s16 p);
139         bool isBlockUnderground(v3s16 blockpos);
140
141         static MapgenFactory *getMapgenFactory(const std::string &mgname);
142         static void getMapgenNames(
143                 std::vector<const char *> *mgnames, bool include_hidden);
144         static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
145
146 private:
147         std::vector<Mapgen *> m_mapgens;
148         std::vector<EmergeThread *> m_threads;
149         bool m_threads_active;
150
151         Mutex m_queue_mutex;
152         std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
153         std::map<u16, u16> m_peer_queue_count;
154
155         u16 m_qlimit_total;
156         u16 m_qlimit_diskonly;
157         u16 m_qlimit_generate;
158
159         // Requires m_queue_mutex held
160         EmergeThread *getOptimalThread();
161         bool pushBlockEmergeData(v3s16 pos, u16 peer_requested, u16 flags,
162                 EmergeCompletionCallback callback, void *callback_param);
163         bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
164
165         friend class EmergeThread;
166 };
167
168 #endif