2
0

MapViewController.cpp 18 KB

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