MapViewController.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. if (obj->ID == Obj::BOAT)
  204. {
  205. auto * boat = dynamic_cast<const CGBoat*>(obj);
  206. if (boat->hero)
  207. {
  208. view->invalidate(context, boat->hero->id);
  209. state->removeObject(boat->hero);
  210. }
  211. }
  212. if (obj->ID == Obj::HERO)
  213. {
  214. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  215. if (hero->boat)
  216. {
  217. view->invalidate(context, hero->boat->id);
  218. state->removeObject(hero->boat);
  219. }
  220. }
  221. view->invalidate(context, obj->id);
  222. state->removeObject(obj);
  223. }
  224. void MapViewController::addObject(const CGObjectInstance * obj)
  225. {
  226. state->addObject(obj);
  227. view->invalidate(context, obj->id);
  228. }
  229. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  230. {
  231. if(isEventVisible(obj, from, dest))
  232. {
  233. fadeOutObject(obj);
  234. setViewCenter(obj->getSightCenter());
  235. }
  236. else
  237. removeObject(obj);
  238. }
  239. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  240. {
  241. if(isEventVisible(obj, from, dest))
  242. setViewCenter(obj->getSightCenter());
  243. }
  244. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  245. {
  246. if(isEventVisible(obj, from, dest))
  247. setViewCenter(obj->getSightCenter());
  248. }
  249. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  250. {
  251. if(isEventVisible(obj, from, dest))
  252. {
  253. fadeInObject(obj);
  254. setViewCenter(obj->getSightCenter());
  255. }
  256. addObject(obj);
  257. }
  258. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  259. {
  260. assert(!hasOngoingAnimations());
  261. if(isEventVisible(obj))
  262. fadeInObject(obj);
  263. addObject(obj);
  264. }
  265. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  266. {
  267. assert(!hasOngoingAnimations());
  268. if(isEventVisible(obj))
  269. fadeOutObject(obj);
  270. else
  271. removeObject(obj);
  272. }
  273. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  274. {
  275. addObject(obj);
  276. };
  277. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  278. {
  279. removeObject(obj);
  280. };
  281. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  282. {
  283. assert(!hasOngoingAnimations());
  284. if(isEventVisible(obj, from, dest))
  285. {
  286. setViewCenter(obj->getSightCenter());
  287. view->createTransitionSnapshot(context);
  288. }
  289. }
  290. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  291. {
  292. assert(!hasOngoingAnimations());
  293. const CGObjectInstance * movingObject = obj;
  294. if(obj->boat)
  295. movingObject = obj->boat;
  296. removeObject(movingObject);
  297. addObject(movingObject);
  298. if(isEventVisible(obj, from, dest))
  299. {
  300. teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
  301. teleportContext->animationTime = adventureContext->animationTime;
  302. adventureContext = teleportContext;
  303. context = teleportContext;
  304. setViewCenter(movingObject->getSightCenter());
  305. }
  306. }
  307. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  308. {
  309. assert(!hasOngoingAnimations());
  310. // revisiting via spacebar, no need to animate
  311. if(from == dest)
  312. return;
  313. const CGObjectInstance * movingObject = obj;
  314. if(obj->boat)
  315. movingObject = obj->boat;
  316. removeObject(movingObject);
  317. if(!isEventVisible(obj, from, dest))
  318. {
  319. addObject(movingObject);
  320. return;
  321. }
  322. double movementTime = LOCPLINT->playerID == obj->tempOwner ?
  323. settings["adventure"]["heroMoveTime"].Float() :
  324. settings["adventure"]["enemyMoveTime"].Float();
  325. if(movementTime > 1)
  326. {
  327. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  328. movementContext->animationTime = adventureContext->animationTime;
  329. adventureContext = movementContext;
  330. context = movementContext;
  331. state->addMovingObject(movingObject, from, dest);
  332. movementContext->target = movingObject->id;
  333. movementContext->tileFrom = from;
  334. movementContext->tileDest = dest;
  335. movementContext->progress = 0.0;
  336. }
  337. else // instant movement
  338. {
  339. addObject(movingObject);
  340. setViewCenter(movingObject->visitablePos());
  341. }
  342. }
  343. bool MapViewController::hasOngoingAnimations()
  344. {
  345. if(movementContext)
  346. return true;
  347. if(fadingOutContext)
  348. return true;
  349. if(fadingInContext)
  350. return true;
  351. if(teleportContext)
  352. return true;
  353. return false;
  354. }
  355. void MapViewController::activateAdventureContext(uint32_t animationTime)
  356. {
  357. resetContext();
  358. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  359. adventureContext->animationTime = animationTime;
  360. context = adventureContext;
  361. }
  362. void MapViewController::activateAdventureContext()
  363. {
  364. activateAdventureContext(0);
  365. }
  366. void MapViewController::activateWorldViewContext()
  367. {
  368. if(worldViewContext)
  369. return;
  370. resetContext();
  371. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  372. context = worldViewContext;
  373. }
  374. void MapViewController::activateSpellViewContext()
  375. {
  376. if(spellViewContext)
  377. return;
  378. resetContext();
  379. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  380. worldViewContext = spellViewContext;
  381. context = spellViewContext;
  382. }
  383. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  384. {
  385. resetContext();
  386. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  387. context = puzzleMapContext;
  388. CGPathNode fakeNode;
  389. fakeNode.coord = grailPosition;
  390. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  391. // create two nodes since 1st one is normally not visible
  392. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  393. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  394. }
  395. void MapViewController::resetContext()
  396. {
  397. adventureContext.reset();
  398. movementContext.reset();
  399. fadingOutContext.reset();
  400. fadingInContext.reset();
  401. teleportContext.reset();
  402. worldViewContext.reset();
  403. spellViewContext.reset();
  404. puzzleMapContext.reset();
  405. }
  406. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  407. {
  408. assert(spellViewContext);
  409. spellViewContext->showAllTerrain = showAllTerrain;
  410. }
  411. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  412. {
  413. assert(spellViewContext);
  414. spellViewContext->additionalOverlayIcons = objectPositions;
  415. }