2
0

MapViewController.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * MapViewController.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 "MapViewController.h"
  12. #include "MapRendererContext.h"
  13. #include "MapRendererContextState.h"
  14. #include "MapViewCache.h"
  15. #include "MapViewModel.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../adventureMap/CAdventureMapInterface.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/CPathfinder.h"
  21. #include "../../lib/mapObjects/CGHeroInstance.h"
  22. #include "../../lib/mapObjects/MiscObjects.h"
  23. #include "../../lib/spells/ViewSpellInt.h"
  24. void MapViewController::setViewCenter(const int3 & position)
  25. {
  26. setViewCenter(Point(position) * model->getSingleTileSize() + model->getSingleTileSize() / 2, position.z);
  27. }
  28. void MapViewController::setViewCenter(const Point & position, int level)
  29. {
  30. Point upperLimit = Point(context->getMapSize()) * model->getSingleTileSize();
  31. Point lowerLimit = Point(0, 0);
  32. if(worldViewContext)
  33. {
  34. Point area = model->getPixelsVisibleDimensions();
  35. Point mapCenter = upperLimit / 2;
  36. Point desiredLowerLimit = lowerLimit + area / 2;
  37. Point desiredUpperLimit = upperLimit - area / 2;
  38. Point actualLowerLimit{
  39. std::min(desiredLowerLimit.x, mapCenter.x),
  40. std::min(desiredLowerLimit.y, mapCenter.y)
  41. };
  42. Point actualUpperLimit{
  43. std::max(desiredUpperLimit.x, mapCenter.x),
  44. std::max(desiredUpperLimit.y, mapCenter.y)
  45. };
  46. upperLimit = actualUpperLimit;
  47. lowerLimit = actualLowerLimit;
  48. }
  49. Point betterPosition = {std::clamp(position.x, lowerLimit.x, upperLimit.x), std::clamp(position.y, lowerLimit.y, upperLimit.y)};
  50. model->setViewCenter(betterPosition);
  51. model->setLevel(std::clamp(level, 0, context->getMapSize().z));
  52. if(adventureInt && !puzzleMapContext) // may be called before adventureInt is initialized
  53. adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel());
  54. }
  55. void MapViewController::setTileSize(const Point & tileSize)
  56. {
  57. model->setTileSize(tileSize);
  58. // force update of view center since changing tile size may invalidated it
  59. setViewCenter(model->getMapViewCenter(), model->getLevel());
  60. }
  61. MapViewController::MapViewController(std::shared_ptr<MapViewModel> model, std::shared_ptr<MapViewCache> view)
  62. : state(new MapRendererContextState())
  63. , model(std::move(model))
  64. , view(view)
  65. {
  66. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  67. context = adventureContext;
  68. }
  69. std::shared_ptr<IMapRendererContext> MapViewController::getContext() const
  70. {
  71. return context;
  72. }
  73. void MapViewController::updateBefore(uint32_t timeDelta)
  74. {
  75. // confirmed to match H3 for
  76. // - hero embarking on boat (500 ms)
  77. // - hero disembarking from boat (500 ms)
  78. // - TODO: picking up resources
  79. // - TODO: killing mosters
  80. // - teleporting ( 250 ms)
  81. static const double fadeOutDuration = 500;
  82. static const double fadeInDuration = 500;
  83. static const double heroTeleportDuration = 250;
  84. if(movementContext)
  85. {
  86. const auto * object = context->getObject(movementContext->target);
  87. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  88. const auto * boat = dynamic_cast<const CGBoat *>(object);
  89. assert(boat || hero);
  90. if(!hero)
  91. hero = boat->hero;
  92. double heroMoveTime = LOCPLINT->makingTurn ?
  93. settings["adventure"]["heroMoveTime"].Float() :
  94. settings["adventure"]["enemyMoveTime"].Float();
  95. movementContext->progress += timeDelta / heroMoveTime;
  96. movementContext->progress = std::min( 1.0, movementContext->progress);
  97. Point positionFrom = Point(hero->convertToVisitablePos(movementContext->tileFrom)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  98. Point positionDest = Point(hero->convertToVisitablePos(movementContext->tileDest)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  99. Point positionCurr = vstd::lerp(positionFrom, positionDest, movementContext->progress);
  100. setViewCenter(positionCurr, movementContext->tileDest.z);
  101. }
  102. if(teleportContext)
  103. {
  104. teleportContext->progress += timeDelta / heroTeleportDuration;
  105. teleportContext->progress = std::min( 1.0, teleportContext->progress);
  106. }
  107. if(fadingOutContext)
  108. {
  109. fadingOutContext->progress -= timeDelta / fadeOutDuration;
  110. fadingOutContext->progress = std::max( 0.0, fadingOutContext->progress);
  111. }
  112. if(fadingInContext)
  113. {
  114. fadingInContext->progress += timeDelta / fadeInDuration;
  115. fadingInContext->progress = std::min( 1.0, fadingInContext->progress);
  116. }
  117. if(adventureContext)
  118. {
  119. adventureContext->animationTime += timeDelta;
  120. adventureContext->settingsSessionSpectate = settings["session"]["spectate"].Bool();
  121. adventureContext->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
  122. adventureContext->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
  123. adventureContext->settingShowGrid = settings["gameTweaks"]["showGrid"].Bool();
  124. adventureContext->settingShowVisitable = settings["session"]["showVisitable"].Bool();
  125. adventureContext->settingShowBlocked = settings["session"]["showBlocked"].Bool();
  126. adventureContext->settingSpellRange = settings["session"]["showSpellRange"].Bool();
  127. }
  128. }
  129. void MapViewController::updateAfter(uint32_t timeDelta)
  130. {
  131. if(movementContext)
  132. {
  133. const auto * object = context->getObject(movementContext->target);
  134. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  135. const auto * boat = dynamic_cast<const CGBoat *>(object);
  136. assert(boat || hero);
  137. if(!hero)
  138. hero = boat->hero;
  139. if(movementContext->progress >= 1.0)
  140. {
  141. setViewCenter(hero->getSightCenter());
  142. removeObject(context->getObject(movementContext->target));
  143. addObject(context->getObject(movementContext->target));
  144. activateAdventureContext(movementContext->animationTime);
  145. }
  146. }
  147. if(teleportContext && teleportContext->progress >= 1.0)
  148. {
  149. activateAdventureContext(teleportContext->animationTime);
  150. }
  151. if(fadingOutContext && fadingOutContext->progress <= 0.0)
  152. {
  153. removeObject(context->getObject(fadingOutContext->target));
  154. activateAdventureContext(fadingOutContext->animationTime);
  155. }
  156. if(fadingInContext && fadingInContext->progress >= 1.0)
  157. {
  158. activateAdventureContext(fadingInContext->animationTime);
  159. }
  160. }
  161. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  162. {
  163. if(adventureContext == nullptr)
  164. return false;
  165. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  166. return false; // enemy move speed set to "hidden/none"
  167. if(GH.topInt() != adventureInt)
  168. return false;
  169. if(obj->isVisitable())
  170. return context->isVisible(obj->visitablePos());
  171. else
  172. return context->isVisible(obj->pos);
  173. }
  174. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  175. {
  176. if(adventureContext == nullptr)
  177. return false;
  178. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  179. return false; // enemy move speed set to "hidden/none"
  180. if(GH.topInt() != adventureInt)
  181. return false;
  182. if(context->isVisible(obj->convertToVisitablePos(from)))
  183. return true;
  184. if(context->isVisible(obj->convertToVisitablePos(dest)))
  185. return true;
  186. return false;
  187. }
  188. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  189. {
  190. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  191. fadingOutContext->animationTime = adventureContext->animationTime;
  192. adventureContext = fadingOutContext;
  193. context = fadingOutContext;
  194. const CGObjectInstance * movingObject = obj;
  195. if (obj->ID == Obj::HERO)
  196. {
  197. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  198. if (hero->boat)
  199. movingObject = hero->boat;
  200. }
  201. fadingOutContext->target = movingObject->id;
  202. fadingOutContext->progress = 1.0;
  203. }
  204. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  205. {
  206. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  207. fadingInContext->animationTime = adventureContext->animationTime;
  208. adventureContext = fadingInContext;
  209. context = fadingInContext;
  210. const CGObjectInstance * movingObject = obj;
  211. if (obj->ID == Obj::HERO)
  212. {
  213. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  214. if (hero->boat)
  215. movingObject = hero->boat;
  216. }
  217. fadingInContext->target = movingObject->id;
  218. fadingInContext->progress = 0.0;
  219. }
  220. void MapViewController::removeObject(const CGObjectInstance * obj)
  221. {
  222. if (obj->ID == Obj::BOAT)
  223. {
  224. auto * boat = dynamic_cast<const CGBoat*>(obj);
  225. if (boat->hero)
  226. {
  227. view->invalidate(context, boat->hero->id);
  228. state->removeObject(boat->hero);
  229. }
  230. }
  231. if (obj->ID == Obj::HERO)
  232. {
  233. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  234. if (hero->boat)
  235. {
  236. view->invalidate(context, hero->boat->id);
  237. state->removeObject(hero->boat);
  238. }
  239. }
  240. view->invalidate(context, obj->id);
  241. state->removeObject(obj);
  242. }
  243. void MapViewController::addObject(const CGObjectInstance * obj)
  244. {
  245. state->addObject(obj);
  246. view->invalidate(context, obj->id);
  247. }
  248. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  249. {
  250. if(isEventVisible(obj, from, dest))
  251. {
  252. fadeOutObject(obj);
  253. setViewCenter(obj->getSightCenter());
  254. }
  255. else
  256. removeObject(obj);
  257. }
  258. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  259. {
  260. if(isEventVisible(obj, from, dest))
  261. setViewCenter(obj->getSightCenter());
  262. }
  263. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  264. {
  265. if(isEventVisible(obj, from, dest))
  266. setViewCenter(obj->getSightCenter());
  267. }
  268. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  269. {
  270. if(isEventVisible(obj, from, dest))
  271. {
  272. fadeInObject(obj);
  273. setViewCenter(obj->getSightCenter());
  274. }
  275. addObject(obj);
  276. }
  277. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  278. {
  279. assert(!hasOngoingAnimations());
  280. if(isEventVisible(obj))
  281. fadeInObject(obj);
  282. addObject(obj);
  283. }
  284. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  285. {
  286. assert(!hasOngoingAnimations());
  287. if(isEventVisible(obj))
  288. fadeOutObject(obj);
  289. else
  290. removeObject(obj);
  291. }
  292. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  293. {
  294. addObject(obj);
  295. };
  296. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  297. {
  298. removeObject(obj);
  299. };
  300. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  301. {
  302. assert(!hasOngoingAnimations());
  303. if(isEventVisible(obj, from, dest))
  304. {
  305. setViewCenter(obj->getSightCenter());
  306. view->createTransitionSnapshot(context);
  307. }
  308. }
  309. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  310. {
  311. assert(!hasOngoingAnimations());
  312. const CGObjectInstance * movingObject = obj;
  313. if(obj->boat)
  314. movingObject = obj->boat;
  315. removeObject(movingObject);
  316. addObject(movingObject);
  317. if(isEventVisible(obj, from, dest))
  318. {
  319. teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
  320. teleportContext->animationTime = adventureContext->animationTime;
  321. adventureContext = teleportContext;
  322. context = teleportContext;
  323. setViewCenter(movingObject->getSightCenter());
  324. }
  325. }
  326. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  327. {
  328. assert(!hasOngoingAnimations());
  329. // revisiting via spacebar, no need to animate
  330. if(from == dest)
  331. return;
  332. const CGObjectInstance * movingObject = obj;
  333. if(obj->boat)
  334. movingObject = obj->boat;
  335. removeObject(movingObject);
  336. if(!isEventVisible(obj, from, dest))
  337. {
  338. addObject(movingObject);
  339. return;
  340. }
  341. double movementTime = LOCPLINT->playerID == obj->tempOwner ?
  342. settings["adventure"]["heroMoveTime"].Float() :
  343. settings["adventure"]["enemyMoveTime"].Float();
  344. if(movementTime > 1)
  345. {
  346. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  347. movementContext->animationTime = adventureContext->animationTime;
  348. adventureContext = movementContext;
  349. context = movementContext;
  350. state->addMovingObject(movingObject, from, dest);
  351. movementContext->target = movingObject->id;
  352. movementContext->tileFrom = from;
  353. movementContext->tileDest = dest;
  354. movementContext->progress = 0.0;
  355. }
  356. else // instant movement
  357. {
  358. addObject(movingObject);
  359. setViewCenter(movingObject->visitablePos());
  360. }
  361. }
  362. bool MapViewController::hasOngoingAnimations()
  363. {
  364. if(movementContext)
  365. return true;
  366. if(fadingOutContext)
  367. return true;
  368. if(fadingInContext)
  369. return true;
  370. if(teleportContext)
  371. return true;
  372. return false;
  373. }
  374. void MapViewController::activateAdventureContext(uint32_t animationTime)
  375. {
  376. resetContext();
  377. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  378. adventureContext->animationTime = animationTime;
  379. context = adventureContext;
  380. }
  381. void MapViewController::activateAdventureContext()
  382. {
  383. activateAdventureContext(0);
  384. }
  385. void MapViewController::activateWorldViewContext()
  386. {
  387. if(worldViewContext)
  388. return;
  389. resetContext();
  390. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  391. context = worldViewContext;
  392. }
  393. void MapViewController::activateSpellViewContext()
  394. {
  395. if(spellViewContext)
  396. return;
  397. resetContext();
  398. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  399. worldViewContext = spellViewContext;
  400. context = spellViewContext;
  401. }
  402. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  403. {
  404. resetContext();
  405. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  406. context = puzzleMapContext;
  407. CGPathNode fakeNode;
  408. fakeNode.coord = grailPosition;
  409. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  410. // create two nodes since 1st one is normally not visible
  411. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  412. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  413. }
  414. void MapViewController::resetContext()
  415. {
  416. adventureContext.reset();
  417. movementContext.reset();
  418. fadingOutContext.reset();
  419. fadingInContext.reset();
  420. teleportContext.reset();
  421. worldViewContext.reset();
  422. spellViewContext.reset();
  423. puzzleMapContext.reset();
  424. }
  425. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  426. {
  427. assert(spellViewContext);
  428. spellViewContext->showAllTerrain = showAllTerrain;
  429. }
  430. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  431. {
  432. assert(spellViewContext);
  433. spellViewContext->additionalOverlayIcons = objectPositions;
  434. }