MapViewController.cpp 16 KB

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