CMinimap.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * CMinimap.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CMinimap.h"
  12. #include "AdventureMapInterface.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../gui/MouseButton.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../render/CAnimation.h"
  19. #include "../render/Canvas.h"
  20. #include "../render/Colors.h"
  21. #include "../render/Graphics.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../widgets/Images.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/TerrainHandler.h"
  27. #include "../../lib/callback/CCallback.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapping/TerrainTile.h"
  30. #include "../../lib/texts/CGeneralTextHandler.h"
  31. ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
  32. {
  33. const TerrainTile * tile = GAME->interface()->cb->getTile(pos, false);
  34. // if tile is not visible it will be black on minimap
  35. if(!tile)
  36. return Colors::BLACK;
  37. // if object at tile is owned - it will be colored as its owner
  38. for (const ObjectInstanceID objectID : tile->blockingObjects)
  39. {
  40. const auto * obj = GAME->interface()->cb->getObj(objectID);
  41. PlayerColor player = obj->getOwner();
  42. if(player == PlayerColor::NEUTRAL)
  43. return graphics->neutralColor;
  44. if (settings["adventure"]["minimapShowHeroes"].Bool() && obj->ID == MapObjectID::HERO)
  45. continue;
  46. if (player.isValidPlayer())
  47. return graphics->playerColors[player.getNum()];
  48. }
  49. if (tile->blocked() && !tile->visitable())
  50. return tile->getTerrain()->minimapBlocked;
  51. else
  52. return tile->getTerrain()->minimapUnblocked;
  53. }
  54. void CMinimapInstance::refreshTile(const int3 &tile)
  55. {
  56. if (level == tile.z)
  57. minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
  58. }
  59. void CMinimapInstance::redrawMinimap()
  60. {
  61. int3 mapSizes = GAME->interface()->cb->getMapSize();
  62. for (int y = 0; y < mapSizes.y; ++y)
  63. for (int x = 0; x < mapSizes.x; ++x)
  64. minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
  65. }
  66. CMinimapInstance::CMinimapInstance(const Point & position, const Point & dimensions, int Level):
  67. minimap(new Canvas(Point(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y), CanvasScalingPolicy::IGNORE)),
  68. level(Level)
  69. {
  70. pos += position;
  71. pos.w = dimensions.x;
  72. pos.h = dimensions.y;
  73. redrawMinimap();
  74. }
  75. CMinimapInstance::~CMinimapInstance() = default;
  76. void CMinimapInstance::showAll(Canvas & to)
  77. {
  78. to.drawScaled(*minimap, pos.topLeft(), pos.dimensions());
  79. }
  80. CMinimap::CMinimap(const Rect & position)
  81. : CIntObject(LCLICK | SHOW_POPUP | DRAG | MOVE | GESTURE, position.topLeft())
  82. , heroIcon(ENGINE->renderHandler().loadImage(ImagePath::builtin("minimapIcons/hero"), EImageBlitMode::WITH_SHADOW_AND_FLAG_COLOR))
  83. , level(0)
  84. {
  85. OBJECT_CONSTRUCTION;
  86. double maxSideLengthSrc = std::max(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y);
  87. double maxSideLengthDst = std::max(position.w, position.h);
  88. double resize = maxSideLengthSrc / maxSideLengthDst;
  89. Point newMinimapSize(GAME->interface()->cb->getMapSize().x/ resize, GAME->interface()->cb->getMapSize().y / resize);
  90. Point offset = Point((std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.x) / 2, (std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.y) / 2);
  91. pos.x += offset.x;
  92. pos.y += offset.y;
  93. pos.w = newMinimapSize.x;
  94. pos.h = newMinimapSize.y;
  95. aiShield = std::make_shared<CPicture>(ImagePath::builtin("AIShield"), -offset);
  96. aiShield->disable();
  97. }
  98. int3 CMinimap::pixelToTile(const Point & cursorPos) const
  99. {
  100. // 0 = top-left corner, 1 = bottom-right corner
  101. double dx = static_cast<double>(cursorPos.x) / pos.w;
  102. double dy = static_cast<double>(cursorPos.y) / pos.h;
  103. int3 mapSizes = GAME->interface()->cb->getMapSize();
  104. int tileX(std::round(mapSizes.x * dx));
  105. int tileY(std::round(mapSizes.y * dy));
  106. return int3(tileX, tileY, level);
  107. }
  108. Point CMinimap::tileToPixels(const int3 &tile) const
  109. {
  110. int3 mapSizes = GAME->interface()->cb->getMapSize();
  111. double stepX = static_cast<double>(pos.w) / mapSizes.x;
  112. double stepY = static_cast<double>(pos.h) / mapSizes.y;
  113. int x = static_cast<int>(stepX * (tile.x + 0.5));
  114. int y = static_cast<int>(stepY * (tile.y + 0.5));
  115. return Point(x,y);
  116. }
  117. void CMinimap::moveAdvMapSelection(const Point & positionGlobal)
  118. {
  119. int3 newLocation = pixelToTile(positionGlobal - pos.topLeft());
  120. adventureInt->centerOnTile(newLocation);
  121. if (!(adventureInt->isActive()))
  122. ENGINE->windows().totalRedraw(); //redraw this as well as inactive adventure map
  123. else
  124. redraw();//redraw only this
  125. }
  126. void CMinimap::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  127. {
  128. if (pos.isInside(currentPosition))
  129. moveAdvMapSelection(currentPosition);
  130. }
  131. void CMinimap::clickPressed(const Point & cursorPosition)
  132. {
  133. moveAdvMapSelection(cursorPosition);
  134. }
  135. void CMinimap::showPopupWindow(const Point & cursorPosition)
  136. {
  137. CRClickPopup::createAndPush(LIBRARY->generaltexth->zelp[291].second);
  138. }
  139. void CMinimap::hover(bool on)
  140. {
  141. if(on)
  142. ENGINE->statusbar()->write(LIBRARY->generaltexth->zelp[291].first);
  143. else
  144. ENGINE->statusbar()->clear();
  145. }
  146. void CMinimap::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  147. {
  148. moveAdvMapSelection(cursorPosition);
  149. }
  150. void CMinimap::showAll(Canvas & to)
  151. {
  152. CanvasClipRectGuard guard(to, aiShield->pos);
  153. CIntObject::showAll(to);
  154. if(minimap)
  155. {
  156. int3 mapSizes = GAME->interface()->cb->getMapSize();
  157. Canvas clippedTarget(to, pos);
  158. if (settings["adventure"]["minimapShowHeroes"].Bool())
  159. {
  160. for (const auto objectID : visibleHeroes)
  161. {
  162. const auto * object = GAME->interface()->cb->getObj(objectID);
  163. if (object->anchorPos().z != level)
  164. continue;
  165. heroIcon->setOverlayColor(graphics->playerColors[object->getOwner().getNum()]);
  166. clippedTarget.draw(heroIcon, tileToPixels(object->visitablePos()) - heroIcon->dimensions() / 2);
  167. }
  168. }
  169. //draw radar
  170. Rect radar =
  171. {
  172. screenArea.x * pos.w / mapSizes.x,
  173. screenArea.y * pos.h / mapSizes.y,
  174. screenArea.w * pos.w / mapSizes.x - 1,
  175. screenArea.h * pos.h / mapSizes.y - 1
  176. };
  177. clippedTarget.drawBorderDashed(radar, Colors::PURPLE);
  178. }
  179. }
  180. void CMinimap::update()
  181. {
  182. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  183. return;
  184. OBJECT_CONSTRUCTION;
  185. minimap = std::make_shared<CMinimapInstance>(Point(0,0), pos.dimensions(), level);
  186. updateVisibleHeroes();
  187. redraw();
  188. }
  189. void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  190. {
  191. if (screenArea == visibleArea && level == mapLevel)
  192. return;
  193. screenArea = visibleArea;
  194. if(level != mapLevel)
  195. {
  196. level = mapLevel;
  197. update();
  198. }
  199. else
  200. redraw();
  201. }
  202. void CMinimap::setAIRadar(bool on)
  203. {
  204. if(on)
  205. {
  206. aiShield->enable();
  207. minimap.reset();
  208. }
  209. else
  210. {
  211. aiShield->disable();
  212. update();
  213. }
  214. redraw();
  215. }
  216. void CMinimap::updateVisibleHeroes()
  217. {
  218. visibleHeroes.clear();
  219. for (const auto & player : PlayerColor::ALL_PLAYERS())
  220. {
  221. if (GAME->interface()->cb->getPlayerStatus(player, false) != EPlayerStatus::INGAME)
  222. continue;
  223. for (const auto & hero : GAME->interface()->cb->getHeroes(player))
  224. visibleHeroes.push_back(hero->id);
  225. }
  226. }
  227. void CMinimap::updateTiles(const std::unordered_set<int3> & positions)
  228. {
  229. if(minimap)
  230. {
  231. for (auto const & tile : positions)
  232. minimap->refreshTile(tile);
  233. }
  234. updateVisibleHeroes();
  235. redraw();
  236. }