MapRenderer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * MapRenderer.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 "MapRenderer.h"
  12. #include "MapRendererContext.h"
  13. #include "mapHandler.h"
  14. #include "../CGameInfo.h"
  15. #include "../render/CAnimation.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/IImage.h"
  18. #include "../../CCallback.h"
  19. #include "../../lib/RiverHandler.h"
  20. #include "../../lib/RoadHandler.h"
  21. #include "../../lib/TerrainHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapping/CMap.h"
  24. #include "../../lib/CPathfinder.h"
  25. struct NeighborTilesInfo
  26. {
  27. bool d7, //789
  28. d8, //456
  29. d9, //123
  30. d4,
  31. d5,
  32. d6,
  33. d1,
  34. d2,
  35. d3;
  36. NeighborTilesInfo( const IMapRendererContext & context, const int3 & pos)
  37. {
  38. auto getTile = [&](int dx, int dy)->bool
  39. {
  40. if ( dx + pos.x < 0 || dx + pos.x >= context.getMapSize().x
  41. || dy + pos.y < 0 || dy + pos.y >= context.getMapSize().y)
  42. return false;
  43. //FIXME: please do not read settings for every tile...
  44. return context.isVisible( pos + int3(dx, dy, 0));
  45. };
  46. d7 = getTile(-1, -1); //789
  47. d8 = getTile( 0, -1); //456
  48. d9 = getTile(+1, -1); //123
  49. d4 = getTile(-1, 0);
  50. d5 = getTile( 0, 0);
  51. d6 = getTile(+1, 0);
  52. d1 = getTile(-1, +1);
  53. d2 = getTile( 0, +1);
  54. d3 = getTile(+1, +1);
  55. }
  56. bool areAllHidden() const
  57. {
  58. return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d9);
  59. }
  60. int getBitmapID() const
  61. {
  62. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  63. // 0-1, 2-3, 4-5, 11-13, 12-14
  64. static const int visBitmaps[256] = {
  65. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  66. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  67. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  68. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  69. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  70. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  71. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  72. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  73. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  74. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  75. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  76. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  77. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  78. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  79. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  80. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  81. };
  82. return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  83. }
  84. };
  85. MapObjectsSorter::MapObjectsSorter(const IMapRendererContext & context)
  86. : context(context)
  87. {
  88. }
  89. bool MapObjectsSorter::operator()(const ObjectInstanceID & left, const ObjectInstanceID & right) const
  90. {
  91. return (*this)(context.getObject(left), context.getObject(right));
  92. }
  93. bool MapObjectsSorter::operator()(const CGObjectInstance * left, const CGObjectInstance * right) const
  94. {
  95. //FIXME: remove mh access
  96. return CGI->mh->compareObjectBlitOrder(left, right);
  97. }
  98. MapTileStorage::MapTileStorage(size_t capacity)
  99. : animations(capacity)
  100. {
  101. }
  102. void MapTileStorage::load(size_t index, const std::string & filename)
  103. {
  104. auto & terrainAnimations = animations[index];
  105. for(auto & entry : terrainAnimations)
  106. {
  107. entry = std::make_unique<CAnimation>(filename);
  108. entry->preload();
  109. }
  110. for(size_t i = 0; i < terrainAnimations[0]->size(); ++i)
  111. {
  112. terrainAnimations[1]->getImage(i)->verticalFlip();
  113. terrainAnimations[3]->getImage(i)->verticalFlip();
  114. terrainAnimations[2]->getImage(i)->horizontalFlip();
  115. terrainAnimations[3]->getImage(i)->horizontalFlip();
  116. }
  117. }
  118. std::shared_ptr<IImage> MapTileStorage::find(size_t fileIndex, size_t rotationIndex, size_t imageIndex)
  119. {
  120. const auto & animation = animations[fileIndex][rotationIndex];
  121. return animation->getImage(imageIndex);
  122. }
  123. MapRendererTerrain::MapRendererTerrain()
  124. : storage(VLC->terrainTypeHandler->objects.size())
  125. {
  126. for(const auto & terrain : VLC->terrainTypeHandler->objects)
  127. storage.load(terrain->getIndex(), terrain->tilesFilename);
  128. }
  129. void MapRendererTerrain::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  130. {
  131. const TerrainTile & mapTile = context.getMapTile(coordinates);
  132. int32_t terrainIndex = mapTile.terType->getIndex();
  133. int32_t imageIndex = mapTile.terView;
  134. int32_t rotationIndex = mapTile.extTileFlags % 4;
  135. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  136. target.draw(image, Point(0, 0));
  137. }
  138. MapRendererRiver::MapRendererRiver()
  139. : storage(VLC->riverTypeHandler->objects.size())
  140. {
  141. for(const auto & river : VLC->riverTypeHandler->objects)
  142. storage.load(river->getIndex(), river->tilesFilename);
  143. }
  144. void MapRendererRiver::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  145. {
  146. const TerrainTile & mapTile = context.getMapTile(coordinates);
  147. if(mapTile.riverType->getId() != River::NO_RIVER)
  148. {
  149. int32_t terrainIndex = mapTile.riverType->getIndex();
  150. int32_t imageIndex = mapTile.riverDir;
  151. int32_t rotationIndex = (mapTile.extTileFlags >> 2) % 4;
  152. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  153. target.draw(image, Point(0, 0));
  154. }
  155. }
  156. MapRendererRoad::MapRendererRoad():
  157. storage(VLC->roadTypeHandler->objects.size())
  158. {
  159. for(const auto & road : VLC->roadTypeHandler->objects)
  160. storage.load(road->getIndex(), road->tilesFilename);
  161. }
  162. void MapRendererRoad::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  163. {
  164. const int3 coordinatesAbove = coordinates - int3(0,1,0);
  165. if (context.isInMap(coordinatesAbove))
  166. {
  167. const TerrainTile & mapTileAbove = context.getMapTile(coordinatesAbove);
  168. if (mapTileAbove.roadType->getId() != Road::NO_ROAD)
  169. {
  170. int32_t terrainIndex = mapTileAbove.roadType->getIndex();
  171. int32_t imageIndex = mapTileAbove.roadDir;
  172. int32_t rotationIndex = (mapTileAbove.extTileFlags >> 4) % 4;
  173. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  174. target.draw(image, Point(0,0), Rect(0, 16, 32, 16));
  175. }
  176. }
  177. const TerrainTile & mapTile = context.getMapTile(coordinates);
  178. if(mapTile.roadType->getId() != Road::NO_ROAD)
  179. {
  180. int32_t terrainIndex = mapTile.roadType->getIndex();
  181. int32_t imageIndex = mapTile.roadDir;
  182. int32_t rotationIndex = (mapTile.extTileFlags >> 4) % 4;
  183. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  184. target.draw(image, Point(0,16), Rect(0, 0, 32, 16));
  185. }
  186. }
  187. MapRendererBorder::MapRendererBorder()
  188. {
  189. animation = std::make_unique<CAnimation>("EDG");
  190. animation->preload();
  191. }
  192. size_t MapRendererBorder::getIndexForTile(const IMapRendererContext & context, const int3 & tile)
  193. {
  194. assert(!context.isInMap(tile));
  195. int3 size = context.getMapSize();
  196. if (tile.x < -1 || tile.x > size.x || tile.y < -1 || tile.y > size.y)
  197. return std::abs(tile.x) % 4 + 4*(std::abs(tile.y) % 4);
  198. if (tile.x == -1 && tile.y == -1)
  199. return 16;
  200. if (tile.x == size.x && tile.y == -1)
  201. return 17;
  202. if (tile.x == size.x && tile.y == size.y)
  203. return 18;
  204. if (tile.x == -1 && tile.y == size.y)
  205. return 19;
  206. if (tile.y == -1)
  207. return 20 + (tile.x % 4);
  208. if (tile.x == size.x)
  209. return 24 + (tile.y % 4);
  210. if (tile.y == size.y)
  211. return 28 + (tile.x % 4);
  212. if (tile.x == -1)
  213. return 32 + (tile.y % 4);
  214. //else - visible area, no renderable border
  215. assert(0);
  216. return 0;
  217. }
  218. void MapRendererBorder::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  219. {
  220. const auto & image = animation->getImage(getIndexForTile(context, coordinates));
  221. target.draw(image, Point(0,0));
  222. }
  223. MapRendererFow::MapRendererFow()
  224. {
  225. fogOfWarFullHide = std::make_unique<CAnimation>("TSHRC");
  226. fogOfWarFullHide->preload();
  227. fogOfWarPartialHide = std::make_unique<CAnimation>("TSHRE");
  228. fogOfWarPartialHide->preload();
  229. static const std::vector<int> rotations = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  230. size_t size = fogOfWarPartialHide->size(0);//group size after next rotation
  231. for(const int rotation : rotations)
  232. {
  233. fogOfWarPartialHide->duplicateImage(0, rotation, 0);
  234. auto image = fogOfWarPartialHide->getImage(size, 0);
  235. image->verticalFlip();
  236. size++;
  237. }
  238. }
  239. void MapRendererFow::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  240. {
  241. assert(!context.isVisible(coordinates));
  242. const NeighborTilesInfo neighborInfo(context, coordinates);
  243. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  244. if (retBitmapID < 0)
  245. {
  246. // generate a number that is predefined for each tile,
  247. // but appears random to break visible pattern in large areas of fow
  248. // current approach (use primes as magic numbers for formula) looks to be suitable
  249. size_t pseudorandomNumber = ((coordinates.x * 997) ^ (coordinates.y * 1009)) / 101;
  250. size_t imageIndex = pseudorandomNumber % fogOfWarFullHide->size();
  251. target.draw(fogOfWarFullHide->getImage(imageIndex), Point(0,0));
  252. }
  253. else
  254. {
  255. target.draw(fogOfWarPartialHide->getImage(retBitmapID), Point(0,0));
  256. }
  257. }
  258. std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const CGObjectInstance* obj)
  259. {
  260. const auto & info = obj->appearance;
  261. //the only(?) invisible object
  262. if(info->id == Obj::EVENT)
  263. return std::shared_ptr<CAnimation>();
  264. if(info->animationFile.empty())
  265. {
  266. logGlobal->warn("Def name for obj (%d,%d) is empty!", info->id, info->subid);
  267. return std::shared_ptr<CAnimation>();
  268. }
  269. bool generateMovementGroups = (info->id == Obj::BOAT) || (info->id == Obj::HERO);
  270. return getAnimation(info->animationFile, generateMovementGroups);
  271. }
  272. std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups)
  273. {
  274. if (animations.count(filename))
  275. return animations[filename];
  276. auto ret = std::make_shared<CAnimation>(filename);
  277. animations[filename] = ret;
  278. ret->preload();
  279. if (generateMovementGroups)
  280. {
  281. ret->createFlippedGroup(1,13);
  282. ret->createFlippedGroup(2,14);
  283. ret->createFlippedGroup(3,15);
  284. ret->createFlippedGroup(6,10);
  285. ret->createFlippedGroup(7,11);
  286. ret->createFlippedGroup(8,12);
  287. }
  288. return ret;
  289. }
  290. MapRendererObjects::MapRendererObjects(const IMapRendererContext & context)
  291. {
  292. auto mapSize = context.getMapSize();
  293. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  294. for(const auto & obj : context.getAllObjects())
  295. onObjectInstantAdd(context, obj);
  296. }
  297. std::shared_ptr<CAnimation> MapRendererObjects::getFlagAnimation(const CGObjectInstance* obj)
  298. {
  299. //TODO: relocate to config file?
  300. static const std::vector<std::string> heroFlags = {
  301. "AF00", "AF01", "AF02", "AF03", "AF04", "AF05", "AF06", "AF07"
  302. };
  303. //TODO: relocate to config file?
  304. static const std::vector<std::vector<std::string>> boatFlags = {
  305. {"ABF01L", "ABF01G", "ABF01R", "ABF01D", "ABF01B", "ABF01P", "ABF01W", "ABF01K"},
  306. {"ABF02L", "ABF02G", "ABF02R", "ABF02D", "ABF02B", "ABF02P", "ABF02W", "ABF02K"},
  307. {"ABF03L", "ABF03G", "ABF03R", "ABF03D", "ABF03B", "ABF03P", "ABF03W", "ABF03K"}
  308. };
  309. if(obj->ID == Obj::HERO)
  310. {
  311. assert(dynamic_cast<const CGHeroInstance *>(obj) != nullptr);
  312. assert(obj->tempOwner.isValidPlayer());
  313. return getAnimation(heroFlags[obj->tempOwner.getNum()], true);
  314. }
  315. if(obj->ID == Obj::BOAT)
  316. {
  317. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  318. assert(boat);
  319. assert(boat->subID < boatFlags.size());
  320. assert(!boat->hero || boat->hero->tempOwner.isValidPlayer());
  321. if(boat->hero)
  322. return getAnimation(boatFlags[obj->subID][boat->hero->tempOwner.getNum()], true);
  323. }
  324. return nullptr;
  325. }
  326. std::shared_ptr<IImage> MapRendererObjects::getImage(const IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation>& animation) const
  327. {
  328. if(!animation)
  329. return nullptr;
  330. size_t groupIndex = getAnimationGroup(context, obj);
  331. if(animation->size(groupIndex) == 0)
  332. return nullptr;
  333. size_t frameCounter = context.getAnimationTime() / context.getAnimationPeriod();
  334. size_t frameIndex = frameCounter % animation->size(groupIndex);
  335. return animation->getImage(frameIndex, groupIndex);
  336. }
  337. size_t MapRendererObjects::getAnimationGroup(const IMapRendererContext & context, const CGObjectInstance * obj) const
  338. {
  339. // TODO
  340. //static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
  341. static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
  342. if(obj->ID == Obj::HERO)
  343. {
  344. const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
  345. return idleGroups[hero->moveDir];
  346. }
  347. if(obj->ID == Obj::BOAT)
  348. {
  349. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  350. return idleGroups[boat->direction];
  351. }
  352. return 0;
  353. }
  354. void MapRendererObjects::renderImage(Canvas & target, const int3 & coordinates, const CGObjectInstance * object, const std::shared_ptr<IImage>& image)
  355. {
  356. if(!image)
  357. return;
  358. image->setFlagColor(object->tempOwner);
  359. int3 offsetTiles(object->getPosition() - coordinates);
  360. Point offsetPixels(offsetTiles.x * 32, offsetTiles.y * 32);
  361. Point imagePos = image->dimensions() - offsetPixels - Point(32, 32);
  362. Point tileDimensions(32, 32);
  363. target.draw(image, Point(0, 0), Rect(imagePos, tileDimensions));
  364. }
  365. void MapRendererObjects::renderObject(const IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance* instance)
  366. {
  367. renderImage(target, coordinates, instance, getImage(context, instance, getAnimation(instance)));
  368. renderImage(target, coordinates, instance, getImage(context, instance, getFlagAnimation(instance)));
  369. }
  370. void MapRendererObjects::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  371. {
  372. for(const auto & objectID : objects[coordinates.z][coordinates.x][coordinates.y])
  373. {
  374. const auto * objectInstance = context.getObject(objectID);
  375. assert(objectInstance);
  376. if(!objectInstance)
  377. {
  378. logGlobal->error("Stray map object that isn't fading");
  379. continue;
  380. }
  381. renderObject(context, target, coordinates, objectInstance);
  382. }
  383. }
  384. void MapRendererObjects::onObjectInstantAdd(const IMapRendererContext & context, const CGObjectInstance * obj)
  385. {
  386. if(!obj)
  387. return;
  388. if(obj->ID == Obj::HERO && dynamic_cast<const CGHeroInstance *>(obj)->inTownGarrison)
  389. return;
  390. if(obj->ID == Obj::BOAT && dynamic_cast<const CGBoat *>(obj)->hero)
  391. return;
  392. std::shared_ptr<CAnimation> animation = getAnimation(obj);
  393. //no animation at all, e.g. Event
  394. if(!animation)
  395. return;
  396. //empty animation. Illegal?
  397. assert(animation->size(0) > 0);
  398. if(animation->size(0) == 0)
  399. return;
  400. auto image = animation->getImage(0, 0);
  401. int imageWidthTiles = (image->width() + 31) / 32;
  402. int imageHeightTiles = (image->height() + 31) / 32;
  403. int objectWidth = std::min(obj->getWidth(), imageWidthTiles);
  404. int objectHeight = std::min(obj->getHeight(), imageHeightTiles);
  405. for(int fx = 0; fx < objectWidth; ++fx)
  406. {
  407. for(int fy = 0; fy < objectHeight; ++fy)
  408. {
  409. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  410. if(context.isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  411. {
  412. auto & container = objects[currTile.z][currTile.x][currTile.y];
  413. container.push_back(obj->id);
  414. boost::range::sort(container, MapObjectsSorter(context));
  415. }
  416. }
  417. }
  418. }
  419. void MapRendererObjects::onObjectInstantRemove(const IMapRendererContext & context, const CGObjectInstance * object)
  420. {
  421. for(int z = 0; z < context.getMapSize().z; z++)
  422. for(int x = 0; x < context.getMapSize().x; x++)
  423. for(int y = 0; y < context.getMapSize().y; y++)
  424. vstd::erase(objects[z][x][y], object->id);
  425. }
  426. void MapRendererDebugGrid::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  427. {
  428. if(context.showGrid())
  429. {
  430. target.drawLine(Point(0, 0), Point(0, 31), {128, 128, 128, 128}, {128, 128, 128, 128});
  431. target.drawLine(Point(0, 0), Point(31, 0), {128, 128, 128, 128}, {128, 128, 128, 128});
  432. }
  433. }
  434. MapRendererPath::MapRendererPath()
  435. : pathNodes(new CAnimation("ADAG"))
  436. {
  437. pathNodes->preload();
  438. }
  439. void MapRendererPath::renderImage(Canvas & target, bool reachableToday, size_t imageIndex)
  440. {
  441. const static size_t unreachableTodayOffset = 25;
  442. if(reachableToday)
  443. target.draw(pathNodes->getImage(imageIndex), Point(0, 0));
  444. else
  445. target.draw(pathNodes->getImage(imageIndex + unreachableTodayOffset), Point(0, 0));
  446. }
  447. void MapRendererPath::renderImageCross(Canvas & target, bool reachableToday, const int3 & curr)
  448. {
  449. renderImage(target, reachableToday, 0);
  450. }
  451. void MapRendererPath::renderImageArrow(Canvas & target, bool reachableToday, const int3 & curr, const int3 & prev, const int3 & next)
  452. {
  453. // Vector directions
  454. // 0 1 2
  455. // |
  456. // 3 - 4 - 5
  457. // |
  458. // 6 7 8
  459. //For example:
  460. // |
  461. // +->
  462. // is (directionToArrowIndex[7][5])
  463. //
  464. const static size_t directionToArrowIndex[9][9] = {
  465. {16, 17, 18, 7, 0, 19, 6, 5, 0 },
  466. {8, 9, 18, 7, 0, 19, 6, 0, 20},
  467. {8, 1, 10, 7, 0, 19, 0, 21, 20},
  468. {24, 17, 18, 15, 0, 0, 6, 5, 4 },
  469. {0, 0, 0, 0, 0, 0, 0, 0, 0 },
  470. {8, 1, 2, 0, 0, 11, 22, 21, 20},
  471. {24, 17, 0, 23, 0, 3, 14, 5, 4 },
  472. {24, 0, 2, 23, 0, 3, 22, 13, 4 },
  473. {0, 1, 2, 23, 0, 3, 22, 21, 12}
  474. };
  475. size_t enterDirection = (curr.x - next.x + 1) + 3 * (curr.y - next.y + 1);
  476. size_t leaveDirection = (prev.x - curr.x + 1) + 3 * (prev.y - curr.y + 1);
  477. size_t imageIndex = directionToArrowIndex[enterDirection][leaveDirection];
  478. renderImage(target, reachableToday, imageIndex);
  479. }
  480. void MapRendererPath::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  481. {
  482. const auto & functor = [&](const CGPathNode & node)
  483. {
  484. return node.coord == coordinates;
  485. };
  486. const auto * path = context.currentPath();
  487. if(!path)
  488. return;
  489. const auto & iter = boost::range::find_if(path->nodes, functor);
  490. if(iter == path->nodes.end())
  491. return;
  492. bool reachableToday = iter->turns == 0;
  493. if(iter == path->nodes.begin())
  494. renderImageCross(target, reachableToday, iter->coord);
  495. auto next = iter + 1;
  496. auto prev = iter - 1;
  497. // start of path - current hero location
  498. if(next == path->nodes.end())
  499. return;
  500. bool pathContinuous = iter->coord.areNeighbours(next->coord) && iter->coord.areNeighbours(prev->coord);
  501. bool embarking = iter->action == CGPathNode::EMBARK || iter->action == CGPathNode::DISEMBARK;
  502. if(pathContinuous && !embarking)
  503. renderImageArrow(target, reachableToday, iter->coord, prev->coord, next->coord);
  504. else
  505. renderImageCross(target, reachableToday, iter->coord);
  506. }
  507. MapRenderer::MapRenderer(const IMapRendererContext & context)
  508. : rendererObjects(context)
  509. {
  510. }
  511. void MapRenderer::renderTile(const IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  512. {
  513. if (!context.isInMap(coordinates))
  514. {
  515. rendererBorder.renderTile(context, target, coordinates);
  516. return;
  517. }
  518. const NeighborTilesInfo neighborInfo(context, coordinates);
  519. if (neighborInfo.areAllHidden())
  520. {
  521. rendererFow.renderTile(context, target, coordinates);
  522. }
  523. else
  524. {
  525. rendererTerrain.renderTile(context, target, coordinates);
  526. rendererRiver.renderTile(context, target, coordinates);
  527. rendererRoad.renderTile(context, target, coordinates);
  528. rendererObjects.renderTile(context, target, coordinates);
  529. rendererPath.renderTile(context, target, coordinates);
  530. if (!context.isVisible(coordinates))
  531. rendererFow.renderTile(context, target, coordinates);
  532. }
  533. rendererDebugGrid.renderTile(context, target,coordinates);
  534. }