MapRendererContext.cpp 10 KB

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