MapRendererContext.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 "../CPlayerInterface.h"
  15. #include "../PlayerLocalState.h"
  16. #include "../GameInstance.h"
  17. #include "../../lib/Point.h"
  18. #include "../../lib/callback/CCallback.h"
  19. #include "../../lib/mapObjects/CGHeroInstance.h"
  20. #include "../../lib/mapObjects/MiscObjects.h"
  21. #include "../../lib/spells/CSpellHandler.h"
  22. #include "../../lib/mapping/CMap.h"
  23. #include "../../lib/pathfinder/CGPathNode.h"
  24. MapRendererBaseContext::MapRendererBaseContext(const MapRendererContextState & viewState)
  25. : viewState(viewState)
  26. {
  27. }
  28. uint32_t MapRendererBaseContext::getObjectRotation(ObjectInstanceID objectID) const
  29. {
  30. const CGObjectInstance * obj = getObject(objectID);
  31. if(obj->ID == Obj::HERO)
  32. {
  33. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  34. return hero->moveDir;
  35. }
  36. if(obj->ID == Obj::BOAT)
  37. {
  38. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  39. if(boat->getBoardedHero())
  40. return boat->getBoardedHero()->moveDir;
  41. return boat->direction;
  42. }
  43. return 0;
  44. }
  45. int3 MapRendererBaseContext::getMapSize() const
  46. {
  47. return GAME->interface()->cb->getMapSize();
  48. }
  49. bool MapRendererBaseContext::isInMap(const int3 & coordinates) const
  50. {
  51. return GAME->interface()->cb->isInTheMap(coordinates);
  52. }
  53. bool MapRendererBaseContext::isVisible(const int3 & coordinates) const
  54. {
  55. if(settingsSessionSpectate)
  56. return GAME->interface()->cb->isInTheMap(coordinates);
  57. else
  58. return GAME->interface()->cb->isVisible(coordinates);
  59. }
  60. bool MapRendererBaseContext::isActiveHero(const CGObjectInstance * obj) const
  61. {
  62. if(obj->ID == Obj::HERO)
  63. {
  64. assert(dynamic_cast<const CGHeroInstance *>(obj) != nullptr);
  65. if(GAME->interface()->localState->getCurrentHero() != nullptr)
  66. {
  67. if(obj->id == GAME->interface()->localState->getCurrentHero()->id)
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. bool MapRendererBaseContext::tileAnimated(const int3 & coordinates) const
  74. {
  75. return false;
  76. }
  77. const TerrainTile & MapRendererBaseContext::getMapTile(const int3 & coordinates) const
  78. {
  79. return GAME->map().getMap()->getTile(coordinates);
  80. }
  81. const MapRendererBaseContext::MapObjectsList & MapRendererBaseContext::getObjects(const int3 & coordinates) const
  82. {
  83. assert(isInMap(coordinates));
  84. return viewState.objects[coordinates.z][coordinates.x][coordinates.y];
  85. }
  86. const CGObjectInstance * MapRendererBaseContext::getObject(ObjectInstanceID objectID) const
  87. {
  88. return GAME->map().getMap()->getObject(objectID);
  89. }
  90. const CGPath * MapRendererBaseContext::currentPath() const
  91. {
  92. return nullptr;
  93. }
  94. size_t MapRendererBaseContext::objectGroupIndex(ObjectInstanceID objectID) const
  95. {
  96. static const std::array<size_t, 9> idleGroups = {0, 13, 0, 1, 2, 3, 4, 15, 14};
  97. return idleGroups[getObjectRotation(objectID)];
  98. }
  99. Point MapRendererBaseContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  100. {
  101. const CGObjectInstance * object = getObject(objectID);
  102. int3 offsetTiles(object->anchorPos() - coordinates);
  103. return Point(offsetTiles) * Point(32, 32);
  104. }
  105. double MapRendererBaseContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  106. {
  107. const CGObjectInstance * object = getObject(objectID);
  108. if(object->ID == Obj::HERO)
  109. {
  110. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  111. if(hero->isGarrisoned())
  112. return 0;
  113. if(hero->inBoat())
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. size_t MapRendererBaseContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  119. {
  120. return 0;
  121. }
  122. size_t MapRendererBaseContext::terrainImageIndex(size_t groupSize) const
  123. {
  124. return 0;
  125. }
  126. size_t MapRendererBaseContext::overlayImageIndex(const int3 & coordinates) const
  127. {
  128. return std::numeric_limits<size_t>::max();
  129. }
  130. std::string MapRendererBaseContext::overlayText(const int3 & coordinates) const
  131. {
  132. return {};
  133. }
  134. ColorRGBA MapRendererBaseContext::overlayTextColor(const int3 & coordinates) const
  135. {
  136. return {};
  137. }
  138. double MapRendererBaseContext::viewTransitionProgress() const
  139. {
  140. return 0;
  141. }
  142. bool MapRendererBaseContext::filterGrayscale() const
  143. {
  144. return false;
  145. }
  146. bool MapRendererBaseContext::showRoads() const
  147. {
  148. return true;
  149. }
  150. bool MapRendererBaseContext::showRivers() const
  151. {
  152. return true;
  153. }
  154. bool MapRendererBaseContext::showBorder() const
  155. {
  156. return false;
  157. }
  158. bool MapRendererBaseContext::showImageOverlay() const
  159. {
  160. return false;
  161. }
  162. bool MapRendererBaseContext::showTextOverlay() const
  163. {
  164. return false;
  165. }
  166. bool MapRendererBaseContext::showGrid() const
  167. {
  168. return false;
  169. }
  170. bool MapRendererBaseContext::showVisitable() const
  171. {
  172. return false;
  173. }
  174. bool MapRendererBaseContext::showBlocked() const
  175. {
  176. return false;
  177. }
  178. bool MapRendererBaseContext::showSpellRange(const int3 & position) const
  179. {
  180. return false;
  181. }
  182. MapRendererAdventureContext::MapRendererAdventureContext(const MapRendererContextState & viewState)
  183. : MapRendererBaseContext(viewState)
  184. {
  185. }
  186. const CGPath * MapRendererAdventureContext::currentPath() const
  187. {
  188. const auto * hero = GAME->interface()->localState->getCurrentHero();
  189. if(!hero)
  190. return nullptr;
  191. if(!GAME->interface()->localState->hasPath(hero))
  192. return nullptr;
  193. return &GAME->interface()->localState->getPath(hero);
  194. }
  195. size_t MapRendererAdventureContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  196. {
  197. assert(groupSize > 0);
  198. if(!settingsAdventureObjectAnimation)
  199. return 0;
  200. if(groupSize == 0)
  201. return 0;
  202. // usign objectID for frameCounter to add pseudo-random element per-object.
  203. // Without it, animation of multiple visible objects of the same type will always be in sync
  204. size_t baseFrameTime = 180;
  205. size_t frameCounter = animationTime / baseFrameTime + objectID.getNum();
  206. size_t frameIndex = frameCounter % groupSize;
  207. return frameIndex;
  208. }
  209. size_t MapRendererAdventureContext::terrainImageIndex(size_t groupSize) const
  210. {
  211. if(!settingsAdventureTerrainAnimation)
  212. return 0;
  213. size_t baseFrameTime = 180;
  214. size_t frameCounter = animationTime / baseFrameTime;
  215. size_t frameIndex = frameCounter % groupSize;
  216. return frameIndex;
  217. }
  218. std::string MapRendererAdventureContext::overlayText(const int3 & coordinates) const
  219. {
  220. if(!isVisible(coordinates))
  221. return {};
  222. const auto & tile = getMapTile(coordinates);
  223. if (!tile.visitable())
  224. return {};
  225. const auto * object = getObject(tile.visitableObjects.back());
  226. if ( object->ID == Obj::EVENT)
  227. return {};
  228. return object->getObjectName();
  229. }
  230. ColorRGBA MapRendererAdventureContext::overlayTextColor(const int3 & coordinates) const
  231. {
  232. if(!isVisible(coordinates))
  233. return {};
  234. const auto & tile = getMapTile(coordinates);
  235. if (!tile.visitable())
  236. return {};
  237. const auto * object = getObject(tile.visitableObjects.back());
  238. if (object->getOwner() == GAME->interface()->playerID)
  239. return { 0, 192, 0};
  240. if (GAME->interface()->cb->getPlayerRelations(object->getOwner(), GAME->interface()->playerID) == PlayerRelations::ALLIES)
  241. return { 0, 128, 255};
  242. if (object->getOwner().isValidPlayer())
  243. return { 255, 0, 0};
  244. if (object->ID == MapObjectID::MONSTER)
  245. return { 255, 0, 0};
  246. auto hero = GAME->interface()->localState->getCurrentHero();
  247. if (hero)
  248. {
  249. if (object->wasVisited(hero))
  250. return { 160, 160, 160 };
  251. }
  252. else
  253. {
  254. if (object->wasVisited(GAME->interface()->playerID))
  255. return { 160, 160, 160 };
  256. }
  257. return { 255, 192, 0 };
  258. }
  259. bool MapRendererAdventureContext::showBorder() const
  260. {
  261. return true;
  262. }
  263. bool MapRendererAdventureContext::showGrid() const
  264. {
  265. return settingShowGrid;
  266. }
  267. bool MapRendererAdventureContext::showVisitable() const
  268. {
  269. return settingShowVisitable;
  270. }
  271. bool MapRendererAdventureContext::showBlocked() const
  272. {
  273. return settingShowBlocked;
  274. }
  275. bool MapRendererAdventureContext::showTextOverlay() const
  276. {
  277. return settingTextOverlay;
  278. }
  279. bool MapRendererAdventureContext::showSpellRange(const int3 & position) const
  280. {
  281. if (!settingSpellRange)
  282. return false;
  283. auto hero = GAME->interface()->localState->getCurrentHero();
  284. if (!hero)
  285. return false;
  286. return !isInScreenRange(hero->getSightCenter(), position);
  287. }
  288. MapRendererAdventureTransitionContext::MapRendererAdventureTransitionContext(const MapRendererContextState & viewState)
  289. : MapRendererAdventureContext(viewState)
  290. {
  291. }
  292. double MapRendererAdventureTransitionContext::viewTransitionProgress() const
  293. {
  294. return progress;
  295. }
  296. MapRendererAdventureFadingContext::MapRendererAdventureFadingContext(const MapRendererContextState & viewState)
  297. : MapRendererAdventureContext(viewState)
  298. {
  299. }
  300. bool MapRendererAdventureFadingContext::tileAnimated(const int3 & coordinates) const
  301. {
  302. if(!isInMap(coordinates))
  303. return false;
  304. auto objects = getObjects(coordinates);
  305. if(vstd::contains(objects, target))
  306. return true;
  307. return false;
  308. }
  309. double MapRendererAdventureFadingContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  310. {
  311. if(objectID == target)
  312. return progress;
  313. return MapRendererAdventureContext::objectTransparency(objectID, coordinates);
  314. }
  315. MapRendererAdventureMovingContext::MapRendererAdventureMovingContext(const MapRendererContextState & viewState)
  316. : MapRendererAdventureContext(viewState)
  317. {
  318. }
  319. size_t MapRendererAdventureMovingContext::objectGroupIndex(ObjectInstanceID objectID) const
  320. {
  321. if(target == objectID)
  322. {
  323. static const std::array<size_t, 9> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
  324. return moveGroups[getObjectRotation(objectID)];
  325. }
  326. return MapRendererAdventureContext::objectGroupIndex(objectID);
  327. }
  328. bool MapRendererAdventureMovingContext::tileAnimated(const int3 & coordinates) const
  329. {
  330. if(!isInMap(coordinates))
  331. return false;
  332. auto objects = getObjects(coordinates);
  333. if(vstd::contains(objects, target))
  334. return true;
  335. return false;
  336. }
  337. Point MapRendererAdventureMovingContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  338. {
  339. if(target == objectID)
  340. {
  341. int3 offsetTilesFrom = tileFrom - coordinates;
  342. int3 offsetTilesDest = tileDest - coordinates;
  343. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
  344. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
  345. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, progress);
  346. return result;
  347. }
  348. return MapRendererAdventureContext::objectImageOffset(objectID, coordinates);
  349. }
  350. size_t MapRendererAdventureMovingContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  351. {
  352. if(target != objectID)
  353. return MapRendererAdventureContext::objectImageIndex(objectID, groupSize);
  354. int32_t baseFrameTime = 50;
  355. size_t frameCounter = animationTime / baseFrameTime;
  356. size_t frameIndex = frameCounter % groupSize;
  357. return frameIndex;
  358. }
  359. size_t MapRendererWorldViewContext::selectOverlayImageForObject(const ObjectPosInfo & object) const
  360. {
  361. size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  362. if(object.owner.isValidPlayer())
  363. ownerIndex = object.owner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  364. switch(object.id)
  365. {
  366. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  367. case Obj::MONOLITH_ONE_WAY_EXIT:
  368. case Obj::MONOLITH_TWO_WAY:
  369. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT);
  370. case Obj::SUBTERRANEAN_GATE:
  371. return ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE);
  372. case Obj::ARTIFACT:
  373. return ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT);
  374. case Obj::TOWN:
  375. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN);
  376. case Obj::HERO:
  377. return ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO);
  378. case Obj::MINE:
  379. return ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object.subId;
  380. case Obj::RESOURCE:
  381. return ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object.subId;
  382. }
  383. return std::numeric_limits<size_t>::max();
  384. }
  385. MapRendererWorldViewContext::MapRendererWorldViewContext(const MapRendererContextState & viewState)
  386. : MapRendererBaseContext(viewState)
  387. {
  388. }
  389. bool MapRendererWorldViewContext::showImageOverlay() const
  390. {
  391. return true;
  392. }
  393. size_t MapRendererWorldViewContext::overlayImageIndex(const int3 & coordinates) const
  394. {
  395. if(!isVisible(coordinates))
  396. return std::numeric_limits<size_t>::max();
  397. for(const auto & objectID : getObjects(coordinates))
  398. {
  399. const auto * object = getObject(objectID);
  400. if(!object->visitableAt(coordinates))
  401. continue;
  402. ObjectPosInfo info(object);
  403. size_t iconIndex = selectOverlayImageForObject(info);
  404. if(iconIndex != std::numeric_limits<size_t>::max())
  405. return iconIndex;
  406. }
  407. return std::numeric_limits<size_t>::max();
  408. }
  409. MapRendererSpellViewContext::MapRendererSpellViewContext(const MapRendererContextState & viewState)
  410. : MapRendererWorldViewContext(viewState)
  411. {
  412. }
  413. double MapRendererSpellViewContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  414. {
  415. if(showAllTerrain)
  416. {
  417. if(getObject(objectID)->isVisitable() && !MapRendererWorldViewContext::isVisible(coordinates))
  418. return 0;
  419. }
  420. return MapRendererWorldViewContext::objectTransparency(objectID, coordinates);
  421. }
  422. bool MapRendererSpellViewContext::isVisible(const int3 & coordinates) const
  423. {
  424. if(showAllTerrain)
  425. return isInMap(coordinates);
  426. return MapRendererBaseContext::isVisible(coordinates);
  427. }
  428. size_t MapRendererSpellViewContext::overlayImageIndex(const int3 & coordinates) const
  429. {
  430. for(const auto & entry : additionalOverlayIcons)
  431. {
  432. if(entry.pos != coordinates)
  433. continue;
  434. size_t iconIndex = selectOverlayImageForObject(entry);
  435. if(iconIndex != std::numeric_limits<size_t>::max())
  436. return iconIndex;
  437. }
  438. if (MapRendererBaseContext::isVisible(coordinates))
  439. return MapRendererWorldViewContext::overlayImageIndex(coordinates);
  440. else
  441. return std::numeric_limits<size_t>::max();
  442. }
  443. MapRendererPuzzleMapContext::MapRendererPuzzleMapContext(const MapRendererContextState & viewState)
  444. : MapRendererBaseContext(viewState)
  445. {
  446. }
  447. MapRendererPuzzleMapContext::~MapRendererPuzzleMapContext() = default;
  448. const CGPath * MapRendererPuzzleMapContext::currentPath() const
  449. {
  450. return grailPos.get();
  451. }
  452. double MapRendererPuzzleMapContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  453. {
  454. const auto * object = getObject(objectID);
  455. if(!object)
  456. return 0;
  457. if(object->isVisitable())
  458. return 0;
  459. if(object->ID == Obj::HOLE)
  460. return 0;
  461. return MapRendererBaseContext::objectTransparency(objectID, coordinates);
  462. }
  463. bool MapRendererPuzzleMapContext::isVisible(const int3 & coordinates) const
  464. {
  465. return GAME->interface()->cb->isInTheMap(coordinates);
  466. }
  467. bool MapRendererPuzzleMapContext::filterGrayscale() const
  468. {
  469. return true;
  470. }
  471. bool MapRendererPuzzleMapContext::showRoads() const
  472. {
  473. return false;
  474. }
  475. bool MapRendererPuzzleMapContext::showRivers() const
  476. {
  477. return false;
  478. }