MapRenderer.cpp 26 KB

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