map.h 13 KB

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