MapRendererContext.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * MapRendererContextState.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 "MapRendererContextState.h"
  13. #include "mapHandler.h"
  14. #include "../../CCallback.h"
  15. #include "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../adventureMap/CAdvMapInt.h"
  18. #include "../../lib/Point.h"
  19. #include "../../lib/mapObjects/CGHeroInstance.h"
  20. #include "../../lib/mapping/CMap.h"
  21. #include "../../lib/CPathfinder.h"
  22. MapRendererBaseContext::MapRendererBaseContext(const MapRendererContextState & viewState)
  23. : viewState(viewState)
  24. {
  25. }
  26. uint32_t MapRendererBaseContext::getObjectRotation(ObjectInstanceID objectID) const
  27. {
  28. const CGObjectInstance * obj = getObject(objectID);
  29. if(obj->ID == Obj::HERO)
  30. {
  31. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  32. return hero->moveDir;
  33. }
  34. if(obj->ID == Obj::BOAT)
  35. {
  36. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  37. if(boat->hero)
  38. return boat->hero->moveDir;
  39. return boat->direction;
  40. }
  41. return 0;
  42. }
  43. int3 MapRendererBaseContext::getMapSize() const
  44. {
  45. return LOCPLINT->cb->getMapSize();
  46. }
  47. bool MapRendererBaseContext::isInMap(const int3 & coordinates) const
  48. {
  49. return LOCPLINT->cb->isInTheMap(coordinates);
  50. }
  51. bool MapRendererBaseContext::isVisible(const int3 & coordinates) const
  52. {
  53. if(settingsSessionSpectate)
  54. return LOCPLINT->cb->isInTheMap(coordinates);
  55. else
  56. return LOCPLINT->cb->isVisible(coordinates);
  57. }
  58. bool MapRendererBaseContext::tileAnimated(const int3 & coordinates) const
  59. {
  60. return false;
  61. }
  62. const TerrainTile & MapRendererBaseContext::getMapTile(const int3 & coordinates) const
  63. {
  64. return CGI->mh->getMap()->getTile(coordinates);
  65. }
  66. const MapRendererBaseContext::MapObjectsList & MapRendererBaseContext::getObjects(const int3 & coordinates) const
  67. {
  68. assert(isInMap(coordinates));
  69. return viewState.objects[coordinates.z][coordinates.x][coordinates.y];
  70. }
  71. const CGObjectInstance * MapRendererBaseContext::getObject(ObjectInstanceID objectID) const
  72. {
  73. return CGI->mh->getMap()->objects.at(objectID.getNum());
  74. }
  75. const CGPath * MapRendererBaseContext::currentPath() const
  76. {
  77. return nullptr;
  78. }
  79. size_t MapRendererBaseContext::objectGroupIndex(ObjectInstanceID objectID) const
  80. {
  81. static const std::vector<size_t> idleGroups = {0, 13, 0, 1, 2, 3, 4, 15, 14};
  82. return idleGroups[getObjectRotation(objectID)];
  83. }
  84. Point MapRendererBaseContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  85. {
  86. const CGObjectInstance * object = getObject(objectID);
  87. int3 offsetTiles(object->getPosition() - coordinates);
  88. return Point(offsetTiles) * Point(32, 32);
  89. }
  90. double MapRendererBaseContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  91. {
  92. const CGObjectInstance * object = getObject(objectID);
  93. if(object->ID == Obj::HERO)
  94. {
  95. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  96. if(hero->inTownGarrison)
  97. return 0;
  98. if(hero->boat)
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. size_t MapRendererBaseContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  104. {
  105. return 0;
  106. }
  107. size_t MapRendererBaseContext::terrainImageIndex(size_t groupSize) const
  108. {
  109. return 0;
  110. }
  111. size_t MapRendererBaseContext::overlayImageIndex(const int3 & coordinates) const
  112. {
  113. return std::numeric_limits<size_t>::max();
  114. }
  115. bool MapRendererBaseContext::filterGrayscale() const
  116. {
  117. return false;
  118. }
  119. bool MapRendererBaseContext::showRoads() const
  120. {
  121. return true;
  122. }
  123. bool MapRendererBaseContext::showRivers() const
  124. {
  125. return true;
  126. }
  127. bool MapRendererBaseContext::showBorder() const
  128. {
  129. return false;
  130. }
  131. bool MapRendererBaseContext::showOverlay() const
  132. {
  133. return false;
  134. }
  135. bool MapRendererBaseContext::showGrid() const
  136. {
  137. return false;
  138. }
  139. bool MapRendererBaseContext::showVisitable() const
  140. {
  141. return false;
  142. }
  143. bool MapRendererBaseContext::showBlockable() const
  144. {
  145. return false;
  146. }
  147. MapRendererAdventureContext::MapRendererAdventureContext(const MapRendererContextState & viewState)
  148. : MapRendererBaseContext(viewState)
  149. {
  150. }
  151. const CGPath * MapRendererAdventureContext::currentPath() const
  152. {
  153. const auto * hero = adventureInt->curHero();
  154. if(!hero)
  155. return nullptr;
  156. if(!LOCPLINT->paths.hasPath(hero))
  157. return nullptr;
  158. return &LOCPLINT->paths.getPath(hero);
  159. }
  160. size_t MapRendererAdventureContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  161. {
  162. assert(groupSize > 0);
  163. if(!settingsAdventureObjectAnimation)
  164. return 0;
  165. if(groupSize == 0)
  166. return 0;
  167. // usign objectID for frameCounter to add pseudo-random element per-object.
  168. // Without it, animation of multiple visible objects of the same type will always be in sync
  169. size_t baseFrameTime = 180;
  170. size_t frameCounter = animationTime / baseFrameTime + objectID.getNum();
  171. size_t frameIndex = frameCounter % groupSize;
  172. return frameIndex;
  173. }
  174. size_t MapRendererAdventureContext::terrainImageIndex(size_t groupSize) const
  175. {
  176. if(!settingsAdventureTerrainAnimation)
  177. return 0;
  178. size_t baseFrameTime = 180;
  179. size_t frameCounter = animationTime / baseFrameTime;
  180. size_t frameIndex = frameCounter % groupSize;
  181. return frameIndex;
  182. }
  183. bool MapRendererAdventureContext::showBorder() const
  184. {
  185. return true;
  186. }
  187. bool MapRendererAdventureContext::showGrid() const
  188. {
  189. return settingShowGrid;
  190. }
  191. bool MapRendererAdventureContext::showVisitable() const
  192. {
  193. return settingShowVisitable;
  194. }
  195. bool MapRendererAdventureContext::showBlockable() const
  196. {
  197. return settingShowBlockable;
  198. }
  199. MapRendererAdventureFadingContext::MapRendererAdventureFadingContext(const MapRendererContextState & viewState)
  200. : MapRendererAdventureContext(viewState)
  201. {
  202. }
  203. bool MapRendererAdventureFadingContext::tileAnimated(const int3 & coordinates) const
  204. {
  205. if(!isInMap(coordinates))
  206. return false;
  207. auto objects = getObjects(coordinates);
  208. if(vstd::contains(objects, target))
  209. return true;
  210. return false;
  211. }
  212. double MapRendererAdventureFadingContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  213. {
  214. if(objectID == target)
  215. return progress;
  216. return 1.0;
  217. }
  218. MapRendererAdventureMovingContext::MapRendererAdventureMovingContext(const MapRendererContextState & viewState)
  219. : MapRendererAdventureContext(viewState)
  220. {
  221. }
  222. size_t MapRendererAdventureMovingContext::objectGroupIndex(ObjectInstanceID objectID) const
  223. {
  224. if(target == objectID)
  225. {
  226. static const std::vector<size_t> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
  227. return moveGroups[getObjectRotation(objectID)];
  228. }
  229. return MapRendererAdventureContext::objectGroupIndex(objectID);
  230. }
  231. bool MapRendererAdventureMovingContext::tileAnimated(const int3 & coordinates) const
  232. {
  233. if(!isInMap(coordinates))
  234. return false;
  235. auto objects = getObjects(coordinates);
  236. if(vstd::contains(objects, target))
  237. return true;
  238. return false;
  239. }
  240. Point MapRendererAdventureMovingContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  241. {
  242. if(target == objectID)
  243. {
  244. int3 offsetTilesFrom = tileFrom - coordinates;
  245. int3 offsetTilesDest = tileDest - coordinates;
  246. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
  247. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
  248. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, progress);
  249. return result;
  250. }
  251. return MapRendererAdventureContext::objectImageOffset(objectID, coordinates);
  252. }
  253. size_t MapRendererAdventureMovingContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  254. {
  255. if(target != objectID)
  256. return MapRendererAdventureContext::objectImageIndex(objectID, groupSize);
  257. int32_t baseFrameTime = 50;
  258. size_t frameCounter = animationTime / baseFrameTime;
  259. size_t frameIndex = frameCounter % groupSize;
  260. return frameIndex;
  261. }
  262. size_t MapRendererWorldViewContext::selectOverlayImageForObject(const ObjectPosInfo & object) const
  263. {
  264. size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  265. if(object.owner.isValidPlayer())
  266. ownerIndex = object.owner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  267. switch(object.id)
  268. {
  269. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  270. case Obj::MONOLITH_ONE_WAY_EXIT:
  271. case Obj::MONOLITH_TWO_WAY:
  272. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT);
  273. case Obj::SUBTERRANEAN_GATE:
  274. return ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE);
  275. case Obj::ARTIFACT:
  276. return ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT);
  277. case Obj::TOWN:
  278. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN);
  279. case Obj::HERO:
  280. return ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO);
  281. case Obj::MINE:
  282. return ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object.subId;
  283. case Obj::RESOURCE:
  284. return ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object.subId;
  285. }
  286. return std::numeric_limits<size_t>::max();
  287. }
  288. MapRendererWorldViewContext::MapRendererWorldViewContext(const MapRendererContextState & viewState)
  289. : MapRendererBaseContext(viewState)
  290. {
  291. }
  292. bool MapRendererWorldViewContext::showOverlay() const
  293. {
  294. return true;
  295. }
  296. size_t MapRendererWorldViewContext::overlayImageIndex(const int3 & coordinates) const
  297. {
  298. if(!isVisible(coordinates))
  299. return std::numeric_limits<size_t>::max();
  300. for(const auto & objectID : getObjects(coordinates))
  301. {
  302. const auto * object = getObject(objectID);
  303. if(!object->visitableAt(coordinates.x, coordinates.y))
  304. continue;
  305. ObjectPosInfo info;
  306. info.pos = coordinates;
  307. info.id = object->ID;
  308. info.subId = object->subID;
  309. info.owner = object->tempOwner;
  310. size_t iconIndex = selectOverlayImageForObject(info);
  311. if(iconIndex != std::numeric_limits<size_t>::max())
  312. return iconIndex;
  313. }
  314. return std::numeric_limits<size_t>::max();
  315. }
  316. MapRendererSpellViewContext::MapRendererSpellViewContext(const MapRendererContextState & viewState)
  317. : MapRendererWorldViewContext(viewState)
  318. {
  319. }
  320. double MapRendererSpellViewContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  321. {
  322. if(showAllTerrain)
  323. {
  324. if(getObject(objectID)->isVisitable() && !MapRendererWorldViewContext::isVisible(coordinates))
  325. return 0;
  326. }
  327. return MapRendererWorldViewContext::objectTransparency(objectID, coordinates);
  328. }
  329. bool MapRendererSpellViewContext::isVisible(const int3 & coordinates) const
  330. {
  331. if (showAllTerrain)
  332. return isInMap(coordinates);
  333. return MapRendererBaseContext::isVisible(coordinates);
  334. }
  335. size_t MapRendererSpellViewContext::overlayImageIndex(const int3 & coordinates) const
  336. {
  337. for(const auto & entry : additionalOverlayIcons)
  338. {
  339. if(entry.pos != coordinates)
  340. continue;
  341. size_t iconIndex = selectOverlayImageForObject(entry);
  342. if(iconIndex != std::numeric_limits<size_t>::max())
  343. return iconIndex;
  344. }
  345. return MapRendererWorldViewContext::overlayImageIndex(coordinates);
  346. }
  347. MapRendererPuzzleMapContext::MapRendererPuzzleMapContext(const MapRendererContextState & viewState)
  348. : MapRendererBaseContext(viewState)
  349. {
  350. }
  351. MapRendererPuzzleMapContext::~MapRendererPuzzleMapContext() = default;
  352. const CGPath * MapRendererPuzzleMapContext::currentPath() const
  353. {
  354. return grailPos.get();
  355. }
  356. double MapRendererPuzzleMapContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  357. {
  358. const auto * object = getObject(objectID);
  359. if(!object)
  360. return 0;
  361. if(object->isVisitable())
  362. return 0;
  363. if(object->ID == Obj::HOLE)
  364. return 0;
  365. return MapRendererBaseContext::objectTransparency(objectID, coordinates);
  366. }
  367. bool MapRendererPuzzleMapContext::isVisible(const int3 & coordinates) const
  368. {
  369. return LOCPLINT->cb->isInTheMap(coordinates);
  370. }
  371. bool MapRendererPuzzleMapContext::filterGrayscale() const
  372. {
  373. return true;
  374. }
  375. bool MapRendererPuzzleMapContext::showRoads() const
  376. {
  377. return false;
  378. }
  379. bool MapRendererPuzzleMapContext::showRivers() const
  380. {
  381. return false;
  382. }