map.h 13 KB

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