MapRendererContext.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool();
  58. }
  59. const CGPath * MapRendererContext::currentPath() const
  60. {
  61. const auto * hero = adventureInt->curHero();
  62. if(!hero)
  63. return nullptr;
  64. if(!LOCPLINT->paths.hasPath(hero))
  65. return nullptr;
  66. return &LOCPLINT->paths.getPath(hero);
  67. }
  68. size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  69. {
  70. assert(groupSize > 0);
  71. if(groupSize == 0)
  72. return 0;
  73. // H3 timing for adventure map objects animation is 180 ms
  74. // Terrain animations also use identical interval, however those are only present in HotA and/or HD Mod
  75. size_t baseFrameTime = 180;
  76. // hero movement animation always plays at ~50ms / frame
  77. // in-game setting only affect movement across screen
  78. if(movementAnimation && movementAnimation->target == objectID)
  79. baseFrameTime = 50;
  80. size_t frameCounter = animationTime / baseFrameTime;
  81. size_t frameIndex = frameCounter % groupSize;
  82. return frameIndex;
  83. }
  84. size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
  85. {
  86. size_t baseFrameTime = 180;
  87. size_t frameCounter = animationTime / baseFrameTime;
  88. size_t frameIndex = frameCounter % groupSize;
  89. return frameIndex;
  90. }
  91. //Point MapRendererContext::getTileSize() const
  92. //{
  93. // return Point(32, 32);
  94. //}
  95. bool MapRendererContext::showOverlay() const
  96. {
  97. return worldViewModeActive;
  98. }
  99. bool MapRendererContext::showGrid() const
  100. {
  101. return settings["gameTweaks"]["showGrid"].Bool();
  102. }
  103. bool MapRendererContext::showVisitable() const
  104. {
  105. return settings["session"]["showVisitable"].Bool();
  106. }
  107. bool MapRendererContext::showBlockable() const
  108. {
  109. return settings["session"]["showBlockable"].Bool();
  110. }
  111. MapRendererContext::MapRendererContext()
  112. {
  113. auto mapSize = getMapSize();
  114. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  115. for(const auto & obj : CGI->mh->getMap()->objects)
  116. addObject(obj);
  117. }
  118. void MapRendererContext::addObject(const CGObjectInstance * obj)
  119. {
  120. if(!obj)
  121. return;
  122. for(int fx = 0; fx < obj->getWidth(); ++fx)
  123. {
  124. for(int fy = 0; fy < obj->getHeight(); ++fy)
  125. {
  126. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  127. if(isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  128. {
  129. auto & container = objects[currTile.z][currTile.x][currTile.y];
  130. container.push_back(obj->id);
  131. boost::range::sort(container, MapObjectsSorter(*this));
  132. }
  133. }
  134. }
  135. }
  136. void MapRendererContext::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  137. {
  138. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  139. int xDest = std::max(tileFrom.x, tileDest.x);
  140. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  141. int yDest = std::max(tileFrom.y, tileDest.y);
  142. for(int x = xFrom; x <= xDest; ++x)
  143. {
  144. for(int y = yFrom; y <= yDest; ++y)
  145. {
  146. int3 currTile(x, y, object->pos.z);
  147. if(isInMap(currTile))
  148. {
  149. auto & container = objects[currTile.z][currTile.x][currTile.y];
  150. container.push_back(object->id);
  151. boost::range::sort(container, MapObjectsSorter(*this));
  152. }
  153. }
  154. }
  155. }
  156. void MapRendererContext::removeObject(const CGObjectInstance * object)
  157. {
  158. for(int z = 0; z < getMapSize().z; z++)
  159. for(int x = 0; x < getMapSize().x; x++)
  160. for(int y = 0; y < getMapSize().y; y++)
  161. vstd::erase(objects[z][x][y], object->id);
  162. }
  163. const MapRendererContext::MapObjectsList & MapRendererContext::getObjects(const int3 & coordinates) const
  164. {
  165. assert(isInMap(coordinates));
  166. return objects[coordinates.z][coordinates.x][coordinates.y];
  167. }
  168. size_t MapRendererContext::objectGroupIndex(ObjectInstanceID objectID) const
  169. {
  170. const CGObjectInstance * obj = getObject(objectID);
  171. // TODO
  172. static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
  173. static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
  174. if(obj->ID == Obj::HERO)
  175. {
  176. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  177. if(movementAnimation && movementAnimation->target == objectID)
  178. return moveGroups[hero->moveDir];
  179. return idleGroups[hero->moveDir];
  180. }
  181. if(obj->ID == Obj::BOAT)
  182. {
  183. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  184. uint8_t direction = boat->hero ? boat->hero->moveDir : boat->direction;
  185. if(movementAnimation && movementAnimation->target == objectID)
  186. return moveGroups[direction];
  187. return idleGroups[direction];
  188. }
  189. return 0;
  190. }
  191. Point MapRendererContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  192. {
  193. if(movementAnimation && movementAnimation->target == objectID)
  194. {
  195. int3 offsetTilesFrom = movementAnimation->tileFrom - coordinates;
  196. int3 offsetTilesDest = movementAnimation->tileDest - coordinates;
  197. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
  198. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
  199. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, movementAnimation->progress);
  200. return result;
  201. }
  202. const CGObjectInstance * object = getObject(objectID);
  203. int3 offsetTiles(object->getPosition() - coordinates);
  204. return Point(offsetTiles) * Point(32, 32);
  205. }
  206. double MapRendererContext::objectTransparency(ObjectInstanceID objectID) const
  207. {
  208. const CGObjectInstance * object = getObject(objectID);
  209. if(object && object->ID == Obj::HERO)
  210. {
  211. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  212. if(hero->inTownGarrison)
  213. return 0;
  214. if(hero->boat)
  215. return 0;
  216. }
  217. if(fadeOutAnimation && objectID == fadeOutAnimation->target)
  218. return 1.0 - fadeOutAnimation->progress;
  219. if(fadeInAnimation && objectID == fadeInAnimation->target)
  220. return fadeInAnimation->progress;
  221. return 1.0;
  222. }