mapHandler.cpp 44 KB

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