map.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <iostream>
  18. #include <set>
  19. #include <map>
  20. #include "irrlichttypes_bloated.h"
  21. #include "mapblock.h"
  22. #include "mapnode.h"
  23. #include "constants.h"
  24. #include "voxel.h"
  25. #include "modifiedstate.h"
  26. #include "util/numeric.h"
  27. #include "nodetimer.h"
  28. #include "debug.h"
  29. class MapSector;
  30. class NodeMetadata;
  31. class IGameDef;
  32. class IRollbackManager;
  33. /*
  34. MapEditEvent
  35. */
  36. enum MapEditEventType {
  37. // Node added (changed from air or something else to something)
  38. MEET_ADDNODE,
  39. // Node removed (changed to air)
  40. MEET_REMOVENODE,
  41. // Node swapped (changed without metadata change)
  42. MEET_SWAPNODE,
  43. // Node metadata changed
  44. MEET_BLOCK_NODE_METADATA_CHANGED,
  45. // Anything else (modified_blocks are set unsent)
  46. MEET_OTHER
  47. };
  48. struct MapEditEvent
  49. {
  50. MapEditEventType type = MEET_OTHER;
  51. v3s16 p;
  52. MapNode n = CONTENT_AIR;
  53. std::vector<v3s16> modified_blocks; // Represents a set
  54. bool is_private_change = false;
  55. MapEditEvent() = default;
  56. // Sets the event's position and marks the block as modified.
  57. void setPositionModified(v3s16 pos)
  58. {
  59. assert(modified_blocks.empty()); // only meant for initialization (once)
  60. p = pos;
  61. modified_blocks.push_back(getNodeBlockPos(pos));
  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. VoxelArea getArea() const
  71. {
  72. switch(type){
  73. case MEET_ADDNODE:
  74. case MEET_REMOVENODE:
  75. case MEET_SWAPNODE:
  76. case MEET_BLOCK_NODE_METADATA_CHANGED:
  77. return VoxelArea(p);
  78. case MEET_OTHER:
  79. {
  80. VoxelArea a;
  81. for (v3s16 p : modified_blocks) {
  82. v3s16 np1 = p*MAP_BLOCKSIZE;
  83. v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
  84. a.addPoint(np1);
  85. a.addPoint(np2);
  86. }
  87. return a;
  88. }
  89. }
  90. return VoxelArea();
  91. }
  92. };
  93. class MapEventReceiver
  94. {
  95. public:
  96. // event shall be deleted by caller after the call.
  97. virtual void onMapEditEvent(const MapEditEvent &event) = 0;
  98. };
  99. class Map /*: public NodeContainer*/
  100. {
  101. public:
  102. Map(IGameDef *gamedef);
  103. virtual ~Map();
  104. DISABLE_CLASS_COPY(Map);
  105. void addEventReceiver(MapEventReceiver *event_receiver);
  106. void removeEventReceiver(MapEventReceiver *event_receiver);
  107. // event shall be deleted by caller after the call.
  108. void dispatchEvent(const MapEditEvent &event);
  109. // On failure returns NULL
  110. MapSector * getSectorNoGenerateNoLock(v2s16 p2d);
  111. // Same as the above (there exists no lock anymore)
  112. MapSector * getSectorNoGenerate(v2s16 p2d);
  113. /*
  114. This is overloaded by ClientMap and ServerMap to allow
  115. their differing fetch methods.
  116. */
  117. virtual MapSector * emergeSector(v2s16 p){ return NULL; }
  118. // Returns InvalidPositionException if not found
  119. MapBlock * getBlockNoCreate(v3s16 p);
  120. // Returns NULL if not found
  121. MapBlock * getBlockNoCreateNoEx(v3s16 p);
  122. /* Server overrides */
  123. virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
  124. { return getBlockNoCreateNoEx(p); }
  125. inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
  126. bool isValidPosition(v3s16 p);
  127. // throws InvalidPositionException if not found
  128. void setNode(v3s16 p, MapNode n);
  129. // Returns a CONTENT_IGNORE node if not found
  130. // If is_valid_position is not NULL then this will be set to true if the
  131. // position is valid, otherwise false
  132. MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
  133. /*
  134. These handle lighting but not faces.
  135. */
  136. virtual void addNodeAndUpdate(v3s16 p, MapNode n,
  137. std::map<v3s16, MapBlock*> &modified_blocks,
  138. bool remove_metadata = true);
  139. void removeNodeAndUpdate(v3s16 p,
  140. std::map<v3s16, MapBlock*> &modified_blocks);
  141. /*
  142. Wrappers for the latter ones.
  143. These emit events.
  144. Return true if succeeded, false if not.
  145. */
  146. bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
  147. bool removeNodeWithEvent(v3s16 p);
  148. // Call these before and after saving of many blocks
  149. virtual void beginSave() {}
  150. virtual void endSave() {}
  151. virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
  152. /*
  153. Return true unless the map definitely cannot save blocks.
  154. */
  155. virtual bool maySaveBlocks() { return true; }
  156. // Server implements these.
  157. // Client leaves them as no-op.
  158. virtual bool saveBlock(MapBlock *block) { return false; }
  159. virtual bool deleteBlock(v3s16 blockpos) { return false; }
  160. /*
  161. Updates usage timers and unloads unused blocks and sectors.
  162. Saves modified blocks before unloading if possible.
  163. */
  164. void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
  165. std::vector<v3s16> *unloaded_blocks=NULL);
  166. /*
  167. Unloads all blocks with a zero refCount().
  168. Saves modified blocks before unloading if possible.
  169. */
  170. void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
  171. // Deletes sectors and their blocks from memory
  172. // Takes cache into account
  173. // If deleted sector is in sector cache, clears cache
  174. void deleteSectors(std::vector<v2s16> &list);
  175. // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
  176. virtual void PrintInfo(std::ostream &out);
  177. /*
  178. Node metadata
  179. These are basically coordinate wrappers to MapBlock
  180. */
  181. std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
  182. NodeMetadata *getNodeMetadata(v3s16 p);
  183. /**
  184. * Sets metadata for a node.
  185. * This method sets the metadata for a given node.
  186. * On success, it returns @c true and the object pointed to
  187. * by @p meta is then managed by the system and should
  188. * not be deleted by the caller.
  189. *
  190. * In case of failure, the method returns @c false and the
  191. * caller is still responsible for deleting the object!
  192. *
  193. * @param p node coordinates
  194. * @param meta pointer to @c NodeMetadata object
  195. * @return @c true on success, false on failure
  196. */
  197. bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
  198. void removeNodeMetadata(v3s16 p);
  199. /*
  200. Node Timers
  201. These are basically coordinate wrappers to MapBlock
  202. */
  203. NodeTimer getNodeTimer(v3s16 p);
  204. void setNodeTimer(const NodeTimer &t);
  205. void removeNodeTimer(v3s16 p);
  206. /*
  207. Utilities
  208. */
  209. // Iterates through all nodes in the area in an unspecified order.
  210. // The given callback takes the position as its first argument and the node
  211. // as its second. If it returns false, forEachNodeInArea returns early.
  212. template<typename F>
  213. void forEachNodeInArea(v3s16 minp, v3s16 maxp, F func)
  214. {
  215. v3s16 bpmin = getNodeBlockPos(minp);
  216. v3s16 bpmax = getNodeBlockPos(maxp);
  217. for (s16 bz = bpmin.Z; bz <= bpmax.Z; bz++)
  218. for (s16 bx = bpmin.X; bx <= bpmax.X; bx++)
  219. for (s16 by = bpmin.Y; by <= bpmax.Y; by++) {
  220. // y is iterated innermost to make use of the sector cache.
  221. v3s16 bp(bx, by, bz);
  222. MapBlock *block = getBlockNoCreateNoEx(bp);
  223. v3s16 basep = bp * MAP_BLOCKSIZE;
  224. s16 minx_block = rangelim(minp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
  225. s16 miny_block = rangelim(minp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
  226. s16 minz_block = rangelim(minp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
  227. s16 maxx_block = rangelim(maxp.X - basep.X, 0, MAP_BLOCKSIZE - 1);
  228. s16 maxy_block = rangelim(maxp.Y - basep.Y, 0, MAP_BLOCKSIZE - 1);
  229. s16 maxz_block = rangelim(maxp.Z - basep.Z, 0, MAP_BLOCKSIZE - 1);
  230. for (s16 z_block = minz_block; z_block <= maxz_block; z_block++)
  231. for (s16 y_block = miny_block; y_block <= maxy_block; y_block++)
  232. for (s16 x_block = minx_block; x_block <= maxx_block; x_block++) {
  233. v3s16 p = basep + v3s16(x_block, y_block, z_block);
  234. MapNode n = block ?
  235. block->getNodeNoCheck(x_block, y_block, z_block) :
  236. MapNode(CONTENT_IGNORE);
  237. if (!func(p, n))
  238. return;
  239. }
  240. }
  241. }
  242. bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
  243. {
  244. return isBlockOccluded(block->getPosRelative(), cam_pos_nodes, false);
  245. }
  246. bool isBlockOccluded(v3s16 pos_relative, v3s16 cam_pos_nodes, bool simple_check = false);
  247. protected:
  248. IGameDef *m_gamedef;
  249. std::set<MapEventReceiver*> m_event_receivers;
  250. std::unordered_map<v2s16, MapSector*> m_sectors;
  251. // Be sure to set this to NULL when the cached sector is deleted
  252. MapSector *m_sector_cache = nullptr;
  253. v2s16 m_sector_cache_p;
  254. // This stores the properties of the nodes on the map.
  255. const NodeDefManager *m_nodedef;
  256. // Can be implemented by child class
  257. virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
  258. bool determineAdditionalOcclusionCheck(v3s16 pos_camera,
  259. const core::aabbox3d<s16> &block_bounds, v3s16 &to_check);
  260. bool isOccluded(v3s16 pos_camera, v3s16 pos_target,
  261. float step, float stepfac, float start_offset, float end_offset,
  262. u32 needed_count);
  263. };
  264. #define VMANIP_BLOCK_DATA_INEXIST 1
  265. #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
  266. class MMVManip : public VoxelManipulator
  267. {
  268. public:
  269. MMVManip(Map *map);
  270. virtual ~MMVManip() = default;
  271. virtual void clear()
  272. {
  273. VoxelManipulator::clear();
  274. m_loaded_blocks.clear();
  275. }
  276. void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
  277. bool load_if_inexistent = true);
  278. // This is much faster with big chunks of generated data
  279. void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
  280. bool overwrite_generated = true);
  281. /*
  282. Creates a copy of this VManip including contents, the copy will not be
  283. associated with a Map.
  284. */
  285. MMVManip *clone() const;
  286. // Reassociates a copied VManip to a map
  287. void reparent(Map *map);
  288. // Is it impossible to call initialEmerge / blitBackAll?
  289. inline bool isOrphan() const { return !m_map; }
  290. bool m_is_dirty = false;
  291. protected:
  292. MMVManip() {};
  293. // may be null
  294. Map *m_map = nullptr;
  295. /*
  296. key = blockpos
  297. value = flags describing the block
  298. */
  299. std::map<v3s16, u8> m_loaded_blocks;
  300. };