MapViewController.cpp 16 KB

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