MapRenderer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 "IMapRendererContext.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/mapObjects/MiscObjects.h"
  24. #include "../../lib/mapObjects/ObjectTemplate.h"
  25. #include "../../lib/mapping/CMapDefines.h"
  26. #include "../../lib/pathfinder/CGPathNode.h"
  27. struct NeighborTilesInfo
  28. {
  29. //567
  30. //3 4
  31. //012
  32. std::bitset<8> d;
  33. NeighborTilesInfo(IMapRendererContext & context, const int3 & pos)
  34. {
  35. auto checkTile = [&](int dx, int dy)
  36. {
  37. return context.isVisible(pos + int3(dx, dy, 0));
  38. };
  39. // sides
  40. d[1] = checkTile(0, +1);
  41. d[3] = checkTile(-1, 0);
  42. d[4] = checkTile(+1, 0);
  43. d[6] = checkTile(0, -1);
  44. // corners - select visible image if either corner or adjacent sides are visible
  45. d[0] = d[1] || d[3] || checkTile(-1, +1);
  46. d[2] = d[1] || d[4] || checkTile(+1, +1);
  47. d[5] = d[3] || d[6] || checkTile(-1, -1);
  48. d[7] = d[4] || d[6] || checkTile(+1, -1);
  49. }
  50. bool areAllHidden() const
  51. {
  52. return d.none();
  53. }
  54. int getBitmapID() const
  55. {
  56. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  57. // 0-1, 2-3, 4-5, 11-13, 12-14
  58. static const int visBitmaps[256] = {
  59. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  60. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  61. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  62. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  63. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  64. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  65. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  66. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  67. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  68. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  69. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  70. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  71. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  72. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  73. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  74. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  75. };
  76. return visBitmaps[d.to_ulong()]; // >=0 -> partial hide, <0 - full hide
  77. }
  78. };
  79. MapTileStorage::MapTileStorage(size_t capacity)
  80. : animations(capacity)
  81. {
  82. }
  83. void MapTileStorage::load(size_t index, const std::string & filename, EImageBlitMode blitMode)
  84. {
  85. auto & terrainAnimations = animations[index];
  86. for(auto & entry : terrainAnimations)
  87. {
  88. if (!filename.empty())
  89. {
  90. entry = std::make_unique<CAnimation>(filename);
  91. entry->preload();
  92. }
  93. else
  94. entry = std::make_unique<CAnimation>();
  95. for(size_t i = 0; i < entry->size(); ++i)
  96. entry->getImage(i)->setBlitMode(blitMode);
  97. }
  98. for(size_t i = 0; i < terrainAnimations[0]->size(); ++i)
  99. {
  100. terrainAnimations[1]->getImage(i)->verticalFlip();
  101. terrainAnimations[3]->getImage(i)->verticalFlip();
  102. terrainAnimations[2]->getImage(i)->horizontalFlip();
  103. terrainAnimations[3]->getImage(i)->horizontalFlip();
  104. }
  105. }
  106. std::shared_ptr<IImage> MapTileStorage::find(size_t fileIndex, size_t rotationIndex, size_t imageIndex)
  107. {
  108. const auto & animation = animations[fileIndex][rotationIndex];
  109. return animation->getImage(imageIndex);
  110. }
  111. MapRendererTerrain::MapRendererTerrain()
  112. : storage(VLC->terrainTypeHandler->objects.size())
  113. {
  114. for(const auto & terrain : VLC->terrainTypeHandler->objects)
  115. storage.load(terrain->getIndex(), terrain->tilesFilename, EImageBlitMode::OPAQUE);
  116. }
  117. void MapRendererTerrain::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  118. {
  119. const TerrainTile & mapTile = context.getMapTile(coordinates);
  120. int32_t terrainIndex = mapTile.terType->getIndex();
  121. int32_t imageIndex = mapTile.terView;
  122. int32_t rotationIndex = mapTile.extTileFlags % 4;
  123. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  124. for( auto const & element : mapTile.terType->paletteAnimation)
  125. image->shiftPalette(element.start, element.length, context.terrainImageIndex(element.length));
  126. target.draw(image, Point(0, 0));
  127. }
  128. uint8_t MapRendererTerrain::checksum(IMapRendererContext & context, const int3 & coordinates)
  129. {
  130. const TerrainTile & mapTile = context.getMapTile(coordinates);
  131. if(!mapTile.terType->paletteAnimation.empty())
  132. return context.terrainImageIndex(250);
  133. return 0xff - 1;
  134. }
  135. MapRendererRiver::MapRendererRiver()
  136. : storage(VLC->riverTypeHandler->objects.size())
  137. {
  138. for(const auto & river : VLC->riverTypeHandler->objects)
  139. storage.load(river->getIndex(), river->tilesFilename, EImageBlitMode::COLORKEY);
  140. }
  141. void MapRendererRiver::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  142. {
  143. const TerrainTile & mapTile = context.getMapTile(coordinates);
  144. if(mapTile.riverType->getId() == River::NO_RIVER)
  145. return;
  146. int32_t terrainIndex = mapTile.riverType->getIndex();
  147. int32_t imageIndex = mapTile.riverDir;
  148. int32_t rotationIndex = (mapTile.extTileFlags >> 2) % 4;
  149. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  150. for( auto const & element : mapTile.riverType->paletteAnimation)
  151. image->shiftPalette(element.start, element.length, context.terrainImageIndex(element.length));
  152. target.draw(image, Point(0, 0));
  153. }
  154. uint8_t MapRendererRiver::checksum(IMapRendererContext & context, const int3 & coordinates)
  155. {
  156. const TerrainTile & mapTile = context.getMapTile(coordinates);
  157. if(!mapTile.riverType->paletteAnimation.empty())
  158. return context.terrainImageIndex(250);
  159. return 0xff-1;
  160. }
  161. MapRendererRoad::MapRendererRoad()
  162. : storage(VLC->roadTypeHandler->objects.size())
  163. {
  164. for(const auto & road : VLC->roadTypeHandler->objects)
  165. storage.load(road->getIndex(), road->tilesFilename, EImageBlitMode::COLORKEY);
  166. }
  167. void MapRendererRoad::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  168. {
  169. const int3 coordinatesAbove = coordinates - int3(0, 1, 0);
  170. if(context.isInMap(coordinatesAbove))
  171. {
  172. const TerrainTile & mapTileAbove = context.getMapTile(coordinatesAbove);
  173. if(mapTileAbove.roadType->getId() != Road::NO_ROAD)
  174. {
  175. int32_t terrainIndex = mapTileAbove.roadType->getIndex();
  176. int32_t imageIndex = mapTileAbove.roadDir;
  177. int32_t rotationIndex = (mapTileAbove.extTileFlags >> 4) % 4;
  178. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  179. target.draw(image, Point(0, 0), Rect(0, 16, 32, 16));
  180. }
  181. }
  182. const TerrainTile & mapTile = context.getMapTile(coordinates);
  183. if(mapTile.roadType->getId() != Road::NO_ROAD)
  184. {
  185. int32_t terrainIndex = mapTile.roadType->getIndex();
  186. int32_t imageIndex = mapTile.roadDir;
  187. int32_t rotationIndex = (mapTile.extTileFlags >> 4) % 4;
  188. const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex);
  189. target.draw(image, Point(0, 16), Rect(0, 0, 32, 16));
  190. }
  191. }
  192. uint8_t MapRendererRoad::checksum(IMapRendererContext & context, const int3 & coordinates)
  193. {
  194. return 0;
  195. }
  196. MapRendererBorder::MapRendererBorder()
  197. {
  198. emptyFill = std::make_unique<Canvas>(Point(32,32));
  199. animation = std::make_unique<CAnimation>("EDG");
  200. animation->preload();
  201. }
  202. size_t MapRendererBorder::getIndexForTile(IMapRendererContext & context, const int3 & tile)
  203. {
  204. assert(!context.isInMap(tile));
  205. int3 size = context.getMapSize();
  206. if(tile.x < -1 || tile.x > size.x || tile.y < -1 || tile.y > size.y)
  207. return std::abs(tile.x) % 4 + 4 * (std::abs(tile.y) % 4);
  208. if(tile.x == -1 && tile.y == -1)
  209. return 16;
  210. if(tile.x == size.x && tile.y == -1)
  211. return 17;
  212. if(tile.x == size.x && tile.y == size.y)
  213. return 18;
  214. if(tile.x == -1 && tile.y == size.y)
  215. return 19;
  216. if(tile.y == -1)
  217. return 20 + (tile.x % 4);
  218. if(tile.x == size.x)
  219. return 24 + (tile.y % 4);
  220. if(tile.y == size.y)
  221. return 28 + (tile.x % 4);
  222. if(tile.x == -1)
  223. return 32 + (tile.y % 4);
  224. //else - visible area, no renderable border
  225. assert(0);
  226. return 0;
  227. }
  228. void MapRendererBorder::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  229. {
  230. if (context.showBorder())
  231. {
  232. const auto & image = animation->getImage(getIndexForTile(context, coordinates));
  233. target.draw(image, Point(0, 0));
  234. }
  235. else
  236. {
  237. target.draw(*emptyFill, Point(0,0));
  238. }
  239. }
  240. uint8_t MapRendererBorder::checksum(IMapRendererContext & context, const int3 & coordinates)
  241. {
  242. return 0;
  243. }
  244. MapRendererFow::MapRendererFow()
  245. {
  246. fogOfWarFullHide = std::make_unique<CAnimation>("TSHRC");
  247. fogOfWarFullHide->preload();
  248. fogOfWarPartialHide = std::make_unique<CAnimation>("TSHRE");
  249. fogOfWarPartialHide->preload();
  250. for(size_t i = 0; i < fogOfWarFullHide->size(); ++i)
  251. fogOfWarFullHide->getImage(i)->setBlitMode(EImageBlitMode::OPAQUE);
  252. static const std::vector<int> rotations = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  253. size_t size = fogOfWarPartialHide->size(0); //group size after next rotation
  254. for(const int rotation : rotations)
  255. {
  256. fogOfWarPartialHide->duplicateImage(0, rotation, 0);
  257. auto image = fogOfWarPartialHide->getImage(size, 0);
  258. image->verticalFlip();
  259. size++;
  260. }
  261. }
  262. void MapRendererFow::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  263. {
  264. assert(!context.isVisible(coordinates));
  265. const NeighborTilesInfo neighborInfo(context, coordinates);
  266. int retBitmapID = neighborInfo.getBitmapID(); // >=0 -> partial hide, <0 - full hide
  267. if(retBitmapID < 0)
  268. {
  269. // generate a number that is predefined for each tile,
  270. // but appears random to break visible pattern in large areas of fow
  271. // current approach (use primes as magic numbers for formula) looks to be suitable
  272. size_t pseudorandomNumber = ((coordinates.x * 997) ^ (coordinates.y * 1009)) / 101;
  273. size_t imageIndex = pseudorandomNumber % fogOfWarFullHide->size();
  274. target.draw(fogOfWarFullHide->getImage(imageIndex), Point(0, 0));
  275. }
  276. else
  277. {
  278. target.draw(fogOfWarPartialHide->getImage(retBitmapID), Point(0, 0));
  279. }
  280. }
  281. uint8_t MapRendererFow::checksum(IMapRendererContext & context, const int3 & coordinates)
  282. {
  283. const NeighborTilesInfo neighborInfo(context, coordinates);
  284. int retBitmapID = neighborInfo.getBitmapID();
  285. if(retBitmapID < 0)
  286. return 0xff - 1;
  287. return retBitmapID;
  288. }
  289. std::shared_ptr<CAnimation> MapRendererObjects::getBaseAnimation(const CGObjectInstance* obj)
  290. {
  291. const auto & info = obj->appearance;
  292. //the only(?) invisible object
  293. if(info->id == Obj::EVENT)
  294. return std::shared_ptr<CAnimation>();
  295. if(info->animationFile.empty())
  296. {
  297. logGlobal->warn("Def name for obj (%d,%d) is empty!", info->id, info->subid);
  298. return std::shared_ptr<CAnimation>();
  299. }
  300. bool generateMovementGroups = (info->id == Obj::BOAT) || (info->id == Obj::HERO);
  301. // Boat appearance files only contain single, unanimated image
  302. // proper boat animations are actually in different file
  303. if (info->id == Obj::BOAT)
  304. if(auto boat = dynamic_cast<const CGBoat*>(obj); boat && !boat->actualAnimation.empty())
  305. return getAnimation(boat->actualAnimation, generateMovementGroups);
  306. return getAnimation(info->animationFile, generateMovementGroups);
  307. }
  308. std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups)
  309. {
  310. auto it = animations.find(filename);
  311. if(it != animations.end())
  312. return it->second;
  313. auto ret = std::make_shared<CAnimation>(filename);
  314. animations[filename] = ret;
  315. ret->preload();
  316. if(generateMovementGroups)
  317. {
  318. ret->createFlippedGroup(1, 13);
  319. ret->createFlippedGroup(2, 14);
  320. ret->createFlippedGroup(3, 15);
  321. ret->createFlippedGroup(6, 10);
  322. ret->createFlippedGroup(7, 11);
  323. ret->createFlippedGroup(8, 12);
  324. }
  325. return ret;
  326. }
  327. std::shared_ptr<CAnimation> MapRendererObjects::getFlagAnimation(const CGObjectInstance* obj)
  328. {
  329. //TODO: relocate to config file?
  330. static const std::vector<std::string> heroFlags = {
  331. "AF00", "AF01", "AF02", "AF03", "AF04", "AF05", "AF06", "AF07"
  332. };
  333. if(obj->ID == Obj::HERO)
  334. {
  335. assert(dynamic_cast<const CGHeroInstance *>(obj) != nullptr);
  336. assert(obj->tempOwner.isValidPlayer());
  337. return getAnimation(heroFlags[obj->tempOwner.getNum()], true);
  338. }
  339. if(obj->ID == Obj::BOAT)
  340. {
  341. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  342. if(boat && boat->hero && !boat->flagAnimations[boat->hero->tempOwner.getNum()].empty())
  343. return getAnimation(boat->flagAnimations[boat->hero->tempOwner.getNum()], true);
  344. }
  345. return nullptr;
  346. }
  347. std::shared_ptr<CAnimation> MapRendererObjects::getOverlayAnimation(const CGObjectInstance * obj)
  348. {
  349. if(obj->ID == Obj::BOAT)
  350. {
  351. // Boats have additional animation with waves around boat
  352. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  353. if(boat && boat->hero && !boat->overlayAnimation.empty())
  354. return getAnimation(boat->overlayAnimation, true);
  355. }
  356. return nullptr;
  357. }
  358. std::shared_ptr<IImage> MapRendererObjects::getImage(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation>& animation) const
  359. {
  360. if(!animation)
  361. return nullptr;
  362. size_t groupIndex = context.objectGroupIndex(obj->id);
  363. if(animation->size(groupIndex) == 0)
  364. return nullptr;
  365. size_t frameIndex = context.objectImageIndex(obj->id, animation->size(groupIndex));
  366. return animation->getImage(frameIndex, groupIndex);
  367. }
  368. void MapRendererObjects::renderImage(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * object, const std::shared_ptr<IImage>& image)
  369. {
  370. if(!image)
  371. return;
  372. auto transparency = static_cast<uint8_t>(std::round(255 * context.objectTransparency(object->id, coordinates)));
  373. if (transparency == 0)
  374. return;
  375. image->setAlpha(transparency);
  376. image->setFlagColor(object->tempOwner);
  377. Point offsetPixels = context.objectImageOffset(object->id, coordinates);
  378. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  379. {
  380. Point imagePos = image->dimensions() - offsetPixels - Point(32, 32);
  381. //if (transparency == 255)
  382. //{
  383. // Canvas intermediate(Point(32,32));
  384. // intermediate.enableTransparency(true);
  385. // image->setBlitMode(EImageBlitMode::OPAQUE);
  386. // intermediate.draw(image, Point(0, 0), Rect(imagePos, Point(32,32)));
  387. // target.draw(intermediate, Point(0,0));
  388. // return;
  389. //}
  390. target.draw(image, Point(0, 0), Rect(imagePos, Point(32,32)));
  391. }
  392. }
  393. void MapRendererObjects::renderObject(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * instance)
  394. {
  395. renderImage(context, target, coordinates, instance, getImage(context, instance, getBaseAnimation(instance)));
  396. renderImage(context, target, coordinates, instance, getImage(context, instance, getFlagAnimation(instance)));
  397. renderImage(context, target, coordinates, instance, getImage(context, instance, getOverlayAnimation(instance)));
  398. }
  399. void MapRendererObjects::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  400. {
  401. for(const auto & objectID : context.getObjects(coordinates))
  402. {
  403. const auto * objectInstance = context.getObject(objectID);
  404. assert(objectInstance);
  405. if(!objectInstance)
  406. {
  407. logGlobal->error("Stray map object that isn't fading");
  408. continue;
  409. }
  410. renderObject(context, target, coordinates, objectInstance);
  411. }
  412. }
  413. uint8_t MapRendererObjects::checksum(IMapRendererContext & context, const int3 & coordinates)
  414. {
  415. for(const auto & objectID : context.getObjects(coordinates))
  416. {
  417. const auto * objectInstance = context.getObject(objectID);
  418. size_t groupIndex = context.objectGroupIndex(objectInstance->id);
  419. Point offsetPixels = context.objectImageOffset(objectInstance->id, coordinates);
  420. auto base = getBaseAnimation(objectInstance);
  421. auto flag = getFlagAnimation(objectInstance);
  422. if (base && base->size(groupIndex) > 1)
  423. {
  424. auto imageIndex = context.objectImageIndex(objectID, base->size(groupIndex));
  425. auto image = base->getImage(imageIndex, groupIndex);
  426. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  427. return context.objectImageIndex(objectID, 250);
  428. }
  429. if (flag && flag->size(groupIndex) > 1)
  430. {
  431. auto imageIndex = context.objectImageIndex(objectID, flag->size(groupIndex));
  432. auto image = flag->getImage(imageIndex, groupIndex);
  433. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  434. return context.objectImageIndex(objectID, 250);
  435. }
  436. }
  437. return 0xff-1;
  438. }
  439. MapRendererOverlay::MapRendererOverlay()
  440. : imageGrid(IImage::createFromFile("debug/grid", EImageBlitMode::ALPHA))
  441. , imageBlocked(IImage::createFromFile("debug/blocked", EImageBlitMode::ALPHA))
  442. , imageVisitable(IImage::createFromFile("debug/visitable", EImageBlitMode::ALPHA))
  443. , imageSpellRange(IImage::createFromFile("debug/spellRange", EImageBlitMode::ALPHA))
  444. {
  445. }
  446. void MapRendererOverlay::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  447. {
  448. if(context.showGrid())
  449. target.draw(imageGrid, Point(0,0));
  450. if(context.showVisitable() || context.showBlocked())
  451. {
  452. bool blocking = false;
  453. bool visitable = false;
  454. for(const auto & objectID : context.getObjects(coordinates))
  455. {
  456. const auto * object = context.getObject(objectID);
  457. if(context.objectTransparency(objectID, coordinates) > 0 && !context.isActiveHero(object))
  458. {
  459. visitable |= object->visitableAt(coordinates.x, coordinates.y);
  460. blocking |= object->blockingAt(coordinates.x, coordinates.y);
  461. }
  462. }
  463. if (context.showBlocked() && blocking)
  464. target.draw(imageBlocked, Point(0,0));
  465. if (context.showVisitable() && visitable)
  466. target.draw(imageVisitable, Point(0,0));
  467. }
  468. if (context.showSpellRange(coordinates))
  469. target.draw(imageSpellRange, Point(0,0));
  470. }
  471. uint8_t MapRendererOverlay::checksum(IMapRendererContext & context, const int3 & coordinates)
  472. {
  473. uint8_t result = 0;
  474. if (context.showVisitable())
  475. result += 1;
  476. if (context.showBlocked())
  477. result += 2;
  478. if (context.showGrid())
  479. result += 4;
  480. if (context.showSpellRange(coordinates))
  481. result += 8;
  482. return result;
  483. }
  484. MapRendererPath::MapRendererPath()
  485. : pathNodes(new CAnimation("ADAG"))
  486. {
  487. pathNodes->preload();
  488. }
  489. size_t MapRendererPath::selectImageReachability(bool reachableToday, size_t imageIndex)
  490. {
  491. const static size_t unreachableTodayOffset = 25;
  492. if(!reachableToday)
  493. return unreachableTodayOffset + imageIndex;
  494. return imageIndex;
  495. }
  496. size_t MapRendererPath::selectImageCross(bool reachableToday, const int3 & curr)
  497. {
  498. return selectImageReachability(reachableToday, 0);
  499. }
  500. size_t MapRendererPath::selectImageArrow(bool reachableToday, const int3 & curr, const int3 & prev, const int3 & next)
  501. {
  502. // Vector directions
  503. // 0 1 2
  504. // |
  505. // 3 - 4 - 5
  506. // |
  507. // 6 7 8
  508. //For example:
  509. // |
  510. // +->
  511. // is (directionToArrowIndex[7][5])
  512. //
  513. const static size_t directionToArrowIndex[9][9] = {
  514. {16, 17, 18, 7, 0, 19, 6, 5, 0 },
  515. {8, 9, 18, 7, 0, 19, 6, 0, 20},
  516. {8, 1, 10, 7, 0, 19, 0, 21, 20},
  517. {24, 17, 18, 15, 0, 0, 6, 5, 4 },
  518. {0, 0, 0, 0, 0, 0, 0, 0, 0 },
  519. {8, 1, 2, 0, 0, 11, 22, 21, 20},
  520. {24, 17, 0, 23, 0, 3, 14, 5, 4 },
  521. {24, 0, 2, 23, 0, 3, 22, 13, 4 },
  522. {0, 1, 2, 23, 0, 3, 22, 21, 12}
  523. };
  524. size_t enterDirection = (curr.x - next.x + 1) + 3 * (curr.y - next.y + 1);
  525. size_t leaveDirection = (prev.x - curr.x + 1) + 3 * (prev.y - curr.y + 1);
  526. size_t imageIndex = directionToArrowIndex[enterDirection][leaveDirection];
  527. return selectImageReachability(reachableToday, imageIndex);
  528. }
  529. void MapRendererPath::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  530. {
  531. size_t imageID = selectImage(context, coordinates);
  532. if (imageID < pathNodes->size())
  533. target.draw(pathNodes->getImage(imageID), Point(0,0));
  534. }
  535. size_t MapRendererPath::selectImage(IMapRendererContext & context, const int3 & coordinates)
  536. {
  537. const auto & functor = [&](const CGPathNode & node)
  538. {
  539. return node.coord == coordinates;
  540. };
  541. const auto * path = context.currentPath();
  542. if(!path)
  543. return std::numeric_limits<size_t>::max();
  544. const auto & iter = boost::range::find_if(path->nodes, functor);
  545. if(iter == path->nodes.end())
  546. return std::numeric_limits<size_t>::max();
  547. bool reachableToday = iter->turns == 0;
  548. if(iter == path->nodes.begin())
  549. return selectImageCross(reachableToday, iter->coord);
  550. auto next = iter + 1;
  551. auto prev = iter - 1;
  552. // start of path - current hero location
  553. if(next == path->nodes.end())
  554. return std::numeric_limits<size_t>::max();
  555. bool pathContinuous = iter->coord.areNeighbours(next->coord) && iter->coord.areNeighbours(prev->coord);
  556. bool embarking = iter->action == EPathNodeAction::EMBARK || iter->action == EPathNodeAction::DISEMBARK;
  557. if(pathContinuous && !embarking)
  558. return selectImageArrow(reachableToday, iter->coord, prev->coord, next->coord);
  559. return selectImageCross(reachableToday, iter->coord);
  560. }
  561. uint8_t MapRendererPath::checksum(IMapRendererContext & context, const int3 & coordinates)
  562. {
  563. return selectImage(context, coordinates) & 0xff;
  564. }
  565. MapRenderer::TileChecksum MapRenderer::getTileChecksum(IMapRendererContext & context, const int3 & coordinates)
  566. {
  567. // computes basic checksum to determine whether tile needs an update
  568. // if any component gives different value, tile will be updated
  569. TileChecksum result;
  570. boost::range::fill(result, std::numeric_limits<uint8_t>::max());
  571. if(!context.isInMap(coordinates))
  572. {
  573. result[0] = rendererBorder.checksum(context, coordinates);
  574. return result;
  575. }
  576. const NeighborTilesInfo neighborInfo(context, coordinates);
  577. if(!context.isVisible(coordinates) && neighborInfo.areAllHidden())
  578. {
  579. result[7] = rendererFow.checksum(context, coordinates);
  580. }
  581. else
  582. {
  583. result[1] = rendererTerrain.checksum(context, coordinates);
  584. if (context.showRivers())
  585. result[2] = rendererRiver.checksum(context, coordinates);
  586. if (context.showRoads())
  587. result[3] = rendererRoad.checksum(context, coordinates);
  588. result[4] = rendererObjects.checksum(context, coordinates);
  589. result[5] = rendererPath.checksum(context, coordinates);
  590. result[6] = rendererOverlay.checksum(context, coordinates);
  591. if(!context.isVisible(coordinates))
  592. result[7] = rendererFow.checksum(context, coordinates);
  593. }
  594. return result;
  595. }
  596. void MapRenderer::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  597. {
  598. if(!context.isInMap(coordinates))
  599. {
  600. rendererBorder.renderTile(context, target, coordinates);
  601. return;
  602. }
  603. const NeighborTilesInfo neighborInfo(context, coordinates);
  604. if(!context.isVisible(coordinates) && neighborInfo.areAllHidden())
  605. {
  606. rendererFow.renderTile(context, target, coordinates);
  607. }
  608. else
  609. {
  610. rendererTerrain.renderTile(context, target, coordinates);
  611. if (context.showRivers())
  612. rendererRiver.renderTile(context, target, coordinates);
  613. if (context.showRoads())
  614. rendererRoad.renderTile(context, target, coordinates);
  615. rendererObjects.renderTile(context, target, coordinates);
  616. rendererPath.renderTile(context, target, coordinates);
  617. rendererOverlay.renderTile(context, target, coordinates);
  618. if(!context.isVisible(coordinates))
  619. rendererFow.renderTile(context, target, coordinates);
  620. }
  621. }