Huge overhaul of the entire MapgenParams system
[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 #include "mapgen.h" // for MapgenParams
28
29 #define MGPARAMS_SET_MGNAME      1
30 #define MGPARAMS_SET_SEED        2
31 #define MGPARAMS_SET_WATER_LEVEL 4
32 #define MGPARAMS_SET_FLAGS       8
33
34 #define BLOCK_EMERGE_ALLOWGEN (1<<0)
35
36 #define EMERGE_DBG_OUT(x) \
37         { if (enable_mapgen_debug_info) \
38         infostream << "EmergeThread: " x << std::endl; }
39
40 class EmergeThread;
41 //class Mapgen;
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         bool threads_active;
91
92         //settings
93         MapgenParams params;
94         bool mapgen_debug_info;
95         u16 qlimit_total;
96         u16 qlimit_diskonly;
97         u16 qlimit_generate;
98
99         u32 gennotify;
100
101         //block emerge queue data structures
102         JMutex queuemutex;
103         std::map<v3s16, BlockEmergeData *> blocks_enqueued;
104         std::map<u16, u16> peer_queue_count;
105
106         //Mapgen-related structures
107         BiomeDefManager *biomedef;
108         std::vector<Ore *> ores;
109         std::vector<Decoration *> decorations;
110
111         EmergeManager(IGameDef *gamedef);
112         ~EmergeManager();
113
114         void initMapgens();
115         Mapgen *getCurrentMapgen();
116         Mapgen *createMapgen(std::string mgname, int mgid,
117                                                 MapgenParams *mgparams);
118         MapgenSpecificParams *createMapgenParams(std::string mgname);
119         void startThreads();
120         void stopThreads();
121         bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
122
123         void registerMapgen(std::string name, MapgenFactory *mgfactory);
124         void loadParamsFromSettings(Settings *settings);
125         void saveParamsToSettings(Settings *settings);
126
127         //mapgen helper methods
128         Biome *getBiomeAtPoint(v3s16 p);
129         int getGroundLevelAtPoint(v2s16 p);
130         bool isBlockUnderground(v3s16 blockpos);
131         u32 getBlockSeed(v3s16 p);
132 };
133
134 #endif