Luanti 5.18.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 <list>
8#include <map>
9#include <ostream>
10#include <set>
11#include <unordered_map>
12#include <memory>
13
15#include "mapblock.h" // for forEachNodeInArea
16#include "mapnode.h"
17#include "constants.h"
18#include "voxel.h"
19#include "modifiedstate.h"
20#include "util/numeric.h" // for forEachNodeInArea
21
22class MapSector;
23class NodeMetadata;
24class NodeTimer;
25class IGameDef;
26
27/*
28 MapEditEvent
29*/
30
32 // Node added (changed from air or something else to something)
34 // Node removed (changed to air)
36 // Node swapped (changed without metadata change)
38 // Node metadata changed
40 // Anything else (modified_blocks are set unsent)
42};
43
45{
49 std::vector<v3s16> modified_blocks; // Represents a set
50 bool is_private_change = false;
51 // Setting low_priority to true allows the server
52 // to send this change to clients with some delay.
53 bool low_priority = false;
54
55 MapEditEvent() = default;
56
57 // Sets the event's position and marks the block as modified.
59 {
60 assert(modified_blocks.empty()); // only meant for initialization (once)
61 p = pos;
62 modified_blocks.push_back(getNodeBlockPos(pos));
63 }
64
65 void setModifiedBlocks(const std::map<v3s16, MapBlock *>& blocks)
66 {
67 assert(modified_blocks.empty()); // only meant for initialization (once)
68 modified_blocks.reserve(blocks.size());
69 for (const auto &block : blocks)
70 modified_blocks.push_back(block.first);
71 }
72
74 {
75 switch(type){
76 case MEET_ADDNODE:
77 case MEET_REMOVENODE:
78 case MEET_SWAPNODE:
80 return VoxelArea(p);
81 case MEET_OTHER:
82 {
83 VoxelArea a;
84 for (v3s16 p : modified_blocks) {
85 v3s16 np1 = p*MAP_BLOCKSIZE;
86 v3s16 np2 = np1 + v3s16(MAP_BLOCKSIZE-1);
87 a.addPoint(np1);
88 a.addPoint(np2);
89 }
90 return a;
91 }
92 }
93 return VoxelArea();
94 }
95};
96
98{
99public:
100 // event shall be deleted by caller after the call.
101 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
102};
103
104class Map /*: public NodeContainer*/
105{
106public:
107
108 Map(IGameDef *gamedef);
109 virtual ~Map();
111
112 void addEventReceiver(MapEventReceiver *event_receiver);
113 void removeEventReceiver(MapEventReceiver *event_receiver);
114 // event shall be deleted by caller after the call.
115 void dispatchEvent(const MapEditEvent &event);
116
117 // On failure returns NULL
119 // Same as the above (there exists no lock anymore)
121
122 /*
123 This is overloaded by ClientMap and ServerMap to allow
124 their differing fetch methods.
125 */
126 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
127
128 // Returns InvalidPositionException if not found
130 // Returns NULL if not found
132
133 /* Server overrides */
134 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
135 { return getBlockNoCreateNoEx(p); }
136
137 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
138
139 bool isValidPosition(v3s16 p);
140
141 // throws InvalidPositionException if not found
142 void setNode(v3s16 p, MapNode n);
143
144 // Returns a CONTENT_IGNORE node if not found
145 // If is_valid_position is not NULL then this will be set to true if the
146 // position is valid, otherwise false
147 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
148
149 /*
150 These handle lighting but not faces.
151 */
152 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
153 std::map<v3s16, MapBlock*> &modified_blocks,
154 bool remove_metadata = true);
156 std::map<v3s16, MapBlock*> &modified_blocks);
157
158 /*
159 Wrappers for the latter ones.
160 These emit events.
161 Return true if succeeded, false if not.
162 */
163 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
165
166 // Call these before and after saving of many blocks
167 virtual void beginSave() {}
168 virtual void endSave() {}
169
170 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
171
172 /*
173 Return true unless the map definitely cannot save blocks.
174 */
175 virtual bool maySaveBlocks() { return true; }
176
177 // Server implements these.
178 // Client leaves them as no-op.
179 virtual bool saveBlock(MapBlock *block) { return false; }
180 virtual bool deleteBlock(v3s16 blockpos) { return false; }
181
182 /*
183 Updates usage timers and unloads unused blocks and sectors.
184 Saves modified blocks before unloading if possible.
185 */
186 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
187 std::vector<v3s16> *unloaded_blocks=NULL);
188
189 /*
190 Unloads all blocks with a zero refCount().
191 Saves modified blocks before unloading if possible.
192 */
193 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
194
195 // Deletes sectors and their blocks from memory
196 // Takes cache into account
197 // If deleted sector is in sector cache, clears cache
198 void deleteSectors(const std::vector<v2s16> &list);
199
200 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
201 virtual void PrintInfo(std::ostream &out);
202
203 /*
204 Node metadata
205 These are basically coordinate wrappers to MapBlock
206 */
207
208 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
210
225 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
227
228 /*
229 Node Timers
230 These are basically coordinate wrappers to MapBlock
231 */
232
234 void setNodeTimer(const NodeTimer &t);
235 void removeNodeTimer(v3s16 p);
236
237 /*
238 Utilities
239 */
240
241 // Iterates through all nodes in the area in an unspecified order.
242 // The given callback takes the position as its first argument and the node
243 // as its second. If it returns false, forEachNodeInArea returns early.
244 template<typename F>
245 void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
246 {
247 v3s16 bpmin = getNodeBlockPos(minp);
248 v3s16 bpmax = getNodeBlockPos(maxp);
249 for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
250 for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
251 for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
252 // y is iterated innermost to make use of the sector cache.
253 v3s16 bp(bx, by, bz);
254 MapBlock *block = getBlockNoCreateNoEx(bp);
255 v3s16 basep = bp * MAP_BLOCKSIZE;
256 s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
257 s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
258 s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
259 s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
260 s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
261 s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
262 for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
263 for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
264 for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
265 v3s16 p = basep + v3s16(x_block, y_block, z_block);
266 MapNode n = block ?
267 block->getNodeNoCheck(x_block, y_block, z_block) :
269 if (!func(p, n))
270 return;
271 }
272 }
273 }
274
275 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
276 {
277 return isBlockOccluded(block->getPosRelative(), cam_pos_nodes);
278 }
279 bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool dense = false);
280
281protected:
283
284 std::set<MapEventReceiver*> m_event_receivers;
285
286 std::unordered_map<v2s16, MapSector*> m_sectors;
287
288 // Be sure to set this to NULL when the cached sector is deleted
291
292 // Delayed deletion of old metadata objects
293 std::vector<std::unique_ptr<NodeMetadata>> m_metadata_trash;
294
295 // This stores the properties of the nodes on the map.
297
298 // Can be implemented by child class
299 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
300
302 const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
303 bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
304 float end_offset, u32 needed_count, bool dense);
305};
306
308{
309public:
310 MMVManip(Map *map);
311 ~MMVManip() override;
313
314 /*
315 Loads specified area from map and *adds* it to the area already
316 contained in the VManip.
317 */
318 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
319 bool load_if_inexistent = true);
320
327 std::map<v3s16, bool> getCoveredBlocks() const;
328
337 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
338 bool overwrite_generated = true) const;
339
340 /*
341 Creates a copy of this VManip including contents, the copy will not be
342 associated with a Map.
343 */
344 MMVManip *clone() const;
345
346 // Reassociates a copied VManip to a map
347 void reparent(Map *map);
348
349 // Is it impossible to call initialEmerge / blitBackAll?
350 inline bool isOrphan() const { return !m_map; }
351
352 std::list<MMVManip **>::iterator addTrackedRef(MMVManip **ref_ref);
353
354 void removeTrackedRef(std::list<MMVManip **>::iterator it);
355
356 bool m_is_dirty = false;
357
358protected:
360
361 // may be null
362 Map *m_map = nullptr;
363
364private:
365 // references to this that need to be cleared on destruction
366 std::list<MMVManip **> m_tracked_refs;
367};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition gamedef.h:26
Definition map.h:308
Map * m_map
Definition map.h:362
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:858
std::list< MMVManip ** >::iterator addTrackedRef(MMVManip **ref_ref)
Definition map.cpp:962
MMVManip()
Definition map.h:359
MMVManip * clone() const
Definition map.cpp:937
~MMVManip() override
Definition map.cpp:797
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition map.cpp:805
MMVManip(Map *map)
Definition map.cpp:790
bool isOrphan() const
Definition map.h:350
void removeTrackedRef(std::list< MMVManip ** >::iterator it)
Definition map.cpp:967
void reparent(Map *map)
Definition map.cpp:956
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true) const
Writes data in VManip back to the map.
Definition map.cpp:894
std::list< MMVManip ** > m_tracked_refs
Definition map.h:366
bool m_is_dirty
Definition map.h:356
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:98
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition mapsector.h:23
Definition map.h:105
void removeNodeTimer(v3s16 p)
Definition map.cpp:592
std::set< MapEventReceiver * > m_event_receivers
Definition map.h:284
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:572
std::vector< std::unique_ptr< NodeMetadata > > m_metadata_trash
Definition map.h:293
bool removeNodeWithEvent(v3s16 p)
Definition map.cpp:246
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition map.h:168
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:179
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
const NodeDefManager * m_nodedef
Definition map.h:296
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:245
const NodeDefManager * getNodeDefManager()
Definition map.h:137
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition map.cpp:453
virtual bool deleteBlock(v3s16 blockpos)
Definition map.h:180
bool isOccluded(v3s16 pos_camera, v3s16 pos_target, float end_offset, u32 needed_count, bool dense)
Definition map.cpp:670
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition map.cpp:89
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition map.h:275
NodeTimer getNodeTimer(v3s16 p)
Definition map.cpp:552
NodeMetadata * getNodeMetadata(v3s16 p)
Definition map.cpp:494
void removeNodeMetadata(v3s16 p)
Definition map.cpp:537
MapSector * m_sector_cache
Definition map.h:289
virtual void save(ModifiedState save_level)
Definition map.h:170
bool determineAdditionalOcclusionCheck(v3s16 pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &to_check)
Definition map.cpp:606
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition map.h:134
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:56
v2s16 m_sector_cache_p
Definition map.h:290
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:282
virtual bool maySaveBlocks()
Definition map.h:175
virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
Definition map.h:299
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition map.cpp:68
std::unordered_map< v2s16, MapSector * > m_sectors
Definition map.h:286
virtual MapSector * emergeSector(v2s16 p)
Definition map.h:126
bool isValidPosition(v3s16 p)
Definition map.cpp:112
virtual void beginSave()
Definition map.h:167
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:514
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:31
@ MEET_ADDNODE
Definition map.h:33
@ MEET_OTHER
Definition map.h:41
@ MEET_REMOVENODE
Definition map.h:35
@ MEET_BLOCK_NODE_METADATA_CHANGED
Definition map.h:39
@ MEET_SWAPNODE
Definition map.h:37
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:45
std::vector< v3s16 > modified_blocks
Definition map.h:49
MapEditEventType type
Definition map.h:46
void setPositionModified(v3s16 pos)
Definition map.h:58
MapNode n
Definition map.h:48
v3s16 p
Definition map.h:47
bool low_priority
Definition map.h:53
bool is_private_change
Definition map.h:50
VoxelArea getArea() const
Definition map.h:73
MapEditEvent()=default
void setModifiedBlocks(const std::map< v3s16, MapBlock * > &blocks)
Definition map.h:65
Definition mapnode.h:115
static std::string p(std::string path)
Definition test_filesys.cpp:69