Re-add jungles, apple trees
[oweals/minetest.git] / src / emerge.h
1 #ifndef EMERGE_HEADER
2 #define EMERGE_HEADER
3
4 #include <map>
5 #include <queue>
6 #include "util/thread.h"
7
8 #define BLOCK_EMERGE_ALLOWGEN (1<<0)
9
10 #define EMERGE_DBG_OUT(x) \
11         { if (enable_mapgen_debug_info) \
12         infostream << "EmergeThread: " x << std::endl; }
13
14 class Mapgen;
15 class MapgenParams;
16 class MapgenFactory;
17 class Biome;
18 class BiomeDefManager;
19 class EmergeThread;
20 class ManualMapVoxelManipulator;
21
22 #include "server.h"
23
24 struct BlockMakeData {
25         ManualMapVoxelManipulator *vmanip;
26         u64 seed;
27         v3s16 blockpos_min;
28         v3s16 blockpos_max;
29         v3s16 blockpos_requested;
30         UniqueQueue<v3s16> transforming_liquid;
31         INodeDefManager *nodedef;
32
33         BlockMakeData():
34                 vmanip(NULL),
35                 seed(0),
36                 nodedef(NULL)
37         {}
38
39         ~BlockMakeData() { delete vmanip; }
40 };
41
42 struct BlockEmergeData {
43         u16 peer_requested;
44         u8 flags;
45 };
46
47 class EmergeManager {
48 public:
49         std::map<std::string, MapgenFactory *> mglist;
50         
51         std::vector<Mapgen *> mapgen;
52         std::vector<EmergeThread *> emergethread;
53         
54         //settings
55         MapgenParams *params;
56         bool mapgen_debug_info;
57         u16 qlimit_total;
58         u16 qlimit_diskonly;
59         u16 qlimit_generate;
60         
61         //block emerge queue data structures
62         JMutex queuemutex;
63         std::map<v3s16, BlockEmergeData *> blocks_enqueued;
64         std::map<u16, u16> peer_queue_count;
65
66         //biome manager
67         BiomeDefManager *biomedef;
68
69         EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef);
70         ~EmergeManager();
71
72         void initMapgens(MapgenParams *mgparams);
73         Mapgen *createMapgen(std::string mgname, int mgid,
74                                                 MapgenParams *mgparams);
75         MapgenParams *createMapgenParams(std::string mgname);
76         bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
77         
78         void registerMapgen(std::string name, MapgenFactory *mgfactory);
79         MapgenParams *getParamsFromSettings(Settings *settings);
80         void setParamsToSettings(Settings *settings);
81         
82         //mapgen helper methods
83         Biome *getBiomeAtPoint(v3s16 p);
84         int getGroundLevelAtPoint(v2s16 p);
85         bool isBlockUnderground(v3s16 blockpos);
86         u32 getBlockSeed(v3s16 p);
87 };
88
89 class EmergeThread : public SimpleThread
90 {
91         Server *m_server;
92         ServerMap *map;
93         EmergeManager *emerge;
94         Mapgen *mapgen;
95         bool enable_mapgen_debug_info;
96         int id;
97         
98 public:
99         Event qevent;
100         std::queue<v3s16> blockqueue;
101         
102         EmergeThread(Server *server, int ethreadid):
103                 SimpleThread(),
104                 m_server(server),
105                 map(NULL),
106                 emerge(NULL),
107                 mapgen(NULL),
108                 id(ethreadid)
109         {
110         }
111
112         void *Thread();
113
114         void trigger()
115         {
116                 setRun(true);
117                 if(IsRunning() == false)
118                 {
119                         Start();
120                 }
121         }
122
123         bool popBlockEmerge(v3s16 *pos, u8 *flags);
124         bool getBlockOrStartGen(v3s16 p, MapBlock **b, 
125                                                         BlockMakeData *data, bool allow_generate);
126 };
127
128 #endif