MapViewController.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. assert(context->isInMap(position));
  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() + 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 = {vstd::clamp(position.x, lowerLimit.x, upperLimit.x), vstd::clamp(position.y, lowerLimit.y, upperLimit.y)};
  50. model->setViewCenter(betterPosition);
  51. model->setLevel(vstd::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::update(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. Point positionFrom = Point(hero->convertToVisitablePos(movementContext->tileFrom)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  97. Point positionDest = Point(hero->convertToVisitablePos(movementContext->tileDest)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  98. Point positionCurr = vstd::lerp(positionFrom, positionDest, movementContext->progress);
  99. if(movementContext->progress >= 1.0)
  100. {
  101. setViewCenter(hero->getSightCenter());
  102. removeObject(context->getObject(movementContext->target));
  103. addObject(context->getObject(movementContext->target));
  104. activateAdventureContext(movementContext->animationTime);
  105. }
  106. else
  107. {
  108. setViewCenter(positionCurr, movementContext->tileDest.z);
  109. }
  110. }
  111. if(teleportContext)
  112. {
  113. teleportContext->progress += timeDelta / heroTeleportDuration;
  114. if(teleportContext->progress >= 1.0)
  115. {
  116. activateAdventureContext(teleportContext->animationTime);
  117. }
  118. }
  119. if(fadingOutContext)
  120. {
  121. fadingOutContext->progress -= timeDelta / fadeOutDuration;
  122. if(fadingOutContext->progress <= 0.0)
  123. {
  124. removeObject(context->getObject(fadingOutContext->target));
  125. activateAdventureContext(fadingOutContext->animationTime);
  126. }
  127. }
  128. if(fadingInContext)
  129. {
  130. fadingInContext->progress += timeDelta / fadeInDuration;
  131. if(fadingInContext->progress >= 1.0)
  132. {
  133. activateAdventureContext(fadingInContext->animationTime);
  134. }
  135. }
  136. if(adventureContext)
  137. {
  138. adventureContext->animationTime += timeDelta;
  139. adventureContext->settingsSessionSpectate = settings["session"]["spectate"].Bool();
  140. adventureContext->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
  141. adventureContext->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
  142. adventureContext->settingShowGrid = settings["gameTweaks"]["showGrid"].Bool();
  143. adventureContext->settingShowVisitable = settings["session"]["showVisitable"].Bool();
  144. adventureContext->settingShowBlockable = settings["session"]["showBlockable"].Bool();
  145. }
  146. }
  147. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  148. {
  149. if(adventureContext == nullptr)
  150. return false;
  151. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  152. return false; // enemy move speed set to "hidden/none"
  153. if(obj->isVisitable())
  154. return context->isVisible(obj->visitablePos());
  155. else
  156. return context->isVisible(obj->pos);
  157. }
  158. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  159. {
  160. if(adventureContext == nullptr)
  161. return false;
  162. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  163. return false; // enemy move speed set to "hidden/none"
  164. if(context->isVisible(obj->convertToVisitablePos(from)))
  165. return true;
  166. if(context->isVisible(obj->convertToVisitablePos(dest)))
  167. return true;
  168. return false;
  169. }
  170. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  171. {
  172. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  173. fadingOutContext->animationTime = adventureContext->animationTime;
  174. adventureContext = fadingOutContext;
  175. context = fadingOutContext;
  176. fadingOutContext->target = obj->id;
  177. fadingOutContext->progress = 1.0;
  178. }
  179. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  180. {
  181. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  182. fadingInContext->animationTime = adventureContext->animationTime;
  183. adventureContext = fadingInContext;
  184. context = fadingInContext;
  185. fadingInContext->target = obj->id;
  186. fadingInContext->progress = 0.0;
  187. }
  188. void MapViewController::removeObject(const CGObjectInstance * obj)
  189. {
  190. view->invalidate(context, obj->id);
  191. state->removeObject(obj);
  192. }
  193. void MapViewController::addObject(const CGObjectInstance * obj)
  194. {
  195. state->addObject(obj);
  196. view->invalidate(context, obj->id);
  197. }
  198. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  199. {
  200. if(isEventVisible(obj, from, dest))
  201. {
  202. onObjectFadeOut(obj);
  203. setViewCenter(obj->getSightCenter());
  204. }
  205. else
  206. removeObject(obj);
  207. }
  208. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  209. {
  210. if(isEventVisible(obj, from, dest))
  211. setViewCenter(obj->getSightCenter());
  212. }
  213. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  214. {
  215. if(isEventVisible(obj, from, dest))
  216. setViewCenter(obj->getSightCenter());
  217. }
  218. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  219. {
  220. if(isEventVisible(obj, from, dest))
  221. {
  222. onObjectFadeIn(obj);
  223. setViewCenter(obj->getSightCenter());
  224. }
  225. addObject(obj);
  226. }
  227. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  228. {
  229. assert(!hasOngoingAnimations());
  230. if(isEventVisible(obj))
  231. fadeInObject(obj);
  232. addObject(obj);
  233. }
  234. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  235. {
  236. assert(!hasOngoingAnimations());
  237. if(isEventVisible(obj))
  238. fadeOutObject(obj);
  239. else
  240. removeObject(obj);
  241. }
  242. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  243. {
  244. addObject(obj);
  245. };
  246. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  247. {
  248. removeObject(obj);
  249. };
  250. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  251. {
  252. assert(!hasOngoingAnimations());
  253. if(isEventVisible(obj, from, dest))
  254. {
  255. setViewCenter(obj->getSightCenter());
  256. view->createTransitionSnapshot(context);
  257. }
  258. }
  259. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  260. {
  261. assert(!hasOngoingAnimations());
  262. removeObject(obj);
  263. addObject(obj);
  264. if(isEventVisible(obj, from, dest))
  265. {
  266. teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
  267. teleportContext->animationTime = adventureContext->animationTime;
  268. adventureContext = teleportContext;
  269. context = teleportContext;
  270. setViewCenter(obj->getSightCenter());
  271. }
  272. }
  273. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  274. {
  275. assert(!hasOngoingAnimations());
  276. // revisiting via spacebar, no need to animate
  277. if(from == dest)
  278. return;
  279. const CGObjectInstance * movingObject = obj;
  280. if(obj->boat)
  281. movingObject = obj->boat;
  282. removeObject(movingObject);
  283. if(!isEventVisible(obj, from, dest))
  284. {
  285. addObject(movingObject);
  286. return;
  287. }
  288. double movementTime = LOCPLINT->playerID == obj->tempOwner ?
  289. settings["adventure"]["heroMoveTime"].Float() :
  290. settings["adventure"]["enemyMoveTime"].Float();
  291. if(movementTime > 1)
  292. {
  293. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  294. movementContext->animationTime = adventureContext->animationTime;
  295. adventureContext = movementContext;
  296. context = movementContext;
  297. state->addMovingObject(movingObject, from, dest);
  298. movementContext->target = movingObject->id;
  299. movementContext->tileFrom = from;
  300. movementContext->tileDest = dest;
  301. movementContext->progress = 0.0;
  302. }
  303. else // instant movement
  304. {
  305. addObject(movingObject);
  306. setViewCenter(movingObject->visitablePos());
  307. }
  308. }
  309. bool MapViewController::hasOngoingAnimations()
  310. {
  311. if(movementContext)
  312. return true;
  313. if(fadingOutContext)
  314. return true;
  315. if(fadingInContext)
  316. return true;
  317. if(teleportContext)
  318. return true;
  319. return false;
  320. }
  321. void MapViewController::activateAdventureContext(uint32_t animationTime)
  322. {
  323. resetContext();
  324. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  325. adventureContext->animationTime = animationTime;
  326. context = adventureContext;
  327. }
  328. void MapViewController::activateAdventureContext()
  329. {
  330. activateAdventureContext(0);
  331. }
  332. void MapViewController::activateWorldViewContext()
  333. {
  334. if(worldViewContext)
  335. return;
  336. resetContext();
  337. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  338. context = worldViewContext;
  339. }
  340. void MapViewController::activateSpellViewContext()
  341. {
  342. if(spellViewContext)
  343. return;
  344. resetContext();
  345. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  346. worldViewContext = spellViewContext;
  347. context = spellViewContext;
  348. }
  349. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  350. {
  351. resetContext();
  352. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  353. context = puzzleMapContext;
  354. CGPathNode fakeNode;
  355. fakeNode.coord = grailPosition;
  356. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  357. // create two nodes since 1st one is normally not visible
  358. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  359. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  360. }
  361. void MapViewController::resetContext()
  362. {
  363. adventureContext.reset();
  364. movementContext.reset();
  365. fadingOutContext.reset();
  366. fadingInContext.reset();
  367. teleportContext.reset();
  368. worldViewContext.reset();
  369. spellViewContext.reset();
  370. puzzleMapContext.reset();
  371. }
  372. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  373. {
  374. assert(spellViewContext);
  375. spellViewContext->showAllTerrain = showAllTerrain;
  376. }
  377. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  378. {
  379. assert(spellViewContext);
  380. spellViewContext->additionalOverlayIcons = objectPositions;
  381. }