MapRenderer.cpp 25 KB

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