MapRenderer.cpp 24 KB

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