MapView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * MapView.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 "MapView.h"
  12. #include <utility>
  13. #include "MapRenderer.h"
  14. #include "mapHandler.h"
  15. #include "CAdvMapInt.h"
  16. #include "../CGameInfo.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../render/CAnimation.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/Colors.h"
  23. #include "../render/Graphics.h"
  24. #include "../render/IImage.h"
  25. #include "../renderSDL/SDL_Extensions.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/CGeneralTextHandler.h"
  29. #include "../../lib/CRandomGenerator.h"
  30. #include "../../lib/CStopWatch.h"
  31. #include "../../lib/Color.h"
  32. #include "../../lib/RiverHandler.h"
  33. #include "../../lib/RoadHandler.h"
  34. #include "../../lib/TerrainHandler.h"
  35. #include "../../lib/mapObjects/CGHeroInstance.h"
  36. #include "../../lib/mapObjects/CObjectClassesHandler.h"
  37. #include "../../lib/mapping/CMap.h"
  38. MapViewCache::~MapViewCache() = default;
  39. MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
  40. : model(model)
  41. , mapRenderer(new MapRenderer())
  42. , terrain(new Canvas(model->getCacheDimensionsPixels()))
  43. {
  44. }
  45. Canvas MapViewCache::getTile(const int3 & coordinates)
  46. {
  47. return Canvas(*terrain, model->getCacheTileArea(coordinates));
  48. }
  49. void MapViewCache::updateTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates)
  50. {
  51. Canvas target = getTile(coordinates);
  52. mapRenderer->renderTile(*context, target, coordinates);
  53. }
  54. void MapViewCache::update(const std::shared_ptr<MapRendererContext> & context)
  55. {
  56. Rect dimensions = model->getTilesTotalRect();
  57. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  58. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  59. updateTile(context, {x, y, model->getLevel()});
  60. }
  61. void MapViewCache::render(Canvas & target)
  62. {
  63. Rect dimensions = model->getTilesTotalRect();
  64. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  65. {
  66. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  67. {
  68. int3 tile(x, y, model->getLevel());
  69. Canvas source = getTile(tile);
  70. Rect targetRect = model->getTargetTileArea(tile);
  71. target.draw(source, targetRect.topLeft());
  72. }
  73. }
  74. }
  75. std::shared_ptr<MapViewModel> MapView::createModel(const Point & dimensions) const
  76. {
  77. auto result = std::make_shared<MapViewModel>();
  78. result->setLevel(0);
  79. result->setTileSize(Point(32, 32));
  80. result->setViewCenter(Point(0, 0));
  81. result->setViewDimensions(dimensions);
  82. return result;
  83. }
  84. MapView::MapView(const Point & offset, const Point & dimensions)
  85. : model(createModel(dimensions))
  86. , context(new MapRendererContext())
  87. , controller(new MapViewController(context, model))
  88. , tilesCache(new MapViewCache(model))
  89. {
  90. pos += offset;
  91. pos.w = dimensions.x;
  92. pos.h = dimensions.y;
  93. }
  94. void MapView::show(SDL_Surface * to)
  95. {
  96. Canvas target(to);
  97. Canvas targetClipped(target, pos);
  98. CSDL_Ext::CClipRectGuard guard(to, pos);
  99. controller->update(GH.mainFPSmng->getElapsedMilliseconds());
  100. tilesCache->update(context);
  101. tilesCache->render(targetClipped);
  102. }
  103. void MapView::showAll(SDL_Surface * to)
  104. {
  105. show(to);
  106. }
  107. int3 MapRendererContext::getMapSize() const
  108. {
  109. return LOCPLINT->cb->getMapSize();
  110. }
  111. bool MapRendererContext::isInMap(const int3 & coordinates) const
  112. {
  113. return LOCPLINT->cb->isInTheMap(coordinates);
  114. }
  115. const TerrainTile & MapRendererContext::getMapTile(const int3 & coordinates) const
  116. {
  117. return CGI->mh->getMap()->getTile(coordinates);
  118. }
  119. const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID) const
  120. {
  121. return CGI->mh->getMap()->objects.at(objectID.getNum());
  122. }
  123. bool MapRendererContext::isVisible(const int3 & coordinates) const
  124. {
  125. return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool();
  126. }
  127. const CGPath * MapRendererContext::currentPath() const
  128. {
  129. const auto * hero = adventureInt->curHero();
  130. if(!hero)
  131. return nullptr;
  132. if(!LOCPLINT->paths.hasPath(hero))
  133. return nullptr;
  134. return &LOCPLINT->paths.getPath(hero);
  135. }
  136. uint32_t MapRendererContext::getAnimationPeriod() const
  137. {
  138. // H3 timing for adventure map objects animation is 180 ms
  139. // Terrain animations also use identical interval, however those are only present in HotA and/or HD Mod
  140. // TODO: duration of fade-in/fade-out for teleport, entering/leaving boat, removal of objects
  141. // TOOD: duration of hero movement animation, frame timing of hero movement animation, effect of hero speed option
  142. // TOOD: duration of enemy hero movement animation, frame timing of enemy hero movement animation, effect of enemy hero speed option
  143. return 180;
  144. }
  145. uint32_t MapRendererContext::getAnimationTime() const
  146. {
  147. return animationTime;
  148. }
  149. Point MapRendererContext::getTileSize() const
  150. {
  151. return Point(32, 32);
  152. }
  153. bool MapRendererContext::showGrid() const
  154. {
  155. return true; // settings["session"]["showGrid"].Bool();
  156. }
  157. void MapViewController::setViewCenter(const int3 & position)
  158. {
  159. model->setViewCenter(Point(position.x, position.y) * model->getSingleTileSize());
  160. model->setLevel(position.z);
  161. }
  162. void MapViewController::setViewCenter(const Point & position, int level)
  163. {
  164. model->setViewCenter(position);
  165. model->setLevel(level);
  166. }
  167. void MapViewController::setTileSize(const Point & tileSize)
  168. {
  169. model->setTileSize(tileSize);
  170. }
  171. std::shared_ptr<const MapViewModel> MapView::getModel() const
  172. {
  173. return model;
  174. }
  175. void MapViewModel::setTileSize(const Point & newValue)
  176. {
  177. tileSize = newValue;
  178. }
  179. void MapViewModel::setViewCenter(const Point & newValue)
  180. {
  181. viewCenter = newValue;
  182. }
  183. void MapViewModel::setViewDimensions(const Point & newValue)
  184. {
  185. viewDimensions = newValue;
  186. }
  187. void MapViewModel::setLevel(int newLevel)
  188. {
  189. mapLevel = newLevel;
  190. }
  191. Point MapViewModel::getSingleTileSize() const
  192. {
  193. return tileSize;
  194. }
  195. Point MapViewModel::getMapViewCenter() const
  196. {
  197. return viewCenter;
  198. }
  199. Point MapViewModel::getPixelsVisibleDimensions() const
  200. {
  201. return viewDimensions;
  202. }
  203. int MapViewModel::getLevel() const
  204. {
  205. return mapLevel;
  206. }
  207. Point MapViewModel::getTilesVisibleDimensions() const
  208. {
  209. // total number of potentially visible tiles is:
  210. // 1) number of completely visible tiles
  211. // 2) additional tile that might be partially visible from left/top size
  212. // 3) additional tile that might be partially visible from right/bottom size
  213. return {
  214. getPixelsVisibleDimensions().x / getSingleTileSize().x + 2,
  215. getPixelsVisibleDimensions().y / getSingleTileSize().y + 2,
  216. };
  217. }
  218. Rect MapViewModel::getTilesTotalRect() const
  219. {
  220. return Rect(
  221. Point(getTileAtPoint(Point(0,0))),
  222. getTilesVisibleDimensions()
  223. );
  224. }
  225. int3 MapViewModel::getTileAtPoint(const Point & position) const
  226. {
  227. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  228. Point absolutePosition = position + topLeftOffset;
  229. // NOTE: using division via double in order to use std::floor
  230. // which rounds to negative infinity and not towards zero (like integer division)
  231. return {
  232. static_cast<int>(std::floor(static_cast<double>(absolutePosition.x) / getSingleTileSize().x)),
  233. static_cast<int>(std::floor(static_cast<double>(absolutePosition.y) / getSingleTileSize().y)),
  234. getLevel()
  235. };
  236. }
  237. Point MapViewModel::getCacheDimensionsPixels() const
  238. {
  239. return getTilesVisibleDimensions() * getSingleTileSize();
  240. }
  241. Rect MapViewModel::getCacheTileArea(const int3 & coordinates) const
  242. {
  243. assert(mapLevel == coordinates.z);
  244. assert(getTilesVisibleDimensions().x + coordinates.x >= 0);
  245. assert(getTilesVisibleDimensions().y + coordinates.y >= 0);
  246. Point tileIndex{
  247. (getTilesVisibleDimensions().x + coordinates.x) % getTilesVisibleDimensions().x,
  248. (getTilesVisibleDimensions().y + coordinates.y) % getTilesVisibleDimensions().y
  249. };
  250. return Rect(tileIndex * tileSize, tileSize);
  251. }
  252. Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
  253. {
  254. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  255. Point tilePosAbsolute = Point(coordinates) * tileSize;
  256. Point tilePosRelative = tilePosAbsolute - topLeftOffset;
  257. return Rect(tilePosRelative, tileSize);
  258. }
  259. MapRendererContext::MapRendererContext()
  260. {
  261. auto mapSize = getMapSize();
  262. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  263. for(const auto & obj : CGI->mh->getMap()->objects)
  264. addObject(obj);
  265. }
  266. void MapRendererContext::addObject(const CGObjectInstance * obj)
  267. {
  268. if(!obj)
  269. return;
  270. for(int fx = 0; fx < obj->getWidth(); ++fx)
  271. {
  272. for(int fy = 0; fy < obj->getHeight(); ++fy)
  273. {
  274. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  275. if(isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  276. {
  277. auto & container = objects[currTile.z][currTile.x][currTile.y];
  278. container.push_back(obj->id);
  279. boost::range::sort(container, MapObjectsSorter(*this));
  280. }
  281. }
  282. }
  283. }
  284. void MapRendererContext::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  285. {
  286. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  287. int xDest = std::max(tileFrom.x, tileDest.x);
  288. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  289. int yDest = std::max(tileFrom.y, tileDest.y);
  290. for(int x = xFrom; x <= xDest; ++x)
  291. {
  292. for(int y = yFrom; y <= yDest; ++y)
  293. {
  294. int3 currTile(x, y, object->pos.z);
  295. if(isInMap(currTile))
  296. {
  297. auto & container = objects[currTile.z][currTile.x][currTile.y];
  298. container.push_back(object->id);
  299. boost::range::sort(container, MapObjectsSorter(*this));
  300. }
  301. }
  302. }
  303. }
  304. void MapRendererContext::removeObject(const CGObjectInstance * object)
  305. {
  306. for(int z = 0; z < getMapSize().z; z++)
  307. for(int x = 0; x < getMapSize().x; x++)
  308. for(int y = 0; y < getMapSize().y; y++)
  309. vstd::erase(objects[z][x][y], object->id);
  310. }
  311. const MapRendererContext::MapObjectsList & MapRendererContext::getObjects(const int3 & coordinates) const
  312. {
  313. assert(isInMap(coordinates));
  314. return objects[coordinates.z][coordinates.x][coordinates.y];
  315. }
  316. size_t MapRendererContext::objectGroupIndex(ObjectInstanceID objectID) const
  317. {
  318. const CGObjectInstance * obj = getObject(objectID);
  319. // TODO
  320. static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
  321. static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
  322. if(obj->ID == Obj::HERO)
  323. {
  324. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  325. if (movementAnimation && movementAnimation->target == objectID)
  326. return moveGroups[hero->moveDir];
  327. return idleGroups[hero->moveDir];
  328. }
  329. if(obj->ID == Obj::BOAT)
  330. {
  331. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  332. if (movementAnimation && movementAnimation->target == objectID)
  333. return moveGroups[boat->direction];
  334. return idleGroups[boat->direction];
  335. }
  336. return 0;
  337. }
  338. Point MapRendererContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  339. {
  340. if (movementAnimation && movementAnimation->target == objectID)
  341. {
  342. int3 offsetTilesFrom = movementAnimation->tileFrom - coordinates;
  343. int3 offsetTilesDest = movementAnimation->tileDest - coordinates;
  344. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32,32);
  345. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32,32);
  346. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, movementAnimation->progress);
  347. return result;
  348. }
  349. const CGObjectInstance * object = getObject(objectID);
  350. int3 offsetTiles(object->getPosition() - coordinates);
  351. return Point(offsetTiles) * Point(32, 32);
  352. }
  353. double MapRendererContext::objectTransparency(ObjectInstanceID objectID) const
  354. {
  355. if (fadeOutAnimation && objectID == fadeOutAnimation->target)
  356. return 1.0 - fadeOutAnimation->progress;
  357. if (fadeInAnimation && objectID == fadeInAnimation->target)
  358. return fadeInAnimation->progress;
  359. return 1.0;
  360. }
  361. MapViewController::MapViewController(std::shared_ptr<MapRendererContext> context, std::shared_ptr<MapViewModel> model)
  362. : context(std::move(context))
  363. , model(std::move(model))
  364. {
  365. }
  366. void MapViewController::update(uint32_t timeDelta)
  367. {
  368. static const double fadeOutDuration = 1.0;
  369. static const double fadeInDuration = 1.0;
  370. static const double heroMoveDuration = 1.0;
  371. static const double heroTeleportDuration = 1.0;
  372. //FIXME: remove code duplication?
  373. if (context->movementAnimation)
  374. {
  375. context->movementAnimation->progress += heroMoveDuration * timeDelta / 1000;
  376. Point positionFrom = Point(context->movementAnimation->tileFrom) * model->getSingleTileSize();
  377. Point positionDest = Point(context->movementAnimation->tileDest) * model->getSingleTileSize();
  378. Point positionCurr = vstd::lerp(positionFrom, positionDest, context->movementAnimation->progress);
  379. setViewCenter(positionCurr, context->movementAnimation->tileDest.z);
  380. if (context->movementAnimation->progress >= 1.0)
  381. {
  382. setViewCenter(context->movementAnimation->tileDest);
  383. context->removeObject(context->getObject(context->movementAnimation->target));
  384. context->addObject(context->getObject(context->movementAnimation->target));
  385. context->movementAnimation.reset();
  386. }
  387. }
  388. if (context->teleportAnimation)
  389. {
  390. context->teleportAnimation->progress += heroTeleportDuration * timeDelta / 1000;
  391. if (context->teleportAnimation->progress >= 1.0)
  392. context->teleportAnimation.reset();
  393. }
  394. if (context->fadeOutAnimation)
  395. {
  396. context->fadeOutAnimation->progress += fadeOutDuration * timeDelta / 1000;
  397. if (context->fadeOutAnimation->progress >= 1.0)
  398. {
  399. context->removeObject(context->getObject(context->fadeOutAnimation->target));
  400. context->fadeOutAnimation.reset();
  401. }
  402. }
  403. if (context->fadeInAnimation)
  404. {
  405. context->fadeInAnimation->progress += fadeInDuration * timeDelta / 1000;
  406. if (context->fadeInAnimation->progress >= 1.0)
  407. context->fadeInAnimation.reset();
  408. }
  409. context->animationTime += timeDelta;
  410. context->tileSize =model->getSingleTileSize();
  411. }
  412. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  413. {
  414. assert(!context->fadeInAnimation);
  415. context->fadeInAnimation = FadingAnimationState{obj->id, 0.0};
  416. context->addObject(obj);
  417. }
  418. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  419. {
  420. assert(!context->fadeOutAnimation);
  421. context->fadeOutAnimation = FadingAnimationState{obj->id, 0.0};
  422. }
  423. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  424. {
  425. context->addObject(obj);
  426. };
  427. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  428. {
  429. context->removeObject(obj);
  430. };
  431. void MapViewController::onHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  432. {
  433. assert(!context->teleportAnimation);
  434. context->teleportAnimation = HeroAnimationState{ obj->id, from, dest, 0.0 };
  435. }
  436. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  437. {
  438. assert(!context->movementAnimation);
  439. context->removeObject(obj);
  440. context->addMovingObject(obj, from, dest);
  441. context->movementAnimation = HeroAnimationState{ obj->id, from, dest, 0.0 };
  442. }
  443. void MapViewController::onHeroRotated(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  444. {
  445. //TODO
  446. }
  447. std::shared_ptr<MapViewController> MapView::getController()
  448. {
  449. return controller;
  450. }
  451. bool MapViewController::hasOngoingAnimations()
  452. {
  453. if(context->movementAnimation)
  454. return true;
  455. if(context->teleportAnimation)
  456. return true;
  457. if(context->fadeOutAnimation)
  458. return true;
  459. if(context->fadeInAnimation)
  460. return true;
  461. return false;
  462. }