2
0

MapRenderer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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(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(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(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(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(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(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(IMapRendererContext & context, const int3 & coordinates)
  208. {
  209. return 0;
  210. }
  211. MapRendererBorder::MapRendererBorder()
  212. {
  213. emptyFill = std::make_unique<Canvas>(Point(32,32));
  214. animation = std::make_unique<CAnimation>("EDG");
  215. animation->preload();
  216. }
  217. size_t MapRendererBorder::getIndexForTile(IMapRendererContext & context, const int3 & tile)
  218. {
  219. assert(!context.isInMap(tile));
  220. int3 size = context.getMapSize();
  221. if(tile.x < -1 || tile.x > size.x || tile.y < -1 || tile.y > size.y)
  222. return std::abs(tile.x) % 4 + 4 * (std::abs(tile.y) % 4);
  223. if(tile.x == -1 && tile.y == -1)
  224. return 16;
  225. if(tile.x == size.x && tile.y == -1)
  226. return 17;
  227. if(tile.x == size.x && tile.y == size.y)
  228. return 18;
  229. if(tile.x == -1 && tile.y == size.y)
  230. return 19;
  231. if(tile.y == -1)
  232. return 20 + (tile.x % 4);
  233. if(tile.x == size.x)
  234. return 24 + (tile.y % 4);
  235. if(tile.y == size.y)
  236. return 28 + (tile.x % 4);
  237. if(tile.x == -1)
  238. return 32 + (tile.y % 4);
  239. //else - visible area, no renderable border
  240. assert(0);
  241. return 0;
  242. }
  243. void MapRendererBorder::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  244. {
  245. if (context.showBorder())
  246. {
  247. const auto & image = animation->getImage(getIndexForTile(context, coordinates));
  248. target.draw(image, Point(0, 0));
  249. }
  250. else
  251. {
  252. target.draw(*emptyFill, Point(0,0));
  253. }
  254. }
  255. uint8_t MapRendererBorder::checksum(IMapRendererContext & context, const int3 & coordinates)
  256. {
  257. return 0;
  258. }
  259. MapRendererFow::MapRendererFow()
  260. {
  261. fogOfWarFullHide = std::make_unique<CAnimation>("TSHRC");
  262. fogOfWarFullHide->preload();
  263. fogOfWarPartialHide = std::make_unique<CAnimation>("TSHRE");
  264. fogOfWarPartialHide->preload();
  265. for(size_t i = 0; i < fogOfWarFullHide->size(); ++i)
  266. fogOfWarFullHide->getImage(i)->setBlitMode(EImageBlitMode::OPAQUE);
  267. static const std::vector<int> rotations = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  268. size_t size = fogOfWarPartialHide->size(0); //group size after next rotation
  269. for(const int rotation : rotations)
  270. {
  271. fogOfWarPartialHide->duplicateImage(0, rotation, 0);
  272. auto image = fogOfWarPartialHide->getImage(size, 0);
  273. image->verticalFlip();
  274. size++;
  275. }
  276. }
  277. void MapRendererFow::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  278. {
  279. assert(!context.isVisible(coordinates));
  280. const NeighborTilesInfo neighborInfo(context, coordinates);
  281. int retBitmapID = neighborInfo.getBitmapID(); // >=0 -> partial hide, <0 - full hide
  282. if(retBitmapID < 0)
  283. {
  284. // generate a number that is predefined for each tile,
  285. // but appears random to break visible pattern in large areas of fow
  286. // current approach (use primes as magic numbers for formula) looks to be suitable
  287. size_t pseudorandomNumber = ((coordinates.x * 997) ^ (coordinates.y * 1009)) / 101;
  288. size_t imageIndex = pseudorandomNumber % fogOfWarFullHide->size();
  289. target.draw(fogOfWarFullHide->getImage(imageIndex), Point(0, 0));
  290. }
  291. else
  292. {
  293. target.draw(fogOfWarPartialHide->getImage(retBitmapID), Point(0, 0));
  294. }
  295. }
  296. uint8_t MapRendererFow::checksum(IMapRendererContext & context, const int3 & coordinates)
  297. {
  298. const NeighborTilesInfo neighborInfo(context, coordinates);
  299. int retBitmapID = neighborInfo.getBitmapID();
  300. if(retBitmapID < 0)
  301. return 0xff - 1;
  302. return retBitmapID;
  303. }
  304. std::shared_ptr<CAnimation> MapRendererObjects::getBaseAnimation(const CGObjectInstance* obj)
  305. {
  306. const auto & info = obj->appearance;
  307. //the only(?) invisible object
  308. if(info->id == Obj::EVENT)
  309. return std::shared_ptr<CAnimation>();
  310. if(info->animationFile.empty())
  311. {
  312. logGlobal->warn("Def name for obj (%d,%d) is empty!", info->id, info->subid);
  313. return std::shared_ptr<CAnimation>();
  314. }
  315. bool generateMovementGroups = (info->id == Obj::BOAT) || (info->id == Obj::HERO);
  316. //TODO: relocate to config file?
  317. // Boat appearance files only contain single, unanimated image
  318. // proper boat animations are actually in different file
  319. static const std::vector<std::string> boatAnimations = {
  320. "AB01_.def", "AB02_.def", "AB03_.def"
  321. };
  322. if (info->id == Obj::BOAT)
  323. return getAnimation(boatAnimations[info->subid], generateMovementGroups);
  324. return getAnimation(info->animationFile, generateMovementGroups);
  325. }
  326. std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups)
  327. {
  328. auto it = animations.find(filename);
  329. if(it != animations.end())
  330. return it->second;
  331. auto ret = std::make_shared<CAnimation>(filename);
  332. animations[filename] = ret;
  333. ret->preload();
  334. if(generateMovementGroups)
  335. {
  336. ret->createFlippedGroup(1, 13);
  337. ret->createFlippedGroup(2, 14);
  338. ret->createFlippedGroup(3, 15);
  339. ret->createFlippedGroup(6, 10);
  340. ret->createFlippedGroup(7, 11);
  341. ret->createFlippedGroup(8, 12);
  342. }
  343. return ret;
  344. }
  345. std::shared_ptr<CAnimation> MapRendererObjects::getFlagAnimation(const CGObjectInstance* obj)
  346. {
  347. //TODO: relocate to config file?
  348. static const std::vector<std::string> heroFlags = {
  349. "AF00", "AF01", "AF02", "AF03", "AF04", "AF05", "AF06", "AF07"
  350. };
  351. //TODO: relocate to config file?
  352. static const std::vector<std::vector<std::string>> boatFlags = {
  353. {"ABF01L", "ABF01G", "ABF01R", "ABF01D", "ABF01B", "ABF01P", "ABF01W", "ABF01K"},
  354. {"ABF02L", "ABF02G", "ABF02R", "ABF02D", "ABF02B", "ABF02P", "ABF02W", "ABF02K"},
  355. {"ABF03L", "ABF03G", "ABF03R", "ABF03D", "ABF03B", "ABF03P", "ABF03W", "ABF03K"}
  356. };
  357. if(obj->ID == Obj::HERO)
  358. {
  359. assert(dynamic_cast<const CGHeroInstance *>(obj) != nullptr);
  360. assert(obj->tempOwner.isValidPlayer());
  361. return getAnimation(heroFlags[obj->tempOwner.getNum()], true);
  362. }
  363. if(obj->ID == Obj::BOAT)
  364. {
  365. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  366. assert(boat->subID < boatFlags.size());
  367. if (boat->hero)
  368. {
  369. assert(boat->hero->tempOwner.isValidPlayer());
  370. return getAnimation(boatFlags[boat->subID][boat->hero->tempOwner.getNum()], true);
  371. }
  372. }
  373. return nullptr;
  374. }
  375. std::shared_ptr<CAnimation> MapRendererObjects::getOverlayAnimation(const CGObjectInstance * obj)
  376. {
  377. if(obj->ID == Obj::BOAT)
  378. {
  379. //TODO: relocate to config file?
  380. // Boats have additional animation with waves around boat
  381. static const std::vector<std::string> boatAnimations = {
  382. "ABM01_.def", "ABM02_.def", "ABM03_.def"
  383. };
  384. const auto * boat = dynamic_cast<const CGBoat *>(obj);
  385. if (boat->hero)
  386. return getAnimation(boatAnimations[obj->subID], true);
  387. }
  388. return nullptr;
  389. }
  390. std::shared_ptr<IImage> MapRendererObjects::getImage(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation>& animation) const
  391. {
  392. if(!animation)
  393. return nullptr;
  394. size_t groupIndex = context.objectGroupIndex(obj->id);
  395. if(animation->size(groupIndex) == 0)
  396. return nullptr;
  397. size_t frameIndex = context.objectImageIndex(obj->id, animation->size(groupIndex));
  398. return animation->getImage(frameIndex, groupIndex);
  399. }
  400. void MapRendererObjects::renderImage(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * object, const std::shared_ptr<IImage>& image)
  401. {
  402. if(!image)
  403. return;
  404. auto transparency = static_cast<uint8_t>(std::round(255 * context.objectTransparency(object->id, coordinates)));
  405. if (transparency == 0)
  406. return;
  407. image->setAlpha(transparency);
  408. image->setFlagColor(object->tempOwner);
  409. Point offsetPixels = context.objectImageOffset(object->id, coordinates);
  410. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  411. {
  412. Point imagePos = image->dimensions() - offsetPixels - Point(32, 32);
  413. //if (transparency == 255)
  414. //{
  415. // Canvas intermediate(Point(32,32));
  416. // intermediate.enableTransparency(true);
  417. // image->setBlitMode(EImageBlitMode::OPAQUE);
  418. // intermediate.draw(image, Point(0, 0), Rect(imagePos, Point(32,32)));
  419. // target.draw(intermediate, Point(0,0));
  420. // return;
  421. //}
  422. target.draw(image, Point(0, 0), Rect(imagePos, Point(32,32)));
  423. }
  424. }
  425. void MapRendererObjects::renderObject(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * instance)
  426. {
  427. renderImage(context, target, coordinates, instance, getImage(context, instance, getBaseAnimation(instance)));
  428. renderImage(context, target, coordinates, instance, getImage(context, instance, getFlagAnimation(instance)));
  429. renderImage(context, target, coordinates, instance, getImage(context, instance, getOverlayAnimation(instance)));
  430. }
  431. void MapRendererObjects::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  432. {
  433. for(const auto & objectID : context.getObjects(coordinates))
  434. {
  435. const auto * objectInstance = context.getObject(objectID);
  436. assert(objectInstance);
  437. if(!objectInstance)
  438. {
  439. logGlobal->error("Stray map object that isn't fading");
  440. continue;
  441. }
  442. renderObject(context, target, coordinates, objectInstance);
  443. }
  444. }
  445. uint8_t MapRendererObjects::checksum(IMapRendererContext & context, const int3 & coordinates)
  446. {
  447. for(const auto & objectID : context.getObjects(coordinates))
  448. {
  449. const auto * objectInstance = context.getObject(objectID);
  450. size_t groupIndex = context.objectGroupIndex(objectInstance->id);
  451. Point offsetPixels = context.objectImageOffset(objectInstance->id, coordinates);
  452. auto base = getBaseAnimation(objectInstance);
  453. auto flag = getFlagAnimation(objectInstance);
  454. if (base && base->size(groupIndex) > 1)
  455. {
  456. auto imageIndex = context.objectImageIndex(objectID, base->size(groupIndex));
  457. auto image = base->getImage(imageIndex, groupIndex);
  458. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  459. return context.objectImageIndex(objectID, 250);
  460. }
  461. if (flag && flag->size(groupIndex) > 1)
  462. {
  463. auto imageIndex = context.objectImageIndex(objectID, flag->size(groupIndex));
  464. auto image = flag->getImage(imageIndex, groupIndex);
  465. if ( offsetPixels.x < image->dimensions().x && offsetPixels.y < image->dimensions().y)
  466. return context.objectImageIndex(objectID, 250);
  467. }
  468. }
  469. return 0xff-1;
  470. }
  471. MapRendererDebug::MapRendererDebug()
  472. : imageGrid(IImage::createFromFile("debug/grid", EImageBlitMode::ALPHA))
  473. , imageBlockable(IImage::createFromFile("debug/blocked", EImageBlitMode::ALPHA))
  474. , imageVisitable(IImage::createFromFile("debug/visitable", EImageBlitMode::ALPHA))
  475. {
  476. }
  477. void MapRendererDebug::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  478. {
  479. if(context.showGrid())
  480. target.draw(imageGrid, Point(0,0));
  481. if(context.showVisitable() || context.showBlockable())
  482. {
  483. bool blockable = false;
  484. bool visitable = false;
  485. for(const auto & objectID : context.getObjects(coordinates))
  486. {
  487. const auto * object = context.getObject(objectID);
  488. if (context.objectTransparency(objectID, coordinates) > 0)
  489. {
  490. visitable |= object->visitableAt(coordinates.x, coordinates.y);
  491. blockable |= object->blockingAt(coordinates.x, coordinates.y);
  492. }
  493. }
  494. if (context.showBlockable() && blockable)
  495. target.draw(imageBlockable, Point(0,0));
  496. if (context.showVisitable() && visitable)
  497. target.draw(imageVisitable, Point(0,0));
  498. }
  499. }
  500. uint8_t MapRendererDebug::checksum(IMapRendererContext & context, const int3 & coordinates)
  501. {
  502. return 0;
  503. }
  504. MapRendererPath::MapRendererPath()
  505. : pathNodes(new CAnimation("ADAG"))
  506. {
  507. pathNodes->preload();
  508. }
  509. size_t MapRendererPath::selectImageReachability(bool reachableToday, size_t imageIndex)
  510. {
  511. const static size_t unreachableTodayOffset = 25;
  512. if(!reachableToday)
  513. return unreachableTodayOffset + imageIndex;
  514. return imageIndex;
  515. }
  516. size_t MapRendererPath::selectImageCross(bool reachableToday, const int3 & curr)
  517. {
  518. return selectImageReachability(reachableToday, 0);
  519. }
  520. size_t MapRendererPath::selectImageArrow(bool reachableToday, const int3 & curr, const int3 & prev, const int3 & next)
  521. {
  522. // Vector directions
  523. // 0 1 2
  524. // |
  525. // 3 - 4 - 5
  526. // |
  527. // 6 7 8
  528. //For example:
  529. // |
  530. // +->
  531. // is (directionToArrowIndex[7][5])
  532. //
  533. const static size_t directionToArrowIndex[9][9] = {
  534. {16, 17, 18, 7, 0, 19, 6, 5, 0 },
  535. {8, 9, 18, 7, 0, 19, 6, 0, 20},
  536. {8, 1, 10, 7, 0, 19, 0, 21, 20},
  537. {24, 17, 18, 15, 0, 0, 6, 5, 4 },
  538. {0, 0, 0, 0, 0, 0, 0, 0, 0 },
  539. {8, 1, 2, 0, 0, 11, 22, 21, 20},
  540. {24, 17, 0, 23, 0, 3, 14, 5, 4 },
  541. {24, 0, 2, 23, 0, 3, 22, 13, 4 },
  542. {0, 1, 2, 23, 0, 3, 22, 21, 12}
  543. };
  544. size_t enterDirection = (curr.x - next.x + 1) + 3 * (curr.y - next.y + 1);
  545. size_t leaveDirection = (prev.x - curr.x + 1) + 3 * (prev.y - curr.y + 1);
  546. size_t imageIndex = directionToArrowIndex[enterDirection][leaveDirection];
  547. return selectImageReachability(reachableToday, imageIndex);
  548. }
  549. void MapRendererPath::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  550. {
  551. size_t imageID = selectImage(context, coordinates);
  552. if (imageID < pathNodes->size())
  553. target.draw(pathNodes->getImage(imageID), Point(0,0));
  554. }
  555. size_t MapRendererPath::selectImage(IMapRendererContext & context, const int3 & coordinates)
  556. {
  557. const auto & functor = [&](const CGPathNode & node)
  558. {
  559. return node.coord == coordinates;
  560. };
  561. const auto * path = context.currentPath();
  562. if(!path)
  563. return std::numeric_limits<size_t>::max();
  564. const auto & iter = boost::range::find_if(path->nodes, functor);
  565. if(iter == path->nodes.end())
  566. return std::numeric_limits<size_t>::max();
  567. bool reachableToday = iter->turns == 0;
  568. if(iter == path->nodes.begin())
  569. return selectImageCross(reachableToday, iter->coord);
  570. auto next = iter + 1;
  571. auto prev = iter - 1;
  572. // start of path - current hero location
  573. if(next == path->nodes.end())
  574. return std::numeric_limits<size_t>::max();
  575. bool pathContinuous = iter->coord.areNeighbours(next->coord) && iter->coord.areNeighbours(prev->coord);
  576. bool embarking = iter->action == CGPathNode::EMBARK || iter->action == CGPathNode::DISEMBARK;
  577. if(pathContinuous && !embarking)
  578. return selectImageArrow(reachableToday, iter->coord, prev->coord, next->coord);
  579. return selectImageCross(reachableToday, iter->coord);
  580. }
  581. uint8_t MapRendererPath::checksum(IMapRendererContext & context, const int3 & coordinates)
  582. {
  583. return selectImage(context, coordinates) & 0xff;
  584. }
  585. MapRenderer::TileChecksum MapRenderer::getTileChecksum(IMapRendererContext & context, const int3 & coordinates)
  586. {
  587. // computes basic checksum to determine whether tile needs an update
  588. // if any component gives different value, tile will be updated
  589. TileChecksum result;
  590. boost::range::fill(result, std::numeric_limits<uint8_t>::max());
  591. if(!context.isInMap(coordinates))
  592. {
  593. result[0] = rendererBorder.checksum(context, coordinates);
  594. return result;
  595. }
  596. const NeighborTilesInfo neighborInfo(context, coordinates);
  597. if(!context.isVisible(coordinates) && neighborInfo.areAllHidden())
  598. {
  599. result[7] = rendererFow.checksum(context, coordinates);
  600. }
  601. else
  602. {
  603. result[1] = rendererTerrain.checksum(context, coordinates);
  604. if (context.showRivers())
  605. result[2] = rendererRiver.checksum(context, coordinates);
  606. if (context.showRoads())
  607. result[3] = rendererRoad.checksum(context, coordinates);
  608. result[4] = rendererObjects.checksum(context, coordinates);
  609. result[5] = rendererPath.checksum(context, coordinates);
  610. result[6] = rendererDebug.checksum(context, coordinates);
  611. if(!context.isVisible(coordinates))
  612. result[7] = rendererFow.checksum(context, coordinates);
  613. }
  614. return result;
  615. }
  616. void MapRenderer::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)
  617. {
  618. if(!context.isInMap(coordinates))
  619. {
  620. rendererBorder.renderTile(context, target, coordinates);
  621. return;
  622. }
  623. const NeighborTilesInfo neighborInfo(context, coordinates);
  624. if(!context.isVisible(coordinates) && neighborInfo.areAllHidden())
  625. {
  626. rendererFow.renderTile(context, target, coordinates);
  627. }
  628. else
  629. {
  630. rendererTerrain.renderTile(context, target, coordinates);
  631. if (context.showRivers())
  632. rendererRiver.renderTile(context, target, coordinates);
  633. if (context.showRoads())
  634. rendererRoad.renderTile(context, target, coordinates);
  635. rendererObjects.renderTile(context, target, coordinates);
  636. rendererPath.renderTile(context, target, coordinates);
  637. rendererDebug.renderTile(context, target, coordinates);
  638. if(!context.isVisible(coordinates))
  639. rendererFow.renderTile(context, target, coordinates);
  640. }
  641. }