MapView.cpp 16 KB

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