MapViewController.cpp 17 KB

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