Fix use of previously deallocated EmergeManager
[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         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         MapgenParams *luaoverride_params;
102         u32 luaoverride_params_modified;
103         u32 luaoverride_flagmask;
104
105         //block emerge queue data structures
106         JMutex queuemutex;
107         std::map<v3s16, BlockEmergeData *> blocks_enqueued;
108         std::map<u16, u16> peer_queue_count;
109
110         //Mapgen-related structures
111         BiomeDefManager *biomedef;
112         std::vector<Ore *> ores;
113         std::vector<Decoration *> decorations;
114
115         EmergeManager(IGameDef *gamedef);
116         ~EmergeManager();
117
118         void initMapgens(MapgenParams *mgparams);
119         MapgenParams *setMapgenType(MapgenParams *mgparams, std::string newname);
120         Mapgen *getCurrentMapgen();
121         Mapgen *createMapgen(std::string mgname, int mgid,
122                                                 MapgenParams *mgparams);
123         MapgenParams *createMapgenParams(std::string mgname);
124         void startThreads();
125         void stopThreads();
126         bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
127
128         void registerMapgen(std::string name, MapgenFactory *mgfactory);
129         MapgenParams *getParamsFromSettings(Settings *settings);
130         void setParamsToSettings(Settings *settings);
131
132         //mapgen helper methods
133         Biome *getBiomeAtPoint(v3s16 p);
134         int getGroundLevelAtPoint(v2s16 p);
135         bool isBlockUnderground(v3s16 blockpos);
136         u32 getBlockSeed(v3s16 p);
137 };
138
139 #endif