emerge.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  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 <map>
  18. #include <mutex>
  19. #include "network/networkprotocol.h"
  20. #include "irr_v3d.h"
  21. #include "util/container.h"
  22. #include "util/metricsbackend.h"
  23. #include "mapgen/mapgen.h" // for MapgenParams
  24. #include "map.h"
  25. #define BLOCK_EMERGE_ALLOW_GEN (1 << 0)
  26. #define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
  27. #define EMERGE_DBG_OUT(x) { \
  28. if (enable_mapgen_debug_info) \
  29. infostream << "EmergeThread: " x << std::endl; \
  30. }
  31. class EmergeThread;
  32. class NodeDefManager;
  33. class Settings;
  34. class MapSettingsManager;
  35. class BiomeManager;
  36. class OreManager;
  37. class DecorationManager;
  38. class SchematicManager;
  39. class Server;
  40. class ModApiMapgen;
  41. // Structure containing inputs/outputs for chunk generation
  42. struct BlockMakeData {
  43. MMVManip *vmanip = nullptr;
  44. u64 seed = 0;
  45. v3s16 blockpos_min;
  46. v3s16 blockpos_max;
  47. UniqueQueue<v3s16> transforming_liquid;
  48. const NodeDefManager *nodedef = nullptr;
  49. BlockMakeData() = default;
  50. ~BlockMakeData() { delete vmanip; }
  51. };
  52. // Result from processing an item on the emerge queue
  53. enum EmergeAction {
  54. EMERGE_CANCELLED,
  55. EMERGE_ERRORED,
  56. EMERGE_FROM_MEMORY,
  57. EMERGE_FROM_DISK,
  58. EMERGE_GENERATED,
  59. };
  60. constexpr const char *emergeActionStrs[] = {
  61. "cancelled",
  62. "errored",
  63. "from_memory",
  64. "from_disk",
  65. "generated",
  66. };
  67. // Callback
  68. typedef void (*EmergeCompletionCallback)(
  69. v3s16 blockpos, EmergeAction action, void *param);
  70. typedef std::vector<
  71. std::pair<
  72. EmergeCompletionCallback,
  73. void *
  74. >
  75. > EmergeCallbackList;
  76. struct BlockEmergeData {
  77. u16 peer_requested;
  78. u16 flags;
  79. EmergeCallbackList callbacks;
  80. };
  81. class EmergeParams {
  82. friend class EmergeManager;
  83. public:
  84. EmergeParams() = delete;
  85. ~EmergeParams();
  86. DISABLE_CLASS_COPY(EmergeParams);
  87. const NodeDefManager *ndef; // shared
  88. bool enable_mapgen_debug_info;
  89. u32 gen_notify_on;
  90. const std::set<u32> *gen_notify_on_deco_ids; // shared
  91. const std::set<std::string> *gen_notify_on_custom; // shared
  92. BiomeGen *biomegen;
  93. BiomeManager *biomemgr;
  94. OreManager *oremgr;
  95. DecorationManager *decomgr;
  96. SchematicManager *schemmgr;
  97. inline GenerateNotifier createNotifier() const {
  98. return GenerateNotifier(gen_notify_on, gen_notify_on_deco_ids,
  99. gen_notify_on_custom);
  100. }
  101. private:
  102. EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
  103. const BiomeManager *biomemgr,
  104. const OreManager *oremgr, const DecorationManager *decomgr,
  105. const SchematicManager *schemmgr);
  106. };
  107. class EmergeManager {
  108. /* The mod API needs unchecked access to allow:
  109. * - using decomgr or oremgr to place decos/ores
  110. * - using schemmgr to load and place schematics
  111. */
  112. friend class ModApiMapgen;
  113. public:
  114. const NodeDefManager *ndef;
  115. bool enable_mapgen_debug_info;
  116. // Generation Notify
  117. u32 gen_notify_on = 0;
  118. std::set<u32> gen_notify_on_deco_ids;
  119. std::set<std::string> gen_notify_on_custom;
  120. // Parameters passed to mapgens owned by ServerMap
  121. // TODO(hmmmm): Remove this after mapgen helper methods using them
  122. // are moved to ServerMap
  123. MapgenParams *mgparams;
  124. // Hackish workaround:
  125. // For now, EmergeManager must hold onto a ptr to the Map's setting manager
  126. // since the Map can only be accessed through the Environment, and the
  127. // Environment is not created until after script initialization.
  128. MapSettingsManager *map_settings_mgr;
  129. // Methods
  130. EmergeManager(Server *server, MetricsBackend *mb);
  131. ~EmergeManager();
  132. DISABLE_CLASS_COPY(EmergeManager);
  133. const BiomeGen *getBiomeGen() const { return biomegen; }
  134. // no usage restrictions
  135. const BiomeManager *getBiomeManager() const { return biomemgr; }
  136. const OreManager *getOreManager() const { return oremgr; }
  137. const DecorationManager *getDecorationManager() const { return decomgr; }
  138. const SchematicManager *getSchematicManager() const { return schemmgr; }
  139. // only usable before mapgen init
  140. BiomeManager *getWritableBiomeManager();
  141. OreManager *getWritableOreManager();
  142. DecorationManager *getWritableDecorationManager();
  143. SchematicManager *getWritableSchematicManager();
  144. void initMapgens(MapgenParams *mgparams);
  145. void startThreads();
  146. void stopThreads();
  147. bool isRunning();
  148. bool enqueueBlockEmerge(
  149. session_t peer_id,
  150. v3s16 blockpos,
  151. bool allow_generate,
  152. bool ignore_queue_limits=false);
  153. bool enqueueBlockEmergeEx(
  154. v3s16 blockpos,
  155. session_t peer_id,
  156. u16 flags,
  157. EmergeCompletionCallback callback,
  158. void *callback_param);
  159. bool isBlockInQueue(v3s16 pos);
  160. Mapgen *getCurrentMapgen();
  161. // Mapgen helpers methods
  162. int getSpawnLevelAtPoint(v2s16 p);
  163. bool isBlockUnderground(v3s16 blockpos);
  164. static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
  165. private:
  166. std::vector<Mapgen *> m_mapgens;
  167. std::vector<EmergeThread *> m_threads;
  168. bool m_threads_active = false;
  169. std::mutex m_queue_mutex;
  170. std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
  171. std::unordered_map<u16, u32> m_peer_queue_count;
  172. u32 m_qlimit_total;
  173. u32 m_qlimit_diskonly;
  174. u32 m_qlimit_generate;
  175. // Emerge metrics
  176. MetricCounterPtr m_completed_emerge_counter[5];
  177. // Managers of various map generation-related components
  178. // Note that each Mapgen gets a copy(!) of these to work with
  179. BiomeGen *biomegen;
  180. BiomeManager *biomemgr;
  181. OreManager *oremgr;
  182. DecorationManager *decomgr;
  183. SchematicManager *schemmgr;
  184. // Requires m_queue_mutex held
  185. EmergeThread *getOptimalThread();
  186. bool pushBlockEmergeData(
  187. v3s16 pos,
  188. u16 peer_requested,
  189. u16 flags,
  190. EmergeCompletionCallback callback,
  191. void *callback_param,
  192. bool *entry_already_exists);
  193. bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
  194. void reportCompletedEmerge(EmergeAction action);
  195. friend class EmergeThread;
  196. };