Minetest  5.4.0
map.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include <iostream>
23 #include <sstream>
24 #include <set>
25 #include <map>
26 #include <list>
27 
28 #include "irrlichttypes_bloated.h"
29 #include "mapnode.h"
30 #include "constants.h"
31 #include "voxel.h"
32 #include "modifiedstate.h"
33 #include "util/container.h"
34 #include "util/metricsbackend.h"
35 #include "nodetimer.h"
36 #include "map_settings_manager.h"
37 #include "debug.h"
38 
39 class Settings;
40 class MapDatabase;
41 class ClientMap;
42 class MapSector;
43 class ServerMapSector;
44 class MapBlock;
45 class NodeMetadata;
46 class IGameDef;
47 class IRollbackManager;
48 class EmergeManager;
49 class MetricsBackend;
50 class ServerEnvironment;
51 struct BlockMakeData;
52 
53 /*
54  MapEditEvent
55 */
56 
57 #define MAPTYPE_BASE 0
58 #define MAPTYPE_SERVER 1
59 #define MAPTYPE_CLIENT 2
60 
62  // Node added (changed from air or something else to something)
64  // Node removed (changed to air)
66  // Node swapped (changed without metadata change)
68  // Node metadata changed
70  // Anything else (modified_blocks are set unsent)
72 };
73 
75 {
79  std::set<v3s16> modified_blocks;
80  bool is_private_change = false;
81 
82  MapEditEvent() = default;
83 
85  {
86  switch(type){
87  case MEET_ADDNODE:
88  return VoxelArea(p);
89  case MEET_REMOVENODE:
90  return VoxelArea(p);
91  case MEET_SWAPNODE:
92  return VoxelArea(p);
94  {
95  v3s16 np1 = p*MAP_BLOCKSIZE;
96  v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
97  return VoxelArea(np1, np2);
98  }
99  case MEET_OTHER:
100  {
101  VoxelArea a;
102  for (v3s16 p : modified_blocks) {
103  v3s16 np1 = p*MAP_BLOCKSIZE;
104  v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
105  a.addPoint(np1);
106  a.addPoint(np2);
107  }
108  return a;
109  }
110  }
111  return VoxelArea();
112  }
113 };
114 
116 {
117 public:
118  // event shall be deleted by caller after the call.
119  virtual void onMapEditEvent(const MapEditEvent &event) = 0;
120 };
121 
122 class Map /*: public NodeContainer*/
123 {
124 public:
125 
126  Map(IGameDef *gamedef);
127  virtual ~Map();
129 
130  virtual s32 mapType() const
131  {
132  return MAPTYPE_BASE;
133  }
134 
135  /*
136  Drop (client) or delete (server) the map.
137  */
138  virtual void drop()
139  {
140  delete this;
141  }
142 
143  void addEventReceiver(MapEventReceiver *event_receiver);
144  void removeEventReceiver(MapEventReceiver *event_receiver);
145  // event shall be deleted by caller after the call.
146  void dispatchEvent(const MapEditEvent &event);
147 
148  // On failure returns NULL
150  // Same as the above (there exists no lock anymore)
152 
153  /*
154  This is overloaded by ClientMap and ServerMap to allow
155  their differing fetch methods.
156  */
157  virtual MapSector * emergeSector(v2s16 p){ return NULL; }
158 
159  // Returns InvalidPositionException if not found
161  // Returns NULL if not found
163 
164  /* Server overrides */
165  virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
166  { return getBlockNoCreateNoEx(p); }
167 
168  inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
169 
170  // Returns InvalidPositionException if not found
171  bool isNodeUnderground(v3s16 p);
172 
173  bool isValidPosition(v3s16 p);
174 
175  // throws InvalidPositionException if not found
176  void setNode(v3s16 p, MapNode & n);
177 
178  // Returns a CONTENT_IGNORE node if not found
179  // If is_valid_position is not NULL then this will be set to true if the
180  // position is valid, otherwise false
181  MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
182 
183  /*
184  These handle lighting but not faces.
185  */
187  std::map<v3s16, MapBlock*> &modified_blocks,
188  bool remove_metadata = true);
190  std::map<v3s16, MapBlock*> &modified_blocks);
191 
192  /*
193  Wrappers for the latter ones.
194  These emit events.
195  Return true if succeeded, false if not.
196  */
197  bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
199 
200  // Call these before and after saving of many blocks
201  virtual void beginSave() {}
202  virtual void endSave() {}
203 
204  virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
205 
206  // Server implements these.
207  // Client leaves them as no-op.
208  virtual bool saveBlock(MapBlock *block) { return false; }
209  virtual bool deleteBlock(v3s16 blockpos) { return false; }
210 
211  /*
212  Updates usage timers and unloads unused blocks and sectors.
213  Saves modified blocks before unloading on MAPTYPE_SERVER.
214  */
215  void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
216  std::vector<v3s16> *unloaded_blocks=NULL);
217 
218  /*
219  Unloads all blocks with a zero refCount().
220  Saves modified blocks before unloading on MAPTYPE_SERVER.
221  */
222  void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
223 
224  // Deletes sectors and their blocks from memory
225  // Takes cache into account
226  // If deleted sector is in sector cache, clears cache
227  void deleteSectors(std::vector<v2s16> &list);
228 
229  // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
230  virtual void PrintInfo(std::ostream &out);
231 
232  void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
233  ServerEnvironment *env);
234 
235  /*
236  Node metadata
237  These are basically coordinate wrappers to MapBlock
238  */
239 
240  std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
242 
257  bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
258  void removeNodeMetadata(v3s16 p);
259 
260  /*
261  Node Timers
262  These are basically coordinate wrappers to MapBlock
263  */
264 
266  void setNodeTimer(const NodeTimer &t);
267  void removeNodeTimer(v3s16 p);
268 
269  /*
270  Variables
271  */
272 
274 
275  bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
276 protected:
277  friend class LuaVoxelManip;
278 
280 
281  std::set<MapEventReceiver*> m_event_receivers;
282 
283  std::map<v2s16, MapSector*> m_sectors;
284 
285  // Be sure to set this to NULL when the cached sector is deleted
288 
289  // Queued transforming water nodes
291 
292  // This stores the properties of the nodes on the map.
294 
295  bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
296  const core::aabbox3d<s16> &block_bounds, v3s16 &check);
297  bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target,
298  float step, float stepfac, float start_offset, float end_offset,
299  u32 needed_count);
300 
301 private:
304  u64 m_inc_trending_up_start_time = 0; // milliseconds
306 };
307 
308 /*
309  ServerMap
310 
311  This is the only map class that is able to generate map.
312 */
313 
314 class ServerMap : public Map
315 {
316 public:
317  /*
318  savedir: directory to which map data should be saved
319  */
320  ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
321  ~ServerMap();
322 
323  s32 mapType() const
324  {
325  return MAPTYPE_SERVER;
326  }
327 
328  /*
329  Get a sector from somewhere.
330  - Check memory
331  - Check disk (doesn't load blocks)
332  - Create blank one
333  */
335 
336  /*
337  Blocks are generated by using these and makeBlock().
338  */
340  bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
341  void finishBlockMake(BlockMakeData *data,
342  std::map<v3s16, MapBlock*> *changed_blocks);
343 
344  /*
345  Get a block from somewhere.
346  - Memory
347  - Create blank
348  */
350 
351  /*
352  Forcefully get a block from somewhere.
353  - Memory
354  - Load from disk
355  - Create blank filled with CONTENT_IGNORE
356 
357  */
358  MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
359 
360  /*
361  Try to get a block.
362  If it does not exist in memory, add it to the emerge queue.
363  - Memory
364  - Emerge Queue (deferred disk or generate)
365  */
367 
368  /*
369  Database functions
370  */
371  static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
372 
373  // Call these before and after saving of blocks
374  void beginSave();
375  void endSave();
376 
377  void save(ModifiedState save_level);
378  void listAllLoadableBlocks(std::vector<v3s16> &dst);
379  void listAllLoadedBlocks(std::vector<v3s16> &dst);
380 
382 
383  bool saveBlock(MapBlock *block);
384  static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
386  // Database version
387  void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
388 
389  bool deleteBlock(v3s16 blockpos);
390 
391  void updateVManip(v3s16 pos);
392 
393  // For debug printing
394  virtual void PrintInfo(std::ostream &out);
395 
397 
398  u64 getSeed();
399 
407  bool repairBlockLight(v3s16 blockpos,
408  std::map<v3s16, MapBlock *> *modified_blocks);
409 
411 
412 private:
413  // Emerge manager
415 
416  std::string m_savedir;
418 
420 
421  std::set<v3s16> m_chunks_in_progress;
422 
423  /*
424  Metadata is re-written on disk only if this is true.
425  This is reset to false when written on disk.
426  */
428  MapDatabase *dbase = nullptr;
429  MapDatabase *dbase_ro = nullptr;
430 
432 };
433 
434 
435 #define VMANIP_BLOCK_DATA_INEXIST 1
436 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
437 
439 {
440 public:
441  MMVManip(Map *map);
442  virtual ~MMVManip() = default;
443 
444  virtual void clear()
445  {
447  m_loaded_blocks.clear();
448  }
449 
450  void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
451  bool load_if_inexistent = true);
452 
453  // This is much faster with big chunks of generated data
454  void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
455  bool overwrite_generated = true);
456 
457  bool m_is_dirty = false;
458 
459 protected:
461  /*
462  key = blockpos
463  value = flags describing the block
464  */
465  std::map<v3s16, u8> m_loaded_blocks;
466 };
Definition: clientmap.h:67
Definition: emerge.h:113
Definition: gamedef.h:49
Definition: rollback_interface.h:112
Definition: l_vmanip.h:34
Definition: map.h:439
Map * m_map
Definition: map.h:460
virtual ~MMVManip()=default
std::map< v3s16, u8 > m_loaded_blocks
Definition: map.h:465
virtual void clear()
Definition: map.h:444
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, bool load_if_inexistent=true)
Definition: map.cpp:1878
MMVManip(Map *map)
Definition: map.cpp:1872
void blitBackAll(std::map< v3s16, MapBlock * > *modified_blocks, bool overwrite_generated=true)
Definition: map.cpp:1961
bool m_is_dirty
Definition: map.h:457
Definition: mapblock.h:74
Definition: database.h:38
Definition: map.h:116
virtual void onMapEditEvent(const MapEditEvent &event)=0
Definition: mapsector.h:40
Definition: map_settings_manager.h:45
Definition: map.h:123
void removeNodeTimer(v3s16 p)
Definition: map.cpp:1016
std::set< MapEventReceiver * > m_event_receivers
Definition: map.h:281
f32 m_transforming_liquid_loop_count_multiplier
Definition: map.h:302
void unloadUnreferencedBlocks(std::vector< v3s16 > *unloaded_blocks=NULL)
Definition: map.cpp:455
bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata=true)
Definition: map.cpp:255
bool m_queue_size_timer_started
Definition: map.h:305
void setNodeTimer(const NodeTimer &t)
Definition: map.cpp:996
bool removeNodeWithEvent(v3s16 p)
Definition: map.cpp:281
DISABLE_CLASS_COPY(Map)
virtual void endSave()
Definition: map.h:202
void addEventReceiver(MapEventReceiver *event_receiver)
Definition: map.cpp:81
virtual bool saveBlock(MapBlock *block)
Definition: map.h:208
Map(IGameDef *gamedef)
Definition: map.cpp:65
bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target, float step, float stepfac, float start_offset, float end_offset, u32 needed_count)
Definition: map.cpp:1094
virtual s32 mapType() const
Definition: map.h:130
UniqueQueue< v3s16 > m_transforming_liquid
Definition: map.h:290
MapBlock * getBlockNoCreate(v3s16 p)
Definition: map.cpp:134
u32 m_unprocessed_count
Definition: map.h:303
const NodeDefManager * m_nodedef
Definition: map.h:293
void transformLiquids(std::map< v3s16, MapBlock * > &modified_blocks, ServerEnvironment *env)
Definition: map.cpp:516
void addNodeAndUpdate(v3s16 p, MapNode n, std::map< v3s16, MapBlock * > &modified_blocks, bool remove_metadata=true)
Definition: map.cpp:193
virtual void drop()
Definition: map.h:138
std::map< v2s16, MapSector * > m_sectors
Definition: map.h:283
void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks, std::vector< v3s16 > *unloaded_blocks=NULL)
Definition: map.cpp:324
std::vector< v3s16 > findNodesWithMetadata(v3s16 p1, v3s16 p2)
Definition: map.cpp:883
virtual bool deleteBlock(v3s16 blockpos)
Definition: map.h:209
bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera, const core::aabbox3d< s16 > &block_bounds, v3s16 &check)
Definition: map.cpp:1030
MapSector * getSectorNoGenerate(v2s16 p2d)
Definition: map.cpp:119
bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
Definition: map.cpp:1126
bool isNodeUnderground(v3s16 p)
Definition: map.cpp:142
NodeTimer getNodeTimer(v3s16 p)
Definition: map.cpp:976
NodeMetadata * getNodeMetadata(v3s16 p)
Definition: map.cpp:924
void removeNodeMetadata(v3s16 p)
Definition: map.cpp:962
MapSector * m_sector_cache
Definition: map.h:286
virtual void save(ModifiedState save_level)
Definition: map.h:204
void deleteSectors(std::vector< v2s16 > &list)
Definition: map.cpp:460
void removeEventReceiver(MapEventReceiver *event_receiver)
Definition: map.cpp:86
v2s16 m_sector_cache_p
Definition: map.h:287
virtual ~Map()
Definition: map.cpp:71
virtual void PrintInfo(std::ostream &out)
Definition: map.cpp:473
void dispatchEvent(const MapEditEvent &event)
Definition: map.cpp:91
void removeNodeAndUpdate(v3s16 p, std::map< v3s16, MapBlock * > &modified_blocks)
Definition: map.cpp:249
MapNode getNode(v3s16 p, bool *is_valid_position=NULL)
Definition: map.cpp:157
IGameDef * m_gamedef
Definition: map.h:279
virtual MapSector * emergeSector(v2s16 p)
Definition: map.h:157
MapSector * getSectorNoGenerateNoLock(v2s16 p2d)
Definition: map.cpp:98
u64 m_inc_trending_up_start_time
Definition: map.h:304
const NodeDefManager * getNodeDefManager()
Definition: map.h:168
void setNode(v3s16 p, MapNode &n)
Definition: map.cpp:176
bool isValidPosition(v3s16 p)
Definition: map.cpp:149
virtual void beginSave()
Definition: map.h:201
bool setNodeMetadata(v3s16 p, NodeMetadata *meta)
Sets metadata for a node.
Definition: map.cpp:943
void transforming_liquid_add(v3s16 p)
Definition: map.cpp:512
virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition: map.h:165
MapBlock * getBlockNoCreateNoEx(v3s16 p)
Definition: map.cpp:124
Definition: metricsbackend.h:126
This class is for getting the actual properties of nodes from their content ID.
Definition: nodedef.h:510
Definition: nodemetadata.h:38
Definition: nodetimer.h:36
Definition: serverenvironment.h:194
Definition: map.h:315
void updateVManip(v3s16 pos)
Definition: map.cpp:1554
MapSettingsManager settings_mgr
Definition: map.h:410
u64 getSeed()
Definition: map.cpp:1297
MapDatabase * dbase_ro
Definition: map.h:429
bool blockpos_over_mapgen_limit(v3s16 p)
Definition: map.cpp:1302
MapDatabase * dbase
Definition: map.h:428
bool repairBlockLight(v3s16 blockpos, std::map< v3s16, MapBlock * > *modified_blocks)
Definition: map.cpp:1862
virtual void PrintInfo(std::ostream &out)
Definition: map.cpp:1857
void finishBlockMake(BlockMakeData *data, std::map< v3s16, MapBlock * > *changed_blocks)
Definition: map.cpp:1380
~ServerMap()
Definition: map.cpp:1263
MapSector * createSector(v2s16 p)
Definition: map.cpp:1442
s32 mapType() const
Definition: map.h:323
std::set< v3s16 > m_chunks_in_progress
Definition: map.h:421
void beginSave()
Definition: map.cpp:1696
void endSave()
Definition: map.cpp:1701
static MapDatabase * createDatabase(const std::string &name, const std::string &savedir, Settings &conf)
Definition: map.cpp:1668
bool deleteBlock(v3s16 blockpos)
Definition: map.cpp:1840
std::string m_savedir
Definition: map.h:416
bool m_map_saving_enabled
Definition: map.h:417
void listAllLoadedBlocks(std::vector< v3s16 > &dst)
Definition: map.cpp:1653
MapBlock * getBlockOrEmerge(v3s16 p3d)
Definition: map.cpp:1543
EmergeManager * m_emerge
Definition: map.h:414
MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
Definition: map.cpp:1519
int m_map_compression_level
Definition: map.h:419
bool m_map_metadata_changed
Definition: map.h:427
bool initBlockMake(v3s16 blockpos, BlockMakeData *data)
Definition: map.cpp:1315
bool saveBlock(MapBlock *block)
Definition: map.cpp:1706
MapBlock * createBlock(v3s16 p)
Definition: map.cpp:1478
ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb)
Definition: map.cpp:1184
MetricCounterPtr m_save_time_counter
Definition: map.h:431
void save(ModifiedState save_level)
Definition: map.cpp:1574
MapgenParams * getMapgenParams()
Definition: map.cpp:1290
void listAllLoadableBlocks(std::vector< v3s16 > &dst)
Definition: map.cpp:1646
bool isSavingEnabled()
Definition: map.h:396
MapBlock * loadBlock(v3s16 p)
Definition: map.cpp:1802
Definition: settings.h:101
Definition: voxel.h:59
void addPoint(const v3s16 &p)
Definition: voxel.h:98
Definition: voxel.h:356
virtual void clear()
Definition: voxel.cpp:41
#define MAP_BLOCKSIZE
Definition: constants.h:77
#define FATAL_ERROR(msg)
Definition: debug.h:58
core::vector2d< s16 > v2s16
Definition: irr_v2d.h:27
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
MapEditEventType
Definition: map.h:61
@ MEET_ADDNODE
Definition: map.h:63
@ MEET_OTHER
Definition: map.h:71
@ MEET_REMOVENODE
Definition: map.h:65
@ MEET_BLOCK_NODE_METADATA_CHANGED
Definition: map.h:69
@ MEET_SWAPNODE
Definition: map.h:67
#define MAPTYPE_SERVER
Definition: map.h:58
#define MAPTYPE_BASE
Definition: map.h:57
#define CONTENT_AIR
Definition: mapnode.h:59
std::shared_ptr< MetricCounter > MetricCounterPtr
Definition: metricsbackend.h:36
ModifiedState
Definition: modifiedstate.h:23
Definition: emerge.h:50
Definition: map.h:75
MapEditEventType type
Definition: map.h:76
std::set< v3s16 > modified_blocks
Definition: map.h:79
MapNode n
Definition: map.h:78
v3s16 p
Definition: map.h:77
bool is_private_change
Definition: map.h:80
VoxelArea getArea() const
Definition: map.h:84
MapEditEvent()=default
Definition: mapnode.h:118
Definition: mapgen.h:116
std::string p(std::string path)
Definition: test_filepath.cpp:59