MapViewController.cpp 16 KB

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