emerge.h 6.0 KB

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