MapViewController.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 == 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 >= 1.0)
  189. {
  190. setViewCenter(hero->getSightCenter());
  191. removeObject(context->getObject(movementContext->target));
  192. addObject(context->getObject(movementContext->target));
  193. activateAdventureContext(movementContext->animationTime);
  194. }
  195. }
  196. if(teleportContext && teleportContext->progress >= 1.0)
  197. {
  198. activateAdventureContext(teleportContext->animationTime);
  199. }
  200. if(fadingOutContext && fadingOutContext->progress <= 0.0)
  201. {
  202. removeObject(context->getObject(fadingOutContext->target));
  203. activateAdventureContext(fadingOutContext->animationTime);
  204. }
  205. if(fadingInContext && fadingInContext->progress >= 1.0)
  206. {
  207. activateAdventureContext(fadingInContext->animationTime);
  208. }
  209. }
  210. bool MapViewController::isEventInstant(const CGObjectInstance * obj)
  211. {
  212. if (!isEventVisible(obj))
  213. return true;
  214. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() <= 0)
  215. return true; // instant movement speed
  216. if(LOCPLINT->makingTurn && settings["adventure"]["heroMoveTime"].Float() <= 0)
  217. return true; // instant movement speed
  218. return false;
  219. }
  220. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  221. {
  222. if(adventureContext == nullptr)
  223. return false;
  224. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  225. return false; // enemy move speed set to "hidden/none"
  226. if(!GH.windows().isTopWindow(adventureInt))
  227. return false;
  228. if(obj->isVisitable())
  229. return context->isVisible(obj->visitablePos());
  230. else
  231. return context->isVisible(obj->pos);
  232. }
  233. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  234. {
  235. if(adventureContext == nullptr)
  236. return false;
  237. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  238. return false; // enemy move speed set to "hidden/none"
  239. if(!GH.windows().isTopWindow(adventureInt))
  240. return false;
  241. if(context->isVisible(obj->convertToVisitablePos(from)))
  242. return true;
  243. if(context->isVisible(obj->convertToVisitablePos(dest)))
  244. return true;
  245. return false;
  246. }
  247. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  248. {
  249. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  250. fadingOutContext->animationTime = adventureContext->animationTime;
  251. adventureContext = fadingOutContext;
  252. context = fadingOutContext;
  253. const CGObjectInstance * movingObject = obj;
  254. if (obj->ID == Obj::HERO)
  255. {
  256. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  257. if (hero->boat)
  258. movingObject = hero->boat;
  259. }
  260. fadingOutContext->target = movingObject->id;
  261. fadingOutContext->progress = 1.0;
  262. }
  263. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  264. {
  265. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  266. fadingInContext->animationTime = adventureContext->animationTime;
  267. adventureContext = fadingInContext;
  268. context = fadingInContext;
  269. const CGObjectInstance * movingObject = obj;
  270. if (obj->ID == Obj::HERO)
  271. {
  272. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  273. if (hero->boat)
  274. movingObject = hero->boat;
  275. }
  276. fadingInContext->target = movingObject->id;
  277. fadingInContext->progress = 0.0;
  278. }
  279. void MapViewController::removeObject(const CGObjectInstance * obj)
  280. {
  281. if (obj->ID == Obj::BOAT)
  282. {
  283. auto * boat = dynamic_cast<const CGBoat*>(obj);
  284. if (boat->hero)
  285. {
  286. view->invalidate(context, boat->hero->id);
  287. state->removeObject(boat->hero);
  288. }
  289. }
  290. if (obj->ID == Obj::HERO)
  291. {
  292. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  293. if (hero->boat)
  294. {
  295. view->invalidate(context, hero->boat->id);
  296. state->removeObject(hero->boat);
  297. }
  298. }
  299. view->invalidate(context, obj->id);
  300. state->removeObject(obj);
  301. }
  302. void MapViewController::addObject(const CGObjectInstance * obj)
  303. {
  304. state->addObject(obj);
  305. view->invalidate(context, obj->id);
  306. }
  307. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  308. {
  309. if(isEventVisible(obj, from, dest))
  310. {
  311. if (!isEventInstant(obj))
  312. fadeOutObject(obj);
  313. setViewCenter(obj->getSightCenter());
  314. }
  315. else
  316. removeObject(obj);
  317. }
  318. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  319. {
  320. if(isEventVisible(obj, from, dest))
  321. setViewCenter(obj->getSightCenter());
  322. }
  323. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  324. {
  325. if(isEventVisible(obj, from, dest))
  326. setViewCenter(obj->getSightCenter());
  327. }
  328. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  329. {
  330. if(isEventVisible(obj, from, dest))
  331. {
  332. if (!isEventInstant(obj))
  333. fadeInObject(obj);
  334. setViewCenter(obj->getSightCenter());
  335. }
  336. addObject(obj);
  337. }
  338. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  339. {
  340. assert(!hasOngoingAnimations());
  341. if(isEventVisible(obj) && !isEventInstant(obj) )
  342. fadeInObject(obj);
  343. addObject(obj);
  344. }
  345. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  346. {
  347. assert(!hasOngoingAnimations());
  348. if(isEventVisible(obj) && !isEventInstant(obj) )
  349. fadeOutObject(obj);
  350. else
  351. removeObject(obj);
  352. }
  353. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  354. {
  355. addObject(obj);
  356. };
  357. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  358. {
  359. removeObject(obj);
  360. };
  361. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  362. {
  363. assert(!hasOngoingAnimations());
  364. if(isEventVisible(obj, from, dest))
  365. {
  366. setViewCenter(obj->getSightCenter());
  367. view->createTransitionSnapshot(context);
  368. }
  369. }
  370. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  371. {
  372. assert(!hasOngoingAnimations());
  373. const CGObjectInstance * movingObject = obj;
  374. if(obj->boat)
  375. movingObject = obj->boat;
  376. removeObject(movingObject);
  377. addObject(movingObject);
  378. if(isEventVisible(obj, from, dest))
  379. {
  380. teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
  381. teleportContext->animationTime = adventureContext->animationTime;
  382. adventureContext = teleportContext;
  383. context = teleportContext;
  384. setViewCenter(movingObject->getSightCenter());
  385. }
  386. }
  387. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  388. {
  389. assert(!hasOngoingAnimations());
  390. // revisiting via spacebar, no need to animate
  391. if(from == dest)
  392. return;
  393. const CGObjectInstance * movingObject = obj;
  394. if(obj->boat)
  395. movingObject = obj->boat;
  396. removeObject(movingObject);
  397. if(!isEventVisible(obj, from, dest))
  398. {
  399. addObject(movingObject);
  400. return;
  401. }
  402. double movementTime = LOCPLINT->playerID == obj->tempOwner ?
  403. settings["adventure"]["heroMoveTime"].Float() :
  404. settings["adventure"]["enemyMoveTime"].Float();
  405. if(movementTime > 1)
  406. {
  407. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  408. movementContext->animationTime = adventureContext->animationTime;
  409. adventureContext = movementContext;
  410. context = movementContext;
  411. state->addMovingObject(movingObject, from, dest);
  412. movementContext->target = movingObject->id;
  413. movementContext->tileFrom = from;
  414. movementContext->tileDest = dest;
  415. movementContext->progress = 0.0;
  416. }
  417. else // instant movement
  418. {
  419. addObject(movingObject);
  420. setViewCenter(movingObject->visitablePos());
  421. }
  422. }
  423. bool MapViewController::hasOngoingAnimations()
  424. {
  425. if(movementContext)
  426. return true;
  427. if(fadingOutContext)
  428. return true;
  429. if(fadingInContext)
  430. return true;
  431. if(teleportContext)
  432. return true;
  433. return false;
  434. }
  435. void MapViewController::activateAdventureContext(uint32_t animationTime)
  436. {
  437. resetContext();
  438. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  439. adventureContext->animationTime = animationTime;
  440. context = adventureContext;
  441. updateState();
  442. }
  443. void MapViewController::activateAdventureContext()
  444. {
  445. activateAdventureContext(0);
  446. }
  447. void MapViewController::activateWorldViewContext()
  448. {
  449. if(worldViewContext)
  450. return;
  451. resetContext();
  452. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  453. context = worldViewContext;
  454. }
  455. void MapViewController::activateSpellViewContext()
  456. {
  457. if(spellViewContext)
  458. return;
  459. resetContext();
  460. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  461. worldViewContext = spellViewContext;
  462. context = spellViewContext;
  463. }
  464. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  465. {
  466. resetContext();
  467. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  468. context = puzzleMapContext;
  469. CGPathNode fakeNode;
  470. fakeNode.coord = grailPosition;
  471. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  472. // create two nodes since 1st one is normally not visible
  473. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  474. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  475. }
  476. void MapViewController::resetContext()
  477. {
  478. adventureContext.reset();
  479. movementContext.reset();
  480. fadingOutContext.reset();
  481. fadingInContext.reset();
  482. teleportContext.reset();
  483. worldViewContext.reset();
  484. spellViewContext.reset();
  485. puzzleMapContext.reset();
  486. }
  487. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  488. {
  489. assert(spellViewContext);
  490. spellViewContext->showAllTerrain = showAllTerrain;
  491. }
  492. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  493. {
  494. assert(spellViewContext);
  495. spellViewContext->additionalOverlayIcons = objectPositions;
  496. }