emerge.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. BiomeManager *biomemgr;
  83. OreManager *oremgr;
  84. DecorationManager *decomgr;
  85. SchematicManager *schemmgr;
  86. private:
  87. EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
  88. const OreManager *oremgr, const DecorationManager *decomgr,
  89. const SchematicManager *schemmgr);
  90. };
  91. class EmergeManager {
  92. /* The mod API needs unchecked access to allow:
  93. * - using decomgr or oremgr to place decos/ores
  94. * - using schemmgr to load and place schematics
  95. */
  96. friend class ModApiMapgen;
  97. public:
  98. const NodeDefManager *ndef;
  99. bool enable_mapgen_debug_info;
  100. // Generation Notify
  101. u32 gen_notify_on = 0;
  102. std::set<u32> gen_notify_on_deco_ids;
  103. // Parameters passed to mapgens owned by ServerMap
  104. // TODO(hmmmm): Remove this after mapgen helper methods using them
  105. // are moved to ServerMap
  106. MapgenParams *mgparams;
  107. // Hackish workaround:
  108. // For now, EmergeManager must hold onto a ptr to the Map's setting manager
  109. // since the Map can only be accessed through the Environment, and the
  110. // Environment is not created until after script initialization.
  111. MapSettingsManager *map_settings_mgr;
  112. // Methods
  113. EmergeManager(Server *server);
  114. ~EmergeManager();
  115. DISABLE_CLASS_COPY(EmergeManager);
  116. // no usage restrictions
  117. const BiomeManager *getBiomeManager() const { return biomemgr; }
  118. const OreManager *getOreManager() const { return oremgr; }
  119. const DecorationManager *getDecorationManager() const { return decomgr; }
  120. const SchematicManager *getSchematicManager() const { return schemmgr; }
  121. // only usable before mapgen init
  122. BiomeManager *getWritableBiomeManager();
  123. OreManager *getWritableOreManager();
  124. DecorationManager *getWritableDecorationManager();
  125. SchematicManager *getWritableSchematicManager();
  126. void initMapgens(MapgenParams *mgparams);
  127. void startThreads();
  128. void stopThreads();
  129. bool isRunning();
  130. bool enqueueBlockEmerge(
  131. session_t peer_id,
  132. v3s16 blockpos,
  133. bool allow_generate,
  134. bool ignore_queue_limits=false);
  135. bool enqueueBlockEmergeEx(
  136. v3s16 blockpos,
  137. session_t peer_id,
  138. u16 flags,
  139. EmergeCompletionCallback callback,
  140. void *callback_param);
  141. v3s16 getContainingChunk(v3s16 blockpos);
  142. Mapgen *getCurrentMapgen();
  143. // Mapgen helpers methods
  144. int getSpawnLevelAtPoint(v2s16 p);
  145. int getGroundLevelAtPoint(v2s16 p);
  146. bool isBlockUnderground(v3s16 blockpos);
  147. static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
  148. private:
  149. std::vector<Mapgen *> m_mapgens;
  150. std::vector<EmergeThread *> m_threads;
  151. bool m_threads_active = false;
  152. std::mutex m_queue_mutex;
  153. std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
  154. std::unordered_map<u16, u16> m_peer_queue_count;
  155. u16 m_qlimit_total;
  156. u16 m_qlimit_diskonly;
  157. u16 m_qlimit_generate;
  158. // Managers of various map generation-related components
  159. // Note that each Mapgen gets a copy(!) of these to work with
  160. BiomeManager *biomemgr;
  161. OreManager *oremgr;
  162. DecorationManager *decomgr;
  163. SchematicManager *schemmgr;
  164. // Requires m_queue_mutex held
  165. EmergeThread *getOptimalThread();
  166. bool pushBlockEmergeData(
  167. v3s16 pos,
  168. u16 peer_requested,
  169. u16 flags,
  170. EmergeCompletionCallback callback,
  171. void *callback_param,
  172. bool *entry_already_exists);
  173. bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
  174. friend class EmergeThread;
  175. };