emerge.h 5.7 KB

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