MapRendererContext.cpp 9.9 KB

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