Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <map>
8#include <ostream>
9#include <set>
10#include <unordered_map>
11
13#include "mapblock.h" // for forEachNodeInArea
14#include "mapnode.h"
15#include "constants.h"
16#include "voxel.h"
17#include "modifiedstate.h"
18#include "util/numeric.h" // for forEachNodeInArea
19
20class MapSector;
21class NodeMetadata;
22class NodeTimer;
23class IGameDef;
24
25/*
26 MapEditEvent
27*/
28
30 // Node added (changed from air or something else to something)
32 // Node removed (changed to air)
34 // Node swapped (changed without metadata change)
36 // Node metadata changed
38 // Anything else (modified_blocks are set unsent)
40};
41
43{
47 std::vector<v3s16> modified_blocks; // Represents a set
48 bool is_private_change = false;
49
50 MapEditEvent() = default;
51
52 // Sets the event's position and marks the block as modified.
54 {
55 assert(modified_blocks.empty()); // only meant for initialization (once)
56 p = pos;
57 modified_blocks.push_back(getNodeBlockPos(pos));
58 }
59
60 void setModifiedBlocks(const std::map<v3s16, MapBlock *>& blocks)
61 {
62 assert(modified_blocks.empty()); // only meant for initialization (once)
63 modified_blocks.reserve(blocks.size());
64 for (const auto &block : blocks)
65 modified_blocks.push_back(block.first);
66 }
67
69 {
70 switch(type){
71 case MEET_ADDNODE:
72 case MEET_REMOVENODE:
73 case MEET_SWAPNODE:
75 return VoxelArea(p);
76 case MEET_OTHER:
77 {
78 VoxelArea a;
79 for (v3s16 p : modified_blocks) {
80 v3s16 np1 = p*MAP_BLOCKSIZE;
81 v3s16 np2 = np1 + v3s16(MAP_BLOCKSIZE-1);
82 a.addPoint(np1);
83 a.addPoint(np2);
84 }
85 return a;
86 }
87 }
88 return VoxelArea();
89 }
90};
91
93{
94public:
95 // event shall be deleted by caller after the call.
96 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
97};
98
99class Map /*: public NodeContainer*/
100{
101public:
102
103 Map(IGameDef *gamedef);
104 virtual ~Map();
106
107 void addEventReceiver(MapEventReceiver *event_receiver);
108 void removeEventReceiver(MapEventReceiver *event_receiver);
109 // event shall be deleted by caller after the call.
110 void dispatchEvent(const MapEditEvent &event);
111
112 // On failure returns NULL
114 // Same as the above (there exists no lock anymore)
116
117 /*
118 This is overloaded by ClientMap and ServerMap to allow
119 their differing fetch methods.
120 */
121 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
122
123 // Returns InvalidPositionException if not found
125 // Returns NULL if not found
127
128 /* Server overrides */
129 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
130 { return getBlockNoCreateNoEx(p); }
131
132 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
133
134 bool isValidPosition(v3s16 p);
135
136 // throws InvalidPositionException if not found
137 void setNode(v3s16 p, MapNode n);
138
139 // Returns a CONTENT_IGNORE node if not found
140 // If is_valid_position is not NULL then this will be set to true if the
141 // position is valid, otherwise false
142 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
143
144 /*
145 These handle lighting but not faces.
146 */
147 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
148 std::map<v3s16, MapBlock*> &modified_blocks,
149 bool remove_metadata = true);
151 std::map<v3s16, MapBlock*> &modified_blocks);
152
153 /*
154 Wrappers for the latter ones.
155 These emit events.
156 Return true if succeeded, false if not.
157 */
158 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
160
161 // Call these before and after saving of many blocks
162 virtual void beginSave() {}
163 virtual void endSave() {}
164
165 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
166
167 /*
168 Return true unless the map definitely cannot save blocks.
169 */
170 virtual bool maySaveBlocks() { return true; }
171
172 // Server implements these.
173 // Client leaves them as no-op.
174 virtual bool saveBlock(MapBlock *block) { return false; }
175 virtual bool deleteBlock(v3s16 blockpos) { return false; }
176
177 /*
178 Updates usage timers and unloads unused blocks and sectors.
179 Saves modified blocks before unloading if possible.
180 */
181 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
182 std::vector<v3s16> *unloaded_blocks=NULL);
183
184 /*
185 Unloads all blocks with a zero refCount().
186 Saves modified blocks before unloading if possible.
187 */
188 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
189
190 // Deletes sectors and their blocks from memory
191 // Takes cache into account
192 // If deleted sector is in sector cache, clears cache
193 void deleteSectors(const std::vector<v2s16> &list);
194
195 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
196 virtual void PrintInfo(std::ostream &out);
197
198 /*
199 Node metadata
200 These are basically coordinate wrappers to MapBlock
201 */
202
203 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
205
220 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
222
223 /*
224 Node Timers
225 These are basically coordinate wrappers to MapBlock
226 */
227
229 void setNodeTimer(const NodeTimer &t);
230 void removeNodeTimer(v3s16 p);
231
232 /*
233 Utilities
234 */
235
236 // Iterates through all nodes in the area in an unspecified order.
237 // The given callback takes the position as its first argument and the node
238 // as its second. If it returns false, forEachNodeInArea returns early.
239 template<typename F>
240 void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
241 {
242 v3s16 bpmin = getNodeBlockPos(minp);
243 v3s16 bpmax = getNodeBlockPos(maxp);
244 for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
245 for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
246 for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
247 // y is iterated innermost to make use of the sector cache.
248 v3s16 bp(bx, by, bz);
249 MapBlock *block = getBlockNoCreateNoEx(bp);
250 v3s16 basep = bp * MAP_BLOCKSIZE;
251 s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
252 s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
253 s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
254 s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
255 s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
256 s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
257 for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
258 for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
259 for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
260 v3s16 p = basep + v3s16(x_block, y_block, z_block);
261 MapNode n = block ?
262 block->getNodeNoCheck(x_block, y_block, z_block) :
264 if (!func(p, n))
265 return;
266 }
267 }
268 }
269
270 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
271 {
272 return isBlockOccluded(block->getPosRelative(), cam_pos_nodes, false);
273 }
274 bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool simple_check = false);
275
276protected:
278
279 std::set<MapEventReceiver*> m_event_receivers;
280
281 std::unordered_map<v2s16, MapSector*> m_sectors;
282
283 // Be sure to set this to NULL when the cached sector is deleted
286
287 // This stores the properties of the nodes on the map.
289
290 // Can be implemented by child class
291 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
292
294 const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
295 bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
296 float step, float stepfac, float start_offset, float end_offset,
297 u32 needed_count);
298};
299
301{
302public:
303 MMVManip(Map *map);
304 virtual ~MMVManip() = default;
305
306 /*
307 Loads specified area from map and *adds* it to the area already
308 contained in the VManip.
309 */
310 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
311 bool load_if_inexistent = true);
312
319 std::map<v3s16, bool> getCoveredBlocks() const;
320
329 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
330 bool overwrite_generated = true) const;
331
332 /*
333 Creates a copy of this VManip including contents, the copy will not be
334 associated with a Map.
335 */
336 MMVManip *clone() const;
337
338 // Reassociates a copied VManip to a map
339 void reparent(Map *map);
340
341 // Is it impossible to call initialEmerge / blitBackAll?
342 inline bool isOrphan() const { return !m_map; }
343
344 bool m_is_dirty = false;
345
346protected:
348
349 // may be null
350 Map *m_map = nullptr;
351};
Definition gamedef.h:26
Definition map.h:301
Map * m_map
Definition map.h:350
std::map< v3s16, bool > getCoveredBlocks() const
Uses the flags array to determine which blocks the VManip covers, and for which of them we have any d...
Definition map.cpp:820
virtual ~MMVManip()=default
MMVManip()
Definition map.h:347
MMVManip * clone() const
Definition map.cpp:899
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition map.cpp:767
bool isOrphan() const
Definition map.h:342
void reparent(Map *map)
Definition map.cpp:918
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true) const
Writes data in VManip back to the map.
Definition map.cpp:856
bool m_is_dirty
Definition map.h:344
Definition mapblock.h:58
MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
Definition mapblock.h:262
v3s16 getPosRelative()
Definition mapblock.h:189
Definition map.h:93
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition mapsector.h:23
Definition map.h:100
void removeNodeTimer(v3s16 p)
Definition map.cpp:586
std::set< MapEventReceiver * > m_event_receivers
Definition map.h:279
void unloadUnreferencedBlocks(std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:430
bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata=true)
Definition map.cpp:223
void setNodeTimer(const NodeTimer &t)
Definition map.cpp:566
bool removeNodeWithEvent(v3s16 p)
Definition map.cpp:246
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition map.h:163
void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:286
void addEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:51
virtual bool saveBlock(MapBlock *block)
Definition map.h:174
Map(IGameDef *gamedef)
Definition map.cpp:24
MapBlock * getBlockNoCreate(v3s16 p)
Definition map.cpp:104
void setNode(v3s16 p, MapNode n)
Definition map.cpp:154
bool isOccluded(v3s16 pos_camera, v3s16 pos_target, float step, float stepfac, float start_offset, float end_offset, u32 needed_count)
Definition map.cpp:664
const NodeDefManager * m_nodedef
Definition map.h:288
virtual void addNodeAndUpdate(v3s16 p, MapNode n, std::map< v3s16, MapBlock * > &modified_blocks, bool remove_metadata=true)
Definition map.cpp:162
void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
Definition map.h:240
const NodeDefManager * getNodeDefManager()
Definition map.h:132
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition map.cpp:453
virtual bool deleteBlock(v3s16 blockpos)
Definition map.h:175
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition map.cpp:89
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition map.h:270
NodeTimer getNodeTimer(v3s16 p)
Definition map.cpp:546
NodeMetadata * getNodeMetadata(v3s16 p)
Definition map.cpp:494
void removeNodeMetadata(v3s16 p)
Definition map.cpp:532
MapSector * m_sector_cache
Definition map.h:284
virtual void save(ModifiedState save_level)
Definition map.h:165
bool determineAdditionalOcclusionCheck(v3s16 pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &to_check)
Definition map.cpp:600
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition map.h:129
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:56
v2s16 m_sector_cache_p
Definition map.h:285
virtual ~Map()
Definition map.cpp:30
virtual void PrintInfo(std::ostream &out)
Definition map.cpp:448
void dispatchEvent(const MapEditEvent &event)
Definition map.cpp:61
void removeNodeAndUpdate(v3s16 p, std::map< v3s16, MapBlock * > &modified_blocks)
Definition map.cpp:217
MapNode getNode(v3s16 p, bool *is_valid_position=NULL)
Definition map.cpp:120
void deleteSectors(const std::vector< v2s16 > &list)
Definition map.cpp:435
IGameDef * m_gamedef
Definition map.h:277
virtual bool maySaveBlocks()
Definition map.h:170
virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
Definition map.h:291
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition map.cpp:68
std::unordered_map< v2s16, MapSector * > m_sectors
Definition map.h:281
virtual MapSector * emergeSector(v2s16 p)
Definition map.h:121
bool isValidPosition(v3s16 p)
Definition map.cpp:112
virtual void beginSave()
Definition map.h:162
bool setNodeMetadata(v3s16 p, NodeMetadata *meta)
Sets metadata for a node.
Definition map.cpp:513
MapBlock * getBlockNoCreateNoEx(v3s16 p)
Definition map.cpp:94
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:509
Definition nodemetadata.h:25
Definition nodetimer.h:21
Definition voxel.h:41
void addPoint(const v3s16 &p)
Definition voxel.h:80
Definition voxel.h:365
#define MAP_BLOCKSIZE
Definition constants.h:64
#define FATAL_ERROR(msg)
Definition debug.h:32
core::vector2d< s16 > v2s16
Definition irr_v2d.h:12
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
MapEditEventType
Definition map.h:29
@ MEET_ADDNODE
Definition map.h:31
@ MEET_OTHER
Definition map.h:39
@ MEET_REMOVENODE
Definition map.h:33
@ MEET_BLOCK_NODE_METADATA_CHANGED
Definition map.h:37
@ MEET_SWAPNODE
Definition map.h:35
v3s16 getNodeBlockPos(v3s16 p)
Definition mapblock.h:592
#define CONTENT_IGNORE
Definition mapnode.h:57
#define CONTENT_AIR
Definition mapnode.h:45
ModifiedState
Definition modifiedstate.h:10
constexpr T rangelim(const T &d, const T2 &min, const T3 &max)
Definition numeric.h:20
Definition map.h:43
std::vector< v3s16 > modified_blocks
Definition map.h:47
MapEditEventType type
Definition map.h:44
void setPositionModified(v3s16 pos)
Definition map.h:53
MapNode n
Definition map.h:46
v3s16 p
Definition map.h:45
bool is_private_change
Definition map.h:48
VoxelArea getArea() const
Definition map.h:68
MapEditEvent()=default
void setModifiedBlocks(const std::map< v3s16, MapBlock * > &blocks)
Definition map.h:60
Definition mapnode.h:123
static std::string p(std::string path)
Definition test_filesys.cpp:64