MapRenderer.cpp 25 KB

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