mapHandler.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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 settings["session"]["spectate"].Bool() ? true : 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)
  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->info("\tPreparing FoW, terrain, roads, rivers, borders: %d ms", th.getDiff());
  338. initObjectRects();
  339. logGlobal->info("\tMaking object rects: %d ms", 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 = settings["session"]["spectate"].Bool() ? true : (*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. CMapHandler::CMapBlitter::CMapBlitter(CMapHandler * p)
  609. :parent(p), tileSize(0), halfTileSizeCeil(0), info(nullptr)
  610. {
  611. }
  612. CMapHandler::CMapBlitter::~CMapBlitter() = default;
  613. void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
  614. {
  615. Rect destRect(realTileRect);
  616. drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
  617. }
  618. void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  619. {
  620. //nothing to do here
  621. }
  622. void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  623. {
  624. drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
  625. }
  626. void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  627. {
  628. Rect dstRect(realTileRect);
  629. drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
  630. }
  631. void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  632. {
  633. auto & objects = tile.objects;
  634. for(auto & object : objects)
  635. {
  636. if (object.fadeAnimKey >= 0)
  637. {
  638. auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
  639. if (fadeIter != parent->fadeAnims.end())
  640. {
  641. // this object is currently fading, so skip normal drawing
  642. Rect r2(realTileRect);
  643. CFadeAnimation * fade = (*fadeIter).second.second;
  644. fade->draw(targetSurf, nullptr, &r2);
  645. continue;
  646. }
  647. logGlobal->error("Fading map object with missing fade anim : %d", object.fadeAnimKey);
  648. continue;
  649. }
  650. const CGObjectInstance * obj = object.obj;
  651. if (!obj)
  652. {
  653. logGlobal->error("Stray map object that isn't fading");
  654. continue;
  655. }
  656. if (!canDrawObject(obj))
  657. continue;
  658. auto objData = findObjectBitmap(obj, info->anim);
  659. if (objData.objBitmap)
  660. {
  661. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  662. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  663. if (objData.flagBitmap)
  664. {
  665. if (objData.isMoving)
  666. {
  667. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  668. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  669. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  670. }
  671. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  672. {
  673. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  674. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  675. }
  676. }
  677. }
  678. }
  679. }
  680. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  681. {
  682. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  683. {
  684. ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
  685. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  686. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  687. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfoUpper->roadType - 1][tinfoUpper->roadDir][rotation],
  688. &source, targetSurf, &dest);
  689. }
  690. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  691. {
  692. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  693. Rect source(0, 0, tileSize, halfTileSizeCeil);
  694. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  695. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfo.roadType - 1][tinfo.roadDir][rotation],
  696. &source, targetSurf, &dest);
  697. }
  698. }
  699. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  700. {
  701. Rect destRect(realTileRect);
  702. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  703. drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType-1][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
  704. }
  705. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  706. {
  707. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  708. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  709. if (retBitmapID < 0)
  710. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  711. const IImage * image = nullptr;
  712. if (retBitmapID >= 0)
  713. image = parent->FoWpartialHide.at(retBitmapID);
  714. else
  715. image = parent->FoWfullHide.at(-retBitmapID - 1);
  716. Rect destRect(realTileRect);
  717. drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
  718. }
  719. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  720. {
  721. init(info);
  722. auto prevClip = clip(targetSurf);
  723. pos = int3(0, 0, topTile.z);
  724. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  725. {
  726. if (pos.x < 0 || pos.x >= parent->sizes.x)
  727. continue;
  728. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  729. {
  730. if (pos.y < 0 || pos.y >= parent->sizes.y)
  731. continue;
  732. const bool isVisible = canDrawCurrentTile();
  733. realTileRect.x = realPos.x;
  734. realTileRect.y = realPos.y;
  735. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  736. const TerrainTile & tinfo = parent->map->getTile(pos);
  737. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  738. if(isVisible || info->showAllTerrain)
  739. {
  740. drawTileTerrain(targetSurf, tinfo, tile);
  741. if (tinfo.riverType)
  742. drawRiver(targetSurf, tinfo);
  743. drawRoad(targetSurf, tinfo, tinfoUpper);
  744. }
  745. if(isVisible)
  746. drawObjects(targetSurf, tile);
  747. }
  748. }
  749. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  750. {
  751. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  752. {
  753. realTileRect.x = realPos.x;
  754. realTileRect.y = realPos.y;
  755. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  756. pos.y < 0 || pos.y >= parent->sizes.y)
  757. {
  758. drawFrame(targetSurf);
  759. }
  760. else
  761. {
  762. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  763. if(!settings["session"]["spectate"].Bool() && !(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  764. drawFow(targetSurf);
  765. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  766. drawTileOverlay(targetSurf, tile);
  767. // drawDebugVisitables()
  768. if (settings["session"]["showBlock"].Bool())
  769. {
  770. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  771. {
  772. static SDL_Surface * block = nullptr;
  773. if (!block)
  774. block = BitmapHandler::loadBitmap("blocked");
  775. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  776. }
  777. }
  778. if (settings["session"]["showVisit"].Bool())
  779. {
  780. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  781. {
  782. static SDL_Surface * visit = nullptr;
  783. if (!visit)
  784. visit = BitmapHandler::loadBitmap("visitable");
  785. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  786. }
  787. }
  788. }
  789. }
  790. }
  791. drawOverlayEx(targetSurf);
  792. // drawDebugGrid()
  793. if (settings["session"]["showGrid"].Bool())
  794. {
  795. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  796. {
  797. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  798. {
  799. const int3 color(0x555555, 0x555555, 0x555555);
  800. if (realPos.y >= info->drawBounds->y &&
  801. realPos.y < info->drawBounds->y + info->drawBounds->h)
  802. for(int i = 0; i < tileSize; i++)
  803. if (realPos.x + i >= info->drawBounds->x &&
  804. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  805. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  806. if (realPos.x >= info->drawBounds->x &&
  807. realPos.x < info->drawBounds->x + info->drawBounds->w)
  808. for(int i = 0; i < tileSize; i++)
  809. if (realPos.y + i >= info->drawBounds->y &&
  810. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  811. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  812. }
  813. }
  814. }
  815. postProcessing(targetSurf);
  816. SDL_SetClipRect(targetSurf, &prevClip);
  817. }
  818. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  819. {
  820. if(hero && hero->moveDir && hero->type) //it's hero or boat
  821. {
  822. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  823. {
  824. logGlobal->error("A neutral hero (%s) at %s. Should not happen!", hero->name, hero->pos.toString());
  825. return CMapHandler::AnimBitmapHolder();
  826. }
  827. //pick graphics of hero (or boat if hero is sailing)
  828. std::shared_ptr<CAnimation> animation;
  829. if (hero->boat)
  830. animation = graphics->boatAnimations[hero->boat->subID];
  831. else
  832. animation = graphics->heroAnimations[hero->appearance.animationFile];
  833. bool moving = !hero->isStanding;
  834. int group = getHeroFrameGroup(hero->moveDir, moving);
  835. if(animation->size(group) > 0)
  836. {
  837. int frame = anim % animation->size(group);
  838. IImage * heroImage = animation->getImage(frame, group);
  839. //get flag overlay only if we have main image
  840. IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
  841. return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
  842. }
  843. }
  844. return CMapHandler::AnimBitmapHolder();
  845. }
  846. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  847. {
  848. auto animation = graphics->boatAnimations.at(boat->subID);
  849. int group = getHeroFrameGroup(boat->direction, false);
  850. if(animation->size(group) > 0)
  851. return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
  852. else
  853. return CMapHandler::AnimBitmapHolder();
  854. }
  855. IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  856. {
  857. if (!hero)
  858. return nullptr;
  859. if (hero->boat)
  860. return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
  861. return findHeroFlagBitmap(hero, anim, color, group);
  862. }
  863. IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  864. {
  865. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
  866. }
  867. IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
  868. {
  869. int boatType = boat->subID;
  870. if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
  871. {
  872. logGlobal->error("Not supported boat subtype: %d", boat->subID);
  873. return nullptr;
  874. }
  875. const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
  876. int colorIndex = color->getNum();
  877. if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
  878. {
  879. logGlobal->error("Invalid player color %d", colorIndex);
  880. return nullptr;
  881. }
  882. return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
  883. }
  884. IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
  885. {
  886. size_t groupSize = animation->size(group);
  887. if(groupSize == 0)
  888. return nullptr;
  889. if(moving)
  890. return animation->getImage(anim % groupSize, group);
  891. else
  892. return animation->getImage((anim / 4) % groupSize, group);
  893. }
  894. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  895. {
  896. if (!obj)
  897. return CMapHandler::AnimBitmapHolder();
  898. if (obj->ID == Obj::HERO)
  899. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  900. if (obj->ID == Obj::BOAT)
  901. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  902. // normal object
  903. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  904. size_t groupSize = animation->size();
  905. if(groupSize == 0)
  906. return CMapHandler::AnimBitmapHolder();
  907. IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % groupSize);
  908. if(!bitmap)
  909. return CMapHandler::AnimBitmapHolder();
  910. bitmap->setFlagColor(obj->tempOwner);
  911. return CMapHandler::AnimBitmapHolder(bitmap);
  912. }
  913. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  914. {
  915. auto i = parent->animationPhase.find(object);
  916. if(i == parent->animationPhase.end())
  917. {
  918. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  919. parent->animationPhase[object] = ret;
  920. return ret;
  921. }
  922. return i->second;
  923. }
  924. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  925. {
  926. //checking if object has non-empty graphic on this tile
  927. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  928. }
  929. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  930. {
  931. if(settings["session"]["spectate"].Bool())
  932. return true;
  933. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  934. return !neighbors.areAllHidden();
  935. }
  936. ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
  937. {
  938. if(isMoving)
  939. {
  940. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  941. return frame[dir];
  942. }
  943. else //if(isMoving)
  944. {
  945. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  946. return frame[dir];
  947. }
  948. }
  949. bool CMapHandler::updateObjectsFade()
  950. {
  951. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  952. {
  953. int3 pos = (*iter).second.first;
  954. CFadeAnimation * anim = (*iter).second.second;
  955. anim->update();
  956. if (anim->isFading())
  957. ++iter;
  958. else // fade finished
  959. {
  960. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  961. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  962. {
  963. if ((*objIter).fadeAnimKey == (*iter).first)
  964. {
  965. logAnim->trace("Fade anim finished for obj at %s; remaining: %d", pos.toString(), fadeAnims.size() - 1);
  966. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  967. objs.erase(objIter); // if this was fadeout, remove the object from the map
  968. else
  969. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  970. break;
  971. }
  972. }
  973. delete (*iter).second.second;
  974. iter = fadeAnims.erase(iter);
  975. }
  976. }
  977. return !fadeAnims.empty();
  978. }
  979. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  980. {
  981. SDL_Surface * fadeBitmap;
  982. assert(obj.obj);
  983. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  984. if (objData.objBitmap)
  985. {
  986. if (objData.isMoving) // ignore fading of moving objects (for now?)
  987. {
  988. logAnim->debug("Ignoring fade of moving object");
  989. return false;
  990. }
  991. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  992. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  993. objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
  994. if (objData.flagBitmap)
  995. {
  996. 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
  997. {
  998. Rect flagSrcRect(32, 0, 32, 32);
  999. objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
  1000. }
  1001. }
  1002. auto anim = new CFadeAnimation();
  1003. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  1004. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  1005. obj.fadeAnimKey = fadeAnimCounter;
  1006. logAnim->trace("Fade anim started for obj %d at %s; anim count: %d", obj.obj->ID, pos.toString(), fadeAnims.size());
  1007. return true;
  1008. }
  1009. return false;
  1010. }
  1011. bool CMapHandler::printObject(const CGObjectInstance * obj, bool fadein)
  1012. {
  1013. auto animation = graphics->getAnimation(obj);
  1014. if(!animation)
  1015. return false;
  1016. IImage * bitmap = animation->getImage(0);
  1017. if(!bitmap)
  1018. return false;
  1019. const int tilesW = bitmap->width()/32;
  1020. const int tilesH = bitmap->height()/32;
  1021. for(int fx=0; fx<tilesW; ++fx)
  1022. {
  1023. for(int fy=0; fy<tilesH; ++fy)
  1024. {
  1025. SDL_Rect cr;
  1026. cr.w = 32;
  1027. cr.h = 32;
  1028. cr.x = fx*32;
  1029. cr.y = fy*32;
  1030. TerrainTileObject toAdd(obj, cr);
  1031. 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)
  1032. {
  1033. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1034. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1035. if (fadein && ADVOPT.objectFading)
  1036. {
  1037. startObjectFade(toAdd, true, pos);
  1038. }
  1039. auto i = curt.objects.begin();
  1040. for(; i != curt.objects.end(); i++)
  1041. {
  1042. if(objectBlitOrderSorter(toAdd, *i))
  1043. {
  1044. curt.objects.insert(i, toAdd);
  1045. i = curt.objects.begin(); //to validate and avoid adding it second time
  1046. break;
  1047. }
  1048. }
  1049. if(i == curt.objects.end())
  1050. curt.objects.insert(i, toAdd);
  1051. }
  1052. }
  1053. }
  1054. return true;
  1055. }
  1056. bool CMapHandler::hideObject(const CGObjectInstance * obj, bool fadeout)
  1057. {
  1058. //optimized version which reveals weird bugs with missing def name
  1059. //auto pos = obj->pos;
  1060. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1061. //{
  1062. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1063. // {
  1064. // int3 t(i, j, pos.z);
  1065. // if (!map->isInTheMap(t))
  1066. // continue;
  1067. // auto &objs = ttiles[i][j][pos.z].objects;
  1068. // for (size_t x = 0; x < objs.size(); x++)
  1069. // {
  1070. // auto ourObj = objs[x].obj;
  1071. // if (ourObj && ourObj->id == obj->id)
  1072. // {
  1073. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1074. // {
  1075. // if (startObjectFade(objs[x], false, t))
  1076. // objs[x].obj = nullptr; //set original pointer to null
  1077. // else
  1078. // objs.erase(objs.begin() + x);
  1079. // }
  1080. // else
  1081. // objs.erase(objs.begin() + x);
  1082. // break;
  1083. // }
  1084. // }
  1085. // }
  1086. //}
  1087. for (size_t i = 0; i<map->width; i++)
  1088. {
  1089. for (size_t j = 0; j<map->height; j++)
  1090. {
  1091. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1092. {
  1093. auto &objs = ttiles[i][j][k].objects;
  1094. for (size_t x = 0; x < objs.size(); x++)
  1095. {
  1096. if (objs[x].obj && objs[x].obj->id == obj->id)
  1097. {
  1098. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1099. {
  1100. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1101. objs[x].obj = nullptr;
  1102. else
  1103. objs.erase(objs.begin() + x);
  1104. }
  1105. else
  1106. objs.erase(objs.begin() + x);
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. }
  1113. return true;
  1114. }
  1115. bool CMapHandler::canStartHeroMovement()
  1116. {
  1117. return fadeAnims.empty(); // don't allow movement during fade animation
  1118. }
  1119. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1120. {
  1121. for(auto & elem : terrainImages[7])
  1122. {
  1123. for(IImage * img : elem)
  1124. img->shiftPalette(246, 9);
  1125. }
  1126. for(auto & elem : terrainImages[8])
  1127. {
  1128. for(IImage * img : elem)
  1129. {
  1130. img->shiftPalette(229, 12);
  1131. img->shiftPalette(242, 14);
  1132. }
  1133. }
  1134. for(auto & elem : riverImages[0])
  1135. {
  1136. for(IImage * img : elem)
  1137. {
  1138. img->shiftPalette(183, 12);
  1139. img->shiftPalette(195, 6);
  1140. }
  1141. }
  1142. for(auto & elem : riverImages[2])
  1143. {
  1144. for(IImage * img : elem)
  1145. {
  1146. img->shiftPalette(228, 12);
  1147. img->shiftPalette(183, 6);
  1148. img->shiftPalette(240, 6);
  1149. }
  1150. }
  1151. for(auto & elem : riverImages[3])
  1152. {
  1153. for(IImage * img : elem)
  1154. img->shiftPalette(240, 9);
  1155. }
  1156. }
  1157. CMapHandler::~CMapHandler()
  1158. {
  1159. delete normalBlitter;
  1160. delete worldViewBlitter;
  1161. delete puzzleViewBlitter;
  1162. for (auto & elem : fadeAnims)
  1163. {
  1164. delete elem.second.second;
  1165. }
  1166. }
  1167. CMapHandler::CMapHandler()
  1168. {
  1169. frameW = frameH = 0;
  1170. normalBlitter = new CMapNormalBlitter(this);
  1171. worldViewBlitter = new CMapWorldViewBlitter(this);
  1172. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1173. fadeAnimCounter = 0;
  1174. map = nullptr;
  1175. tilesW = tilesH = 0;
  1176. offsetX = offsetY = 0;
  1177. egdeAnimation = make_unique<CAnimation>("EDG");
  1178. egdeAnimation->preload();
  1179. }
  1180. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1181. {
  1182. out.clear();
  1183. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1184. const TerrainTile &t = map->getTile(pos);
  1185. for(auto & elem : tt.objects)
  1186. {
  1187. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1188. {
  1189. out = elem.obj->getObjectName();
  1190. return;
  1191. }
  1192. }
  1193. if(t.hasFavorableWinds())
  1194. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1195. else if(terName)
  1196. {
  1197. out = CGI->generaltexth->terrainNames[t.terType];
  1198. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1199. {
  1200. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1201. }
  1202. }
  1203. }
  1204. void CMapHandler::discardWorldViewCache()
  1205. {
  1206. cache.discardWorldViewCache();
  1207. }
  1208. CMapHandler::CMapCache::CMapCache()
  1209. {
  1210. worldViewCachedScale = 0;
  1211. }
  1212. void CMapHandler::CMapCache::discardWorldViewCache()
  1213. {
  1214. for(auto & cache : data)
  1215. cache.clear();
  1216. logAnim->debug("Discarded world view cache");
  1217. }
  1218. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1219. {
  1220. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1221. discardWorldViewCache();
  1222. worldViewCachedScale = scale;
  1223. }
  1224. IImage * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, const IImage * fullSurface)
  1225. {
  1226. intptr_t key = (intptr_t) fullSurface;
  1227. auto & cache = data[(ui8)type];
  1228. auto iter = cache.find(key);
  1229. if(iter == cache.end())
  1230. {
  1231. auto scaled = fullSurface->scaleFast(worldViewCachedScale);
  1232. IImage * ret = scaled.get();
  1233. cache[key] = std::move(scaled);
  1234. return ret;
  1235. }
  1236. else
  1237. {
  1238. return (*iter).second.get();
  1239. }
  1240. }
  1241. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1242. {
  1243. if (!a)
  1244. return true;
  1245. if (!b)
  1246. return false;
  1247. if (a->appearance.printPriority != b->appearance.printPriority)
  1248. return a->appearance.printPriority > b->appearance.printPriority;
  1249. if(a->pos.y != b->pos.y)
  1250. return a->pos.y < b->pos.y;
  1251. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1252. return true;
  1253. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1254. return false;
  1255. if(!a->isVisitable() && b->isVisitable())
  1256. return true;
  1257. if(!b->isVisitable() && a->isVisitable())
  1258. return false;
  1259. if(a->pos.x < b->pos.x)
  1260. return true;
  1261. return false;
  1262. }
  1263. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1264. : obj(obj_),
  1265. rect(rect_),
  1266. fadeAnimKey(-1)
  1267. {
  1268. }
  1269. TerrainTileObject::~TerrainTileObject()
  1270. {
  1271. }