MapRenderer.cpp 24 KB

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