emerge.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Structure containing inputs/outputs for chunk generation
  39. struct BlockMakeData {
  40. MMVManip *vmanip = nullptr;
  41. u64 seed = 0;
  42. v3s16 blockpos_min;
  43. v3s16 blockpos_max;
  44. v3s16 blockpos_requested;
  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 EmergeManager {
  73. public:
  74. const NodeDefManager *ndef;
  75. bool enable_mapgen_debug_info;
  76. // Generation Notify
  77. u32 gen_notify_on = 0;
  78. std::set<u32> gen_notify_on_deco_ids;
  79. // Parameters passed to mapgens owned by ServerMap
  80. // TODO(hmmmm): Remove this after mapgen helper methods using them
  81. // are moved to ServerMap
  82. MapgenParams *mgparams;
  83. // Hackish workaround:
  84. // For now, EmergeManager must hold onto a ptr to the Map's setting manager
  85. // since the Map can only be accessed through the Environment, and the
  86. // Environment is not created until after script initialization.
  87. MapSettingsManager *map_settings_mgr;
  88. // Managers of various map generation-related components
  89. BiomeManager *biomemgr;
  90. OreManager *oremgr;
  91. DecorationManager *decomgr;
  92. SchematicManager *schemmgr;
  93. // Methods
  94. EmergeManager(Server *server);
  95. ~EmergeManager();
  96. DISABLE_CLASS_COPY(EmergeManager);
  97. void initMapgens(MapgenParams *mgparams);
  98. void startThreads();
  99. void stopThreads();
  100. bool isRunning();
  101. bool enqueueBlockEmerge(
  102. session_t peer_id,
  103. v3s16 blockpos,
  104. bool allow_generate,
  105. bool ignore_queue_limits=false);
  106. bool enqueueBlockEmergeEx(
  107. v3s16 blockpos,
  108. session_t peer_id,
  109. u16 flags,
  110. EmergeCompletionCallback callback,
  111. void *callback_param);
  112. v3s16 getContainingChunk(v3s16 blockpos);
  113. Mapgen *getCurrentMapgen();
  114. // Mapgen helpers methods
  115. int getSpawnLevelAtPoint(v2s16 p);
  116. int getGroundLevelAtPoint(v2s16 p);
  117. bool isBlockUnderground(v3s16 blockpos);
  118. static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
  119. private:
  120. std::vector<Mapgen *> m_mapgens;
  121. std::vector<EmergeThread *> m_threads;
  122. bool m_threads_active = false;
  123. std::mutex m_queue_mutex;
  124. std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
  125. std::unordered_map<u16, u16> m_peer_queue_count;
  126. u16 m_qlimit_total;
  127. u16 m_qlimit_diskonly;
  128. u16 m_qlimit_generate;
  129. // Requires m_queue_mutex held
  130. EmergeThread *getOptimalThread();
  131. bool pushBlockEmergeData(
  132. v3s16 pos,
  133. u16 peer_requested,
  134. u16 flags,
  135. EmergeCompletionCallback callback,
  136. void *callback_param,
  137. bool *entry_already_exists);
  138. bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
  139. friend class EmergeThread;
  140. };