minimap_manager.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "minimap_manager.h"
  2. #include "game/core/component.h"
  3. #include "game/core/world.h"
  4. #include "game/map/map_loader.h"
  5. #include "game/map/minimap/minimap_generator.h"
  6. #include "game/map/minimap/unit_layer.h"
  7. #include "game/map/visibility_service.h"
  8. #include "game/systems/selection_system.h"
  9. #include "game/units/troop_type.h"
  10. #include <QDebug>
  11. #include <QPainter>
  12. #include <algorithm>
  13. #include <unordered_set>
  14. MinimapManager::MinimapManager() = default;
  15. MinimapManager::~MinimapManager() = default;
  16. void MinimapManager::generate_for_map(const Game::Map::MapDefinition &map_def) {
  17. Game::Map::Minimap::MinimapGenerator generator;
  18. m_minimap_base_image = generator.generate(map_def);
  19. if (!m_minimap_base_image.isNull()) {
  20. qDebug() << "MinimapManager: Generated minimap of size"
  21. << m_minimap_base_image.width() << "x"
  22. << m_minimap_base_image.height();
  23. m_world_width = static_cast<float>(map_def.grid.width);
  24. m_world_height = static_cast<float>(map_def.grid.height);
  25. m_unit_layer = std::make_unique<Game::Map::Minimap::UnitLayer>();
  26. m_unit_layer->init(m_minimap_base_image.width(),
  27. m_minimap_base_image.height(), m_world_width,
  28. m_world_height);
  29. qDebug() << "MinimapManager: Initialized unit layer for world"
  30. << m_world_width << "x" << m_world_height;
  31. m_minimap_fog_version = 0;
  32. m_minimap_update_timer = MINIMAP_UPDATE_INTERVAL;
  33. update_fog(0.0F, 1);
  34. } else {
  35. qWarning() << "MinimapManager: Failed to generate minimap";
  36. }
  37. }
  38. void MinimapManager::update_fog(float dt, int local_owner_id) {
  39. if (m_minimap_base_image.isNull()) {
  40. return;
  41. }
  42. m_minimap_update_timer += dt;
  43. if (m_minimap_update_timer < MINIMAP_UPDATE_INTERVAL) {
  44. return;
  45. }
  46. m_minimap_update_timer = 0.0F;
  47. auto &visibility_service = Game::Map::VisibilityService::instance();
  48. if (!visibility_service.is_initialized()) {
  49. if (m_minimap_image != m_minimap_base_image) {
  50. m_minimap_image = m_minimap_base_image;
  51. }
  52. return;
  53. }
  54. const auto current_version = visibility_service.version();
  55. if (current_version == m_minimap_fog_version && !m_minimap_image.isNull()) {
  56. return;
  57. }
  58. m_minimap_fog_version = current_version;
  59. const int vis_width = visibility_service.getWidth();
  60. const int vis_height = visibility_service.getHeight();
  61. const auto cells = visibility_service.snapshotCells();
  62. if (cells.empty() || vis_width <= 0 || vis_height <= 0) {
  63. m_minimap_image = m_minimap_base_image;
  64. return;
  65. }
  66. m_minimap_image = m_minimap_base_image.copy();
  67. const int img_width = m_minimap_image.width();
  68. const int img_height = m_minimap_image.height();
  69. constexpr float k_inv_cos = -0.70710678118F;
  70. constexpr float k_inv_sin = 0.70710678118F;
  71. const float scale_x =
  72. static_cast<float>(vis_width) / static_cast<float>(img_width);
  73. const float scale_y =
  74. static_cast<float>(vis_height) / static_cast<float>(img_height);
  75. constexpr int FOG_R = 45;
  76. constexpr int FOG_G = 38;
  77. constexpr int FOG_B = 30;
  78. constexpr int ALPHA_UNSEEN = 180;
  79. constexpr int ALPHA_EXPLORED = 60;
  80. constexpr int ALPHA_VISIBLE = 0;
  81. constexpr float ALPHA_THRESHOLD = 0.5F;
  82. constexpr float ALPHA_SCALE = 1.0F / 255.0F;
  83. auto get_alpha = [&cells, vis_width, ALPHA_VISIBLE, ALPHA_EXPLORED,
  84. ALPHA_UNSEEN](int vx, int vy) -> float {
  85. const size_t idx = static_cast<size_t>(vy * vis_width + vx);
  86. if (idx >= cells.size()) {
  87. return static_cast<float>(ALPHA_UNSEEN);
  88. }
  89. const auto state = static_cast<Game::Map::VisibilityState>(cells[idx]);
  90. switch (state) {
  91. case Game::Map::VisibilityState::Visible:
  92. return static_cast<float>(ALPHA_VISIBLE);
  93. case Game::Map::VisibilityState::Explored:
  94. return static_cast<float>(ALPHA_EXPLORED);
  95. default:
  96. return static_cast<float>(ALPHA_UNSEEN);
  97. }
  98. };
  99. const float half_img_w = static_cast<float>(img_width) * 0.5F;
  100. const float half_img_h = static_cast<float>(img_height) * 0.5F;
  101. const float half_vis_w = static_cast<float>(vis_width) * 0.5F;
  102. const float half_vis_h = static_cast<float>(vis_height) * 0.5F;
  103. for (int y = 0; y < img_height; ++y) {
  104. auto *scanline = reinterpret_cast<QRgb *>(m_minimap_image.scanLine(y));
  105. for (int x = 0; x < img_width; ++x) {
  106. const float centered_x = static_cast<float>(x) - half_img_w;
  107. const float centered_y = static_cast<float>(y) - half_img_h;
  108. const float world_x = centered_x * k_inv_cos - centered_y * k_inv_sin;
  109. const float world_y = centered_x * k_inv_sin + centered_y * k_inv_cos;
  110. const float vis_x = (world_x * scale_x) + half_vis_w;
  111. const float vis_y = (world_y * scale_y) + half_vis_h;
  112. const int vx0 = std::clamp(static_cast<int>(vis_x), 0, vis_width - 1);
  113. const int vx1 = std::clamp(vx0 + 1, 0, vis_width - 1);
  114. const float fx = vis_x - static_cast<float>(vx0);
  115. const int vy0 = std::clamp(static_cast<int>(vis_y), 0, vis_height - 1);
  116. const int vy1 = std::clamp(vy0 + 1, 0, vis_height - 1);
  117. const float fy = vis_y - static_cast<float>(vy0);
  118. const float a00 = get_alpha(vx0, vy0);
  119. const float a10 = get_alpha(vx1, vy0);
  120. const float a01 = get_alpha(vx0, vy1);
  121. const float a11 = get_alpha(vx1, vy1);
  122. const float alpha_top = a00 + (a10 - a00) * fx;
  123. const float alpha_bot = a01 + (a11 - a01) * fx;
  124. const float fog_alpha = alpha_top + (alpha_bot - alpha_top) * fy;
  125. if (fog_alpha > ALPHA_THRESHOLD) {
  126. const QRgb original = scanline[x];
  127. const int orig_r = qRed(original);
  128. const int orig_g = qGreen(original);
  129. const int orig_b = qBlue(original);
  130. const float blend = fog_alpha * ALPHA_SCALE;
  131. const float inv_blend = 1.0F - blend;
  132. const int new_r = static_cast<int>(orig_r * inv_blend + FOG_R * blend);
  133. const int new_g = static_cast<int>(orig_g * inv_blend + FOG_G * blend);
  134. const int new_b = static_cast<int>(orig_b * inv_blend + FOG_B * blend);
  135. scanline[x] = qRgba(new_r, new_g, new_b, 255);
  136. }
  137. }
  138. }
  139. }
  140. void MinimapManager::update_units(
  141. Engine::Core::World *world,
  142. Game::Systems::SelectionSystem *selection_system) {
  143. if (m_minimap_image.isNull() || !m_unit_layer || !world) {
  144. return;
  145. }
  146. std::vector<Game::Map::Minimap::UnitMarker> markers;
  147. constexpr size_t EXPECTED_MAX_UNITS = 128;
  148. markers.reserve(EXPECTED_MAX_UNITS);
  149. std::unordered_set<Engine::Core::EntityID> selected_ids;
  150. if (selection_system) {
  151. const auto &sel = selection_system->get_selected_units();
  152. selected_ids.insert(sel.begin(), sel.end());
  153. }
  154. {
  155. const std::lock_guard<std::recursive_mutex> lock(world->get_entity_mutex());
  156. const auto &entities = world->get_entities();
  157. for (const auto &[entity_id, entity] : entities) {
  158. const auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  159. if (!unit) {
  160. continue;
  161. }
  162. const auto *transform =
  163. entity->get_component<Engine::Core::TransformComponent>();
  164. if (!transform) {
  165. continue;
  166. }
  167. Game::Map::Minimap::UnitMarker marker;
  168. marker.world_x = transform->position.x;
  169. marker.world_z = transform->position.z;
  170. marker.owner_id = unit->owner_id;
  171. marker.is_selected = selected_ids.count(entity_id) > 0;
  172. marker.is_building = Game::Units::is_building_spawn(unit->spawn_type);
  173. markers.push_back(marker);
  174. }
  175. }
  176. m_unit_layer->update(markers);
  177. const QImage &unit_overlay = m_unit_layer->get_image();
  178. if (!unit_overlay.isNull()) {
  179. QPainter painter(&m_minimap_image);
  180. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  181. painter.drawImage(0, 0, unit_overlay);
  182. }
  183. }