Luanti 5.11.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 <iostream>
8#include <set>
9#include <map>
10
12#include "mapblock.h"
13#include "mapnode.h"
14#include "constants.h"
15#include "voxel.h"
16#include "modifiedstate.h"
17#include "util/numeric.h"
18#include "nodetimer.h"
19#include "debug.h"
20
21class MapSector;
22class NodeMetadata;
23class 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
51 MapEditEvent() = default;
52
53 // Sets the event's position and marks the block as modified.
55 {
56 assert(modified_blocks.empty()); // only meant for initialization (once)
57 p = pos;
58 modified_blocks.push_back(getNodeBlockPos(pos));
59 }
60
61 void setModifiedBlocks(const std::map<v3s16, MapBlock *>& blocks)
62 {
63 assert(modified_blocks.empty()); // only meant for initialization (once)
64 modified_blocks.reserve(blocks.size());
65 for (const auto &block : blocks)
66 modified_blocks.push_back(block.first);
67 }
68
70 {
71 switch(type){
72 case MEET_ADDNODE:
73 case MEET_REMOVENODE:
74 case MEET_SWAPNODE:
76 return VoxelArea(p);
77 case MEET_OTHER:
78 {
79 VoxelArea a;
80 for (v3s16 p : modified_blocks) {
81 v3s16 np1 = p*MAP_BLOCKSIZE;
82 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
83 a.addPoint(np1);
84 a.addPoint(np2);
85 }
86 return a;
87 }
88 }
89 return VoxelArea();
90 }
91};
92
94{
95public:
96 // event shall be deleted by caller after the call.
97 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
98};
99
100class Map /*: public NodeContainer*/
101{
102public:
103
104 Map(IGameDef *gamedef);
105 virtual ~Map();
107
108 void addEventReceiver(MapEventReceiver *event_receiver);
109 void removeEventReceiver(MapEventReceiver *event_receiver);
110 // event shall be deleted by caller after the call.
111 void dispatchEvent(const MapEditEvent &event);
112
113 // On failure returns NULL
115 // Same as the above (there exists no lock anymore)
117
118 /*
119 This is overloaded by ClientMap and ServerMap to allow
120 their differing fetch methods.
121 */
122 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
123
124 // Returns InvalidPositionException if not found
126 // Returns NULL if not found
128
129 /* Server overrides */
130 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
131 { return getBlockNoCreateNoEx(p); }
132
133 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
134
135 bool isValidPosition(v3s16 p);
136
137 // throws InvalidPositionException if not found
138 void setNode(v3s16 p, MapNode n);
139
140 // Returns a CONTENT_IGNORE node if not found
141 // If is_valid_position is not NULL then this will be set to true if the
142 // position is valid, otherwise false
143 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
144
145 /*
146 These handle lighting but not faces.
147 */
148 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
149 std::map<v3s16, MapBlock*> &modified_blocks,
150 bool remove_metadata = true);
152 std::map<v3s16, MapBlock*> &modified_blocks);
153
154 /*
155 Wrappers for the latter ones.
156 These emit events.
157 Return true if succeeded, false if not.
158 */
159 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
161
162 // Call these before and after saving of many blocks
163 virtual void beginSave() {}
164 virtual void endSave() {}
165
166 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
167
168 /*
169 Return true unless the map definitely cannot save blocks.
170 */
171 virtual bool maySaveBlocks() { return true; }
172
173 // Server implements these.
174 // Client leaves them as no-op.
175 virtual bool saveBlock(MapBlock *block) { return false; }
176 virtual bool deleteBlock(v3s16 blockpos) { return false; }
177
178 /*
179 Updates usage timers and unloads unused blocks and sectors.
180 Saves modified blocks before unloading if possible.
181 */
182 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
183 std::vector<v3s16> *unloaded_blocks=NULL);
184
185 /*
186 Unloads all blocks with a zero refCount().
187 Saves modified blocks before unloading if possible.
188 */
189 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
190
191 // Deletes sectors and their blocks from memory
192 // Takes cache into account
193 // If deleted sector is in sector cache, clears cache
194 void deleteSectors(std::vector<v2s16> &list);
195
196 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
197 virtual void PrintInfo(std::ostream &out);
198
199 /*
200 Node metadata
201 These are basically coordinate wrappers to MapBlock
202 */
203
204 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
206
221 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
223
224 /*
225 Node Timers
226 These are basically coordinate wrappers to MapBlock
227 */
228
230 void setNodeTimer(const NodeTimer &t);
231 void removeNodeTimer(v3s16 p);
232
233 /*
234 Utilities
235 */
236
237 // Iterates through all nodes in the area in an unspecified order.
238 // The given callback takes the position as its first argument and the node
239 // as its second. If it returns false, forEachNodeInArea returns early.
240 template<typename F>
241 void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
242 {
243 v3s16 bpmin = getNodeBlockPos(minp);
244 v3s16 bpmax = getNodeBlockPos(maxp);
245 for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
246 for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
247 for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
248 // y is iterated innermost to make use of the sector cache.
249 v3s16 bp(bx, by, bz);
250 MapBlock *block = getBlockNoCreateNoEx(bp);
251 v3s16 basep = bp * MAP_BLOCKSIZE;
252 s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
253 s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
254 s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
255 s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
256 s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
257 s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
258 for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
259 for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
260 for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
261 v3s16 p = basep + v3s16(x_block, y_block, z_block);
262 MapNode n = block ?
263 block->getNodeNoCheck(x_block, y_block, z_block) :
265 if (!func(p, n))
266 return;
267 }
268 }
269 }
270
271 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
272 {
273 return isBlockOccluded(block->getPosRelative(), cam_pos_nodes, false);
274 }
275 bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool simple_check = false);
276
277protected:
279
280 std::set<MapEventReceiver*> m_event_receivers;
281
282 std::unordered_map<v2s16, MapSector*> m_sectors;
283
284 // Be sure to set this to NULL when the cached sector is deleted
287
288 // This stores the properties of the nodes on the map.
290
291 // Can be implemented by child class
292 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
293
295 const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
296 bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
297 float step, float stepfac, float start_offset, float end_offset,
298 u32 needed_count);
299};
300
301#define VMANIP_BLOCK_DATA_INEXIST 1
302#define VMANIP_BLOCK_CONTAINS_CIGNORE 2
303
305{
306public:
307 MMVManip(Map *map);
308 virtual ~MMVManip() = default;
309
310 virtual void clear()
311 {
313 m_loaded_blocks.clear();
314 }
315
316 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
317 bool load_if_inexistent = true);
318
319 // This is much faster with big chunks of generated data
320 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
321 bool overwrite_generated = true) const;
322
323 /*
324 Creates a copy of this VManip including contents, the copy will not be
325 associated with a Map.
326 */
327 MMVManip *clone() const;
328
329 // Reassociates a copied VManip to a map
330 void reparent(Map *map);
331
332 // Is it impossible to call initialEmerge / blitBackAll?
333 inline bool isOrphan() const { return !m_map; }
334
335 bool m_is_dirty = false;
336
337protected:
339
340 // may be null
341 Map *m_map = nullptr;
342 /*
343 key = blockpos
344 value = flags describing the block
345 */
346 std::map<v3s16, u8> m_loaded_blocks;
347};
Definition gamedef.h:36
Definition rollback_interface.h:97
Definition map.h:305
Map * m_map
Definition map.h:341
virtual ~MMVManip()=default
MMVManip()
Definition map.h:338
std::map< v3s16, u8 > m_loaded_blocks
Definition map.h:346
MMVManip * clone() const
Definition map.cpp:854
virtual void clear()
Definition map.h:310
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition map.cpp:755
bool isOrphan() const
Definition map.h:333
void reparent(Map *map)
Definition map.cpp:877
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true) const
Definition map.cpp:827
bool m_is_dirty
Definition map.h:335
Definition mapblock.h:58
MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
Definition mapblock.h:271
v3s16 getPosRelative()
Definition mapblock.h:199
Definition map.h:94
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition mapsector.h:25
Definition map.h:101
void removeNodeTimer(v3s16 p)
Definition map.cpp:574
std::set< MapEventReceiver * > m_event_receivers
Definition map.h:280
void unloadUnreferencedBlocks(std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:418
bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata=true)
Definition map.cpp:213
void setNodeTimer(const NodeTimer &t)
Definition map.cpp:554
bool removeNodeWithEvent(v3s16 p)
Definition map.cpp:236
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition map.h:164
void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, std::vector< v3s16 > *unloaded_blocks=NULL)
Definition map.cpp:276
void addEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:41
virtual bool saveBlock(MapBlock *block)
Definition map.h:175
Map(IGameDef *gamedef)
Definition map.cpp:25
MapBlock * getBlockNoCreate(v3s16 p)
Definition map.cpp:94
void setNode(v3s16 p, MapNode n)
Definition map.cpp:144
bool isOccluded(v3s16 pos_camera, v3s16 pos_target, float step, float stepfac, float start_offset, float end_offset, u32 needed_count)
Definition map.cpp:652
const NodeDefManager * m_nodedef
Definition map.h:289
virtual void addNodeAndUpdate(v3s16 p, MapNode n, std::map< v3s16, MapBlock * > &modified_blocks, bool remove_metadata=true)
Definition map.cpp:152
void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
Definition map.h:241
const NodeDefManager * getNodeDefManager()
Definition map.h:133
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition map.cpp:441
virtual bool deleteBlock(v3s16 blockpos)
Definition map.h:176
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition map.cpp:79
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition map.h:271
NodeTimer getNodeTimer(v3s16 p)
Definition map.cpp:534
NodeMetadata * getNodeMetadata(v3s16 p)
Definition map.cpp:482
void removeNodeMetadata(v3s16 p)
Definition map.cpp:520
MapSector * m_sector_cache
Definition map.h:285
virtual void save(ModifiedState save_level)
Definition map.h:166
void deleteSectors(std::vector< v2s16 > &list)
Definition map.cpp:423
bool determineAdditionalOcclusionCheck(v3s16 pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &to_check)
Definition map.cpp:588
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition map.h:130
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition map.cpp:46
v2s16 m_sector_cache_p
Definition map.h:286
virtual ~Map()
Definition map.cpp:31
virtual void PrintInfo(std::ostream &out)
Definition map.cpp:436
void dispatchEvent(const MapEditEvent &event)
Definition map.cpp:51
void removeNodeAndUpdate(v3s16 p, std::map< v3s16, MapBlock * > &modified_blocks)
Definition map.cpp:207
MapNode getNode(v3s16 p, bool *is_valid_position=NULL)
Definition map.cpp:110
IGameDef * m_gamedef
Definition map.h:278
virtual bool maySaveBlocks()
Definition map.h:171
virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
Definition map.h:292
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition map.cpp:58
std::unordered_map< v2s16, MapSector * > m_sectors
Definition map.h:282
virtual MapSector * emergeSector(v2s16 p)
Definition map.h:122
bool isValidPosition(v3s16 p)
Definition map.cpp:102
virtual void beginSave()
Definition map.h:163
bool setNodeMetadata(v3s16 p, NodeMetadata *meta)
Sets metadata for a node.
Definition map.cpp:501
MapBlock * getBlockNoCreateNoEx(v3s16 p)
Definition map.cpp:84
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:540
Definition nodemetadata.h:24
Definition nodetimer.h:21
Definition voxel.h:44
void addPoint(const v3s16 &p)
Definition voxel.h:83
Definition voxel.h:360
virtual void clear()
Definition voxel.cpp:25
#define MAP_BLOCKSIZE
Definition constants.h:64
#define FATAL_ERROR(msg)
Definition debug.h:33
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:581
#define CONTENT_IGNORE
Definition mapnode.h:58
#define CONTENT_AIR
Definition mapnode.h:46
ModifiedState
Definition modifiedstate.h:10
#define rangelim(d, min, max)
Definition numeric.h:18
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:54
MapNode n
Definition map.h:47
v3s16 p
Definition map.h:46
bool is_private_change
Definition map.h:49
VoxelArea getArea() const
Definition map.h:69
MapEditEvent()=default
void setModifiedBlocks(const std::map< v3s16, MapBlock * > &blocks)
Definition map.h:61
Definition mapnode.h:124
static std::string p(std::string path)
Definition test_filesys.cpp:53