MapViewController.cpp 15 KB

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