MapRendererContext.cpp 9.8 KB

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