mapHandler.cpp 50 KB

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