MapViewController.cpp 18 KB

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