Add map feature generation notify Lua API
[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 "map.h" // for ManualMapVoxelManipulator
27
28 #define MGPARAMS_SET_MGNAME      1
29 #define MGPARAMS_SET_SEED        2
30 #define MGPARAMS_SET_WATER_LEVEL 4
31 #define MGPARAMS_SET_FLAGS       8
32
33 #define BLOCK_EMERGE_ALLOWGEN (1<<0)
34
35 #define EMERGE_DBG_OUT(x) \
36         { if (enable_mapgen_debug_info) \
37         infostream << "EmergeThread: " x << std::endl; }
38
39 class EmergeThread;
40 class Mapgen;
41 struct MapgenParams;
42 struct MapgenFactory;
43 class Biome;
44 class BiomeDefManager;
45 class Decoration;
46 class Ore;
47 class INodeDefManager;
48 class Settings;
49
50 struct BlockMakeData {
51         ManualMapVoxelManipulator *vmanip;
52         u64 seed;
53         v3s16 blockpos_min;
54         v3s16 blockpos_max;
55         v3s16 blockpos_requested;
56         UniqueQueue<v3s16> transforming_liquid;
57         INodeDefManager *nodedef;
58
59         BlockMakeData():
60                 vmanip(NULL),
61                 seed(0),
62                 nodedef(NULL)
63         {}
64
65         ~BlockMakeData() { delete vmanip; }
66 };
67
68 struct BlockEmergeData {
69         u16 peer_requested;
70         u8 flags;
71 };
72
73 class IBackgroundBlockEmerger
74 {
75 public:
76         virtual bool enqueueBlockEmerge(u16 peer_id, v3s16 p,
77                         bool allow_generate) = 0;
78         virtual ~IBackgroundBlockEmerger() {}
79 };
80
81 class EmergeManager : public IBackgroundBlockEmerger {
82 public:
83         INodeDefManager *ndef;
84
85         std::map<std::string, MapgenFactory *> mglist;
86
87         std::vector<Mapgen *> mapgen;
88         std::vector<EmergeThread *> emergethread;
89
90         //settings
91         MapgenParams *params;
92         bool mapgen_debug_info;
93         u16 qlimit_total;
94         u16 qlimit_diskonly;
95         u16 qlimit_generate;
96
97         u32 gennotify;
98
99         MapgenParams *luaoverride_params;
100         u32 luaoverride_params_modified;
101         u32 luaoverride_flagmask;
102
103         //block emerge queue data structures
104         JMutex queuemutex;
105         std::map<v3s16, BlockEmergeData *> blocks_enqueued;
106         std::map<u16, u16> peer_queue_count;
107
108         //Mapgen-related structures
109         BiomeDefManager *biomedef;
110         std::vector<Ore *> ores;
111         std::vector<Decoration *> decorations;
112
113         EmergeManager(IGameDef *gamedef);
114         ~EmergeManager();
115
116         void initMapgens(MapgenParams *mgparams);
117         MapgenParams *setMapgenType(MapgenParams *mgparams, std::string newname);
118         Mapgen *getCurrentMapgen();
119         Mapgen *createMapgen(std::string mgname, int mgid,
120                                                 MapgenParams *mgparams);
121         MapgenParams *createMapgenParams(std::string mgname);
122         void triggerAllThreads();
123         bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
124
125         void registerMapgen(std::string name, MapgenFactory *mgfactory);
126         MapgenParams *getParamsFromSettings(Settings *settings);
127         void setParamsToSettings(Settings *settings);
128
129         //mapgen helper methods
130         Biome *getBiomeAtPoint(v3s16 p);
131         int getGroundLevelAtPoint(v2s16 p);
132         bool isBlockUnderground(v3s16 blockpos);
133         u32 getBlockSeed(v3s16 p);
134 };
135
136 #endif