MapViewController.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "MapViewModel.h"
  15. #include "MapViewCache.h"
  16. #include "../adventureMap/CAdvMapInt.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../../lib/CConfigHandler.h"
  19. #include "../../lib/mapObjects/CGHeroInstance.h"
  20. #include "../../lib/mapObjects/MiscObjects.h"
  21. #include "../../lib/spells/ViewSpellInt.h"
  22. #include "../../lib/CPathfinder.h"
  23. void MapViewController::setViewCenter(const int3 & position)
  24. {
  25. assert(context->isInMap(position));
  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() + 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 = {
  50. vstd::clamp(position.x, lowerLimit.x, upperLimit.x),
  51. vstd::clamp(position.y, lowerLimit.y, upperLimit.y)
  52. };
  53. model->setViewCenter(betterPosition);
  54. model->setLevel(vstd::clamp(level, 0, context->getMapSize().z));
  55. if (adventureInt) // may be called before adventureInt is initialized
  56. adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel());
  57. }
  58. void MapViewController::setTileSize(const Point & tileSize)
  59. {
  60. model->setTileSize(tileSize);
  61. // force update of view center since changing tile size may invalidated it
  62. setViewCenter(model->getMapViewCenter(), model->getLevel());
  63. }
  64. MapViewController::MapViewController(std::shared_ptr<MapViewModel> model, std::shared_ptr<MapViewCache> view)
  65. : state(new MapRendererContextState())
  66. , model(std::move(model))
  67. , view(view)
  68. {
  69. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  70. context = adventureContext;
  71. }
  72. std::shared_ptr<IMapRendererContext> MapViewController::getContext() const
  73. {
  74. return context;
  75. }
  76. void MapViewController::update(uint32_t timeDelta)
  77. {
  78. // confirmed to match H3 for
  79. // - hero embarking on boat (500 ms)
  80. // - hero disembarking from boat (500 ms)
  81. // - TODO: picking up resources
  82. // - TODO: killing mosters
  83. // - teleporting ( 250 ms)
  84. static const double fadeOutDuration = 500;
  85. static const double fadeInDuration = 500;
  86. //static const double heroTeleportDuration = 250;
  87. //FIXME: remove code duplication?
  88. if(movementContext)
  89. {
  90. const auto * object = context->getObject(movementContext->target);
  91. const auto * hero = dynamic_cast<const CGHeroInstance*>(object);
  92. const auto * boat = dynamic_cast<const CGBoat*>(object);
  93. assert(boat || hero);
  94. if (!hero)
  95. hero = boat->hero;
  96. double heroMoveTime =
  97. LOCPLINT->makingTurn ?
  98. settings["adventure"]["heroMoveTime"].Float():
  99. settings["adventure"]["enemyMoveTime"].Float();
  100. movementContext->progress += timeDelta / heroMoveTime;
  101. Point positionFrom = Point(hero->convertToVisitablePos(movementContext->tileFrom)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  102. Point positionDest = Point(hero->convertToVisitablePos(movementContext->tileDest)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
  103. Point positionCurr = vstd::lerp(positionFrom, positionDest, movementContext->progress);
  104. if(movementContext->progress >= 1.0)
  105. {
  106. setViewCenter(hero->getSightCenter());
  107. removeObject(context->getObject(movementContext->target));
  108. addObject(context->getObject(movementContext->target));
  109. activateAdventureContext(movementContext->animationTime);
  110. }
  111. else
  112. {
  113. setViewCenter(positionCurr, movementContext->tileDest.z);
  114. }
  115. }
  116. //if(teleportContext)
  117. //{
  118. // teleportContext->progress += timeDelta / heroTeleportDuration;
  119. // moveFocusToSelection();
  120. // if(teleportContext->progress >= 1.0)
  121. // teleportContext.reset();
  122. //}
  123. if(fadingOutContext)
  124. {
  125. fadingOutContext->progress -= timeDelta / fadeOutDuration;
  126. if(fadingOutContext->progress <= 0.0)
  127. {
  128. removeObject(context->getObject(fadingOutContext->target));
  129. activateAdventureContext(fadingOutContext->animationTime);
  130. }
  131. }
  132. if(fadingInContext)
  133. {
  134. fadingInContext->progress += timeDelta / fadeInDuration;
  135. if(fadingInContext->progress >= 1.0)
  136. {
  137. activateAdventureContext(fadingInContext->animationTime);
  138. }
  139. }
  140. if (adventureContext)
  141. {
  142. adventureContext->animationTime += timeDelta;
  143. adventureContext->settingsSessionSpectate = settings["session"]["spectate"].Bool();
  144. adventureContext->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
  145. adventureContext->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
  146. adventureContext->settingShowGrid = settings["gameTweaks"]["showGrid"].Bool();
  147. adventureContext->settingShowVisitable = settings["session"]["showVisitable"].Bool();
  148. adventureContext->settingShowBlockable = settings["session"]["showBlockable"].Bool();
  149. }
  150. }
  151. bool MapViewController::isEventVisible(const CGObjectInstance * obj)
  152. {
  153. if (adventureContext == nullptr)
  154. return false;
  155. if (!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  156. return false; // enemy move speed set to "hidden/none"
  157. if (obj->isVisitable())
  158. return context->isVisible(obj->visitablePos());
  159. else
  160. return context->isVisible(obj->pos);
  161. }
  162. bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  163. {
  164. if (adventureContext == nullptr)
  165. return false;
  166. if (!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
  167. return false; // enemy move speed set to "hidden/none"
  168. if (context->isVisible(obj->convertToVisitablePos(from)))
  169. return true;
  170. if (context->isVisible(obj->convertToVisitablePos(dest)))
  171. return true;
  172. return false;
  173. }
  174. void MapViewController::fadeOutObject(const CGObjectInstance * obj)
  175. {
  176. fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  177. fadingOutContext->animationTime = adventureContext->animationTime;
  178. adventureContext = fadingOutContext;
  179. context = fadingOutContext;
  180. fadingOutContext->target = obj->id;
  181. fadingOutContext->progress = 1.0;
  182. }
  183. void MapViewController::fadeInObject(const CGObjectInstance * obj)
  184. {
  185. fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
  186. fadingInContext->animationTime = adventureContext->animationTime;
  187. adventureContext = fadingInContext;
  188. context = fadingInContext;
  189. fadingInContext->target = obj->id;
  190. fadingInContext->progress = 0.0;
  191. }
  192. void MapViewController::removeObject(const CGObjectInstance * obj)
  193. {
  194. view->invalidate(context, obj->id);
  195. state->removeObject(obj);
  196. }
  197. void MapViewController::addObject(const CGObjectInstance * obj)
  198. {
  199. state->addObject(obj);
  200. view->invalidate(context, obj->id);
  201. }
  202. void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  203. {
  204. if (isEventVisible(obj, from, dest))
  205. {
  206. onObjectFadeOut(obj);
  207. setViewCenter(obj->getSightCenter());
  208. }
  209. else
  210. removeObject(obj);
  211. }
  212. void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  213. {
  214. if (isEventVisible(obj, from, dest))
  215. setViewCenter(obj->getSightCenter());
  216. }
  217. void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  218. {
  219. if (isEventVisible(obj, from, dest))
  220. setViewCenter(obj->getSightCenter());
  221. }
  222. void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  223. {
  224. if (isEventVisible(obj, from, dest))
  225. {
  226. onObjectFadeIn(obj);
  227. setViewCenter(obj->getSightCenter());
  228. }
  229. addObject(obj);
  230. }
  231. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  232. {
  233. assert(!hasOngoingAnimations());
  234. if (isEventVisible(obj))
  235. fadeInObject(obj);
  236. addObject(obj);
  237. }
  238. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  239. {
  240. assert(!hasOngoingAnimations());
  241. if (isEventVisible(obj))
  242. fadeOutObject(obj);
  243. else
  244. removeObject(obj);
  245. }
  246. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  247. {
  248. addObject(obj);
  249. };
  250. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  251. {
  252. removeObject(obj);
  253. };
  254. void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  255. {
  256. assert(!hasOngoingAnimations());
  257. if (isEventVisible(obj, from, dest))
  258. {
  259. // TODO: generate view with old state
  260. setViewCenter(obj->getSightCenter());
  261. removeObject(obj);
  262. }
  263. }
  264. void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  265. {
  266. assert(!hasOngoingAnimations());
  267. if (isEventVisible(obj, from, dest))
  268. {
  269. // TODO: animation
  270. setViewCenter(obj->getSightCenter());
  271. addObject(obj);
  272. }
  273. else
  274. {
  275. removeObject(obj);
  276. addObject(obj);
  277. }
  278. }
  279. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  280. {
  281. assert(!hasOngoingAnimations());
  282. // revisiting via spacebar, no need to animate
  283. if (from == dest)
  284. return;
  285. const CGObjectInstance * movingObject = obj;
  286. if(obj->boat)
  287. movingObject = obj->boat;
  288. removeObject(movingObject);
  289. if (!isEventVisible(obj, from, dest))
  290. {
  291. addObject(movingObject);
  292. return;
  293. }
  294. double movementTime =
  295. LOCPLINT->playerID == obj->tempOwner ?
  296. settings["adventure"]["heroMoveTime"].Float():
  297. settings["adventure"]["enemyMoveTime"].Float();
  298. if(movementTime > 1)
  299. {
  300. movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
  301. movementContext->animationTime = adventureContext->animationTime;
  302. adventureContext = movementContext;
  303. context = movementContext;
  304. state->addMovingObject(movingObject, from, dest);
  305. movementContext->target = movingObject->id;
  306. movementContext->tileFrom = from;
  307. movementContext->tileDest = dest;
  308. movementContext->progress = 0.0;
  309. }
  310. else // instant movement
  311. {
  312. addObject(movingObject);
  313. setViewCenter(movingObject->visitablePos());
  314. }
  315. }
  316. bool MapViewController::hasOngoingAnimations()
  317. {
  318. if(movementContext)
  319. return true;
  320. if(fadingOutContext)
  321. return true;
  322. if(fadingInContext)
  323. return true;
  324. return false;
  325. }
  326. void MapViewController::activateAdventureContext(uint32_t animationTime)
  327. {
  328. resetContext();
  329. adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
  330. adventureContext->animationTime = animationTime;
  331. context = adventureContext;
  332. }
  333. void MapViewController::activateAdventureContext()
  334. {
  335. activateAdventureContext(0);
  336. }
  337. void MapViewController::activateWorldViewContext()
  338. {
  339. if (worldViewContext)
  340. return;
  341. resetContext();
  342. worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
  343. context = worldViewContext;
  344. }
  345. void MapViewController::activateSpellViewContext()
  346. {
  347. if (spellViewContext)
  348. return;
  349. resetContext();
  350. spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
  351. worldViewContext = spellViewContext;
  352. context = spellViewContext;
  353. }
  354. void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
  355. {
  356. resetContext();
  357. puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
  358. context = puzzleMapContext;
  359. CGPathNode fakeNode;
  360. fakeNode.coord = grailPosition;
  361. puzzleMapContext->grailPos = std::make_unique<CGPath>();
  362. // create two nodes since 1st one is normally not visible
  363. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  364. puzzleMapContext->grailPos->nodes.push_back(fakeNode);
  365. }
  366. void MapViewController::resetContext()
  367. {
  368. adventureContext.reset();
  369. movementContext.reset();
  370. fadingOutContext.reset();
  371. fadingInContext.reset();
  372. worldViewContext.reset();
  373. spellViewContext.reset();
  374. puzzleMapContext.reset();
  375. }
  376. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  377. {
  378. assert(spellViewContext);
  379. spellViewContext->showAllTerrain = showAllTerrain;
  380. }
  381. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  382. {
  383. assert(spellViewContext);
  384. spellViewContext->additionalOverlayIcons = objectPositions;
  385. }