mapHandler.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * mapHandler.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 "mapHandler.h"
  12. #include "CBitmapHandler.h"
  13. #include "gui/CAnimation.h"
  14. #include "gui/SDL_Extensions.h"
  15. #include "CGameInfo.h"
  16. #include "../lib/mapObjects/CGHeroInstance.h"
  17. #include "../lib/mapObjects/CObjectClassesHandler.h"
  18. #include "../lib/CGameState.h"
  19. #include "../lib/CHeroHandler.h"
  20. #include "../lib/CTownHandler.h"
  21. #include "Graphics.h"
  22. #include "../lib/mapping/CMap.h"
  23. #include "../lib/CConfigHandler.h"
  24. #include "../lib/CGeneralTextHandler.h"
  25. #include "../lib/GameConstants.h"
  26. #include "../lib/CStopWatch.h"
  27. #include "CMT.h"
  28. #include "../lib/CRandomGenerator.h"
  29. #define ADVOPT (conf.go()->ac)
  30. static bool objectBlitOrderSorter(const TerrainTileObject & a, const TerrainTileObject & b)
  31. {
  32. return CMapHandler::compareObjectBlitOrder(a.obj, b.obj);
  33. }
  34. struct NeighborTilesInfo
  35. {
  36. bool d7, //789
  37. d8, //456
  38. d9, //123
  39. d4,
  40. d5,
  41. d6,
  42. d1,
  43. d2,
  44. d3;
  45. NeighborTilesInfo(const int3 & pos, const int3 & sizes, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap)
  46. {
  47. auto getTile = [&](int dx, int dy)->bool
  48. {
  49. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  50. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  51. return false;
  52. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  53. };
  54. d7 = getTile(-1, -1); //789
  55. d8 = getTile( 0, -1); //456
  56. d9 = getTile(+1, -1); //123
  57. d4 = getTile(-1, 0);
  58. d5 = visibilityMap[pos.x][pos.y][pos.z];
  59. d6 = getTile(+1, 0);
  60. d1 = getTile(-1, +1);
  61. d2 = getTile( 0, +1);
  62. d3 = getTile(+1, +1);
  63. }
  64. bool areAllHidden() const
  65. {
  66. return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d8 );
  67. }
  68. int getBitmapID() const
  69. {
  70. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  71. // 0-1, 2-3, 4-5, 11-13, 12-14
  72. static const int visBitmaps[256] = {
  73. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  74. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  75. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  76. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  77. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  78. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  79. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  80. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  81. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  82. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  83. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  84. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  85. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  86. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  87. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  88. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  89. };
  90. return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  91. }
  92. };
  93. void CMapHandler::prepareFOWDefs()
  94. {
  95. //assume all frames in group 0
  96. size_t size = graphics->fogOfWarFullHide->size(0);
  97. FoWfullHide.resize(size);
  98. for(size_t frame = 0; frame < size; frame++)
  99. FoWfullHide[frame] = graphics->fogOfWarFullHide->getImage(frame);
  100. //initialization of type of full-hide image
  101. hideBitmap.resize(sizes.x);
  102. for (auto & elem : hideBitmap)
  103. {
  104. elem.resize(sizes.y);
  105. }
  106. for (auto & elem : hideBitmap)
  107. {
  108. for (int j = 0; j < sizes.y; ++j)
  109. {
  110. elem[j].resize(sizes.z);
  111. for(int k = 0; k < sizes.z; ++k)
  112. {
  113. elem[j][k] = CRandomGenerator::getDefault().nextInt(size - 1);
  114. }
  115. }
  116. }
  117. size = graphics->fogOfWarPartialHide->size(0);
  118. FoWpartialHide.resize(size);
  119. for(size_t frame = 0; frame < size; frame++)
  120. FoWpartialHide[frame] = graphics->fogOfWarPartialHide->getImage(frame);
  121. }
  122. EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim /* = false */)
  123. {
  124. assert(info);
  125. bool hasActiveFade = updateObjectsFade();
  126. resolveBlitter(info)->blit(targetSurface, info);
  127. return hasActiveFade ? EMapAnimRedrawStatus::REDRAW_REQUESTED : EMapAnimRedrawStatus::OK;
  128. }
  129. void CMapHandler::initTerrainGraphics()
  130. {
  131. static const std::vector<std::string> TERRAIN_FILES =
  132. {
  133. "DIRTTL",
  134. "SANDTL",
  135. "GRASTL",
  136. "SNOWTL",
  137. "SWMPTL",
  138. "ROUGTL",
  139. "SUBBTL",
  140. "LAVATL",
  141. "WATRTL",
  142. "ROCKTL"
  143. };
  144. static const std::vector<std::string> ROAD_FILES =
  145. {
  146. "dirtrd",
  147. "gravrd",
  148. "cobbrd"
  149. };
  150. static const std::vector<std::string> RIVER_FILES =
  151. {
  152. "clrrvr",
  153. "icyrvr",
  154. "mudrvr",
  155. "lavrvr"
  156. };
  157. auto loadFlipped = [this](int types, TFlippedAnimations & animation, TFlippedCache & cache, const std::vector<std::string> & files)
  158. {
  159. animation.resize(types);
  160. cache.resize(types);
  161. //no rotation and basic setup
  162. for(int i = 0; i < types; i++)
  163. {
  164. animation[i][0] = make_unique<CAnimation>(files[i]);
  165. animation[i][0]->preload();
  166. const size_t views = animation[i][0]->size(0);
  167. cache[i].resize(views);
  168. for(int j = 0; j < views; j++)
  169. cache[i][j][0] = animation[i][0]->getImage(j);
  170. }
  171. for(int rotation = 1; rotation < 4; rotation++)
  172. {
  173. for(int i = 0; i < types; i++)
  174. {
  175. animation[i][rotation] = make_unique<CAnimation>(files[i]);
  176. animation[i][rotation]->preload();
  177. const size_t views = animation[i][rotation]->size(0);
  178. for(int j = 0; j < views; j++)
  179. {
  180. IImage * image = animation[i][rotation]->getImage(j);
  181. if(rotation == 2 || rotation == 3)
  182. image->horizontalFlip();
  183. if(rotation == 1 || rotation == 3)
  184. image->verticalFlip();
  185. cache[i][j][rotation] = image;
  186. }
  187. }
  188. }
  189. };
  190. loadFlipped(GameConstants::TERRAIN_TYPES, terrainAnimations, terrainImages, TERRAIN_FILES);
  191. loadFlipped(3, roadAnimations, roadImages, ROAD_FILES);
  192. loadFlipped(4, riverAnimations, riverImages, RIVER_FILES);
  193. // Create enough room for the whole map and its frame
  194. ttiles.resize(sizes.x, frameW, frameW);
  195. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  196. {
  197. ttiles[i].resize(sizes.y, frameH, frameH);
  198. }
  199. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  200. {
  201. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  202. ttiles[i][j].resize(sizes.z, 0, 0);
  203. }
  204. }
  205. void CMapHandler::initBorderGraphics()
  206. {
  207. egdeImages.resize(egdeAnimation->size(0));
  208. for(size_t i = 0; i < egdeImages.size(); i++)
  209. egdeImages[i] = egdeAnimation->getImage(i);
  210. edgeFrames.resize(sizes.x, frameW, frameW);
  211. for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
  212. {
  213. edgeFrames[i].resize(sizes.y, frameH, frameH);
  214. }
  215. for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
  216. {
  217. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  218. edgeFrames[i][j].resize(sizes.z, 0, 0);
  219. }
  220. auto & rand = CRandomGenerator::getDefault();
  221. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  222. {
  223. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  224. {
  225. for(int k=0; k<sizes.z; ++k) //by levels
  226. {
  227. ui8 terBitmapNum = 0;
  228. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  229. {
  230. if(i==-1 && j==-1)
  231. terBitmapNum = 16;
  232. else if(i==-1 && j==(sizes.y))
  233. terBitmapNum = 19;
  234. else if(i==(sizes.x) && j==-1)
  235. terBitmapNum = 17;
  236. else if(i==(sizes.x) && j==(sizes.y))
  237. terBitmapNum = 18;
  238. else if(j == -1 && i > -1 && i < sizes.x)
  239. terBitmapNum = rand.nextInt(22, 23);
  240. else if(i == -1 && j > -1 && j < sizes.y)
  241. terBitmapNum = rand.nextInt(33, 34);
  242. else if(j == sizes.y && i >-1 && i < sizes.x)
  243. terBitmapNum = rand.nextInt(29, 30);
  244. else if(i == sizes.x && j > -1 && j < sizes.y)
  245. terBitmapNum = rand.nextInt(25, 26);
  246. else
  247. terBitmapNum = rand.nextInt(15);
  248. }
  249. edgeFrames[i][j][k] = terBitmapNum;
  250. }
  251. }
  252. }
  253. }
  254. void CMapHandler::initObjectRects()
  255. {
  256. //initializing objects / rects
  257. for(auto & elem : map->objects)
  258. {
  259. const CGObjectInstance *obj = elem;
  260. if( !obj
  261. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  262. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  263. {
  264. continue;
  265. }
  266. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  267. //no animation at all
  268. if(!animation)
  269. continue;
  270. //empty animation
  271. if(animation->size(0) == 0)
  272. continue;
  273. IImage * image = animation->getImage(0,0);
  274. for(int fx=0; fx < obj->getWidth(); ++fx)
  275. {
  276. for(int fy=0; fy < obj->getHeight(); ++fy)
  277. {
  278. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  279. SDL_Rect cr;
  280. cr.w = 32;
  281. cr.h = 32;
  282. cr.x = image->width() - fx * 32 - 32;
  283. cr.y = image->height() - fy * 32 - 32;
  284. TerrainTileObject toAdd(obj,cr);
  285. if( map->isInTheMap(currTile) && // within map
  286. cr.x + cr.w > 0 && // image has data on this tile
  287. cr.y + cr.h > 0 &&
  288. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  289. )
  290. {
  291. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  292. }
  293. }
  294. }
  295. }
  296. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  297. {
  298. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  299. {
  300. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  301. {
  302. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
  303. }
  304. }
  305. }
  306. }
  307. void CMapHandler::init()
  308. {
  309. CStopWatch th;
  310. th.getDiff();
  311. // Size of visible terrain.
  312. int mapW = conf.go()->ac.advmapW;
  313. int mapH = conf.go()->ac.advmapH;
  314. //sizes of terrain
  315. sizes.x = map->width;
  316. sizes.y = map->height;
  317. sizes.z = map->twoLevel ? 2 : 1;
  318. // Total number of visible tiles. Subtract the center tile, then
  319. // compute the number of tiles on each side, and reassemble.
  320. int t1, t2;
  321. t1 = (mapW-32)/2;
  322. t2 = mapW - 32 - t1;
  323. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  324. t1 = (mapH-32)/2;
  325. t2 = mapH - 32 - t1;
  326. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  327. // Size of the frame around the map. In extremes positions, the
  328. // frame must not be on the center of the map, but right on the
  329. // edge of the center tile.
  330. frameW = (mapW+31) /32 / 2;
  331. frameH = (mapH+31) /32 / 2;
  332. offsetX = (mapW - (2*frameW+1)*32)/2;
  333. offsetY = (mapH - (2*frameH+1)*32)/2;
  334. prepareFOWDefs();
  335. initTerrainGraphics();
  336. initBorderGraphics();
  337. logGlobal->infoStream()<<"\tPreparing FoW, terrain, roads, rivers, borders: "<<th.getDiff();
  338. initObjectRects();
  339. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  340. }
  341. CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
  342. {
  343. if (info->scaled)
  344. return worldViewBlitter;
  345. if (info->puzzleMode)
  346. return puzzleViewBlitter;
  347. return normalBlitter;
  348. }
  349. void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  350. {
  351. source->draw(targetSurf, destRect, sourceRect);
  352. }
  353. void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
  354. {
  355. info = drawingInfo;
  356. // Width and height of the portion of the map to process. Units in tiles.
  357. tileCount.x = parent->tilesW;
  358. tileCount.y = parent->tilesH;
  359. topTile = info->topTile;
  360. initPos.x = parent->offsetX + info->drawBounds->x;
  361. initPos.y = parent->offsetY + info->drawBounds->y;
  362. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  363. // If moving, we need to add an extra column/line
  364. if (info->movement.x != 0)
  365. {
  366. tileCount.x++;
  367. initPos.x += info->movement.x;
  368. if (info->movement.x > 0)
  369. {
  370. // Moving right. We still need to draw the old tile on the
  371. // left, so adjust our referential
  372. topTile.x--;
  373. initPos.x -= tileSize;
  374. }
  375. }
  376. if (info->movement.y != 0)
  377. {
  378. tileCount.y++;
  379. initPos.y += info->movement.y;
  380. if (info->movement.y > 0)
  381. {
  382. // Moving down. We still need to draw the tile on the top,
  383. // so adjust our referential.
  384. topTile.y--;
  385. initPos.y -= tileSize;
  386. }
  387. }
  388. // Reduce sizes if we go out of the full map.
  389. if (topTile.x < -parent->frameW)
  390. topTile.x = -parent->frameW;
  391. if (topTile.y < -parent->frameH)
  392. topTile.y = -parent->frameH;
  393. if (topTile.x + tileCount.x > parent->sizes.x + parent->frameW)
  394. tileCount.x = parent->sizes.x + parent->frameW - topTile.x;
  395. if (topTile.y + tileCount.y > parent->sizes.y + parent->frameH)
  396. tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
  397. }
  398. SDL_Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
  399. {
  400. SDL_Rect prevClip;
  401. SDL_GetClipRect(targetSurf, &prevClip);
  402. SDL_SetClipRect(targetSurf, info->drawBounds);
  403. return prevClip;
  404. }
  405. CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
  406. : CMapBlitter(parent)
  407. {
  408. tileSize = 32;
  409. halfTileSizeCeil = 16;
  410. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  411. }
  412. IImage * CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
  413. {
  414. int ownerIndex = 0;
  415. if(owner < PlayerColor::PLAYER_LIMIT)
  416. {
  417. ownerIndex = owner.getNum() * 19;
  418. }
  419. else if (owner == PlayerColor::NEUTRAL)
  420. {
  421. ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
  422. }
  423. switch(id)
  424. {
  425. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  426. case Obj::MONOLITH_ONE_WAY_EXIT:
  427. case Obj::MONOLITH_TWO_WAY:
  428. return info->icons->getImage((int)EWorldViewIcon::TELEPORT);
  429. case Obj::SUBTERRANEAN_GATE:
  430. return info->icons->getImage((int)EWorldViewIcon::GATE);
  431. case Obj::ARTIFACT:
  432. return info->icons->getImage((int)EWorldViewIcon::ARTIFACT);
  433. case Obj::TOWN:
  434. return info->icons->getImage((int)EWorldViewIcon::TOWN + ownerIndex);
  435. case Obj::HERO:
  436. return info->icons->getImage((int)EWorldViewIcon::HERO + ownerIndex);
  437. case Obj::MINE:
  438. return info->icons->getImage((int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex);
  439. case Obj::RESOURCE:
  440. return info->icons->getImage((int)EWorldViewIcon::RES_WOOD + subId + ownerIndex);
  441. }
  442. return nullptr;
  443. }
  444. void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
  445. {
  446. bool outsideLeft = topTile.x < 0;
  447. bool outsideTop = topTile.y < 0;
  448. bool outsideRight = std::max(0, topTile.x) + tileCount.x > parent->sizes.x;
  449. bool outsideBottom = std::max(0, topTile.y) + tileCount.y > parent->sizes.y;
  450. if (tileCount.x > parent->sizes.x)
  451. topTile.x = parent->sizes.x / 2 - tileCount.x / 2; // center viewport if the whole map can fit into the screen at once
  452. else if (outsideLeft)
  453. {
  454. if (outsideRight)
  455. topTile.x = parent->sizes.x / 2 - tileCount.x / 2;
  456. else
  457. topTile.x = 0;
  458. }
  459. else if (outsideRight)
  460. topTile.x = parent->sizes.x - tileCount.x;
  461. if (tileCount.y > parent->sizes.y)
  462. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  463. else if (outsideTop)
  464. {
  465. if (outsideBottom)
  466. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  467. else
  468. topTile.y = 0;
  469. }
  470. else if (outsideBottom)
  471. topTile.y = parent->sizes.y - tileCount.y;
  472. }
  473. void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  474. {
  475. IImage * scaled = parent->cache.requestWorldViewCacheOrCreate(cacheType, source);
  476. if(scaled)
  477. scaled->draw(targetSurf, destRect, sourceRect);
  478. }
  479. void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  480. {
  481. auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
  482. {
  483. IImage * wvIcon = this->objectToIcon(id, subId, owner);
  484. if(nullptr != wvIcon)
  485. {
  486. // centering icon on the object
  487. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  488. wvIcon->draw(targetSurf, dest.x, dest.y);
  489. }
  490. };
  491. auto & objects = tile.objects;
  492. for(auto & object : objects)
  493. {
  494. const CGObjectInstance * obj = object.obj;
  495. const bool sameLevel = obj->pos.z == pos.z;
  496. const bool isVisible = (*info->visibilityMap)[pos.x][pos.y][pos.z];
  497. const bool isVisitable = obj->visitableAt(pos.x, pos.y);
  498. if(sameLevel && isVisible && isVisitable)
  499. drawIcon(obj->ID, obj->subID, obj->tempOwner);
  500. }
  501. }
  502. void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  503. {
  504. if(nullptr == info->additionalIcons)
  505. return;
  506. const int3 bottomRight = pos + tileCount;
  507. for(const ObjectPosInfo & iconInfo : *(info->additionalIcons))
  508. {
  509. if( iconInfo.pos.z != pos.z)
  510. continue;
  511. if((iconInfo.pos.x < topTile.x) || (iconInfo.pos.y < topTile.y))
  512. continue;
  513. if((iconInfo.pos.x > bottomRight.x) || (iconInfo.pos.y > bottomRight.y))
  514. continue;
  515. realPos.x = initPos.x + (iconInfo.pos.x - topTile.x) * tileSize;
  516. realPos.y = initPos.x + (iconInfo.pos.y - topTile.y) * tileSize;
  517. IImage * wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
  518. if(nullptr != wvIcon)
  519. {
  520. // centering icon on the object
  521. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  522. wvIcon->draw(targetSurf, dest.x, dest.y);
  523. }
  524. }
  525. }
  526. void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  527. {
  528. if (moving)
  529. return;
  530. CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
  531. }
  532. void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  533. {
  534. if (moving)
  535. return;
  536. Rect scaledSourceRect(sourceRect->x * info->scale, sourceRect->y * info->scale, sourceRect->w, sourceRect->h);
  537. CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
  538. }
  539. void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
  540. {
  541. Rect destRect(realTileRect);
  542. ui8 rotation = tinfo.extTileFlags % 4;
  543. drawElement(EMapCacheType::TERRAIN, parent->terrainImages[tinfo.terType][tinfo.terView][rotation], nullptr, targetSurf, &destRect);
  544. }
  545. void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
  546. {
  547. info = drawingInfo;
  548. parent->cache.updateWorldViewScale(info->scale);
  549. topTile = info->topTile;
  550. tileSize = (int) floorf(32.0f * info->scale);
  551. halfTileSizeCeil = (int)ceilf(tileSize / 2.0f);
  552. tileCount.x = (int) ceilf((float)info->drawBounds->w / tileSize);
  553. tileCount.y = (int) ceilf((float)info->drawBounds->h / tileSize);
  554. initPos.x = info->drawBounds->x;
  555. initPos.y = info->drawBounds->y;
  556. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  557. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  558. calculateWorldViewCameraPos();
  559. }
  560. SDL_Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
  561. {
  562. SDL_Rect prevClip;
  563. SDL_FillRect(targetSurf, info->drawBounds, SDL_MapRGB(targetSurf->format, 0, 0, 0));
  564. // makes the clip area smaller if the map is smaller than the screen frame
  565. // (actually, it could be made 1 tile bigger so that overlay icons on edge tiles could be drawn partly outside)
  566. Rect clipRect(std::max<int>(info->drawBounds->x, info->drawBounds->x - topTile.x * tileSize),
  567. std::max<int>(info->drawBounds->y, info->drawBounds->y - topTile.y * tileSize),
  568. std::min<int>(info->drawBounds->w, parent->sizes.x * tileSize),
  569. std::min<int>(info->drawBounds->h, parent->sizes.y * tileSize));
  570. SDL_GetClipRect(targetSurf, &prevClip);
  571. SDL_SetClipRect(targetSurf, &clipRect); //preventing blitting outside of that rect
  572. return prevClip;
  573. }
  574. CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
  575. : CMapBlitter(parent)
  576. {
  577. }
  578. void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  579. {
  580. CMapBlitter::drawObjects(targetSurf, tile);
  581. // grail X mark
  582. if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
  583. {
  584. const IImage * mark = graphics->heroMoveArrows->getImage(0);
  585. mark->draw(targetSurf,realTileRect.x,realTileRect.y);
  586. }
  587. }
  588. void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
  589. {
  590. CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
  591. }
  592. bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
  593. {
  594. if (!CMapBlitter::canDrawObject(obj))
  595. return false;
  596. //don't print flaggable objects in puzzle mode
  597. if (obj->isVisitable())
  598. return false;
  599. if(std::find(unblittableObjects.begin(), unblittableObjects.end(), obj->ID) != unblittableObjects.end())
  600. return false;
  601. return true;
  602. }
  603. CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
  604. : CMapNormalBlitter(parent)
  605. {
  606. unblittableObjects.push_back(Obj::HOLE);
  607. }
  608. void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
  609. {
  610. Rect destRect(realTileRect);
  611. drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
  612. }
  613. void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  614. {
  615. //nothing to do here
  616. }
  617. void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  618. {
  619. drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
  620. }
  621. void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  622. {
  623. Rect dstRect(realTileRect);
  624. drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
  625. }
  626. void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  627. {
  628. auto & objects = tile.objects;
  629. for(auto & object : objects)
  630. {
  631. if (object.fadeAnimKey >= 0)
  632. {
  633. auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
  634. if (fadeIter != parent->fadeAnims.end())
  635. {
  636. // this object is currently fading, so skip normal drawing
  637. Rect r2(realTileRect);
  638. CFadeAnimation * fade = (*fadeIter).second.second;
  639. fade->draw(targetSurf, nullptr, &r2);
  640. continue;
  641. }
  642. logGlobal->errorStream() << "Fading map object with missing fade anim : " << object.fadeAnimKey;
  643. continue;
  644. }
  645. const CGObjectInstance * obj = object.obj;
  646. if (!obj)
  647. {
  648. logGlobal->errorStream() << "Stray map object that isn't fading";
  649. continue;
  650. }
  651. if (!canDrawObject(obj))
  652. continue;
  653. auto objData = findObjectBitmap(obj, info->anim);
  654. if (objData.objBitmap)
  655. {
  656. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  657. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  658. if (objData.flagBitmap)
  659. {
  660. if (objData.isMoving)
  661. {
  662. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  663. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  664. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  665. }
  666. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  667. {
  668. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  669. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  676. {
  677. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  678. {
  679. ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
  680. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  681. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  682. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfoUpper->roadType - 1][tinfoUpper->roadDir][rotation],
  683. &source, targetSurf, &dest);
  684. }
  685. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  686. {
  687. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  688. Rect source(0, 0, tileSize, halfTileSizeCeil);
  689. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  690. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfo.roadType - 1][tinfo.roadDir][rotation],
  691. &source, targetSurf, &dest);
  692. }
  693. }
  694. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  695. {
  696. Rect destRect(realTileRect);
  697. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  698. drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType-1][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
  699. }
  700. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  701. {
  702. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  703. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  704. if (retBitmapID < 0)
  705. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  706. const IImage * image = nullptr;
  707. if (retBitmapID >= 0)
  708. image = parent->FoWpartialHide.at(retBitmapID);
  709. else
  710. image = parent->FoWfullHide.at(-retBitmapID - 1);
  711. Rect destRect(realTileRect);
  712. drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
  713. }
  714. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  715. {
  716. init(info);
  717. auto prevClip = clip(targetSurf);
  718. pos = int3(0, 0, topTile.z);
  719. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  720. {
  721. if (pos.x < 0 || pos.x >= parent->sizes.x)
  722. continue;
  723. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  724. {
  725. if (pos.y < 0 || pos.y >= parent->sizes.y)
  726. continue;
  727. const bool isVisible = canDrawCurrentTile();
  728. realTileRect.x = realPos.x;
  729. realTileRect.y = realPos.y;
  730. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  731. const TerrainTile & tinfo = parent->map->getTile(pos);
  732. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  733. if(isVisible || info->showAllTerrain)
  734. {
  735. drawTileTerrain(targetSurf, tinfo, tile);
  736. if (tinfo.riverType)
  737. drawRiver(targetSurf, tinfo);
  738. drawRoad(targetSurf, tinfo, tinfoUpper);
  739. }
  740. if(isVisible)
  741. drawObjects(targetSurf, tile);
  742. }
  743. }
  744. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  745. {
  746. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  747. {
  748. realTileRect.x = realPos.x;
  749. realTileRect.y = realPos.y;
  750. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  751. pos.y < 0 || pos.y >= parent->sizes.y)
  752. {
  753. drawFrame(targetSurf);
  754. }
  755. else
  756. {
  757. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  758. if (!(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  759. drawFow(targetSurf);
  760. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  761. drawTileOverlay(targetSurf, tile);
  762. // drawDebugVisitables()
  763. if (settings["session"]["showBlock"].Bool())
  764. {
  765. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  766. {
  767. static SDL_Surface * block = nullptr;
  768. if (!block)
  769. block = BitmapHandler::loadBitmap("blocked");
  770. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  771. }
  772. }
  773. if (settings["session"]["showVisit"].Bool())
  774. {
  775. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  776. {
  777. static SDL_Surface * visit = nullptr;
  778. if (!visit)
  779. visit = BitmapHandler::loadBitmap("visitable");
  780. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. drawOverlayEx(targetSurf);
  787. // drawDebugGrid()
  788. if (settings["session"]["showGrid"].Bool())
  789. {
  790. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  791. {
  792. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  793. {
  794. const int3 color(0x555555, 0x555555, 0x555555);
  795. if (realPos.y >= info->drawBounds->y &&
  796. realPos.y < info->drawBounds->y + info->drawBounds->h)
  797. for(int i = 0; i < tileSize; i++)
  798. if (realPos.x + i >= info->drawBounds->x &&
  799. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  800. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  801. if (realPos.x >= info->drawBounds->x &&
  802. realPos.x < info->drawBounds->x + info->drawBounds->w)
  803. for(int i = 0; i < tileSize; i++)
  804. if (realPos.y + i >= info->drawBounds->y &&
  805. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  806. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  807. }
  808. }
  809. }
  810. postProcessing(targetSurf);
  811. SDL_SetClipRect(targetSurf, &prevClip);
  812. }
  813. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  814. {
  815. if(hero && hero->moveDir && hero->type) //it's hero or boat
  816. {
  817. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  818. {
  819. logGlobal->errorStream() << "A neutral hero (" << hero->name << ") at " << hero->pos << ". Should not happen!";
  820. return CMapHandler::AnimBitmapHolder();
  821. }
  822. //pick graphics of hero (or boat if hero is sailing)
  823. std::shared_ptr<CAnimation> animation;
  824. if (hero->boat)
  825. animation = graphics->boatAnimations[hero->boat->subID];
  826. else
  827. animation = graphics->heroAnimations[hero->appearance.animationFile];
  828. bool moving = !hero->isStanding;
  829. int group = getHeroFrameGroup(hero->moveDir, moving);
  830. if(animation->size(group) > 0)
  831. {
  832. int frame = anim % animation->size(group);
  833. IImage * heroImage = animation->getImage(frame, group);
  834. //get flag overlay only if we have main image
  835. IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
  836. return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
  837. }
  838. }
  839. return CMapHandler::AnimBitmapHolder();
  840. }
  841. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  842. {
  843. auto animation = graphics->boatAnimations.at(boat->subID);
  844. int group = getHeroFrameGroup(boat->direction, false);
  845. if(animation->size(group) > 0)
  846. return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
  847. else
  848. return CMapHandler::AnimBitmapHolder();
  849. }
  850. IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  851. {
  852. if (!hero)
  853. return nullptr;
  854. if (hero->boat)
  855. return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
  856. return findHeroFlagBitmap(hero, anim, color, group);
  857. }
  858. IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  859. {
  860. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
  861. }
  862. IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
  863. {
  864. int boatType = boat->subID;
  865. if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
  866. {
  867. logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID;
  868. return nullptr;
  869. }
  870. const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
  871. int colorIndex = color->getNum();
  872. if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
  873. {
  874. logGlobal->errorStream() << "Invalid player color " << colorIndex;
  875. return nullptr;
  876. }
  877. return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
  878. }
  879. IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
  880. {
  881. size_t groupSize = animation->size(group);
  882. if(groupSize == 0)
  883. return nullptr;
  884. if(moving)
  885. return animation->getImage(anim % groupSize, group);
  886. else
  887. return animation->getImage((anim / 4) % groupSize, group);
  888. }
  889. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  890. {
  891. if (!obj)
  892. return CMapHandler::AnimBitmapHolder();
  893. if (obj->ID == Obj::HERO)
  894. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  895. if (obj->ID == Obj::BOAT)
  896. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  897. // normal object
  898. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  899. size_t groupSize = animation->size();
  900. if(groupSize == 0)
  901. return CMapHandler::AnimBitmapHolder();
  902. IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % groupSize);
  903. if(!bitmap)
  904. return CMapHandler::AnimBitmapHolder();
  905. bitmap->setFlagColor(obj->tempOwner);
  906. return CMapHandler::AnimBitmapHolder(bitmap);
  907. }
  908. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  909. {
  910. auto i = parent->animationPhase.find(object);
  911. if(i == parent->animationPhase.end())
  912. {
  913. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  914. parent->animationPhase[object] = ret;
  915. return ret;
  916. }
  917. return i->second;
  918. }
  919. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  920. {
  921. //checking if object has non-empty graphic on this tile
  922. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  923. }
  924. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  925. {
  926. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  927. return !neighbors.areAllHidden();
  928. }
  929. ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
  930. {
  931. if(isMoving)
  932. {
  933. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  934. return frame[dir];
  935. }
  936. else //if(isMoving)
  937. {
  938. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  939. return frame[dir];
  940. }
  941. }
  942. bool CMapHandler::updateObjectsFade()
  943. {
  944. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  945. {
  946. int3 pos = (*iter).second.first;
  947. CFadeAnimation * anim = (*iter).second.second;
  948. anim->update();
  949. if (anim->isFading())
  950. ++iter;
  951. else // fade finished
  952. {
  953. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  954. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  955. {
  956. if ((*objIter).fadeAnimKey == (*iter).first)
  957. {
  958. logAnim->traceStream() << "Fade anim finished for obj at " << pos << "; remaining: " << (fadeAnims.size() - 1);
  959. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  960. objs.erase(objIter); // if this was fadeout, remove the object from the map
  961. else
  962. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  963. break;
  964. }
  965. }
  966. delete (*iter).second.second;
  967. iter = fadeAnims.erase(iter);
  968. }
  969. }
  970. return !fadeAnims.empty();
  971. }
  972. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  973. {
  974. SDL_Surface * fadeBitmap;
  975. assert(obj.obj);
  976. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  977. if (objData.objBitmap)
  978. {
  979. if (objData.isMoving) // ignore fading of moving objects (for now?)
  980. {
  981. logAnim->debugStream() << "Ignoring fade of moving object";
  982. return false;
  983. }
  984. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  985. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  986. objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
  987. if (objData.flagBitmap)
  988. {
  989. if (obj.obj->pos.x - 1 == pos.x && obj.obj->pos.y - 1 == pos.y) // -1 to draw flag in top-center instead of right-bottom; kind of a hack
  990. {
  991. Rect flagSrcRect(32, 0, 32, 32);
  992. objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
  993. }
  994. }
  995. auto anim = new CFadeAnimation();
  996. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  997. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  998. obj.fadeAnimKey = fadeAnimCounter;
  999. logAnim->traceStream() << "Fade anim started for obj " << obj.obj->ID
  1000. << " at " << pos << "; anim count: " << fadeAnims.size();
  1001. return true;
  1002. }
  1003. return false;
  1004. }
  1005. bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = false */)
  1006. {
  1007. auto animation = graphics->getAnimation(obj);
  1008. if(!animation)
  1009. return false;
  1010. IImage * bitmap = animation->getImage(0);
  1011. if(!bitmap)
  1012. return false;
  1013. const int tilesW = bitmap->width()/32;
  1014. const int tilesH = bitmap->height()/32;
  1015. for(int fx=0; fx<tilesW; ++fx)
  1016. {
  1017. for(int fy=0; fy<tilesH; ++fy)
  1018. {
  1019. SDL_Rect cr;
  1020. cr.w = 32;
  1021. cr.h = 32;
  1022. cr.x = fx*32;
  1023. cr.y = fy*32;
  1024. TerrainTileObject toAdd(obj, cr);
  1025. if((obj->pos.x + fx - tilesW+1)>=0 && (obj->pos.x + fx - tilesW+1)<ttiles.size()-frameW && (obj->pos.y + fy - tilesH+1)>=0 && (obj->pos.y + fy - tilesH+1)<ttiles[0].size()-frameH)
  1026. {
  1027. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1028. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1029. if (fadein && ADVOPT.objectFading)
  1030. {
  1031. startObjectFade(toAdd, true, pos);
  1032. }
  1033. auto i = curt.objects.begin();
  1034. for(; i != curt.objects.end(); i++)
  1035. {
  1036. if(objectBlitOrderSorter(toAdd, *i))
  1037. {
  1038. curt.objects.insert(i, toAdd);
  1039. i = curt.objects.begin(); //to validate and avoid adding it second time
  1040. break;
  1041. }
  1042. }
  1043. if(i == curt.objects.end())
  1044. curt.objects.insert(i, toAdd);
  1045. }
  1046. }
  1047. }
  1048. return true;
  1049. }
  1050. bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
  1051. {
  1052. //optimized version which reveals weird bugs with missing def name
  1053. //auto pos = obj->pos;
  1054. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1055. //{
  1056. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1057. // {
  1058. // int3 t(i, j, pos.z);
  1059. // if (!map->isInTheMap(t))
  1060. // continue;
  1061. // auto &objs = ttiles[i][j][pos.z].objects;
  1062. // for (size_t x = 0; x < objs.size(); x++)
  1063. // {
  1064. // auto ourObj = objs[x].obj;
  1065. // if (ourObj && ourObj->id == obj->id)
  1066. // {
  1067. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1068. // {
  1069. // if (startObjectFade(objs[x], false, t))
  1070. // objs[x].obj = nullptr; //set original pointer to null
  1071. // else
  1072. // objs.erase(objs.begin() + x);
  1073. // }
  1074. // else
  1075. // objs.erase(objs.begin() + x);
  1076. // break;
  1077. // }
  1078. // }
  1079. // }
  1080. //}
  1081. for (size_t i = 0; i<map->width; i++)
  1082. {
  1083. for (size_t j = 0; j<map->height; j++)
  1084. {
  1085. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1086. {
  1087. auto &objs = ttiles[i][j][k].objects;
  1088. for (size_t x = 0; x < objs.size(); x++)
  1089. {
  1090. if (objs[x].obj && objs[x].obj->id == obj->id)
  1091. {
  1092. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1093. {
  1094. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1095. objs[x].obj = nullptr;
  1096. else
  1097. objs.erase(objs.begin() + x);
  1098. }
  1099. else
  1100. objs.erase(objs.begin() + x);
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. return true;
  1108. }
  1109. bool CMapHandler::canStartHeroMovement()
  1110. {
  1111. return fadeAnims.empty(); // don't allow movement during fade animation
  1112. }
  1113. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1114. {
  1115. for(auto & elem : terrainImages[7])
  1116. {
  1117. for(IImage * img : elem)
  1118. img->shiftPalette(246, 9);
  1119. }
  1120. for(auto & elem : terrainImages[8])
  1121. {
  1122. for(IImage * img : elem)
  1123. {
  1124. img->shiftPalette(229, 12);
  1125. img->shiftPalette(242, 14);
  1126. }
  1127. }
  1128. for(auto & elem : riverImages[0])
  1129. {
  1130. for(IImage * img : elem)
  1131. {
  1132. img->shiftPalette(183, 12);
  1133. img->shiftPalette(195, 6);
  1134. }
  1135. }
  1136. for(auto & elem : riverImages[2])
  1137. {
  1138. for(IImage * img : elem)
  1139. {
  1140. img->shiftPalette(228, 12);
  1141. img->shiftPalette(183, 6);
  1142. img->shiftPalette(240, 6);
  1143. }
  1144. }
  1145. for(auto & elem : riverImages[3])
  1146. {
  1147. for(IImage * img : elem)
  1148. img->shiftPalette(240, 9);
  1149. }
  1150. }
  1151. CMapHandler::~CMapHandler()
  1152. {
  1153. delete normalBlitter;
  1154. delete worldViewBlitter;
  1155. delete puzzleViewBlitter;
  1156. for (auto & elem : fadeAnims)
  1157. {
  1158. delete elem.second.second;
  1159. }
  1160. }
  1161. CMapHandler::CMapHandler()
  1162. {
  1163. frameW = frameH = 0;
  1164. normalBlitter = new CMapNormalBlitter(this);
  1165. worldViewBlitter = new CMapWorldViewBlitter(this);
  1166. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1167. fadeAnimCounter = 0;
  1168. egdeAnimation = make_unique<CAnimation>("EDG");
  1169. egdeAnimation->preload();
  1170. }
  1171. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1172. {
  1173. out.clear();
  1174. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1175. const TerrainTile &t = map->getTile(pos);
  1176. for(auto & elem : tt.objects)
  1177. {
  1178. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1179. {
  1180. out = elem.obj->getObjectName();
  1181. return;
  1182. }
  1183. }
  1184. if(t.hasFavorableWinds())
  1185. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1186. else if(terName)
  1187. {
  1188. out = CGI->generaltexth->terrainNames[t.terType];
  1189. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1190. {
  1191. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1192. }
  1193. }
  1194. }
  1195. void CMapHandler::discardWorldViewCache()
  1196. {
  1197. cache.discardWorldViewCache();
  1198. }
  1199. void CMapHandler::CMapCache::discardWorldViewCache()
  1200. {
  1201. for(auto & cache : data)
  1202. cache.clear();
  1203. logAnim->debug("Discarded world view cache");
  1204. }
  1205. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1206. {
  1207. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1208. discardWorldViewCache();
  1209. worldViewCachedScale = scale;
  1210. }
  1211. IImage * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, const IImage * fullSurface)
  1212. {
  1213. intptr_t key = (intptr_t) fullSurface;
  1214. auto & cache = data[(ui8)type];
  1215. auto iter = cache.find(key);
  1216. if(iter == cache.end())
  1217. {
  1218. auto scaled = fullSurface->scaleFast(worldViewCachedScale);
  1219. IImage * ret = scaled.get();
  1220. cache[key] = std::move(scaled);
  1221. return ret;
  1222. }
  1223. else
  1224. {
  1225. return (*iter).second.get();
  1226. }
  1227. }
  1228. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1229. {
  1230. if (!a)
  1231. return true;
  1232. if (!b)
  1233. return false;
  1234. if (a->appearance.printPriority != b->appearance.printPriority)
  1235. return a->appearance.printPriority > b->appearance.printPriority;
  1236. if(a->pos.y != b->pos.y)
  1237. return a->pos.y < b->pos.y;
  1238. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1239. return true;
  1240. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1241. return false;
  1242. if(!a->isVisitable() && b->isVisitable())
  1243. return true;
  1244. if(!b->isVisitable() && a->isVisitable())
  1245. return false;
  1246. if(a->pos.x < b->pos.x)
  1247. return true;
  1248. return false;
  1249. }
  1250. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1251. : obj(obj_),
  1252. rect(rect_),
  1253. fadeAnimKey(-1)
  1254. {
  1255. }
  1256. TerrainTileObject::~TerrainTileObject()
  1257. {
  1258. }