MapView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
  140. {
  141. size_t baseFrameTime = 180;
  142. size_t frameCounter = animationTime / baseFrameTime;
  143. size_t frameIndex = frameCounter % groupSize;
  144. return frameIndex;
  145. }
  146. Point MapRendererContext::getTileSize() const
  147. {
  148. return Point(32, 32);
  149. }
  150. bool MapRendererContext::showGrid() const
  151. {
  152. return true; // settings["gameTweaks"]["showGrid"].Bool();
  153. }
  154. void MapViewController::setViewCenter(const int3 & position)
  155. {
  156. assert(context->isInMap(position));
  157. setViewCenter(Point(position) * model->getSingleTileSize(), position.z);
  158. }
  159. void MapViewController::setViewCenter(const Point & position, int level)
  160. {
  161. Point betterPosition = {
  162. vstd::clamp(position.x, 0, context->getMapSize().x * model->getSingleTileSize().x),
  163. vstd::clamp(position.y, 0, context->getMapSize().y * model->getSingleTileSize().y)
  164. };
  165. model->setViewCenter(betterPosition);
  166. model->setLevel(vstd::clamp(level, 0, context->getMapSize().z));
  167. }
  168. void MapViewController::setTileSize(const Point & tileSize)
  169. {
  170. model->setTileSize(tileSize);
  171. }
  172. std::shared_ptr<const MapViewModel> MapView::getModel() const
  173. {
  174. return model;
  175. }
  176. void MapViewModel::setTileSize(const Point & newValue)
  177. {
  178. tileSize = newValue;
  179. }
  180. void MapViewModel::setViewCenter(const Point & newValue)
  181. {
  182. viewCenter = newValue;
  183. }
  184. void MapViewModel::setViewDimensions(const Point & newValue)
  185. {
  186. viewDimensions = newValue;
  187. }
  188. void MapViewModel::setLevel(int newLevel)
  189. {
  190. mapLevel = newLevel;
  191. }
  192. Point MapViewModel::getSingleTileSize() const
  193. {
  194. return tileSize;
  195. }
  196. Point MapViewModel::getMapViewCenter() const
  197. {
  198. return viewCenter;
  199. }
  200. Point MapViewModel::getPixelsVisibleDimensions() const
  201. {
  202. return viewDimensions;
  203. }
  204. int MapViewModel::getLevel() const
  205. {
  206. return mapLevel;
  207. }
  208. Point MapViewModel::getTilesVisibleDimensions() const
  209. {
  210. // total number of potentially visible tiles is:
  211. // 1) number of completely visible tiles
  212. // 2) additional tile that might be partially visible from left/top size
  213. // 3) additional tile that might be partially visible from right/bottom size
  214. return {
  215. getPixelsVisibleDimensions().x / getSingleTileSize().x + 2,
  216. getPixelsVisibleDimensions().y / getSingleTileSize().y + 2,
  217. };
  218. }
  219. Rect MapViewModel::getTilesTotalRect() const
  220. {
  221. return Rect(
  222. Point(getTileAtPoint(Point(0,0))),
  223. getTilesVisibleDimensions()
  224. );
  225. }
  226. int3 MapViewModel::getTileAtPoint(const Point & position) const
  227. {
  228. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  229. Point absolutePosition = position + topLeftOffset;
  230. // NOTE: using division via double in order to use std::floor
  231. // which rounds to negative infinity and not towards zero (like integer division)
  232. return {
  233. static_cast<int>(std::floor(static_cast<double>(absolutePosition.x) / getSingleTileSize().x)),
  234. static_cast<int>(std::floor(static_cast<double>(absolutePosition.y) / getSingleTileSize().y)),
  235. getLevel()
  236. };
  237. }
  238. Point MapViewModel::getCacheDimensionsPixels() const
  239. {
  240. return getTilesVisibleDimensions() * getSingleTileSize();
  241. }
  242. Rect MapViewModel::getCacheTileArea(const int3 & coordinates) const
  243. {
  244. assert(mapLevel == coordinates.z);
  245. assert(getTilesVisibleDimensions().x + coordinates.x >= 0);
  246. assert(getTilesVisibleDimensions().y + coordinates.y >= 0);
  247. Point tileIndex{
  248. (getTilesVisibleDimensions().x + coordinates.x) % getTilesVisibleDimensions().x,
  249. (getTilesVisibleDimensions().y + coordinates.y) % getTilesVisibleDimensions().y
  250. };
  251. return Rect(tileIndex * tileSize, tileSize);
  252. }
  253. Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
  254. {
  255. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  256. Point tilePosAbsolute = Point(coordinates) * tileSize;
  257. Point tilePosRelative = tilePosAbsolute - topLeftOffset;
  258. return Rect(tilePosRelative, tileSize);
  259. }
  260. MapRendererContext::MapRendererContext()
  261. {
  262. auto mapSize = getMapSize();
  263. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  264. for(const auto & obj : CGI->mh->getMap()->objects)
  265. addObject(obj);
  266. }
  267. void MapRendererContext::addObject(const CGObjectInstance * obj)
  268. {
  269. if(!obj)
  270. return;
  271. for(int fx = 0; fx < obj->getWidth(); ++fx)
  272. {
  273. for(int fy = 0; fy < obj->getHeight(); ++fy)
  274. {
  275. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  276. if(isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  277. {
  278. auto & container = objects[currTile.z][currTile.x][currTile.y];
  279. container.push_back(obj->id);
  280. boost::range::sort(container, MapObjectsSorter(*this));
  281. }
  282. }
  283. }
  284. }
  285. void MapRendererContext::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  286. {
  287. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  288. int xDest = std::max(tileFrom.x, tileDest.x);
  289. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  290. int yDest = std::max(tileFrom.y, tileDest.y);
  291. for(int x = xFrom; x <= xDest; ++x)
  292. {
  293. for(int y = yFrom; y <= yDest; ++y)
  294. {
  295. int3 currTile(x, y, object->pos.z);
  296. if(isInMap(currTile))
  297. {
  298. auto & container = objects[currTile.z][currTile.x][currTile.y];
  299. container.push_back(object->id);
  300. boost::range::sort(container, MapObjectsSorter(*this));
  301. }
  302. }
  303. }
  304. }
  305. void MapRendererContext::removeObject(const CGObjectInstance * object)
  306. {
  307. for(int z = 0; z < getMapSize().z; z++)
  308. for(int x = 0; x < getMapSize().x; x++)
  309. for(int y = 0; y < getMapSize().y; y++)
  310. vstd::erase(objects[z][x][y], object->id);
  311. }
  312. const MapRendererContext::MapObjectsList & MapRendererContext::getObjects(const int3 & coordinates) const
  313. {
  314. assert(isInMap(coordinates));
  315. return objects[coordinates.z][coordinates.x][coordinates.y];
  316. }
  317. size_t MapRendererContext::objectGroupIndex(ObjectInstanceID objectID) const
  318. {
  319. const CGObjectInstance * obj = getObject(objectID);
  320. // TODO
  321. static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
  322. static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
  323. if(obj->ID == Obj::HERO)
  324. {
  325. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  326. if (movementAnimation && movementAnimation->target == objectID)
  327. return moveGroups[hero->moveDir];
  328. return idleGroups[hero->moveDir];
  329. }
  330. if(obj->ID == Obj::BOAT)
  331. {
  332. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  333. uint8_t direction = boat->hero ? boat->hero->moveDir : boat->direction;
  334. if (movementAnimation && movementAnimation->target == objectID)
  335. return moveGroups[direction];
  336. return idleGroups[direction];
  337. }
  338. return 0;
  339. }
  340. Point MapRendererContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
  341. {
  342. if (movementAnimation && movementAnimation->target == objectID)
  343. {
  344. int3 offsetTilesFrom = movementAnimation->tileFrom - coordinates;
  345. int3 offsetTilesDest = movementAnimation->tileDest - coordinates;
  346. Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32,32);
  347. Point offsetPixelsDest = Point(offsetTilesDest) * Point(32,32);
  348. Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, movementAnimation->progress);
  349. return result;
  350. }
  351. const CGObjectInstance * object = getObject(objectID);
  352. int3 offsetTiles(object->getPosition() - coordinates);
  353. return Point(offsetTiles) * Point(32, 32);
  354. }
  355. double MapRendererContext::objectTransparency(ObjectInstanceID objectID) const
  356. {
  357. const CGObjectInstance * object = getObject(objectID);
  358. if (object && object->ID == Obj::HERO)
  359. {
  360. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  361. if (hero->inTownGarrison)
  362. return 0;
  363. if (hero->boat)
  364. return 0;
  365. }
  366. if (fadeOutAnimation && objectID == fadeOutAnimation->target)
  367. return 1.0 - fadeOutAnimation->progress;
  368. if (fadeInAnimation && objectID == fadeInAnimation->target)
  369. return fadeInAnimation->progress;
  370. return 1.0;
  371. }
  372. MapViewController::MapViewController(std::shared_ptr<MapRendererContext> context, std::shared_ptr<MapViewModel> model)
  373. : context(std::move(context))
  374. , model(std::move(model))
  375. {
  376. }
  377. void MapViewController::update(uint32_t timeDelta)
  378. {
  379. // confirmed to match H3 for
  380. // - hero embarking on boat (500 ms)
  381. // - hero disembarking from boat (500 ms)
  382. // - TODO: picking up resources
  383. // - TODO: killing mosters
  384. // - teleporting ( 250 ms)
  385. static const double fadeOutDuration = 500;
  386. static const double fadeInDuration = 500;
  387. static const double heroTeleportDuration = 250;
  388. //FIXME: remove code duplication?
  389. if (context->movementAnimation)
  390. {
  391. // TODO: enemyMoveTime
  392. double heroMoveTime = settings["adventure"]["heroMoveTime"].Float();
  393. context->movementAnimation->progress += timeDelta / heroMoveTime;
  394. Point positionFrom = Point(context->movementAnimation->tileFrom) * model->getSingleTileSize();
  395. Point positionDest = Point(context->movementAnimation->tileDest) * model->getSingleTileSize();
  396. Point positionCurr = vstd::lerp(positionFrom, positionDest, context->movementAnimation->progress);
  397. setViewCenter(positionCurr, context->movementAnimation->tileDest.z);
  398. if (context->movementAnimation->progress >= 1.0)
  399. {
  400. setViewCenter(context->movementAnimation->tileDest);
  401. context->removeObject(context->getObject(context->movementAnimation->target));
  402. context->addObject(context->getObject(context->movementAnimation->target));
  403. context->movementAnimation.reset();
  404. }
  405. }
  406. if (context->teleportAnimation)
  407. {
  408. context->teleportAnimation->progress += timeDelta / heroTeleportDuration;
  409. if (context->teleportAnimation->progress >= 1.0)
  410. context->teleportAnimation.reset();
  411. }
  412. if (context->fadeOutAnimation)
  413. {
  414. context->fadeOutAnimation->progress += timeDelta / fadeOutDuration;
  415. if (context->fadeOutAnimation->progress >= 1.0)
  416. {
  417. context->removeObject(context->getObject(context->fadeOutAnimation->target));
  418. context->fadeOutAnimation.reset();
  419. }
  420. }
  421. if (context->fadeInAnimation)
  422. {
  423. context->fadeInAnimation->progress += timeDelta / fadeInDuration;
  424. if (context->fadeInAnimation->progress >= 1.0)
  425. context->fadeInAnimation.reset();
  426. }
  427. context->animationTime += timeDelta;
  428. context->tileSize = model->getSingleTileSize();
  429. }
  430. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  431. {
  432. assert(!context->fadeInAnimation);
  433. context->fadeInAnimation = FadingAnimationState{obj->id, 0.0};
  434. context->addObject(obj);
  435. }
  436. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  437. {
  438. assert(!context->fadeOutAnimation);
  439. context->fadeOutAnimation = FadingAnimationState{obj->id, 0.0};
  440. }
  441. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  442. {
  443. context->addObject(obj);
  444. };
  445. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  446. {
  447. context->removeObject(obj);
  448. };
  449. void MapViewController::onHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  450. {
  451. assert(!context->teleportAnimation);
  452. context->teleportAnimation = HeroAnimationState{ obj->id, from, dest, 0.0 };
  453. }
  454. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  455. {
  456. assert(!context->movementAnimation);
  457. const CGObjectInstance * movingObject = obj;
  458. if (obj->boat)
  459. movingObject = obj->boat;
  460. context->removeObject(movingObject);
  461. if (settings["adventure"]["heroMoveTime"].Float() > 1)
  462. {
  463. context->addMovingObject(movingObject, from, dest);
  464. context->movementAnimation = HeroAnimationState{ movingObject->id, from, dest, 0.0 };
  465. }
  466. else
  467. {
  468. // instant movement
  469. context->addObject(movingObject);
  470. }
  471. }
  472. void MapViewController::onHeroRotated(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  473. {
  474. //TODO
  475. }
  476. std::shared_ptr<MapViewController> MapView::getController()
  477. {
  478. return controller;
  479. }
  480. bool MapViewController::hasOngoingAnimations()
  481. {
  482. if(context->movementAnimation)
  483. return true;
  484. if(context->teleportAnimation)
  485. return true;
  486. if(context->fadeOutAnimation)
  487. return true;
  488. if(context->fadeInAnimation)
  489. return true;
  490. return false;
  491. }