Luanti 5.16.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 // Setting low_priority to true allows the server
50 // to send this change to clients with some delay.
51 bool low_priority = false;
52
53 MapEditEvent() = default;
54
55 // Sets the event's position and marks the block as modified.
57 {
58 assert(modified_blocks.empty()); // only meant for initialization (once)
59 p = pos;
60 modified_blocks.push_back(getNodeBlockPos(pos));
61 }
62
63 void setModifiedBlocks(const std::map<v3s16, MapBlock *>& blocks)
64 {
65 assert(modified_blocks.empty()); // only meant for initialization (once)
66 modified_blocks.reserve(blocks.size());
67 for (const auto &block : blocks)
68 modified_blocks.push_back(block.first);
69 }
70
72 {
73 switch(type){
74 case MEET_ADDNODE:
75 case MEET_REMOVENODE:
76 case MEET_SWAPNODE:
78 return VoxelArea(p);
79 case MEET_OTHER:
80 {
81 VoxelArea a;
82 for (v3s16 p : modified_blocks) {
83 v3s16 np1 = p*MAP_BLOCKSIZE;
84 v3s16 np2 = np1 + v3s16(MAP_BLOCKSIZE-1);
85 a.addPoint(np1);
86 a.addPoint(np2);
87 }
88 return a;
89 }
90 }
91 return VoxelArea();
92 }
93};
94
96{
97public:
98 // event shall be deleted by caller after the call.
99 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
100};
101
102class Map /*: public NodeContainer*/
103{
104public:
105
106 Map(IGameDef *gamedef);
107 virtual ~Map();
109
110 void addEventReceiver(MapEventReceiver *event_receiver);
111 void removeEventReceiver(MapEventReceiver *event_receiver);
112 // event shall be deleted by caller after the call.
113 void dispatchEvent(const MapEditEvent &event);
114
115 // On failure returns NULL
117 // Same as the above (there exists no lock anymore)
119
120 /*
121 This is overloaded by ClientMap and ServerMap to allow
122 their differing fetch methods.
123 */
124 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
125
126 // Returns InvalidPositionException if not found
128 // Returns NULL if not found
130
131 /* Server overrides */
132 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
133 { return getBlockNoCreateNoEx(p); }
134
135 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
136
137 bool isValidPosition(v3s16 p);
138
139 // throws InvalidPositionException if not found
140 void setNode(v3s16 p, MapNode n);
141
142 // Returns a CONTENT_IGNORE node if not found
143 // If is_valid_position is not NULL then this will be set to true if the
144 // position is valid, otherwise false
145 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
146
147 /*
148 These handle lighting but not faces.
149 */
150 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
151 std::map<v3s16, MapBlock*> &modified_blocks,
152 bool remove_metadata = true);
154 std::map<v3s16, MapBlock*> &modified_blocks);
155
156 /*
157 Wrappers for the latter ones.
158 These emit events.
159 Return true if succeeded, false if not.
160 */
161 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
163
164 // Call these before and after saving of many blocks
165 virtual void beginSave() {}
166 virtual void endSave() {}
167
168 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
169
170 /*
171 Return true unless the map definitely cannot save blocks.
172 */
173 virtual bool maySaveBlocks() { return true; }
174
175 // Server implements these.
176 // Client leaves them as no-op.
177 virtual bool saveBlock(MapBlock *block) { return false; }
178 virtual bool deleteBlock(v3s16 blockpos) { return false; }
179
180 /*
181 Updates usage timers and unloads unused blocks and sectors.
182 Saves modified blocks before unloading if possible.
183 */
184 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
185 std::vector<v3s16> *unloaded_blocks=NULL);
186
187 /*
188 Unloads all blocks with a zero refCount().
189 Saves modified blocks before unloading if possible.
190 */
191 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
192
193 // Deletes sectors and their blocks from memory
194 // Takes cache into account
195 // If deleted sector is in sector cache, clears cache
196 void deleteSectors(const std::vector<v2s16> &list);
197
198 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
199 virtual void PrintInfo(std::ostream &out);
200
201 /*
202 Node metadata
203 These are basically coordinate wrappers to MapBlock
204 */
205
206 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
208
223 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
225
226 /*
227 Node Timers
228 These are basically coordinate wrappers to MapBlock
229 */
230
232 void setNodeTimer(const NodeTimer &t);
233 void removeNodeTimer(v3s16 p);
234
235 /*
236 Utilities
237 */
238
239 // Iterates through all nodes in the area in an unspecified order.
240 // The given callback takes the position as its first argument and the node
241 // as its second. If it returns false, forEachNodeInArea returns early.
242 template<typename F>
243 void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
244 {
245 v3s16 bpmin = getNodeBlockPos(minp);
246 v3s16 bpmax = getNodeBlockPos(maxp);
247 for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
248 for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
249 for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
250 // y is iterated innermost to make use of the sector cache.
251 v3s16 bp(bx, by, bz);
252 MapBlock *block = getBlockNoCreateNoEx(bp);
253 v3s16 basep = bp * MAP_BLOCKSIZE;
254 s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
255 s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
256 s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
257 s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
258 s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
259 s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
260 for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
261 for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
262 for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
263 v3s16 p = basep + v3s16(x_block, y_block, z_block);
264 MapNode n = block ?
265 block->getNodeNoCheck(x_block, y_block, z_block) :
267 if (!func(p, n))
268 return;
269 }
270 }
271 }
272
273 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
274 {
275 return isBlockOccluded(block->getPosRelative(), cam_pos_nodes, false);
276 }
277 bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool simple_check = false);
278
279protected:
281
282 std::set<MapEventReceiver*> m_event_receivers;
283
284 std::unordered_map<v2s16, MapSector*> m_sectors;
285
286 // Be sure to set this to NULL when the cached sector is deleted
289
290 // This stores the properties of the nodes on the map.
292
293 // Can be implemented by child class
294 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
295
297 const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
298 bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
299 float step, float stepfac, float start_offset, float end_offset,
300 u32 needed_count);
301};
302
304{
305public:
306 MMVManip(Map *map);
307 virtual ~MMVManip() = default;
308
309 /*
310 Loads specified area from map and *adds* it to the area already
311 contained in the VManip.
312 */
313 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
314 bool load_if_inexistent = true);
315
322 std::map<v3s16, bool> getCoveredBlocks() const;
323
332 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
333 bool overwrite_generated = true) const;
334
335 /*
336 Creates a copy of this VManip including contents, the copy will not be
337 associated with a Map.
338 */
339 MMVManip *clone() const;
340
341 // Reassociates a copied VManip to a map
342 void reparent(Map *map);
343
344 // Is it impossible to call initialEmerge / blitBackAll?
345 inline bool isOrphan() const { return !m_map; }
346
347 bool m_is_dirty = false;
348
349protected:
351
352 // may be null
353 Map *m_map = nullptr;
354};
Definition gamedef.h:26
Map * m_map
Definition map.h:353
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:816
virtual ~MMVManip()=default
MMVManip()
Definition map.h:350
MMVManip * clone() const
Definition map.cpp:895
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition map.cpp:763
MMVManip(Map *map)
Definition map.cpp:756
bool isOrphan() const
Definition map.h:345
void reparent(Map *map)
Definition map.cpp:914
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true) const
Writes data in VManip back to the map.
Definition map.cpp:852
bool m_is_dirty
Definition map.h:347
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:96
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition mapsector.h:23
Definition map.h:103
void removeNodeTimer(v3s16 p)
Definition map.cpp:582
std::set< MapEventReceiver * > m_event_receivers
Definition map.h:282
void unloadUnreferencedBlocks(std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:426
bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata=true)
Definition map.cpp:223
void setNodeTimer(const NodeTimer &t)
Definition map.cpp:562
bool removeNodeWithEvent(v3s16 p)
Definition map.cpp:246
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition map.h:166
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:177
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:660
const NodeDefManager * m_nodedef
Definition map.h:291
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:243
const NodeDefManager * getNodeDefManager()
Definition map.h:135
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition map.cpp:449
virtual bool deleteBlock(v3s16 blockpos)
Definition map.h:178
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition map.cpp:89
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition map.h:273
NodeTimer getNodeTimer(v3s16 p)
Definition map.cpp:542
NodeMetadata * getNodeMetadata(v3s16 p)
Definition map.cpp:490
void removeNodeMetadata(v3s16 p)
Definition map.cpp:528
MapSector * m_sector_cache
Definition map.h:287
virtual void save(ModifiedState save_level)
Definition map.h:168
bool determineAdditionalOcclusionCheck(v3s16 pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &to_check)
Definition map.cpp:596
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition map.h:132
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:56
v2s16 m_sector_cache_p
Definition map.h:288
virtual ~Map()
Definition map.cpp:30
virtual void PrintInfo(std::ostream &out)
Definition map.cpp:444
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:431
IGameDef * m_gamedef
Definition map.h:280
virtual bool maySaveBlocks()
Definition map.h:173
virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
Definition map.h:294
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition map.cpp:68
std::unordered_map< v2s16, MapSector * > m_sectors
Definition map.h:284
virtual MapSector * emergeSector(v2s16 p)
Definition map.h:124
bool isValidPosition(v3s16 p)
Definition map.cpp:112
virtual void beginSave()
Definition map.h:165
bool setNodeMetadata(v3s16 p, NodeMetadata *meta)
Sets metadata for a node.
Definition map.cpp:509
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
VoxelManipulator()=default
#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:52
#define CONTENT_AIR
Definition mapnode.h:40
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:56
MapNode n
Definition map.h:46
v3s16 p
Definition map.h:45
bool low_priority
Definition map.h:51
bool is_private_change
Definition map.h:48
VoxelArea getArea() const
Definition map.h:71
MapEditEvent()=default
void setModifiedBlocks(const std::map< v3s16, MapBlock * > &blocks)
Definition map.h:63
Definition mapnode.h:115
static std::string p(std::string path)
Definition test_filesys.cpp:64