MapViewController.cpp 13 KB

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