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