MapView.cpp 16 KB

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