CMinimap.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * AdventureMapClasses.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 "AdventureMapClasses.h"
  12. #include <SDL_timer.h>
  13. #include "MiscWidgets.h"
  14. #include "CComponent.h"
  15. #include "Images.h"
  16. #include "../CGameInfo.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../mainmenu/CMainMenu.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../gui/SDL_PixelAccess.h"
  22. #include "../gui/CAnimation.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../windows/CAdvmapInterface.h"
  25. #include "../battle/BattleInterfaceClasses.h"
  26. #include "../battle/BattleInterface.h"
  27. #include "../../CCallback.h"
  28. #include "../../lib/StartInfo.h"
  29. #include "../../lib/CGameState.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. #include "../../lib/CHeroHandler.h"
  32. #include "../../lib/CModHandler.h"
  33. #include "../../lib/CTownHandler.h"
  34. #include "../../lib/TerrainHandler.h"
  35. #include "../../lib/mapObjects/CGHeroInstance.h"
  36. #include "../../lib/mapping/CMap.h"
  37. #include "ClientCommandManager.h"
  38. #include <SDL_surface.h>
  39. #include <SDL_keyboard.h>
  40. #include <SDL_events.h>
  41. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  42. {
  43. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  44. // if tile is not visible it will be black on minimap
  45. if(!tile)
  46. return Colors::BLACK;
  47. // if object at tile is owned - it will be colored as its owner
  48. for (const CGObjectInstance *obj : tile->blockingObjects)
  49. {
  50. //heroes will be blitted later
  51. switch (obj->ID)
  52. {
  53. case Obj::HERO:
  54. case Obj::PRISON:
  55. continue;
  56. }
  57. PlayerColor player = obj->getOwner();
  58. if(player == PlayerColor::NEUTRAL)
  59. return *graphics->neutralColor;
  60. else
  61. if (player < PlayerColor::PLAYER_LIMIT)
  62. return graphics->playerColors[player.getNum()];
  63. }
  64. // else - use terrain color (blocked version or normal)
  65. const auto & colorPair = parent->colors.find(tile->terType->getId())->second;
  66. if (tile->blocked && (!tile->visitable))
  67. return colorPair.second;
  68. else
  69. return colorPair.first;
  70. }
  71. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  72. {
  73. int3 mapSizes = LOCPLINT->cb->getMapSize();
  74. double stepX = double(pos.w) / mapSizes.x;
  75. double stepY = double(pos.h) / mapSizes.y;
  76. x = static_cast<int>(toX + stepX * tile.x);
  77. y = static_cast<int>(toY + stepY * tile.y);
  78. }
  79. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  80. {
  81. //coordinates of rectangle on minimap representing this tile
  82. // begin - first to blit, end - first NOT to blit
  83. int xBegin, yBegin, xEnd, yEnd;
  84. tileToPixels (tile, xBegin, yBegin, toX, toY);
  85. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  86. for (int y=yBegin; y<yEnd; y++)
  87. {
  88. uint8_t *ptr = (uint8_t*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  89. for (int x=xBegin; x<xEnd; x++)
  90. ColorPutter<4, 1>::PutColor(ptr, color);
  91. }
  92. }
  93. void CMinimapInstance::refreshTile(const int3 &tile)
  94. {
  95. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  96. }
  97. void CMinimapInstance::drawScaled(int level)
  98. {
  99. int3 mapSizes = LOCPLINT->cb->getMapSize();
  100. //size of one map tile on our minimap
  101. double stepX = double(pos.w) / mapSizes.x;
  102. double stepY = double(pos.h) / mapSizes.y;
  103. double currY = 0;
  104. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  105. {
  106. double currX = 0;
  107. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  108. {
  109. const SDL_Color &color = getTileColor(int3(x,y,level));
  110. //coordinates of rectangle on minimap representing this tile
  111. // begin - first to blit, end - first NOT to blit
  112. int xBegin = static_cast<int>(currX);
  113. int yBegin = static_cast<int>(currY);
  114. int xEnd = static_cast<int>(currX + stepX);
  115. int yEnd = static_cast<int>(currY + stepY);
  116. for (int y=yBegin; y<yEnd; y++)
  117. {
  118. uint8_t *ptr = (uint8_t*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  119. for (int x=xBegin; x<xEnd; x++)
  120. ColorPutter<4, 1>::PutColor(ptr, color);
  121. }
  122. }
  123. }
  124. }
  125. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  126. parent(Parent),
  127. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  128. level(Level)
  129. {
  130. pos.w = parent->pos.w;
  131. pos.h = parent->pos.h;
  132. drawScaled(level);
  133. }
  134. CMinimapInstance::~CMinimapInstance()
  135. {
  136. SDL_FreeSurface(minimap);
  137. }
  138. void CMinimapInstance::showAll(SDL_Surface * to)
  139. {
  140. blitAtLoc(minimap, 0, 0, to);
  141. //draw heroes
  142. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  143. for(auto & hero : heroes)
  144. {
  145. int3 position = hero->visitablePos();
  146. if(position.z == level)
  147. {
  148. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  149. blitTileWithColor(color, position, to, pos.x, pos.y);
  150. }
  151. }
  152. }
  153. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
  154. {
  155. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > ret;
  156. for(const auto & terrain : CGI->terrainTypeHandler->objects)
  157. {
  158. SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked);
  159. SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked);
  160. ret[terrain->getId()] = std::make_pair(normal, blocked);
  161. }
  162. return ret;
  163. }
  164. CMinimap::CMinimap(const Rect & position)
  165. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  166. level(0),
  167. colors(loadColors())
  168. {
  169. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  170. pos.w = position.w;
  171. pos.h = position.h;
  172. aiShield = std::make_shared<CPicture>("AIShield");
  173. aiShield->disable();
  174. }
  175. int3 CMinimap::translateMousePosition()
  176. {
  177. // 0 = top-left corner, 1 = bottom-right corner
  178. double dx = double(GH.getCursorPosition().x - pos.x) / pos.w;
  179. double dy = double(GH.getCursorPosition().y - pos.y) / pos.h;
  180. int3 mapSizes = LOCPLINT->cb->getMapSize();
  181. int3 tile ((si32)(mapSizes.x * dx), (si32)(mapSizes.y * dy), level);
  182. return tile;
  183. }
  184. void CMinimap::moveAdvMapSelection()
  185. {
  186. int3 newLocation = translateMousePosition();
  187. adventureInt->centerOn(newLocation);
  188. if (!(adventureInt->active & GENERAL))
  189. GH.totalRedraw(); //redraw this as well as inactive adventure map
  190. else
  191. redraw();//redraw only this
  192. }
  193. void CMinimap::clickLeft(tribool down, bool previousState)
  194. {
  195. if(down)
  196. moveAdvMapSelection();
  197. }
  198. void CMinimap::clickRight(tribool down, bool previousState)
  199. {
  200. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  201. }
  202. void CMinimap::hover(bool on)
  203. {
  204. if(on)
  205. GH.statusbar->write(CGI->generaltexth->zelp[291].first);
  206. else
  207. GH.statusbar->clear();
  208. }
  209. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  210. {
  211. if(mouseState(EIntObjMouseBtnType::LEFT))
  212. moveAdvMapSelection();
  213. }
  214. void CMinimap::showAll(SDL_Surface * to)
  215. {
  216. CIntObject::showAll(to);
  217. if(minimap)
  218. {
  219. int3 mapSizes = LOCPLINT->cb->getMapSize();
  220. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  221. //draw radar
  222. Rect oldClip;
  223. Rect radar =
  224. {
  225. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  226. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  227. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  228. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  229. };
  230. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  231. {
  232. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  233. radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  234. radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  235. if(radar.x < pos.x && radar.y < pos.y)
  236. return; // whole map is visible at once, no point in redrawing border
  237. }
  238. CSDL_Ext::getClipRect(to, oldClip);
  239. CSDL_Ext::setClipRect(to, pos);
  240. CSDL_Ext::drawDashedBorder(to, radar, Colors::PURPLE);
  241. CSDL_Ext::setClipRect(to, oldClip);
  242. }
  243. }
  244. void CMinimap::update()
  245. {
  246. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  247. return;
  248. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  249. minimap = std::make_shared<CMinimapInstance>(this, level);
  250. redraw();
  251. }
  252. void CMinimap::setLevel(int newLevel)
  253. {
  254. level = newLevel;
  255. update();
  256. }
  257. void CMinimap::setAIRadar(bool on)
  258. {
  259. if(on)
  260. {
  261. aiShield->enable();
  262. minimap.reset();
  263. }
  264. else
  265. {
  266. aiShield->disable();
  267. update();
  268. }
  269. // this my happen during AI turn when this interface is inactive
  270. // force redraw in order to properly update interface
  271. GH.totalRedraw();
  272. }
  273. void CMinimap::hideTile(const int3 &pos)
  274. {
  275. if(minimap)
  276. minimap->refreshTile(pos);
  277. }
  278. void CMinimap::showTile(const int3 &pos)
  279. {
  280. if(minimap)
  281. minimap->refreshTile(pos);
  282. }