2
0

MapRendererContext.cpp 14 KB

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