MapViewController.cpp 17 KB

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