MapViewController.cpp 13 KB

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