MapRendererContext.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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/adventure/AdventureSpellEffect.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::showInvisible() const
  192. {
  193. return false;
  194. }
  195. bool MapRendererBaseContext::showSpellRange(const int3 & position) const
  196. {
  197. return false;
  198. }
  199. MapRendererAdventureContext::MapRendererAdventureContext(const MapRendererContextState & viewState)
  200. : MapRendererBaseContext(viewState)
  201. {
  202. }
  203. const CGPath * MapRendererAdventureContext::currentPath() const
  204. {
  205. const auto * hero = GAME->interface()->localState->getCurrentHero();
  206. if(!hero)
  207. return nullptr;
  208. if(!GAME->interface()->localState->hasPath(hero))
  209. return nullptr;
  210. return &GAME->interface()->localState->getPath(hero);
  211. }
  212. size_t MapRendererAdventureContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  213. {
  214. assert(groupSize > 0);
  215. if(!settingsAdventureObjectAnimation)
  216. return 0;
  217. if(groupSize == 0)
  218. return 0;
  219. // usign objectID for frameCounter to add pseudo-random element per-object.
  220. // Without it, animation of multiple visible objects of the same type will always be in sync
  221. size_t baseFrameTime = 180;
  222. size_t frameCounter = animationTime / baseFrameTime + objectID.getNum();
  223. size_t frameIndex = frameCounter % groupSize;
  224. return frameIndex;
  225. }
  226. size_t MapRendererAdventureContext::terrainImageIndex(size_t groupSize) const
  227. {
  228. if(!settingsAdventureTerrainAnimation)
  229. return 0;
  230. size_t baseFrameTime = 180;
  231. size_t frameCounter = animationTime / baseFrameTime;
  232. size_t frameIndex = frameCounter % groupSize;
  233. return frameIndex;
  234. }
  235. std::string MapRendererAdventureContext::overlayText(const int3 & coordinates) const
  236. {
  237. if(!isVisible(coordinates))
  238. return {};
  239. const auto & tile = getMapTile(coordinates);
  240. if (!tile.visitable())
  241. return {};
  242. const auto * object = getObject(tile.visitableObjects.back());
  243. if ( object->ID == Obj::EVENT)
  244. return {};
  245. return object->getObjectName();
  246. }
  247. ColorRGBA MapRendererAdventureContext::overlayTextColor(const int3 & coordinates) const
  248. {
  249. if(!isVisible(coordinates))
  250. return {};
  251. const auto & tile = getMapTile(coordinates);
  252. if (!tile.visitable())
  253. return {};
  254. const auto * object = getObject(tile.visitableObjects.back());
  255. if (object->getOwner() == GAME->interface()->playerID)
  256. return { 0, 192, 0};
  257. if (GAME->interface()->cb->getPlayerRelations(object->getOwner(), GAME->interface()->playerID) == PlayerRelations::ALLIES)
  258. return { 0, 128, 255};
  259. if (object->getOwner().isValidPlayer())
  260. return { 255, 0, 0};
  261. if (object->ID == MapObjectID::MONSTER)
  262. return { 255, 0, 0};
  263. auto hero = GAME->interface()->localState->getCurrentHero();
  264. if (hero)
  265. {
  266. if (object->wasVisited(hero))
  267. return { 160, 160, 160 };
  268. }
  269. else
  270. {
  271. if (object->wasVisited(GAME->interface()->playerID))
  272. return { 160, 160, 160 };
  273. }
  274. return { 255, 192, 0 };
  275. }
  276. bool MapRendererAdventureContext::showBorder() const
  277. {
  278. return true;
  279. }
  280. bool MapRendererAdventureContext::showGrid() const
  281. {
  282. return settingShowGrid;
  283. }
  284. bool MapRendererAdventureContext::showVisitable() const
  285. {
  286. return settingShowVisitable;
  287. }
  288. bool MapRendererAdventureContext::showBlocked() const
  289. {
  290. return settingShowBlocked;
  291. }
  292. bool MapRendererAdventureContext::showInvisible() const
  293. {
  294. return settingShowInvisible;
  295. }
  296. bool MapRendererAdventureContext::showTextOverlay() const
  297. {
  298. return settingTextOverlay;
  299. }
  300. bool MapRendererAdventureContext::showSpellRange(const int3 & position) const
  301. {
  302. auto hero = GAME->interface()->localState->getCurrentHero();
  303. auto spell = GAME->interface()->localState->getCurrentSpell();
  304. if (!hero || !spell.hasValue())
  305. return false;
  306. const auto * spellEffect = spell.toSpell()->getAdventureMechanics().getEffectAs<AdventureSpellRangedEffect>(hero);
  307. return !spellEffect->isTargetInRange(GAME->interface()->cb.get(), hero, position);
  308. }
  309. MapRendererAdventureTransitionContext::MapRendererAdventureTransitionContext(const MapRendererContextState & viewState)
  310. : MapRendererAdventureContext(viewState)
  311. {
  312. }
  313. double MapRendererAdventureTransitionContext::viewTransitionProgress() const
  314. {
  315. return progress;
  316. }
  317. MapRendererAdventureFadingContext::MapRendererAdventureFadingContext(const MapRendererContextState & viewState)
  318. : MapRendererAdventureContext(viewState)
  319. {
  320. }
  321. bool MapRendererAdventureFadingContext::tileAnimated(const int3 & coordinates) const
  322. {
  323. if(!isInMap(coordinates))
  324. return false;
  325. auto objects = getObjects(coordinates);
  326. if(vstd::contains(objects, target))
  327. return true;
  328. return false;
  329. }
  330. double MapRendererAdventureFadingContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  331. {
  332. if(objectID == target)
  333. return progress;
  334. return MapRendererAdventureContext::objectTransparency(objectID, coordinates);
  335. }
  336. MapRendererAdventureMovingContext::MapRendererAdventureMovingContext(const MapRendererContextState & viewState)
  337. : MapRendererAdventureContext(viewState)
  338. {
  339. }
  340. size_t MapRendererAdventureMovingContext::objectGroupIndex(ObjectInstanceID objectID) const
  341. {
  342. if(target == objectID)
  343. {
  344. static const std::array<size_t, 9> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
  345. return moveGroups[getObjectRotation(objectID)];
  346. }
  347. return MapRendererAdventureContext::objectGroupIndex(objectID);
  348. }
  349. bool MapRendererAdventureMovingContext::tileAnimated(const int3 & coordinates) const
  350. {
  351. if(!isInMap(coordinates))
  352. return false;
  353. auto objects = getObjects(coordinates);
  354. if(vstd::contains(objects, target))
  355. return true;
  356. return false;
  357. }
  358. Point MapRendererAdventureMovingContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  359. {
  360. if(target == objectID)
  361. {
  362. int3 offsetTilesFrom = tileFrom - coordinates;
  363. int3 offsetTilesDest = tileDest - coordinates;
  364. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
  365. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
  366. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, progress);
  367. return result;
  368. }
  369. return MapRendererAdventureContext::objectImageOffset(objectID, coordinates);
  370. }
  371. size_t MapRendererAdventureMovingContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
  372. {
  373. if(target != objectID)
  374. return MapRendererAdventureContext::objectImageIndex(objectID, groupSize);
  375. int32_t baseFrameTime = 50;
  376. size_t frameCounter = animationTime / baseFrameTime;
  377. size_t frameIndex = frameCounter % groupSize;
  378. return frameIndex;
  379. }
  380. size_t MapRendererWorldViewContext::selectOverlayImageForObject(const ObjectPosInfo & object) const
  381. {
  382. size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  383. if(object.owner.isValidPlayer())
  384. ownerIndex = object.owner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  385. switch(object.id)
  386. {
  387. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  388. case Obj::MONOLITH_ONE_WAY_EXIT:
  389. case Obj::MONOLITH_TWO_WAY:
  390. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT);
  391. case Obj::SUBTERRANEAN_GATE:
  392. return ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE);
  393. case Obj::ARTIFACT:
  394. return ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT);
  395. case Obj::TOWN:
  396. return ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN);
  397. case Obj::HERO:
  398. return ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO);
  399. case Obj::MINE:
  400. return ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object.subId;
  401. case Obj::RESOURCE:
  402. return ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object.subId;
  403. }
  404. return std::numeric_limits<size_t>::max();
  405. }
  406. MapRendererWorldViewContext::MapRendererWorldViewContext(const MapRendererContextState & viewState)
  407. : MapRendererBaseContext(viewState)
  408. {
  409. }
  410. bool MapRendererWorldViewContext::showImageOverlay() const
  411. {
  412. return true;
  413. }
  414. size_t MapRendererWorldViewContext::overlayImageIndex(const int3 & coordinates) const
  415. {
  416. if(!isVisible(coordinates))
  417. return std::numeric_limits<size_t>::max();
  418. for(const auto & objectID : getObjects(coordinates))
  419. {
  420. const auto * object = getObject(objectID);
  421. if(!object->visitableAt(coordinates))
  422. continue;
  423. ObjectPosInfo info(object);
  424. size_t iconIndex = selectOverlayImageForObject(info);
  425. if(iconIndex != std::numeric_limits<size_t>::max())
  426. return iconIndex;
  427. }
  428. return std::numeric_limits<size_t>::max();
  429. }
  430. MapRendererSpellViewContext::MapRendererSpellViewContext(const MapRendererContextState & viewState)
  431. : MapRendererWorldViewContext(viewState)
  432. {
  433. }
  434. double MapRendererSpellViewContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  435. {
  436. if(showAllTerrain)
  437. {
  438. if(getObject(objectID)->isVisitable() && !MapRendererWorldViewContext::isVisible(coordinates))
  439. return 0;
  440. }
  441. return MapRendererWorldViewContext::objectTransparency(objectID, coordinates);
  442. }
  443. bool MapRendererSpellViewContext::isVisible(const int3 & coordinates) const
  444. {
  445. if(showAllTerrain)
  446. return isInMap(coordinates);
  447. return MapRendererBaseContext::isVisible(coordinates);
  448. }
  449. size_t MapRendererSpellViewContext::overlayImageIndex(const int3 & coordinates) const
  450. {
  451. for(const auto & entry : additionalOverlayIcons)
  452. {
  453. if(entry.pos != coordinates)
  454. continue;
  455. size_t iconIndex = selectOverlayImageForObject(entry);
  456. if(iconIndex != std::numeric_limits<size_t>::max())
  457. return iconIndex;
  458. }
  459. if (MapRendererBaseContext::isVisible(coordinates))
  460. return MapRendererWorldViewContext::overlayImageIndex(coordinates);
  461. else
  462. return std::numeric_limits<size_t>::max();
  463. }
  464. MapRendererPuzzleMapContext::MapRendererPuzzleMapContext(const MapRendererContextState & viewState)
  465. : MapRendererBaseContext(viewState)
  466. {
  467. }
  468. MapRendererPuzzleMapContext::~MapRendererPuzzleMapContext() = default;
  469. const CGPath * MapRendererPuzzleMapContext::currentPath() const
  470. {
  471. return grailPos.get();
  472. }
  473. double MapRendererPuzzleMapContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
  474. {
  475. const auto * object = getObject(objectID);
  476. if(!object)
  477. return 0;
  478. if(object->isVisitable())
  479. return 0;
  480. if(object->ID == Obj::HOLE)
  481. return 0;
  482. return MapRendererBaseContext::objectTransparency(objectID, coordinates);
  483. }
  484. bool MapRendererPuzzleMapContext::isVisible(const int3 & coordinates) const
  485. {
  486. return GAME->interface()->cb->isInTheMap(coordinates);
  487. }
  488. bool MapRendererPuzzleMapContext::filterGrayscale() const
  489. {
  490. return true;
  491. }
  492. bool MapRendererPuzzleMapContext::showRoads() const
  493. {
  494. return false;
  495. }
  496. bool MapRendererPuzzleMapContext::showRivers() const
  497. {
  498. return false;
  499. }