MapViewController.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 = targetTileSize.x / static_cast<double>(defaultTileSize);
  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 = targetTileSize;
  79. Point desiredZoom = Point(defaultTileSize,defaultTileSize) * 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. {
  92. targetTileSize = actualZoom;
  93. if(actualZoom.x >= defaultTileSize - zoomTileDeadArea && actualZoom.x <= defaultTileSize + zoomTileDeadArea)
  94. actualZoom.x = defaultTileSize;
  95. if(actualZoom.y >= defaultTileSize - zoomTileDeadArea && actualZoom.y <= defaultTileSize + zoomTileDeadArea)
  96. actualZoom.y = defaultTileSize;
  97. setTileSize(actualZoom);
  98. }
  99. }
  100. MapViewController::MapViewController(std::shared_ptr<MapViewModel> model, std::shared_ptr<MapViewCache> view)
  101. : state(new MapRendererContextState())
  102. , model(std::move(model))
  103. , view(view)
  104. {
  105. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  106. context = adventureContext;
  107. }
  108. std::shared_ptr<IMapRendererContext> MapViewController::getContext() const
  109. {
  110. return context;
  111. }
  112. void MapViewController::tick(uint32_t timeDelta)
  113. {
  114. // confirmed to match H3 for
  115. // - hero embarking on boat (500 ms)
  116. // - hero disembarking from boat (500 ms)
  117. // - TODO: picking up resources
  118. // - TODO: killing mosters
  119. // - teleporting ( 250 ms)
  120. static const double fadeOutDuration = 500;
  121. static const double fadeInDuration = 500;
  122. static const double heroTeleportDuration = 250;
  123. if(movementContext)
  124. {
  125. const auto * object = context->getObject(movementContext->target);
  126. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  127. const auto * boat = dynamic_cast<const CGBoat *>(object);
  128. assert(boat || hero);
  129. if(!hero)
  130. hero = boat->hero;
  131. double heroMoveTime = LOCPLINT->makingTurn ?
  132. settings["adventure"]["heroMoveTime"].Float() :
  133. settings["adventure"]["enemyMoveTime"].Float();
  134. movementContext->progress += timeDelta / heroMoveTime;
  135. movementContext->progress = std::min( 1.0, movementContext->progress);
  136. Point positionFrom = Point(hero->convertToVisitablePos(movementContext->tileFrom)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  137. Point positionDest = Point(hero->convertToVisitablePos(movementContext->tileDest)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  138. Point positionCurr = vstd::lerp(positionFrom, positionDest, movementContext->progress);
  139. setViewCenter(positionCurr, movementContext->tileDest.z);
  140. }
  141. if(teleportContext)
  142. {
  143. teleportContext->progress += timeDelta / heroTeleportDuration;
  144. teleportContext->progress = std::min( 1.0, teleportContext->progress);
  145. }
  146. if(fadingOutContext)
  147. {
  148. fadingOutContext->progress -= timeDelta / fadeOutDuration;
  149. fadingOutContext->progress = std::max( 0.0, fadingOutContext->progress);
  150. }
  151. if(fadingInContext)
  152. {
  153. fadingInContext->progress += timeDelta / fadeInDuration;
  154. fadingInContext->progress = std::min( 1.0, fadingInContext->progress);
  155. }
  156. if (adventureContext)
  157. adventureContext->animationTime += timeDelta;
  158. updateState();
  159. }
  160. void MapViewController::updateState()
  161. {
  162. if(adventureContext)
  163. {
  164. adventureContext->settingsSessionSpectate = settings["session"]["spectate"].Bool();
  165. adventureContext->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
  166. adventureContext->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
  167. adventureContext->settingShowGrid = settings["gameTweaks"]["showGrid"].Bool();
  168. adventureContext->settingShowVisitable = settings["session"]["showVisitable"].Bool();
  169. adventureContext->settingShowBlocked = settings["session"]["showBlocked"].Bool();
  170. adventureContext->settingSpellRange = settings["session"]["showSpellRange"].Bool();
  171. }
  172. }
  173. void MapViewController::afterRender()
  174. {
  175. if(movementContext)
  176. {
  177. const auto * object = context->getObject(movementContext->target);
  178. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  179. const auto * boat = dynamic_cast<const CGBoat *>(object);
  180. assert(boat || hero);
  181. if(!hero)
  182. hero = boat->hero;
  183. if(movementContext->progress >= 1.0)
  184. {
  185. setViewCenter(hero->getSightCenter());
  186. removeObject(context->getObject(movementContext->target));
  187. addObject(context->getObject(movementContext->target));
  188. activateAdventureContext(movementContext->animationTime);
  189. }
  190. }
  191. if(teleportContext && teleportContext->progress >= 1.0)
  192. {
  193. activateAdventureContext(teleportContext->animationTime);
  194. }
  195. if(fadingOutContext && fadingOutContext->progress <= 0.0)
  196. {
  197. removeObject(context->getObject(fadingOutContext->target));
  198. activateAdventureContext(fadingOutContext->animationTime);
  199. }
  200. if(fadingInContext && fadingInContext->progress >= 1.0)
  201. {
  202. activateAdventureContext(fadingInContext->animationTime);
  203. }
  204. }
  205. bool MapViewController::isEventInstant(const CGObjectInstance * obj)
  206. {
  207. if (!isEventVisible(obj))
  208. return true;
  209. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() <= 0)
  210. return true; // instant movement speed
  211. if(LOCPLINT->makingTurn && settings["adventure"]["heroMoveTime"].Float() <= 0)
  212. return true; // instant movement speed
  213. return false;
  214. }
  215. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  216. {
  217. if(adventureContext == nullptr)
  218. return false;
  219. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  220. return false; // enemy move speed set to "hidden/none"
  221. if(!GH.windows().isTopWindow(adventureInt))
  222. return false;
  223. if(obj->isVisitable())
  224. return context->isVisible(obj->visitablePos());
  225. else
  226. return context->isVisible(obj->pos);
  227. }
  228. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  229. {
  230. if(adventureContext == nullptr)
  231. return false;
  232. if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  233. return false; // enemy move speed set to "hidden/none"
  234. if(!GH.windows().isTopWindow(adventureInt))
  235. return false;
  236. if(context->isVisible(obj->convertToVisitablePos(from)))
  237. return true;
  238. if(context->isVisible(obj->convertToVisitablePos(dest)))
  239. return true;
  240. return false;
  241. }
  242. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  243. {
  244. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  245. fadingOutContext->animationTime = adventureContext->animationTime;
  246. adventureContext = fadingOutContext;
  247. context = fadingOutContext;
  248. const CGObjectInstance * movingObject = obj;
  249. if (obj->ID == Obj::HERO)
  250. {
  251. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  252. if (hero->boat)
  253. movingObject = hero->boat;
  254. }
  255. fadingOutContext->target = movingObject->id;
  256. fadingOutContext->progress = 1.0;
  257. }
  258. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  259. {
  260. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  261. fadingInContext->animationTime = adventureContext->animationTime;
  262. adventureContext = fadingInContext;
  263. context = fadingInContext;
  264. const CGObjectInstance * movingObject = obj;
  265. if (obj->ID == Obj::HERO)
  266. {
  267. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  268. if (hero->boat)
  269. movingObject = hero->boat;
  270. }
  271. fadingInContext->target = movingObject->id;
  272. fadingInContext->progress = 0.0;
  273. }
  274. void MapViewController::removeObject(const CGObjectInstance * obj)
  275. {
  276. if (obj->ID == Obj::BOAT)
  277. {
  278. auto * boat = dynamic_cast<const CGBoat*>(obj);
  279. if (boat->hero)
  280. {
  281. view->invalidate(context, boat->hero->id);
  282. state->removeObject(boat->hero);
  283. }
  284. }
  285. if (obj->ID == Obj::HERO)
  286. {
  287. auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
  288. if (hero->boat)
  289. {
  290. view->invalidate(context, hero->boat->id);
  291. state->removeObject(hero->boat);
  292. }
  293. }
  294. view->invalidate(context, obj->id);
  295. state->removeObject(obj);
  296. }
  297. void MapViewController::addObject(const CGObjectInstance * obj)
  298. {
  299. state->addObject(obj);
  300. view->invalidate(context, obj->id);
  301. }
  302. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  303. {
  304. if(isEventVisible(obj, from, dest))
  305. {
  306. if (!isEventInstant(obj))
  307. fadeOutObject(obj);
  308. setViewCenter(obj->getSightCenter());
  309. }
  310. else
  311. removeObject(obj);
  312. }
  313. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  314. {
  315. if(isEventVisible(obj, from, dest))
  316. setViewCenter(obj->getSightCenter());
  317. }
  318. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  319. {
  320. if(isEventVisible(obj, from, dest))
  321. setViewCenter(obj->getSightCenter());
  322. }
  323. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  324. {
  325. if(isEventVisible(obj, from, dest))
  326. {
  327. if (!isEventInstant(obj))
  328. fadeInObject(obj);
  329. setViewCenter(obj->getSightCenter());
  330. }
  331. addObject(obj);
  332. }
  333. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  334. {
  335. assert(!hasOngoingAnimations());
  336. if(isEventVisible(obj) && !isEventInstant(obj) )
  337. fadeInObject(obj);
  338. addObject(obj);
  339. }
  340. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  341. {
  342. assert(!hasOngoingAnimations());
  343. if(isEventVisible(obj) && !isEventInstant(obj) )
  344. fadeOutObject(obj);
  345. else
  346. removeObject(obj);
  347. }
  348. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  349. {
  350. addObject(obj);
  351. };
  352. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  353. {
  354. removeObject(obj);
  355. };
  356. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  357. {
  358. assert(!hasOngoingAnimations());
  359. if(isEventVisible(obj, from, dest))
  360. {
  361. setViewCenter(obj->getSightCenter());
  362. view->createTransitionSnapshot(context);
  363. }
  364. }
  365. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  366. {
  367. assert(!hasOngoingAnimations());
  368. const CGObjectInstance * movingObject = obj;
  369. if(obj->boat)
  370. movingObject = obj->boat;
  371. removeObject(movingObject);
  372. addObject(movingObject);
  373. if(isEventVisible(obj, from, dest))
  374. {
  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. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  403. movementContext->animationTime = adventureContext->animationTime;
  404. adventureContext = movementContext;
  405. context = movementContext;
  406. state->addMovingObject(movingObject, from, dest);
  407. movementContext->target = movingObject->id;
  408. movementContext->tileFrom = from;
  409. movementContext->tileDest = dest;
  410. movementContext->progress = 0.0;
  411. }
  412. else // instant movement
  413. {
  414. addObject(movingObject);
  415. setViewCenter(movingObject->visitablePos());
  416. }
  417. }
  418. bool MapViewController::hasOngoingAnimations()
  419. {
  420. if(movementContext)
  421. return true;
  422. if(fadingOutContext)
  423. return true;
  424. if(fadingInContext)
  425. return true;
  426. if(teleportContext)
  427. return true;
  428. return false;
  429. }
  430. void MapViewController::activateAdventureContext(uint32_t animationTime)
  431. {
  432. resetContext();
  433. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  434. adventureContext->animationTime = animationTime;
  435. context = adventureContext;
  436. updateState();
  437. }
  438. void MapViewController::activateAdventureContext()
  439. {
  440. activateAdventureContext(0);
  441. }
  442. void MapViewController::activateWorldViewContext()
  443. {
  444. if(worldViewContext)
  445. return;
  446. resetContext();
  447. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  448. context = worldViewContext;
  449. }
  450. void MapViewController::activateSpellViewContext()
  451. {
  452. if(spellViewContext)
  453. return;
  454. resetContext();
  455. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  456. worldViewContext = spellViewContext;
  457. context = spellViewContext;
  458. }
  459. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  460. {
  461. resetContext();
  462. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  463. context = puzzleMapContext;
  464. CGPathNode fakeNode;
  465. fakeNode.coord = grailPosition;
  466. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  467. // create two nodes since 1st one is normally not visible
  468. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  469. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  470. }
  471. void MapViewController::resetContext()
  472. {
  473. adventureContext.reset();
  474. movementContext.reset();
  475. fadingOutContext.reset();
  476. fadingInContext.reset();
  477. teleportContext.reset();
  478. worldViewContext.reset();
  479. spellViewContext.reset();
  480. puzzleMapContext.reset();
  481. }
  482. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  483. {
  484. assert(spellViewContext);
  485. spellViewContext->showAllTerrain = showAllTerrain;
  486. }
  487. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  488. {
  489. assert(spellViewContext);
  490. spellViewContext->additionalOverlayIcons = objectPositions;
  491. }