servermap.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2024 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 <vector>
  18. #include <memory>
  19. #include "map.h"
  20. #include "util/container.h"
  21. #include "util/metricsbackend.h"
  22. #include "map_settings_manager.h"
  23. class Settings;
  24. class MapDatabase;
  25. class IRollbackManager;
  26. class EmergeManager;
  27. class ServerEnvironment;
  28. struct BlockMakeData;
  29. class MetricsBackend;
  30. /*
  31. ServerMap
  32. This is the only map class that is able to generate map.
  33. */
  34. class ServerMap : public Map
  35. {
  36. public:
  37. /*
  38. savedir: directory to which map data should be saved
  39. */
  40. ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
  41. ~ServerMap();
  42. /*
  43. Get a sector from somewhere.
  44. - Check memory
  45. - Check disk (doesn't load blocks)
  46. - Create blank one
  47. */
  48. MapSector *createSector(v2s16 p);
  49. /*
  50. Blocks are generated by using these and makeBlock().
  51. */
  52. bool blockpos_over_mapgen_limit(v3s16 p);
  53. bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
  54. void finishBlockMake(BlockMakeData *data,
  55. std::map<v3s16, MapBlock*> *changed_blocks);
  56. /*
  57. Get a block from somewhere.
  58. - Memory
  59. - Create blank
  60. */
  61. MapBlock *createBlock(v3s16 p);
  62. /*
  63. Forcefully get a block from somewhere.
  64. - Memory
  65. - Load from disk
  66. - Create blank filled with CONTENT_IGNORE
  67. */
  68. MapBlock *emergeBlock(v3s16 p, bool create_blank=true) override;
  69. /*
  70. Try to get a block.
  71. If it does not exist in memory, add it to the emerge queue.
  72. - Memory
  73. - Emerge Queue (deferred disk or generate)
  74. */
  75. MapBlock *getBlockOrEmerge(v3s16 p3d, bool generate);
  76. bool isBlockInQueue(v3s16 pos);
  77. void addNodeAndUpdate(v3s16 p, MapNode n,
  78. std::map<v3s16, MapBlock*> &modified_blocks,
  79. bool remove_metadata) override;
  80. /*
  81. Database functions
  82. */
  83. static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
  84. // Call these before and after saving of blocks
  85. void beginSave() override;
  86. void endSave() override;
  87. void save(ModifiedState save_level) override;
  88. void listAllLoadableBlocks(std::vector<v3s16> &dst);
  89. void listAllLoadedBlocks(std::vector<v3s16> &dst);
  90. MapgenParams *getMapgenParams();
  91. bool saveBlock(MapBlock *block) override;
  92. static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
  93. MapBlock* loadBlock(v3s16 p);
  94. // Database version
  95. void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
  96. // Blocks are removed from the map but not deleted from memory until
  97. // deleteDetachedBlocks() is called, since pointers to them may still exist
  98. // when deleteBlock() is called.
  99. bool deleteBlock(v3s16 blockpos) override;
  100. void deleteDetachedBlocks();
  101. void step();
  102. void updateVManip(v3s16 pos);
  103. // For debug printing
  104. void PrintInfo(std::ostream &out) override;
  105. bool isSavingEnabled(){ return m_map_saving_enabled; }
  106. u64 getSeed();
  107. /*!
  108. * Fixes lighting in one map block.
  109. * May modify other blocks as well, as light can spread
  110. * out of the specified block.
  111. * Returns false if the block is not generated (so nothing
  112. * changed), true otherwise.
  113. */
  114. bool repairBlockLight(v3s16 blockpos,
  115. std::map<v3s16, MapBlock *> *modified_blocks);
  116. void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
  117. ServerEnvironment *env);
  118. void transforming_liquid_add(v3s16 p);
  119. MapSettingsManager settings_mgr;
  120. protected:
  121. void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
  122. private:
  123. friend class ModApiMapgen; // for m_transforming_liquid
  124. // Emerge manager
  125. EmergeManager *m_emerge;
  126. std::string m_savedir;
  127. bool m_map_saving_enabled;
  128. int m_map_compression_level;
  129. std::set<v3s16> m_chunks_in_progress;
  130. // used by deleteBlock() and deleteDetachedBlocks()
  131. std::vector<std::unique_ptr<MapBlock>> m_detached_blocks;
  132. // Queued transforming water nodes
  133. UniqueQueue<v3s16> m_transforming_liquid;
  134. f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
  135. u32 m_unprocessed_count = 0;
  136. u64 m_inc_trending_up_start_time = 0; // milliseconds
  137. bool m_queue_size_timer_started = false;
  138. /*
  139. Metadata is re-written on disk only if this is true.
  140. This is reset to false when written on disk.
  141. */
  142. bool m_map_metadata_changed = true;
  143. MapDatabase *dbase = nullptr;
  144. MapDatabase *dbase_ro = nullptr;
  145. // Map metrics
  146. MetricGaugePtr m_loaded_blocks_gauge;
  147. MetricCounterPtr m_save_time_counter;
  148. MetricCounterPtr m_save_count_counter;
  149. };