CMinimap.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. if (obj)
  42. {
  43. PlayerColor player = obj->getOwner();
  44. if(player == PlayerColor::NEUTRAL)
  45. return graphics->neutralColor;
  46. if (settings["adventure"]["minimapShowHeroes"].Bool() && obj->ID == MapObjectID::HERO)
  47. continue;
  48. if (player.isValidPlayer())
  49. return graphics->playerColors[player.getNum()];
  50. }
  51. }
  52. if (tile->blocked() && !tile->visitable())
  53. return tile->getTerrain()->minimapBlocked;
  54. else
  55. return tile->getTerrain()->minimapUnblocked;
  56. }
  57. void CMinimapInstance::refreshTile(const int3 &tile)
  58. {
  59. if (level == tile.z)
  60. minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
  61. }
  62. void CMinimapInstance::redrawMinimap()
  63. {
  64. int3 mapSizes = GAME->interface()->cb->getMapSize();
  65. for (int y = 0; y < mapSizes.y; ++y)
  66. for (int x = 0; x < mapSizes.x; ++x)
  67. minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
  68. }
  69. CMinimapInstance::CMinimapInstance(const Point & position, const Point & dimensions, int Level):
  70. minimap(new Canvas(Point(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y), CanvasScalingPolicy::IGNORE)),
  71. level(Level)
  72. {
  73. pos += position;
  74. pos.w = dimensions.x;
  75. pos.h = dimensions.y;
  76. redrawMinimap();
  77. }
  78. CMinimapInstance::~CMinimapInstance() = default;
  79. void CMinimapInstance::showAll(Canvas & to)
  80. {
  81. to.drawScaled(*minimap, pos.topLeft(), pos.dimensions());
  82. }
  83. CMinimap::CMinimap(const Rect & position)
  84. : CIntObject(LCLICK | SHOW_POPUP | DRAG | MOVE | GESTURE, position.topLeft())
  85. , heroIcon(ENGINE->renderHandler().loadImage(ImagePath::builtin("minimapIcons/hero"), EImageBlitMode::WITH_SHADOW_AND_FLAG_COLOR))
  86. , level(0)
  87. {
  88. OBJECT_CONSTRUCTION;
  89. double maxSideLengthSrc = std::max(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y);
  90. double maxSideLengthDst = std::max(position.w, position.h);
  91. double resize = maxSideLengthSrc / maxSideLengthDst;
  92. Point newMinimapSize(GAME->interface()->cb->getMapSize().x/ resize, GAME->interface()->cb->getMapSize().y / resize);
  93. Point offset = Point((std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.x) / 2, (std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.y) / 2);
  94. pos.x += offset.x;
  95. pos.y += offset.y;
  96. pos.w = newMinimapSize.x;
  97. pos.h = newMinimapSize.y;
  98. aiShield = std::make_shared<CPicture>(ImagePath::builtin("AIShield"), -offset);
  99. aiShield->disable();
  100. }
  101. int3 CMinimap::pixelToTile(const Point & cursorPos) const
  102. {
  103. // 0 = top-left corner, 1 = bottom-right corner
  104. double dx = static_cast<double>(cursorPos.x) / pos.w;
  105. double dy = static_cast<double>(cursorPos.y) / pos.h;
  106. int3 mapSizes = GAME->interface()->cb->getMapSize();
  107. int tileX(std::round(mapSizes.x * dx));
  108. int tileY(std::round(mapSizes.y * dy));
  109. return int3(tileX, tileY, level);
  110. }
  111. Point CMinimap::tileToPixels(const int3 &tile) const
  112. {
  113. int3 mapSizes = GAME->interface()->cb->getMapSize();
  114. double stepX = static_cast<double>(pos.w) / mapSizes.x;
  115. double stepY = static_cast<double>(pos.h) / mapSizes.y;
  116. int x = static_cast<int>(stepX * (tile.x + 0.5));
  117. int y = static_cast<int>(stepY * (tile.y + 0.5));
  118. return Point(x,y);
  119. }
  120. void CMinimap::moveAdvMapSelection(const Point & positionGlobal)
  121. {
  122. int3 newLocation = pixelToTile(positionGlobal - pos.topLeft());
  123. adventureInt->centerOnTile(newLocation);
  124. if (!(adventureInt->isActive()))
  125. ENGINE->windows().totalRedraw(); //redraw this as well as inactive adventure map
  126. else
  127. redraw();//redraw only this
  128. }
  129. void CMinimap::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  130. {
  131. if (pos.isInside(currentPosition))
  132. moveAdvMapSelection(currentPosition);
  133. }
  134. void CMinimap::clickPressed(const Point & cursorPosition)
  135. {
  136. moveAdvMapSelection(cursorPosition);
  137. }
  138. void CMinimap::showPopupWindow(const Point & cursorPosition)
  139. {
  140. CRClickPopup::createAndPush(LIBRARY->generaltexth->zelp[291].second);
  141. }
  142. void CMinimap::hover(bool on)
  143. {
  144. if(on)
  145. ENGINE->statusbar()->write(LIBRARY->generaltexth->zelp[291].first);
  146. else
  147. ENGINE->statusbar()->clear();
  148. }
  149. void CMinimap::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  150. {
  151. moveAdvMapSelection(cursorPosition);
  152. }
  153. void CMinimap::showAll(Canvas & to)
  154. {
  155. CanvasClipRectGuard guard(to, aiShield->pos);
  156. CIntObject::showAll(to);
  157. if(minimap)
  158. {
  159. int3 mapSizes = GAME->interface()->cb->getMapSize();
  160. Canvas clippedTarget(to, pos);
  161. if (settings["adventure"]["minimapShowHeroes"].Bool())
  162. {
  163. const auto & visibleHeroes = getVisibleHeroes();
  164. for (const auto objectID : visibleHeroes)
  165. {
  166. const auto * object = GAME->interface()->cb->getObj(objectID);
  167. if (object->anchorPos().z != level)
  168. continue;
  169. heroIcon->setOverlayColor(graphics->playerColors[object->getOwner().getNum()]);
  170. clippedTarget.draw(heroIcon, tileToPixels(object->visitablePos()) - heroIcon->dimensions() / 2);
  171. }
  172. }
  173. //draw radar
  174. Rect radar =
  175. {
  176. screenArea.x * pos.w / mapSizes.x,
  177. screenArea.y * pos.h / mapSizes.y,
  178. screenArea.w * pos.w / mapSizes.x - 1,
  179. screenArea.h * pos.h / mapSizes.y - 1
  180. };
  181. clippedTarget.drawBorderDashed(radar, Colors::PURPLE);
  182. }
  183. }
  184. void CMinimap::update()
  185. {
  186. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  187. return;
  188. OBJECT_CONSTRUCTION;
  189. minimap = std::make_shared<CMinimapInstance>(Point(0,0), pos.dimensions(), level);
  190. redraw();
  191. }
  192. void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  193. {
  194. if (screenArea == visibleArea && level == mapLevel)
  195. return;
  196. screenArea = visibleArea;
  197. if(level != mapLevel)
  198. {
  199. level = mapLevel;
  200. update();
  201. }
  202. else
  203. {
  204. setRedrawParent(true); // needed for non square map to redraw black background when viewarea rectangle is moved
  205. redraw();
  206. }
  207. }
  208. void CMinimap::setAIRadar(bool on)
  209. {
  210. if(on)
  211. {
  212. aiShield->enable();
  213. minimap.reset();
  214. }
  215. else
  216. {
  217. aiShield->disable();
  218. update();
  219. }
  220. redraw();
  221. }
  222. std::vector<ObjectInstanceID> CMinimap::getVisibleHeroes()
  223. {
  224. std::vector<ObjectInstanceID> visibleHeroes;
  225. for (const auto & player : PlayerColor::ALL_PLAYERS())
  226. {
  227. if (GAME->interface()->cb->getPlayerStatus(player, false) != EPlayerStatus::INGAME)
  228. continue;
  229. for (const auto & hero : GAME->interface()->cb->getHeroes(player))
  230. visibleHeroes.push_back(hero->id);
  231. }
  232. return visibleHeroes;
  233. }
  234. void CMinimap::updateTiles(const FowTilesType & positions)
  235. {
  236. if(minimap)
  237. {
  238. for (auto const & tile : positions)
  239. minimap->refreshTile(tile);
  240. }
  241. redraw();
  242. }