2
0

MapViewController.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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/CAdvMapInt.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) // 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. }
  127. }
  128. void MapViewController::updateAfter(uint32_t timeDelta)
  129. {
  130. if(movementContext)
  131. {
  132. const auto * object = context->getObject(movementContext->target);
  133. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  134. const auto * boat = dynamic_cast<const CGBoat *>(object);
  135. assert(boat || hero);
  136. if(!hero)
  137. hero = boat->hero;
  138. if(movementContext->progress >= 1.0)
  139. {
  140. setViewCenter(hero->getSightCenter());
  141. removeObject(context->getObject(movementContext->target));
  142. addObject(context->getObject(movementContext->target));
  143. activateAdventureContext(movementContext->animationTime);
  144. }
  145. }
  146. if(teleportContext && teleportContext->progress >= 1.0)
  147. {
  148. activateAdventureContext(teleportContext->animationTime);
  149. }
  150. if(fadingOutContext && fadingOutContext->progress <= 0.0)
  151. {
  152. removeObject(context->getObject(fadingOutContext->target));
  153. activateAdventureContext(fadingOutContext->animationTime);
  154. }
  155. if(fadingInContext && fadingInContext->progress >= 1.0)
  156. {
  157. activateAdventureContext(fadingInContext->animationTime);
  158. }
  159. }
  160. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  161. {
  162. if(adventureContext == nullptr)
  163. return false;
  164. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  165. return false; // enemy move speed set to "hidden/none"
  166. if(GH.topInt() != adventureInt)
  167. return false;
  168. if(obj->isVisitable())
  169. return context->isVisible(obj->visitablePos());
  170. else
  171. return context->isVisible(obj->pos);
  172. }
  173. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  174. {
  175. if(adventureContext == nullptr)
  176. return false;
  177. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  178. return false; // enemy move speed set to "hidden/none"
  179. if(GH.topInt() != adventureInt)
  180. return false;
  181. if(context->isVisible(obj->convertToVisitablePos(from)))
  182. return true;
  183. if(context->isVisible(obj->convertToVisitablePos(dest)))
  184. return true;
  185. return false;
  186. }
  187. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  188. {
  189. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  190. fadingOutContext->animationTime = adventureContext->animationTime;
  191. adventureContext = fadingOutContext;
  192. context = fadingOutContext;
  193. const CGObjectInstance * movingObject = obj;
  194. if (obj->ID == Obj::HERO)
  195. {
  196. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  197. if (hero->boat)
  198. movingObject = hero->boat;
  199. }
  200. fadingOutContext->target = movingObject->id;
  201. fadingOutContext->progress = 1.0;
  202. }
  203. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  204. {
  205. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  206. fadingInContext->animationTime = adventureContext->animationTime;
  207. adventureContext = fadingInContext;
  208. context = fadingInContext;
  209. const CGObjectInstance * movingObject = obj;
  210. if (obj->ID == Obj::HERO)
  211. {
  212. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  213. if (hero->boat)
  214. movingObject = hero->boat;
  215. }
  216. fadingInContext->target = movingObject->id;
  217. fadingInContext->progress = 0.0;
  218. }
  219. void MapViewController::removeObject(const CGObjectInstance * obj)
  220. {
  221. if (obj->ID == Obj::BOAT)
  222. {
  223. auto * boat = dynamic_cast<const CGBoat*>(obj);
  224. if (boat->hero)
  225. {
  226. view->invalidate(context, boat->hero->id);
  227. state->removeObject(boat->hero);
  228. }
  229. }
  230. if (obj->ID == Obj::HERO)
  231. {
  232. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  233. if (hero->boat)
  234. {
  235. view->invalidate(context, hero->boat->id);
  236. state->removeObject(hero->boat);
  237. }
  238. }
  239. view->invalidate(context, obj->id);
  240. state->removeObject(obj);
  241. }
  242. void MapViewController::addObject(const CGObjectInstance * obj)
  243. {
  244. state->addObject(obj);
  245. view->invalidate(context, obj->id);
  246. }
  247. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  248. {
  249. if(isEventVisible(obj, from, dest))
  250. {
  251. fadeOutObject(obj);
  252. setViewCenter(obj->getSightCenter());
  253. }
  254. else
  255. removeObject(obj);
  256. }
  257. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  258. {
  259. if(isEventVisible(obj, from, dest))
  260. setViewCenter(obj->getSightCenter());
  261. }
  262. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  263. {
  264. if(isEventVisible(obj, from, dest))
  265. setViewCenter(obj->getSightCenter());
  266. }
  267. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  268. {
  269. if(isEventVisible(obj, from, dest))
  270. {
  271. fadeInObject(obj);
  272. setViewCenter(obj->getSightCenter());
  273. }
  274. addObject(obj);
  275. }
  276. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  277. {
  278. assert(!hasOngoingAnimations());
  279. if(isEventVisible(obj))
  280. fadeInObject(obj);
  281. addObject(obj);
  282. }
  283. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  284. {
  285. assert(!hasOngoingAnimations());
  286. if(isEventVisible(obj))
  287. fadeOutObject(obj);
  288. else
  289. removeObject(obj);
  290. }
  291. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  292. {
  293. addObject(obj);
  294. };
  295. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  296. {
  297. removeObject(obj);
  298. };
  299. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  300. {
  301. assert(!hasOngoingAnimations());
  302. if(isEventVisible(obj, from, dest))
  303. {
  304. setViewCenter(obj->getSightCenter());
  305. view->createTransitionSnapshot(context);
  306. }
  307. }
  308. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  309. {
  310. assert(!hasOngoingAnimations());
  311. const CGObjectInstance * movingObject = obj;
  312. if(obj->boat)
  313. movingObject = obj->boat;
  314. removeObject(movingObject);
  315. addObject(movingObject);
  316. if(isEventVisible(obj, from, dest))
  317. {
  318. teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
  319. teleportContext->animationTime = adventureContext->animationTime;
  320. adventureContext = teleportContext;
  321. context = teleportContext;
  322. setViewCenter(movingObject->getSightCenter());
  323. }
  324. }
  325. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  326. {
  327. assert(!hasOngoingAnimations());
  328. // revisiting via spacebar, no need to animate
  329. if(from == dest)
  330. return;
  331. const CGObjectInstance * movingObject = obj;
  332. if(obj->boat)
  333. movingObject = obj->boat;
  334. removeObject(movingObject);
  335. if(!isEventVisible(obj, from, dest))
  336. {
  337. addObject(movingObject);
  338. return;
  339. }
  340. double movementTime = LOCPLINT->playerID == obj->tempOwner ?
  341. settings["adventure"]["heroMoveTime"].Float() :
  342. settings["adventure"]["enemyMoveTime"].Float();
  343. if(movementTime > 1)
  344. {
  345. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  346. movementContext->animationTime = adventureContext->animationTime;
  347. adventureContext = movementContext;
  348. context = movementContext;
  349. state->addMovingObject(movingObject, from, dest);
  350. movementContext->target = movingObject->id;
  351. movementContext->tileFrom = from;
  352. movementContext->tileDest = dest;
  353. movementContext->progress = 0.0;
  354. }
  355. else // instant movement
  356. {
  357. addObject(movingObject);
  358. setViewCenter(movingObject->visitablePos());
  359. }
  360. }
  361. bool MapViewController::hasOngoingAnimations()
  362. {
  363. if(movementContext)
  364. return true;
  365. if(fadingOutContext)
  366. return true;
  367. if(fadingInContext)
  368. return true;
  369. if(teleportContext)
  370. return true;
  371. return false;
  372. }
  373. void MapViewController::activateAdventureContext(uint32_t animationTime)
  374. {
  375. resetContext();
  376. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  377. adventureContext->animationTime = animationTime;
  378. context = adventureContext;
  379. }
  380. void MapViewController::activateAdventureContext()
  381. {
  382. activateAdventureContext(0);
  383. }
  384. void MapViewController::activateWorldViewContext()
  385. {
  386. if(worldViewContext)
  387. return;
  388. resetContext();
  389. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  390. context = worldViewContext;
  391. }
  392. void MapViewController::activateSpellViewContext()
  393. {
  394. if(spellViewContext)
  395. return;
  396. resetContext();
  397. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  398. worldViewContext = spellViewContext;
  399. context = spellViewContext;
  400. }
  401. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  402. {
  403. resetContext();
  404. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  405. context = puzzleMapContext;
  406. CGPathNode fakeNode;
  407. fakeNode.coord = grailPosition;
  408. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  409. // create two nodes since 1st one is normally not visible
  410. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  411. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  412. }
  413. void MapViewController::resetContext()
  414. {
  415. adventureContext.reset();
  416. movementContext.reset();
  417. fadingOutContext.reset();
  418. fadingInContext.reset();
  419. teleportContext.reset();
  420. worldViewContext.reset();
  421. spellViewContext.reset();
  422. puzzleMapContext.reset();
  423. }
  424. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  425. {
  426. assert(spellViewContext);
  427. spellViewContext->showAllTerrain = showAllTerrain;
  428. }
  429. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  430. {
  431. assert(spellViewContext);
  432. spellViewContext->additionalOverlayIcons = objectPositions;
  433. }