MapRendererContext.cpp 15 KB

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