MapRendererContext.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * MapRendererContext.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 "MapRendererContext.h"
  12. #include "mapHandler.h"
  13. #include "../CGameInfo.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../adventureMap/CAdvMapInt.h"
  16. #include "../../CCallback.h"
  17. #include "../../lib/mapObjects/CGHeroInstance.h"
  18. #include "../../lib/mapping/CMap.h"
  19. MapObjectsSorter::MapObjectsSorter(IMapRendererContext & context)
  20. : context(context)
  21. {
  22. }
  23. bool MapObjectsSorter::operator()(const ObjectInstanceID & left, const ObjectInstanceID & right) const
  24. {
  25. return (*this)(context.getObject(left), context.getObject(right));
  26. }
  27. bool MapObjectsSorter::operator()(const CGObjectInstance * left, const CGObjectInstance * right) const
  28. {
  29. //FIXME: remove mh access
  30. return CGI->mh->compareObjectBlitOrder(left, right);
  31. }
  32. int3 MapRendererContext::getMapSize() const
  33. {
  34. return LOCPLINT->cb->getMapSize();
  35. }
  36. bool MapRendererContext::isInMap(const int3 & coordinates) const
  37. {
  38. return LOCPLINT->cb->isInTheMap(coordinates);
  39. }
  40. const TerrainTile & MapRendererContext::getMapTile(const int3 & coordinates) const
  41. {
  42. return CGI->mh->getMap()->getTile(coordinates);
  43. }
  44. const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID) const
  45. {
  46. return CGI->mh->getMap()->objects.at(objectID.getNum());
  47. }
  48. bool MapRendererContext::isVisible(const int3 & coordinates) const
  49. {
  50. if (settingsSessionSpectate || showAllTerrain)
  51. return LOCPLINT->cb->isInTheMap(coordinates);
  52. return LOCPLINT->cb->isVisible(coordinates);
  53. }
  54. const CGPath * MapRendererContext::currentPath() const
  55. {
  56. if (worldViewModeActive)
  57. return nullptr;
  58. const auto * hero = adventureInt->curHero();
  59. if(!hero)
  60. return nullptr;
  61. if(!LOCPLINT->paths.hasPath(hero))
  62. return nullptr;
  63. return &LOCPLINT->paths.getPath(hero);
  64. }
  65. size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  66. {
  67. assert(groupSize > 0);
  68. if(groupSize == 0)
  69. return 0;
  70. if (!settingsAdventureObjectAnimation)
  71. return 0;
  72. if (worldViewModeActive)
  73. return 0;
  74. // H3 timing for adventure map objects animation is 180 ms
  75. // Terrain animations also use identical interval, however those are only present in HotA and/or HD Mod
  76. size_t baseFrameTime = 180;
  77. // hero movement animation always plays at ~50ms / frame
  78. // in-game setting only affect movement across screen
  79. if(movementAnimation && movementAnimation->target == objectID)
  80. baseFrameTime = 50;
  81. size_t frameCounter = animationTime / baseFrameTime;
  82. size_t frameIndex = frameCounter % groupSize;
  83. return frameIndex;
  84. }
  85. size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
  86. {
  87. if (!settingsAdventureTerrainAnimation)
  88. return 0;
  89. if (worldViewModeActive)
  90. return 0;
  91. size_t baseFrameTime = 180;
  92. size_t frameCounter = animationTime / baseFrameTime;
  93. size_t frameIndex = frameCounter % groupSize;
  94. return frameIndex;
  95. }
  96. bool MapRendererContext::tileAnimated(const int3 & coordinates) const
  97. {
  98. if (!isInMap(coordinates))
  99. return false;
  100. if(movementAnimation)
  101. {
  102. auto objects = getObjects(coordinates);
  103. if(vstd::contains(objects, movementAnimation->target))
  104. return true;
  105. }
  106. if(fadeInAnimation)
  107. {
  108. auto objects = getObjects(coordinates);
  109. if(vstd::contains(objects, fadeInAnimation->target))
  110. return true;
  111. }
  112. if(fadeOutAnimation)
  113. {
  114. auto objects = getObjects(coordinates);
  115. if(vstd::contains(objects, fadeOutAnimation->target))
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool MapRendererContext::filterGrayscale() const
  121. {
  122. return false;
  123. }
  124. bool MapRendererContext::showRoads() const
  125. {
  126. return true;
  127. }
  128. bool MapRendererContext::showRivers() const
  129. {
  130. return true;
  131. }
  132. bool MapRendererContext::showBorder() const
  133. {
  134. return !worldViewModeActive;
  135. }
  136. bool MapRendererContext::showOverlay() const
  137. {
  138. return worldViewModeActive;
  139. }
  140. bool MapRendererContext::showGrid() const
  141. {
  142. return settingsSessionShowGrid;
  143. }
  144. bool MapRendererContext::showVisitable() const
  145. {
  146. return settingsSessionShowVisitable;
  147. }
  148. bool MapRendererContext::showBlockable() const
  149. {
  150. return settingsSessionShowBlockable;
  151. }
  152. MapRendererContext::MapRendererContext()
  153. {
  154. auto mapSize = getMapSize();
  155. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  156. for(const auto & obj : CGI->mh->getMap()->objects)
  157. addObject(obj);
  158. }
  159. void MapRendererContext::addObject(const CGObjectInstance * obj)
  160. {
  161. if(!obj)
  162. return;
  163. for(int fx = 0; fx < obj->getWidth(); ++fx)
  164. {
  165. for(int fy = 0; fy < obj->getHeight(); ++fy)
  166. {
  167. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  168. if(isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  169. {
  170. auto & container = objects[currTile.z][currTile.x][currTile.y];
  171. container.push_back(obj->id);
  172. boost::range::sort(container, MapObjectsSorter(*this));
  173. }
  174. }
  175. }
  176. }
  177. void MapRendererContext::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  178. {
  179. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  180. int xDest = std::max(tileFrom.x, tileDest.x);
  181. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  182. int yDest = std::max(tileFrom.y, tileDest.y);
  183. for(int x = xFrom; x <= xDest; ++x)
  184. {
  185. for(int y = yFrom; y <= yDest; ++y)
  186. {
  187. int3 currTile(x, y, object->pos.z);
  188. if(isInMap(currTile))
  189. {
  190. auto & container = objects[currTile.z][currTile.x][currTile.y];
  191. container.push_back(object->id);
  192. boost::range::sort(container, MapObjectsSorter(*this));
  193. }
  194. }
  195. }
  196. }
  197. void MapRendererContext::removeObject(const CGObjectInstance * object)
  198. {
  199. for(int z = 0; z < getMapSize().z; z++)
  200. for(int x = 0; x < getMapSize().x; x++)
  201. for(int y = 0; y < getMapSize().y; y++)
  202. vstd::erase(objects[z][x][y], object->id);
  203. }
  204. const MapRendererContext::MapObjectsList & MapRendererContext::getObjects(const int3 & coordinates) const
  205. {
  206. assert(isInMap(coordinates));
  207. return objects[coordinates.z][coordinates.x][coordinates.y];
  208. }
  209. size_t MapRendererContext::objectGroupIndex(ObjectInstanceID objectID) const
  210. {
  211. const CGObjectInstance * obj = getObject(objectID);
  212. // TODO
  213. static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
  214. static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
  215. if(obj->ID == Obj::HERO)
  216. {
  217. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  218. if(movementAnimation && movementAnimation->target == objectID)
  219. return moveGroups[hero->moveDir];
  220. return idleGroups[hero->moveDir];
  221. }
  222. if(obj->ID == Obj::BOAT)
  223. {
  224. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  225. uint8_t direction = boat->hero ? boat->hero->moveDir : boat->direction;
  226. if(movementAnimation && movementAnimation->target == objectID)
  227. return moveGroups[direction];
  228. return idleGroups[direction];
  229. }
  230. return 0;
  231. }
  232. Point MapRendererContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  233. {
  234. if(movementAnimation && movementAnimation->target == objectID)
  235. {
  236. int3 offsetTilesFrom = movementAnimation->tileFrom - coordinates;
  237. int3 offsetTilesDest = movementAnimation->tileDest - coordinates;
  238. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
  239. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
  240. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, movementAnimation->progress);
  241. return result;
  242. }
  243. const CGObjectInstance * object = getObject(objectID);
  244. int3 offsetTiles(object->getPosition() - coordinates);
  245. return Point(offsetTiles) * Point(32, 32);
  246. }
  247. double MapRendererContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  248. {
  249. const CGObjectInstance * object = getObject(objectID);
  250. if(object->ID == Obj::HERO)
  251. {
  252. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  253. if(hero->inTownGarrison)
  254. return 0;
  255. if(hero->boat)
  256. return 0;
  257. }
  258. if(showAllTerrain)
  259. {
  260. if(object->isVisitable() && !LOCPLINT->cb->isVisible(coordinates))
  261. return 0;
  262. }
  263. if(fadeOutAnimation && objectID == fadeOutAnimation->target)
  264. return 1.0 - fadeOutAnimation->progress;
  265. if(fadeInAnimation && objectID == fadeInAnimation->target)
  266. return fadeInAnimation->progress;
  267. return 1.0;
  268. }
  269. size_t MapRendererContext::selectOverlayImageForObject(const ObjectPosInfo & object) const
  270. {
  271. size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  272. if(object.owner.isValidPlayer())
  273. ownerIndex = object.owner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  274. switch(object.id)
  275. {
  276. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  277. case Obj::MONOLITH_ONE_WAY_EXIT:
  278. case Obj::MONOLITH_TWO_WAY:
  279. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT);
  280. case Obj::SUBTERRANEAN_GATE:
  281. return ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE);
  282. case Obj::ARTIFACT:
  283. return ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT);
  284. case Obj::TOWN:
  285. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN);
  286. case Obj::HERO:
  287. return ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO);
  288. case Obj::MINE:
  289. return ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object.subId;
  290. case Obj::RESOURCE:
  291. return ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object.subId;
  292. }
  293. return std::numeric_limits<size_t>::max();
  294. }
  295. size_t MapRendererContext::overlayImageIndex(const int3 & coordinates) const
  296. {
  297. for(const auto & entry : additionalOverlayIcons)
  298. {
  299. if(entry.pos != coordinates)
  300. continue;
  301. size_t iconIndex = selectOverlayImageForObject(entry);
  302. if(iconIndex != std::numeric_limits<size_t>::max())
  303. return iconIndex;
  304. }
  305. if(!isVisible(coordinates))
  306. return std::numeric_limits<size_t>::max();
  307. for(const auto & objectID : getObjects(coordinates))
  308. {
  309. const auto * object = getObject(objectID);
  310. if(!object->visitableAt(coordinates.x, coordinates.y))
  311. continue;
  312. ObjectPosInfo info;
  313. info.pos = coordinates;
  314. info.id = object->ID;
  315. info.subId = object->subID;
  316. info.owner = object->tempOwner;
  317. size_t iconIndex = selectOverlayImageForObject(info);
  318. if(iconIndex != std::numeric_limits<size_t>::max())
  319. return iconIndex;
  320. }
  321. return std::numeric_limits<size_t>::max();
  322. }