map.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <sstream>
  19. #include <set>
  20. #include <map>
  21. #include <list>
  22. #include "irrlichttypes_bloated.h"
  23. #include "mapnode.h"
  24. #include "constants.h"
  25. #include "voxel.h"
  26. #include "modifiedstate.h"
  27. #include "util/container.h"
  28. #include "util/metricsbackend.h"
  29. #include "nodetimer.h"
  30. #include "map_settings_manager.h"
  31. #include "debug.h"
  32. class Settings;
  33. class MapDatabase;
  34. class ClientMap;
  35. class MapSector;
  36. class ServerMapSector;
  37. class MapBlock;
  38. class NodeMetadata;
  39. class IGameDef;
  40. class IRollbackManager;
  41. class EmergeManager;
  42. class MetricsBackend;
  43. class ServerEnvironment;
  44. struct BlockMakeData;
  45. /*
  46. MapEditEvent
  47. */
  48. #define MAPTYPE_BASE 0
  49. #define MAPTYPE_SERVER 1
  50. #define MAPTYPE_CLIENT 2
  51. enum MapEditEventType{
  52. // Node added (changed from air or something else to something)
  53. MEET_ADDNODE,
  54. // Node removed (changed to air)
  55. MEET_REMOVENODE,
  56. // Node swapped (changed without metadata change)
  57. MEET_SWAPNODE,
  58. // Node metadata changed
  59. MEET_BLOCK_NODE_METADATA_CHANGED,
  60. // Anything else (modified_blocks are set unsent)
  61. MEET_OTHER
  62. };
  63. struct MapEditEvent
  64. {
  65. MapEditEventType type = MEET_OTHER;
  66. v3s16 p;
  67. MapNode n = CONTENT_AIR;
  68. std::set<v3s16> modified_blocks;
  69. bool is_private_change = false;
  70. MapEditEvent() = default;
  71. VoxelArea getArea() const
  72. {
  73. switch(type){
  74. case MEET_ADDNODE:
  75. return VoxelArea(p);
  76. case MEET_REMOVENODE:
  77. return VoxelArea(p);
  78. case MEET_SWAPNODE:
  79. return VoxelArea(p);
  80. case MEET_BLOCK_NODE_METADATA_CHANGED:
  81. {
  82. v3s16 np1 = p*MAP_BLOCKSIZE;
  83. v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
  84. return VoxelArea(np1, np2);
  85. }
  86. case MEET_OTHER:
  87. {
  88. VoxelArea a;
  89. for (v3s16 p : modified_blocks) {
  90. v3s16 np1 = p*MAP_BLOCKSIZE;
  91. v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
  92. a.addPoint(np1);
  93. a.addPoint(np2);
  94. }
  95. return a;
  96. }
  97. }
  98. return VoxelArea();
  99. }
  100. };
  101. class MapEventReceiver
  102. {
  103. public:
  104. // event shall be deleted by caller after the call.
  105. virtual void onMapEditEvent(const MapEditEvent &event) = 0;
  106. };
  107. class Map /*: public NodeContainer*/
  108. {
  109. public:
  110. Map(IGameDef *gamedef);
  111. virtual ~Map();
  112. DISABLE_CLASS_COPY(Map);
  113. virtual s32 mapType() const
  114. {
  115. return MAPTYPE_BASE;
  116. }
  117. /*
  118. Drop (client) or delete (server) the map.
  119. */
  120. virtual void drop()
  121. {
  122. delete this;
  123. }
  124. void addEventReceiver(MapEventReceiver *event_receiver);
  125. void removeEventReceiver(MapEventReceiver *event_receiver);
  126. // event shall be deleted by caller after the call.
  127. void dispatchEvent(const MapEditEvent &event);
  128. // On failure returns NULL
  129. MapSector * getSectorNoGenerateNoLock(v2s16 p2d);
  130. // Same as the above (there exists no lock anymore)
  131. MapSector * getSectorNoGenerate(v2s16 p2d);
  132. /*
  133. This is overloaded by ClientMap and ServerMap to allow
  134. their differing fetch methods.
  135. */
  136. virtual MapSector * emergeSector(v2s16 p){ return NULL; }
  137. // Returns InvalidPositionException if not found
  138. MapBlock * getBlockNoCreate(v3s16 p);
  139. // Returns NULL if not found
  140. MapBlock * getBlockNoCreateNoEx(v3s16 p);
  141. /* Server overrides */
  142. virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
  143. { return getBlockNoCreateNoEx(p); }
  144. inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
  145. // Returns InvalidPositionException if not found
  146. bool isNodeUnderground(v3s16 p);
  147. bool isValidPosition(v3s16 p);
  148. // throws InvalidPositionException if not found
  149. void setNode(v3s16 p, MapNode & n);
  150. // Returns a CONTENT_IGNORE node if not found
  151. // If is_valid_position is not NULL then this will be set to true if the
  152. // position is valid, otherwise false
  153. MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
  154. /*
  155. These handle lighting but not faces.
  156. */
  157. void addNodeAndUpdate(v3s16 p, MapNode n,
  158. std::map<v3s16, MapBlock*> &modified_blocks,
  159. bool remove_metadata = true);
  160. void removeNodeAndUpdate(v3s16 p,
  161. std::map<v3s16, MapBlock*> &modified_blocks);
  162. /*
  163. Wrappers for the latter ones.
  164. These emit events.
  165. Return true if succeeded, false if not.
  166. */
  167. bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
  168. bool removeNodeWithEvent(v3s16 p);
  169. // Call these before and after saving of many blocks
  170. virtual void beginSave() {}
  171. virtual void endSave() {}
  172. virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
  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. Updates usage timers and unloads unused blocks and sectors.
  179. Saves modified blocks before unloading on MAPTYPE_SERVER.
  180. */
  181. void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
  182. std::vector<v3s16> *unloaded_blocks=NULL);
  183. /*
  184. Unloads all blocks with a zero refCount().
  185. Saves modified blocks before unloading on MAPTYPE_SERVER.
  186. */
  187. void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
  188. // Deletes sectors and their blocks from memory
  189. // Takes cache into account
  190. // If deleted sector is in sector cache, clears cache
  191. void deleteSectors(std::vector<v2s16> &list);
  192. // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
  193. virtual void PrintInfo(std::ostream &out);
  194. void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
  195. ServerEnvironment *env);
  196. /*
  197. Node metadata
  198. These are basically coordinate wrappers to MapBlock
  199. */
  200. std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
  201. NodeMetadata *getNodeMetadata(v3s16 p);
  202. /**
  203. * Sets metadata for a node.
  204. * This method sets the metadata for a given node.
  205. * On success, it returns @c true and the object pointed to
  206. * by @p meta is then managed by the system and should
  207. * not be deleted by the caller.
  208. *
  209. * In case of failure, the method returns @c false and the
  210. * caller is still responsible for deleting the object!
  211. *
  212. * @param p node coordinates
  213. * @param meta pointer to @c NodeMetadata object
  214. * @return @c true on success, false on failure
  215. */
  216. bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
  217. void removeNodeMetadata(v3s16 p);
  218. /*
  219. Node Timers
  220. These are basically coordinate wrappers to MapBlock
  221. */
  222. NodeTimer getNodeTimer(v3s16 p);
  223. void setNodeTimer(const NodeTimer &t);
  224. void removeNodeTimer(v3s16 p);
  225. /*
  226. Variables
  227. */
  228. void transforming_liquid_add(v3s16 p);
  229. bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
  230. protected:
  231. friend class LuaVoxelManip;
  232. IGameDef *m_gamedef;
  233. std::set<MapEventReceiver*> m_event_receivers;
  234. std::map<v2s16, MapSector*> m_sectors;
  235. // Be sure to set this to NULL when the cached sector is deleted
  236. MapSector *m_sector_cache = nullptr;
  237. v2s16 m_sector_cache_p;
  238. // Queued transforming water nodes
  239. UniqueQueue<v3s16> m_transforming_liquid;
  240. // This stores the properties of the nodes on the map.
  241. const NodeDefManager *m_nodedef;
  242. bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
  243. const core::aabbox3d<s16> &block_bounds, v3s16 &check);
  244. bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target,
  245. float step, float stepfac, float start_offset, float end_offset,
  246. u32 needed_count);
  247. private:
  248. f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
  249. u32 m_unprocessed_count = 0;
  250. u64 m_inc_trending_up_start_time = 0; // milliseconds
  251. bool m_queue_size_timer_started = false;
  252. };
  253. /*
  254. ServerMap
  255. This is the only map class that is able to generate map.
  256. */
  257. class ServerMap : public Map
  258. {
  259. public:
  260. /*
  261. savedir: directory to which map data should be saved
  262. */
  263. ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
  264. ~ServerMap();
  265. s32 mapType() const
  266. {
  267. return MAPTYPE_SERVER;
  268. }
  269. /*
  270. Get a sector from somewhere.
  271. - Check memory
  272. - Check disk (doesn't load blocks)
  273. - Create blank one
  274. */
  275. MapSector *createSector(v2s16 p);
  276. /*
  277. Blocks are generated by using these and makeBlock().
  278. */
  279. bool blockpos_over_mapgen_limit(v3s16 p);
  280. bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
  281. void finishBlockMake(BlockMakeData *data,
  282. std::map<v3s16, MapBlock*> *changed_blocks);
  283. /*
  284. Get a block from somewhere.
  285. - Memory
  286. - Create blank
  287. */
  288. MapBlock *createBlock(v3s16 p);
  289. /*
  290. Forcefully get a block from somewhere.
  291. - Memory
  292. - Load from disk
  293. - Create blank filled with CONTENT_IGNORE
  294. */
  295. MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
  296. /*
  297. Try to get a block.
  298. If it does not exist in memory, add it to the emerge queue.
  299. - Memory
  300. - Emerge Queue (deferred disk or generate)
  301. */
  302. MapBlock *getBlockOrEmerge(v3s16 p3d);
  303. bool isBlockInQueue(v3s16 pos);
  304. /*
  305. Database functions
  306. */
  307. static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
  308. // Call these before and after saving of blocks
  309. void beginSave();
  310. void endSave();
  311. void save(ModifiedState save_level);
  312. void listAllLoadableBlocks(std::vector<v3s16> &dst);
  313. void listAllLoadedBlocks(std::vector<v3s16> &dst);
  314. MapgenParams *getMapgenParams();
  315. bool saveBlock(MapBlock *block);
  316. static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
  317. MapBlock* loadBlock(v3s16 p);
  318. // Database version
  319. void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
  320. bool deleteBlock(v3s16 blockpos);
  321. void updateVManip(v3s16 pos);
  322. // For debug printing
  323. virtual void PrintInfo(std::ostream &out);
  324. bool isSavingEnabled(){ return m_map_saving_enabled; }
  325. u64 getSeed();
  326. /*!
  327. * Fixes lighting in one map block.
  328. * May modify other blocks as well, as light can spread
  329. * out of the specified block.
  330. * Returns false if the block is not generated (so nothing
  331. * changed), true otherwise.
  332. */
  333. bool repairBlockLight(v3s16 blockpos,
  334. std::map<v3s16, MapBlock *> *modified_blocks);
  335. MapSettingsManager settings_mgr;
  336. private:
  337. // Emerge manager
  338. EmergeManager *m_emerge;
  339. std::string m_savedir;
  340. bool m_map_saving_enabled;
  341. int m_map_compression_level;
  342. std::set<v3s16> m_chunks_in_progress;
  343. /*
  344. Metadata is re-written on disk only if this is true.
  345. This is reset to false when written on disk.
  346. */
  347. bool m_map_metadata_changed = true;
  348. MapDatabase *dbase = nullptr;
  349. MapDatabase *dbase_ro = nullptr;
  350. MetricCounterPtr m_save_time_counter;
  351. };
  352. #define VMANIP_BLOCK_DATA_INEXIST 1
  353. #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
  354. class MMVManip : public VoxelManipulator
  355. {
  356. public:
  357. MMVManip(Map *map);
  358. virtual ~MMVManip() = default;
  359. virtual void clear()
  360. {
  361. VoxelManipulator::clear();
  362. m_loaded_blocks.clear();
  363. }
  364. void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
  365. bool load_if_inexistent = true);
  366. // This is much faster with big chunks of generated data
  367. void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
  368. bool overwrite_generated = true);
  369. bool m_is_dirty = false;
  370. protected:
  371. Map *m_map;
  372. /*
  373. key = blockpos
  374. value = flags describing the block
  375. */
  376. std::map<v3s16, u8> m_loaded_blocks;
  377. };