MapRendererContext.cpp 15 KB

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