Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <iostream>
23#include <set>
24#include <map>
25
27#include "mapblock.h"
28#include "mapnode.h"
29#include "constants.h"
30#include "voxel.h"
31#include "modifiedstate.h"
32#include "util/numeric.h"
33#include "nodetimer.h"
34#include "debug.h"
35
36class MapSector;
37class NodeMetadata;
38class IGameDef;
40
41/*
42 MapEditEvent
43*/
44
46 // Node added (changed from air or something else to something)
48 // Node removed (changed to air)
50 // Node swapped (changed without metadata change)
52 // Node metadata changed
54 // Anything else (modified_blocks are set unsent)
56};
57
59{
63 std::vector<v3s16> modified_blocks; // Represents a set
64 bool is_private_change = false;
65
66 MapEditEvent() = default;
67
68 // Sets the event's position and marks the block as modified.
70 {
71 assert(modified_blocks.empty()); // only meant for initialization (once)
72 p = pos;
73 modified_blocks.push_back(getNodeBlockPos(pos));
74 }
75
76 void setModifiedBlocks(const std::map<v3s16, MapBlock *>& blocks)
77 {
78 assert(modified_blocks.empty()); // only meant for initialization (once)
79 modified_blocks.reserve(blocks.size());
80 for (const auto &block : blocks)
81 modified_blocks.push_back(block.first);
82 }
83
85 {
86 switch(type){
87 case MEET_ADDNODE:
88 case MEET_REMOVENODE:
89 case MEET_SWAPNODE:
91 return VoxelArea(p);
92 case MEET_OTHER:
93 {
94 VoxelArea a;
95 for (v3s16 p : modified_blocks) {
96 v3s16 np1 = p*MAP_BLOCKSIZE;
97 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
98 a.addPoint(np1);
99 a.addPoint(np2);
100 }
101 return a;
102 }
103 }
104 return VoxelArea();
105 }
106};
107
109{
110public:
111 // event shall be deleted by caller after the call.
112 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
113};
114
115class Map /*: public NodeContainer*/
116{
117public:
118
119 Map(IGameDef *gamedef);
120 virtual ~Map();
122
123 void addEventReceiver(MapEventReceiver *event_receiver);
124 void removeEventReceiver(MapEventReceiver *event_receiver);
125 // event shall be deleted by caller after the call.
126 void dispatchEvent(const MapEditEvent &event);
127
128 // On failure returns NULL
130 // Same as the above (there exists no lock anymore)
132
133 /*
134 This is overloaded by ClientMap and ServerMap to allow
135 their differing fetch methods.
136 */
137 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
138
139 // Returns InvalidPositionException if not found
141 // Returns NULL if not found
143
144 /* Server overrides */
145 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
146 { return getBlockNoCreateNoEx(p); }
147
148 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
149
150 bool isValidPosition(v3s16 p);
151
152 // throws InvalidPositionException if not found
153 void setNode(v3s16 p, MapNode n);
154
155 // Returns a CONTENT_IGNORE node if not found
156 // If is_valid_position is not NULL then this will be set to true if the
157 // position is valid, otherwise false
158 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
159
160 /*
161 These handle lighting but not faces.
162 */
163 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
164 std::map<v3s16, MapBlock*> &modified_blocks,
165 bool remove_metadata = true);
167 std::map<v3s16, MapBlock*> &modified_blocks);
168
169 /*
170 Wrappers for the latter ones.
171 These emit events.
172 Return true if succeeded, false if not.
173 */
174 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
176
177 // Call these before and after saving of many blocks
178 virtual void beginSave() {}
179 virtual void endSave() {}
180
181 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
182
183 /*
184 Return true unless the map definitely cannot save blocks.
185 */
186 virtual bool maySaveBlocks() { return true; }
187
188 // Server implements these.
189 // Client leaves them as no-op.
190 virtual bool saveBlock(MapBlock *block) { return false; }
191 virtual bool deleteBlock(v3s16 blockpos) { return false; }
192
193 /*
194 Updates usage timers and unloads unused blocks and sectors.
195 Saves modified blocks before unloading if possible.
196 */
197 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
198 std::vector<v3s16> *unloaded_blocks=NULL);
199
200 /*
201 Unloads all blocks with a zero refCount().
202 Saves modified blocks before unloading if possible.
203 */
204 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
205
206 // Deletes sectors and their blocks from memory
207 // Takes cache into account
208 // If deleted sector is in sector cache, clears cache
209 void deleteSectors(std::vector<v2s16> &list);
210
211 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
212 virtual void PrintInfo(std::ostream &out);
213
214 /*
215 Node metadata
216 These are basically coordinate wrappers to MapBlock
217 */
218
219 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
221
236 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
238
239 /*
240 Node Timers
241 These are basically coordinate wrappers to MapBlock
242 */
243
245 void setNodeTimer(const NodeTimer &t);
246 void removeNodeTimer(v3s16 p);
247
248 /*
249 Utilities
250 */
251
252 // Iterates through all nodes in the area in an unspecified order.
253 // The given callback takes the position as its first argument and the node
254 // as its second. If it returns false, forEachNodeInArea returns early.
255 template<typename F>
256 void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
257 {
258 v3s16 bpmin = getNodeBlockPos(minp);
259 v3s16 bpmax = getNodeBlockPos(maxp);
260 for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
261 for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
262 for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
263 // y is iterated innermost to make use of the sector cache.
264 v3s16 bp(bx, by, bz);
265 MapBlock *block = getBlockNoCreateNoEx(bp);
266 v3s16 basep = bp * MAP_BLOCKSIZE;
267 s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
268 s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
269 s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
270 s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
271 s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
272 s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
273 for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
274 for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
275 for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
276 v3s16 p = basep + v3s16(x_block, y_block, z_block);
277 MapNode n = block ?
278 block->getNodeNoCheck(x_block, y_block, z_block) :
280 if (!func(p, n))
281 return;
282 }
283 }
284 }
285
286 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
287 {
288 return isBlockOccluded(block->getPosRelative(), cam_pos_nodes, false);
289 }
290 bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool simple_check = false);
291
292protected:
294
295 std::set<MapEventReceiver*> m_event_receivers;
296
297 std::unordered_map<v2s16, MapSector*> m_sectors;
298
299 // Be sure to set this to NULL when the cached sector is deleted
302
303 // This stores the properties of the nodes on the map.
305
306 // Can be implemented by child class
307 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
308
310 const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
311 bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
312 float step, float stepfac, float start_offset, float end_offset,
313 u32 needed_count);
314};
315
316#define VMANIP_BLOCK_DATA_INEXIST 1
317#define VMANIP_BLOCK_CONTAINS_CIGNORE 2
318
320{
321public:
322 MMVManip(Map *map);
323 virtual ~MMVManip() = default;
324
325 virtual void clear()
326 {
328 m_loaded_blocks.clear();
329 }
330
331 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
332 bool load_if_inexistent = true);
333
334 // This is much faster with big chunks of generated data
335 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
336 bool overwrite_generated = true);
337
338 /*
339 Creates a copy of this VManip including contents, the copy will not be
340 associated with a Map.
341 */
342 MMVManip *clone() const;
343
344 // Reassociates a copied VManip to a map
345 void reparent(Map *map);
346
347 // Is it impossible to call initialEmerge / blitBackAll?
348 inline bool isOrphan() const { return !m_map; }
349
350 bool m_is_dirty = false;
351
352protected:
354
355 // may be null
356 Map *m_map = nullptr;
357 /*
358 key = blockpos
359 value = flags describing the block
360 */
361 std::map<v3s16, u8> m_loaded_blocks;
362};
Definition gamedef.h:51
Definition rollback_interface.h:112
Definition map.h:320
Map * m_map
Definition map.h:356
virtual ~MMVManip()=default
MMVManip()
Definition map.h:353
std::map< v3s16, u8 > m_loaded_blocks
Definition map.h:361
MMVManip * clone() const
Definition map.cpp:877
virtual void clear()
Definition map.h:325
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition map.cpp:770
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true)
Definition map.cpp:850
bool isOrphan() const
Definition map.h:348
void reparent(Map *map)
Definition map.cpp:900
bool m_is_dirty
Definition map.h:350
Definition mapblock.h:73
MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
Definition mapblock.h:286
v3s16 getPosRelative()
Definition mapblock.h:214
Definition map.h:109
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition mapsector.h:40
Definition map.h:116
void removeNodeTimer(v3s16 p)
Definition map.cpp:589
std::set< MapEventReceiver * > m_event_receivers
Definition map.h:295
void unloadUnreferencedBlocks(std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:433
bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata=true)
Definition map.cpp:228
void setNodeTimer(const NodeTimer &t)
Definition map.cpp:569
bool removeNodeWithEvent(v3s16 p)
Definition map.cpp:251
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition map.h:179
void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:291
void addEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:56
virtual bool saveBlock(MapBlock *block)
Definition map.h:190
Map(IGameDef *gamedef)
Definition map.cpp:40
MapBlock * getBlockNoCreate(v3s16 p)
Definition map.cpp:109
void setNode(v3s16 p, MapNode n)
Definition map.cpp:159
bool isOccluded(v3s16 pos_camera, v3s16 pos_target, float step, float stepfac, float start_offset, float end_offset, u32 needed_count)
Definition map.cpp:667
const NodeDefManager * m_nodedef
Definition map.h:304
virtual void addNodeAndUpdate(v3s16 p, MapNode n, std::map< v3s16, MapBlock * > &modified_blocks, bool remove_metadata=true)
Definition map.cpp:167
void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
Definition map.h:256
const NodeDefManager * getNodeDefManager()
Definition map.h:148
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition map.cpp:456
virtual bool deleteBlock(v3s16 blockpos)
Definition map.h:191
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition map.cpp:94
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition map.h:286
NodeTimer getNodeTimer(v3s16 p)
Definition map.cpp:549
NodeMetadata * getNodeMetadata(v3s16 p)
Definition map.cpp:497
void removeNodeMetadata(v3s16 p)
Definition map.cpp:535
MapSector * m_sector_cache
Definition map.h:300
virtual void save(ModifiedState save_level)
Definition map.h:181
void deleteSectors(std::vector< v2s16 > &list)
Definition map.cpp:438
bool determineAdditionalOcclusionCheck(v3s16 pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &to_check)
Definition map.cpp:603
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition map.h:145
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:61
v2s16 m_sector_cache_p
Definition map.h:301
virtual ~Map()
Definition map.cpp:46
virtual void PrintInfo(std::ostream &out)
Definition map.cpp:451
void dispatchEvent(const MapEditEvent &event)
Definition map.cpp:66
void removeNodeAndUpdate(v3s16 p, std::map< v3s16, MapBlock * > &modified_blocks)
Definition map.cpp:222
MapNode getNode(v3s16 p, bool *is_valid_position=NULL)
Definition map.cpp:125
IGameDef * m_gamedef
Definition map.h:293
virtual bool maySaveBlocks()
Definition map.h:186
virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
Definition map.h:307
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition map.cpp:73
std::unordered_map< v2s16, MapSector * > m_sectors
Definition map.h:297
virtual MapSector * emergeSector(v2s16 p)
Definition map.h:137
bool isValidPosition(v3s16 p)
Definition map.cpp:117
virtual void beginSave()
Definition map.h:178
bool setNodeMetadata(v3s16 p, NodeMetadata *meta)
Sets metadata for a node.
Definition map.cpp:516
MapBlock * getBlockNoCreateNoEx(v3s16 p)
Definition map.cpp:99
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:556
Definition nodemetadata.h:39
Definition nodetimer.h:36
Definition voxel.h:59
void addPoint(const v3s16 &p)
Definition voxel.h:98
Definition voxel.h:381
virtual void clear()
Definition voxel.cpp:42
#define MAP_BLOCKSIZE
Definition constants.h:79
#define FATAL_ERROR(msg)
Definition debug.h:48
core::vector2d< s16 > v2s16
Definition irr_v2d.h:27
core::vector3d< s16 > v3s16
Definition irr_v3d.h:28
MapEditEventType
Definition map.h:45
@ MEET_ADDNODE
Definition map.h:47
@ MEET_OTHER
Definition map.h:55
@ MEET_REMOVENODE
Definition map.h:49
@ MEET_BLOCK_NODE_METADATA_CHANGED
Definition map.h:53
@ MEET_SWAPNODE
Definition map.h:51
v3s16 getNodeBlockPos(v3s16 p)
Definition mapblock.h:596
#define CONTENT_IGNORE
Definition mapnode.h:73
#define CONTENT_AIR
Definition mapnode.h:61
ModifiedState
Definition modifiedstate.h:25
#define rangelim(d, min, max)
Definition numeric.h:32
Definition map.h:59
std::vector< v3s16 > modified_blocks
Definition map.h:63
MapEditEventType type
Definition map.h:60
void setPositionModified(v3s16 pos)
Definition map.h:69
MapNode n
Definition map.h:62
v3s16 p
Definition map.h:61
bool is_private_change
Definition map.h:64
VoxelArea getArea() const
Definition map.h:84
MapEditEvent()=default
void setModifiedBlocks(const std::map< v3s16, MapBlock * > &blocks)
Definition map.h:76
Definition mapnode.h:139
static std::string p(std::string path)
Definition test_filesys.cpp:66