mapHandler.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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::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. static void processDef (const ObjectTemplate & objTempl)
  253. {
  254. if(objTempl.id == Obj::EVENT)
  255. {
  256. graphics->advmapobjGraphics[objTempl.animationFile] = nullptr;
  257. return;
  258. }
  259. CDefEssential * ourDef = graphics->getDef(objTempl);
  260. if(!ourDef) //if object has already set handler (eg. heroes) it should not be overwritten
  261. {
  262. if(objTempl.animationFile.size())
  263. {
  264. graphics->advmapobjGraphics[objTempl.animationFile] = CDefHandler::giveDefEss(objTempl.animationFile);
  265. }
  266. else
  267. {
  268. logGlobal->warnStream() << "No def name for " << objTempl.id << " " << objTempl.subid;
  269. return;
  270. }
  271. ourDef = graphics->getDef(objTempl);
  272. }
  273. //alpha transformation
  274. for(auto & elem : ourDef->ourImages)
  275. {
  276. CSDL_Ext::alphaTransform(elem.bitmap);
  277. }
  278. }
  279. void CMapHandler::initObjectRects()
  280. {
  281. //initializing objects / rects
  282. for(auto & elem : map->objects)
  283. {
  284. const CGObjectInstance *obj = elem;
  285. if( !obj
  286. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  287. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  288. {
  289. continue;
  290. }
  291. if (!graphics->getDef(obj)) //try to load it
  292. processDef(obj->appearance);
  293. if (!graphics->getDef(obj)) // stil no graphics? exit
  294. continue;
  295. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  296. for(int fx=0; fx < obj->getWidth(); ++fx)
  297. {
  298. for(int fy=0; fy < obj->getHeight(); ++fy)
  299. {
  300. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  301. SDL_Rect cr;
  302. cr.w = 32;
  303. cr.h = 32;
  304. cr.x = bitmap->w - fx * 32 - 32;
  305. cr.y = bitmap->h - fy * 32 - 32;
  306. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  307. if( map->isInTheMap(currTile) && // within map
  308. cr.x + cr.w > 0 && // image has data on this tile
  309. cr.y + cr.h > 0 &&
  310. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  311. )
  312. {
  313. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  314. }
  315. }
  316. }
  317. }
  318. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  319. {
  320. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  321. {
  322. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  323. {
  324. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
  325. }
  326. }
  327. }
  328. }
  329. void CMapHandler::init()
  330. {
  331. CStopWatch th;
  332. th.getDiff();
  333. graphics->advmapobjGraphics["AB01_.DEF"] = graphics->boatAnims[0];
  334. graphics->advmapobjGraphics["AB02_.DEF"] = graphics->boatAnims[1];
  335. graphics->advmapobjGraphics["AB03_.DEF"] = graphics->boatAnims[2];
  336. // Size of visible terrain.
  337. int mapW = conf.go()->ac.advmapW;
  338. int mapH = conf.go()->ac.advmapH;
  339. //sizes of terrain
  340. sizes.x = map->width;
  341. sizes.y = map->height;
  342. sizes.z = map->twoLevel ? 2 : 1;
  343. // Total number of visible tiles. Subtract the center tile, then
  344. // compute the number of tiles on each side, and reassemble.
  345. int t1, t2;
  346. t1 = (mapW-32)/2;
  347. t2 = mapW - 32 - t1;
  348. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  349. t1 = (mapH-32)/2;
  350. t2 = mapH - 32 - t1;
  351. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  352. // Size of the frame around the map. In extremes positions, the
  353. // frame must not be on the center of the map, but right on the
  354. // edge of the center tile.
  355. frameW = (mapW+31) /32 / 2;
  356. frameH = (mapH+31) /32 / 2;
  357. offsetX = (mapW - (2*frameW+1)*32)/2;
  358. offsetY = (mapH - (2*frameH+1)*32)/2;
  359. prepareFOWDefs();
  360. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  361. borderAndTerrainBitmapInit();
  362. logGlobal->infoStream()<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDiff();
  363. initObjectRects();
  364. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  365. }
  366. void CMapHandler::drawWorldViewOverlay(int targetTilesX, int targetTilesY, int srx_init, int sry_init, CDefHandler * iconsDef, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, float scale, int targetTileSize, int3 top_tile, SDL_Surface * extSurf)
  367. {
  368. int srx = srx_init;
  369. int bxStart = top_tile.x;
  370. int byStart = top_tile.y;
  371. for (int bx = bxStart; bx < top_tile.x + targetTilesX; bx++, srx+=targetTileSize)
  372. {
  373. if (bx < 0 || bx >= sizes.x)
  374. {
  375. continue;
  376. }
  377. int sry = sry_init;
  378. for (int by = byStart; by < top_tile.y + targetTilesY; by++, sry+=targetTileSize)
  379. {
  380. if (by < 0 || by >= sizes.y)
  381. {
  382. continue;
  383. }
  384. int3 pos(bx, by, top_tile.z);
  385. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  386. auto &objects = tile.objects;
  387. for(auto & object : objects)
  388. {
  389. const CGObjectInstance *obj = object.first;
  390. if (obj->pos.z != top_tile.z)
  391. continue;
  392. if (!(*visibilityMap)[pos.x][pos.y][top_tile.z])
  393. continue;
  394. if (!obj->visitableAt(pos.x, pos.y))
  395. continue;
  396. auto &ownerRaw = obj->tempOwner;
  397. int ownerIndex = 0;
  398. if (ownerRaw < PlayerColor::PLAYER_LIMIT)
  399. {
  400. ownerIndex = ownerRaw.getNum() * 19;
  401. }
  402. else if (ownerRaw == PlayerColor::NEUTRAL)
  403. {
  404. ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
  405. }
  406. SDL_Surface * wvIcon = nullptr;
  407. switch (obj->ID)
  408. {
  409. default:
  410. continue;
  411. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  412. case Obj::MONOLITH_ONE_WAY_EXIT:
  413. case Obj::MONOLITH_TWO_WAY:
  414. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::TELEPORT].bitmap;
  415. break;
  416. case Obj::SUBTERRANEAN_GATE:
  417. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::GATE].bitmap;
  418. break;
  419. case Obj::ARTIFACT:
  420. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::ARTIFACT].bitmap;
  421. break;
  422. case Obj::TOWN:
  423. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::TOWN + ownerIndex].bitmap;
  424. break;
  425. case Obj::HERO:
  426. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::HERO + ownerIndex].bitmap;
  427. break;
  428. case Obj::MINE:
  429. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::MINE_WOOD + obj->subID + ownerIndex].bitmap;
  430. break;
  431. case Obj::RESOURCE:
  432. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::RES_WOOD + obj->subID + ownerIndex].bitmap;
  433. break;
  434. }
  435. if (wvIcon)
  436. {
  437. Rect tempDst;
  438. tempDst.x = srx + targetTileSize / 2 - wvIcon->w / 2;
  439. tempDst.y = sry + targetTileSize / 2 - wvIcon->h / 2;
  440. tempDst.w = wvIcon->w;
  441. tempDst.h = wvIcon->h;
  442. CSDL_Ext::blitSurface(wvIcon, nullptr, extSurf, &tempDst);
  443. }
  444. }
  445. }
  446. }
  447. }
  448. void CMapHandler::calculateWorldViewCameraPos(int targetTilesX, int targetTilesY, int3 &top_tile)
  449. {
  450. bool outsideLeft = top_tile.x < 0;
  451. bool outsideTop = top_tile.y < 0;
  452. bool outsideRight = std::max(0, top_tile.x) + targetTilesX > sizes.x;
  453. bool outsideBottom = std::max(0, top_tile.y) + targetTilesY > sizes.y;
  454. if (targetTilesX > sizes.x)
  455. top_tile.x = sizes.x / 2 - targetTilesX / 2; // center viewport if the whole map can fit into the screen at once
  456. else if (outsideLeft)
  457. {
  458. if (outsideRight)
  459. {
  460. top_tile.x = sizes.x / 2 - targetTilesX / 2;
  461. }
  462. else
  463. top_tile.x = 0;
  464. }
  465. else if (outsideRight)
  466. top_tile.x = sizes.x - targetTilesX;
  467. if (targetTilesY > sizes.y)
  468. top_tile.y = sizes.y / 2 - targetTilesY / 2;
  469. else if (outsideTop)
  470. {
  471. if (outsideBottom)
  472. {
  473. top_tile.y = sizes.y / 2 - targetTilesY / 2;
  474. }
  475. else
  476. top_tile.y = 0;
  477. }
  478. else if (outsideBottom)
  479. top_tile.y = sizes.y - targetTilesY;
  480. }
  481. // updates map terrain when adventure map is in world view mode;
  482. // this method was copied from terrainRect(), so some parts are the same -- that probably should be refactored slightly
  483. void CMapHandler::terrainRectScaled(int3 topTile, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, SDL_Surface * extSurf, const SDL_Rect * extRect, float scale, CDefHandler * iconsDef)
  484. {
  485. cache.updateWorldViewScale(scale);
  486. int targetTileSize = (int) floorf(32.0f * scale);
  487. int targetTilesX = (int) ceilf(tilesW / scale) + 1;
  488. int targetTilesY = (int) ceilf(tilesH / scale) + 1;
  489. SDL_Rect rtile = { 0, 0, targetTileSize, targetTileSize };
  490. // Absolute coords of the first pixel in the top left corner
  491. int srx_init = offsetX + extRect->x;
  492. int sry_init = offsetY + extRect->y;
  493. int srx, sry; // absolute screen coordinates in pixels
  494. calculateWorldViewCameraPos(targetTilesX, targetTilesY, topTile);
  495. SDL_FillRect(extSurf, extRect, SDL_MapRGB(extSurf->format, 0, 0, 0));
  496. // makes the clip area smaller if the map is smaller than the screen frame
  497. Rect clipRect(std::max(extRect->x, -topTile.x * targetTileSize),
  498. std::max(extRect->y, -topTile.y * targetTileSize),
  499. std::min(extRect->w, sizes.x * targetTileSize),
  500. std::min(extRect->h, sizes.y * targetTileSize));
  501. SDL_Rect prevClip;
  502. SDL_GetClipRect(extSurf, &prevClip);
  503. SDL_SetClipRect(extSurf, &clipRect); //preventing blitting outside of that rect
  504. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  505. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  506. // printing terrain
  507. srx = srx_init;
  508. for (int bx = topTile.x; bx < topTile.x + targetTilesX; bx++, srx+=targetTileSize)
  509. {
  510. // Skip column if not in map
  511. if (bx < 0 || bx >= sizes.x)
  512. {
  513. continue;
  514. }
  515. sry = sry_init;
  516. for (int by = topTile.y; by < topTile.y + targetTilesY; by++, sry+=targetTileSize)
  517. {
  518. int3 pos(bx, by, topTile.z); //blitted tile position
  519. // Skip tile if not in map
  520. if (by < 0 || by >= sizes.y)
  521. {
  522. continue;
  523. }
  524. const NeighborTilesInfo info(pos,sizes,*visibilityMap);
  525. if(info.areAllHidden())
  526. continue;
  527. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  528. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  529. SDL_Rect sr;
  530. sr.x=srx;
  531. sr.y=sry;
  532. sr.h=sr.w=targetTileSize;
  533. //blit terrain with river/road
  534. if(tile.terbitmap)
  535. { //if custom terrain graphic - use it
  536. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::TERRAIN_CUSTOM, (int)tile.terbitmap, tile.terbitmap, scale);
  537. // Rect tempSrc = Rect(0, 0, scaledSurf->w, scaledSurf->h);
  538. Rect tempDst = Rect(sr.x, sr.y, scaledSurf->w, scaledSurf->h);
  539. CSDL_Ext::blitSurface(scaledSurf, nullptr, extSurf, &tempDst);
  540. }
  541. else //use default terrain graphic
  542. {
  543. auto baseSurf = terrainGraphics[tinfo.terType][tinfo.terView];
  544. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::TERRAIN, (int)baseSurf, baseSurf, scale);
  545. Rect tempSrc = Rect(0, 0, scaledSurf->w, scaledSurf->h);
  546. Rect tempDst = Rect(sr.x, sr.y, scaledSurf->w, scaledSurf->h);
  547. blitterWithRotation(scaledSurf, tempSrc, extSurf, tempDst, tinfo.extTileFlags%4);
  548. }
  549. if(tinfo.riverType) //print river if present
  550. {
  551. auto baseSurf = staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap;
  552. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::RIVERS, (int)baseSurf, baseSurf, scale);
  553. blitterWithRotationAndAlpha(scaledSurf, rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4);
  554. }
  555. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  556. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  557. { //part from top tile
  558. const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  559. Rect source(0, targetTileSize / 2, targetTileSize, targetTileSize / 2);
  560. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  561. auto baseSurf = roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap;
  562. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::ROADS, (int)baseSurf, baseSurf, scale);
  563. blitterWithRotationAndAlpha(scaledSurf, source, extSurf, dest, (topTile.extTileFlags>>4)%4);
  564. }
  565. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  566. {
  567. Rect source(0, 0, targetTileSize, targetTileSize);
  568. Rect dest(sr.x, sr.y + targetTileSize / 2, sr.w, sr.h / 2);
  569. auto baseSurf = roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap;
  570. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::ROADS, (int)baseSurf, baseSurf, scale);
  571. blitterWithRotationAndAlpha(scaledSurf, source, extSurf, dest, (tinfo.extTileFlags>>4)%4);
  572. }
  573. //blit objects
  574. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  575. for(auto & object : objects)
  576. {
  577. const CGObjectInstance *obj = object.first;
  578. if (!graphics->getDef(obj))
  579. processDef(obj->appearance);
  580. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  581. {
  582. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  583. }
  584. PlayerColor color = obj->tempOwner;
  585. //checking if object has non-empty graphic on this tile
  586. if(obj->ID != Obj::HERO && !obj->coveringAt(bx, by))
  587. continue;
  588. SDL_Rect sr2(sr);
  589. SDL_Rect pp = object.second;
  590. pp.h = sr.h;
  591. pp.w = sr.w;
  592. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  593. ? nullptr
  594. : static_cast<const CGHeroInstance*>(obj));
  595. //print hero / boat and flag
  596. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  597. {
  598. const int IMGVAL = 8; //frames per group of movement animation
  599. ui8 dir;
  600. std::vector<Cimage> * iv = nullptr;
  601. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  602. SDL_Surface * tb = nullptr; //surface to blitted
  603. if(themp) //hero
  604. {
  605. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  606. {
  607. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  608. continue;
  609. }
  610. dir = themp->moveDir;
  611. //pick graphics of hero (or boat if hero is sailing)
  612. if (themp->boat)
  613. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  614. else
  615. iv = &graphics->heroAnims[themp->appearance.animationFile]->ourImages;
  616. //pick appropriate flag set
  617. if(themp->boat)
  618. {
  619. switch (themp->boat->subID)
  620. {
  621. case 0: flg = &Graphics::flags1; break;
  622. case 1: flg = &Graphics::flags2; break;
  623. case 2: flg = &Graphics::flags3; break;
  624. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  625. }
  626. }
  627. else
  628. {
  629. flg = &Graphics::flags4;
  630. }
  631. }
  632. else //boat
  633. {
  634. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  635. dir = boat->direction;
  636. iv = &graphics->boatAnims[boat->subID]->ourImages;
  637. }
  638. if(themp && !themp->isStanding) //hero is moving
  639. {
  640. size_t gg;
  641. for(gg=0; gg<iv->size(); ++gg)
  642. {
  643. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  644. {
  645. tb = (*iv)[gg].bitmap;
  646. break;
  647. }
  648. }
  649. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  650. //printing flag
  651. pp.y+=IMGVAL*2-32;
  652. sr2.y-=16;
  653. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+35].bitmap, &pp, extSurf, &sr2);
  654. }
  655. else //hero / boat stands still
  656. {
  657. size_t gg;
  658. for(gg=0; gg < iv->size(); ++gg)
  659. {
  660. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  661. {
  662. tb = (*iv)[gg].bitmap;
  663. break;
  664. }
  665. }
  666. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  667. //printing flag
  668. if(flg
  669. && obj->pos.x == pos.x
  670. && obj->pos.y == pos.y)
  671. {
  672. SDL_Rect bufr = sr2;
  673. bufr.x-=2*32;
  674. bufr.y-=1*32;
  675. bufr.h = 64;
  676. bufr.w = 96;
  677. if(bufr.x-extRect->x>-64)
  678. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) * 8].bitmap, nullptr, extSurf, &bufr);
  679. }
  680. }
  681. }
  682. else //blit normal object
  683. {
  684. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  685. SDL_Surface *bitmap = ourImages[0].bitmap;
  686. //setting appropriate flag color
  687. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  688. CSDL_Ext::setPlayerColor(bitmap, color);
  689. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::OBJECTS, obj->id.getNum(), bitmap, scale);
  690. Rect tempSrc = Rect(pp.x * scale, pp.y * scale, pp.w, pp.h);
  691. Rect tempDst = Rect(sr2.x, sr2.y, pp.w, pp.h);
  692. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf,&tempSrc,extSurf,&tempDst);
  693. }
  694. }
  695. //objects blitted
  696. }
  697. }
  698. // terrain printed
  699. // blitting world view overlay
  700. drawWorldViewOverlay(targetTilesX, targetTilesY, srx_init, sry_init, iconsDef, visibilityMap, scale, targetTileSize, topTile, extSurf);
  701. // world view overlay blitted
  702. // printing borders
  703. srx = srx_init;
  704. for (int bx = topTile.x; bx < topTile.x + targetTilesX; bx++, srx+=targetTileSize)
  705. {
  706. sry = sry_init;
  707. for (int by = topTile.y; by < topTile.y + targetTilesY; by++, sry+=targetTileSize)
  708. {
  709. int3 pos(bx, by, topTile.z); //blitted tile position
  710. SDL_Rect sr;
  711. sr.x=srx;
  712. sr.y=sry;
  713. sr.h=sr.w=targetTileSize;
  714. if (pos.x < 0 || pos.x >= sizes.x ||
  715. pos.y < 0 || pos.y >= sizes.y)
  716. {
  717. // no borders in world view
  718. }
  719. else
  720. {
  721. //blitting Fog of War
  722. if (pos.x >= 0 &&
  723. pos.y >= 0 &&
  724. pos.x < sizes.x &&
  725. pos.y < sizes.y &&
  726. !(*visibilityMap)[pos.x][pos.y][topTile.z])
  727. {
  728. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  729. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::FOW, (int)hide.first, hide.first, scale);
  730. if(hide.second)
  731. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, &rtile, extSurf, &sr);
  732. else
  733. CSDL_Ext::blitSurface(scaledSurf, &rtile, extSurf, &sr);
  734. }
  735. //FoW blitted
  736. }
  737. }
  738. }
  739. // borders printed
  740. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  741. }
  742. // Update map window screen
  743. // top_tile top left tile to draw. Not necessarily visible.
  744. // extRect, extRect = map window on screen
  745. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  746. void CMapHandler::terrainRect( int3 top_tile, ui8 anim, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, bool otherHeroAnim, ui8 heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode, int3 grailPosRel ) const
  747. {
  748. // Width and height of the portion of the map to process. Units in tiles.
  749. ui32 dx = tilesW;
  750. ui32 dy = tilesH;
  751. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  752. SDL_Rect rtile = { 0, 0, 32, 32 };
  753. // Absolute coords of the first pixel in the top left corner
  754. int srx_init = offsetX + extRect->x;
  755. int sry_init = offsetY + extRect->y;
  756. int srx, sry; // absolute screen coordinates in pixels
  757. // If moving, we need to add an extra column/line
  758. if (moveX != 0)
  759. {
  760. dx++;
  761. srx_init += moveX;
  762. if (moveX > 0)
  763. {
  764. // Moving right. We still need to draw the old tile on the
  765. // left, so adjust our referential
  766. top_tile.x --;
  767. srx_init -= 32;
  768. }
  769. }
  770. if (moveY != 0)
  771. {
  772. dy++;
  773. sry_init += moveY;
  774. if (moveY > 0)
  775. {
  776. // Moving down. We still need to draw the tile on the top,
  777. // so adjust our referential.
  778. top_tile.y --;
  779. sry_init -= 32;
  780. }
  781. }
  782. // Reduce sizes if we go out of the full map.
  783. if (top_tile.x < -frameW)
  784. top_tile.x = -frameW;
  785. if (top_tile.y < -frameH)
  786. top_tile.y = -frameH;
  787. if (top_tile.x + dx > sizes.x + frameW)
  788. dx = sizes.x + frameW - top_tile.x;
  789. if (top_tile.y + dy > sizes.y + frameH)
  790. dy = sizes.y + frameH - top_tile.y;
  791. if(!otherHeroAnim)
  792. heroAnim = anim; //the same, as it should be
  793. SDL_Rect prevClip;
  794. SDL_GetClipRect(extSurf, &prevClip);
  795. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  796. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  797. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  798. //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  799. // printing terrain
  800. srx = srx_init;
  801. for (int bx = 0; bx < dx; bx++, srx+=32)
  802. {
  803. // Skip column if not in map
  804. if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x)
  805. continue;
  806. sry = sry_init;
  807. for (int by=0; by < dy; by++, sry+=32)
  808. {
  809. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  810. // Skip tile if not in map
  811. if (pos.y < 0 || pos.y >= sizes.y)
  812. continue;
  813. //we should not render fully hidden tiles
  814. if(!puzzleMode)
  815. {
  816. const NeighborTilesInfo info(pos,sizes,*visibilityMap);
  817. if(info.areAllHidden())
  818. continue;
  819. }
  820. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  821. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  822. SDL_Rect sr;
  823. sr.x=srx;
  824. sr.y=sry;
  825. sr.h=sr.w=32;
  826. //blit terrain with river/road
  827. if(tile.terbitmap)
  828. { //if custom terrain graphic - use it
  829. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  830. CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr);
  831. }
  832. else //use default terrain graphic
  833. {
  834. blitterWithRotation(terrainGraphics[tinfo.terType][tinfo.terView],rtile, extSurf, sr, tinfo.extTileFlags%4);
  835. }
  836. if(tinfo.riverType) //print river if present
  837. {
  838. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4);
  839. }
  840. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  841. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  842. { //part from top tile
  843. const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  844. Rect source(0, 16, 32, 16);
  845. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  846. blitterWithRotationAndAlpha(roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.extTileFlags>>4)%4);
  847. }
  848. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  849. {
  850. Rect source(0, 0, 32, 32);
  851. Rect dest(sr.x, sr.y+16, sr.w, sr.h/2);
  852. blitterWithRotationAndAlpha(roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.extTileFlags>>4)%4);
  853. }
  854. //blit objects
  855. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  856. for(auto & object : objects)
  857. {
  858. const CGObjectInstance *obj = object.first;
  859. if (!graphics->getDef(obj))
  860. processDef(obj->appearance);
  861. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  862. {
  863. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  864. }
  865. PlayerColor color = obj->tempOwner;
  866. //checking if object has non-empty graphic on this tile
  867. if(obj->ID != Obj::HERO && !obj->coveringAt(top_tile.x + bx, top_tile.y + by))
  868. continue;
  869. static const int notBlittedInPuzzleMode[] = {Obj::HOLE};
  870. //don't print flaggable objects in puzzle mode
  871. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  872. continue;
  873. SDL_Rect sr2(sr);
  874. SDL_Rect pp = object.second;
  875. pp.h = sr.h;
  876. pp.w = sr.w;
  877. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  878. ? nullptr
  879. : static_cast<const CGHeroInstance*>(obj));
  880. //print hero / boat and flag
  881. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  882. {
  883. const int IMGVAL = 8; //frames per group of movement animation
  884. ui8 dir;
  885. std::vector<Cimage> * iv = nullptr;
  886. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  887. SDL_Surface * tb = nullptr; //surface to blitted
  888. if(themp) //hero
  889. {
  890. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  891. {
  892. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  893. continue;
  894. }
  895. dir = themp->moveDir;
  896. //pick graphics of hero (or boat if hero is sailing)
  897. if (themp->boat)
  898. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  899. else
  900. iv = &graphics->heroAnims[themp->appearance.animationFile]->ourImages;
  901. //pick appropriate flag set
  902. if(themp->boat)
  903. {
  904. switch (themp->boat->subID)
  905. {
  906. case 0: flg = &Graphics::flags1; break;
  907. case 1: flg = &Graphics::flags2; break;
  908. case 2: flg = &Graphics::flags3; break;
  909. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  910. }
  911. }
  912. else
  913. {
  914. flg = &Graphics::flags4;
  915. }
  916. }
  917. else //boat
  918. {
  919. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  920. dir = boat->direction;
  921. iv = &graphics->boatAnims[boat->subID]->ourImages;
  922. }
  923. if(themp && !themp->isStanding) //hero is moving
  924. {
  925. size_t gg;
  926. for(gg=0; gg<iv->size(); ++gg)
  927. {
  928. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  929. {
  930. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  931. break;
  932. }
  933. }
  934. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  935. //printing flag
  936. pp.y+=IMGVAL*2-32;
  937. sr2.y-=16;
  938. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  939. }
  940. else //hero / boat stands still
  941. {
  942. size_t gg;
  943. for(gg=0; gg < iv->size(); ++gg)
  944. {
  945. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  946. {
  947. tb = (*iv)[gg].bitmap;
  948. break;
  949. }
  950. }
  951. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  952. //printing flag
  953. if(flg
  954. && obj->pos.x == top_tile.x + bx
  955. && obj->pos.y == top_tile.y + by)
  956. {
  957. SDL_Rect bufr = sr2;
  958. bufr.x-=2*32;
  959. bufr.y-=1*32;
  960. bufr.h = 64;
  961. bufr.w = 96;
  962. if(bufr.x-extRect->x>-64)
  963. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, nullptr, extSurf, &bufr);
  964. }
  965. }
  966. }
  967. else //blit normal object
  968. {
  969. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  970. SDL_Surface *bitmap = ourImages[(anim+getPhaseShift(obj))%ourImages.size()].bitmap;
  971. //setting appropriate flag color
  972. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  973. CSDL_Ext::setPlayerColor(bitmap, color);
  974. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  975. }
  976. }
  977. //objects blitted
  978. //X sign
  979. if(puzzleMode)
  980. {
  981. if(bx == grailPosRel.x && by == grailPosRel.y)
  982. {
  983. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, extSurf, &sr);
  984. }
  985. }
  986. }
  987. }
  988. // terrain printed
  989. // printing borders
  990. srx = srx_init;
  991. for (int bx = 0; bx < dx; bx++, srx+=32)
  992. {
  993. sry = sry_init;
  994. for (int by = 0; by<dy; by++, sry+=32)
  995. {
  996. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  997. SDL_Rect sr;
  998. sr.x=srx;
  999. sr.y=sry;
  1000. sr.h=sr.w=32;
  1001. if (pos.x < 0 || pos.x >= sizes.x ||
  1002. pos.y < 0 || pos.y >= sizes.y)
  1003. {
  1004. // outside of the map - print borders
  1005. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  1006. SDL_Surface * src = ttiles[pos.x][pos.y][top_tile.z].terbitmap;
  1007. assert(src);
  1008. CSDL_Ext::blitSurface(src, &temp_rect,extSurf,&sr);
  1009. }
  1010. else
  1011. {
  1012. //blitting Fog of War
  1013. if (!puzzleMode)
  1014. {
  1015. if (pos.x >= 0 &&
  1016. pos.y >= 0 &&
  1017. pos.x < sizes.x &&
  1018. pos.y < sizes.y &&
  1019. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  1020. {
  1021. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  1022. if(hide.second)
  1023. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  1024. else
  1025. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  1026. }
  1027. }
  1028. //FoW blitted
  1029. SDL_Rect tileRect = genRect(sr.h, sr.w, 0, 0);
  1030. if (settings["session"]["showBlock"].Bool())
  1031. {
  1032. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).blocked) //temporary hiding blocked positions
  1033. {
  1034. static SDL_Surface * block = nullptr;
  1035. if (!block)
  1036. block = BitmapHandler::loadBitmap("blocked");
  1037. SDL_Rect sr;
  1038. sr.x=srx;
  1039. sr.y=sry;
  1040. sr.h=sr.w=32;
  1041. CSDL_Ext::blitSurface(block, &tileRect, extSurf, &sr);
  1042. }
  1043. }
  1044. if (settings["session"]["showVisit"].Bool())
  1045. {
  1046. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).visitable) //temporary hiding visitable positions
  1047. {
  1048. static SDL_Surface * visit = nullptr;
  1049. if (!visit)
  1050. visit = BitmapHandler::loadBitmap("visitable");
  1051. SDL_Rect sr;
  1052. sr.x=srx;
  1053. sr.y=sry;
  1054. sr.h=sr.w=32;
  1055. CSDL_Ext::blitSurface(visit, &tileRect, extSurf, &sr);
  1056. }
  1057. }
  1058. }
  1059. }
  1060. }
  1061. // borders printed
  1062. // print grid
  1063. if (settings["session"]["showGrid"].Bool())
  1064. {
  1065. srx = srx_init;
  1066. for (int bx = 0; bx < dx; bx++, srx+=32)
  1067. {
  1068. sry = sry_init;
  1069. for (int by = 0; by<dy; by++, sry+=32)
  1070. {
  1071. SDL_Rect sr;
  1072. sr.x=srx;
  1073. sr.y=sry;
  1074. sr.h=sr.w=32;
  1075. const int3 color(0x555555, 0x555555, 0x555555);
  1076. if (sr.y >= extRect->y &&
  1077. sr.y < extRect->y+extRect->h)
  1078. for(int i=0;i<sr.w;i++)
  1079. if (sr.x+i >= extRect->x &&
  1080. sr.x+i < extRect->x+extRect->w)
  1081. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  1082. if (sr.x >= extRect->x &&
  1083. sr.x < extRect->x+extRect->w)
  1084. for(int i=0; i<sr.h;i++)
  1085. if (sr.y+i >= extRect->y &&
  1086. sr.y+i < extRect->y+extRect->h)
  1087. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  1088. }
  1089. }
  1090. }
  1091. // grid
  1092. //applying sepia / gray effect
  1093. if(puzzleMode)
  1094. {
  1095. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  1096. }
  1097. //sepia / gray effect applied
  1098. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  1099. }
  1100. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap ) const
  1101. {
  1102. const NeighborTilesInfo info(pos,sizes,visibilityMap);
  1103. int retBitmapID = info.getBitmapID();// >=0 -> partial hide, <0 - full hide
  1104. if (retBitmapID < 0)
  1105. {
  1106. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  1107. }
  1108. if (retBitmapID >= 0)
  1109. {
  1110. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  1111. }
  1112. else
  1113. {
  1114. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  1115. }
  1116. }
  1117. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1118. {
  1119. if (!graphics->getDef(obj))
  1120. processDef(obj->appearance);
  1121. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  1122. const int tilesW = bitmap->w/32;
  1123. const int tilesH = bitmap->h/32;
  1124. for(int fx=0; fx<tilesW; ++fx)
  1125. {
  1126. for(int fy=0; fy<tilesH; ++fy)
  1127. {
  1128. SDL_Rect cr;
  1129. cr.w = 32;
  1130. cr.h = 32;
  1131. cr.x = fx*32;
  1132. cr.y = fy*32;
  1133. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1134. 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)
  1135. {
  1136. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  1137. auto i = curt.objects.begin();
  1138. for(; i != curt.objects.end(); i++)
  1139. {
  1140. if(objectBlitOrderSorter(toAdd, *i))
  1141. {
  1142. curt.objects.insert(i, toAdd);
  1143. i = curt.objects.begin(); //to validate and avoid adding it second time
  1144. break;
  1145. }
  1146. }
  1147. if(i == curt.objects.end())
  1148. curt.objects.insert(i, toAdd);
  1149. }
  1150. } // for(int fy=0; fy<tilesH; ++fy)
  1151. } //for(int fx=0; fx<tilesW; ++fx)
  1152. return true;
  1153. }
  1154. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1155. {
  1156. for (size_t i=0; i<map->width; i++)
  1157. {
  1158. for (size_t j=0; j<map->height; j++)
  1159. {
  1160. for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
  1161. {
  1162. for(size_t x=0; x < ttiles[i][j][k].objects.size(); x++)
  1163. {
  1164. if (ttiles[i][j][k].objects[x].first->id == obj->id)
  1165. {
  1166. ttiles[i][j][k].objects.erase(ttiles[i][j][k].objects.begin() + x);
  1167. break;
  1168. }
  1169. }
  1170. }
  1171. }
  1172. }
  1173. return true;
  1174. }
  1175. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1176. {
  1177. hideObject(obj);
  1178. return true;
  1179. }
  1180. ui8 CMapHandler::getHeroFrameNum(ui8 dir, bool isMoving) const
  1181. {
  1182. if(isMoving)
  1183. {
  1184. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  1185. return frame[dir];
  1186. }
  1187. else //if(isMoving)
  1188. {
  1189. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  1190. return frame[dir];
  1191. }
  1192. }
  1193. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1194. {
  1195. if(ext)
  1196. {
  1197. if(val->x<0)
  1198. {
  1199. val->w += val->x;
  1200. val->x = ext->x;
  1201. }
  1202. else
  1203. {
  1204. val->x += ext->x;
  1205. }
  1206. if(val->y<0)
  1207. {
  1208. val->h += val->y;
  1209. val->y = ext->y;
  1210. }
  1211. else
  1212. {
  1213. val->y += ext->y;
  1214. }
  1215. if(val->x+val->w > ext->x+ext->w)
  1216. {
  1217. val->w = ext->x+ext->w-val->x;
  1218. }
  1219. if(val->y+val->h > ext->y+ext->h)
  1220. {
  1221. val->h = ext->y+ext->h-val->y;
  1222. }
  1223. //for sign problems
  1224. if(val->h > 20000 || val->w > 20000)
  1225. {
  1226. val->h = val->w = 0;
  1227. }
  1228. }
  1229. }
  1230. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  1231. {
  1232. if(a.z!=b.z)
  1233. return -1; //error!
  1234. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1235. return 0;
  1236. else if(a.x==b.x && a.y==b.y+1) //t
  1237. return 1;
  1238. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1239. return 2;
  1240. else if(a.x==b.x-1 && a.y==b.y) //r
  1241. return 3;
  1242. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1243. return 4;
  1244. else if(a.x==b.x && a.y==b.y-1) //b
  1245. return 5;
  1246. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1247. return 6;
  1248. else if(a.x==b.x+1 && a.y==b.y) //l
  1249. return 7;
  1250. return -2; //shouldn't happen
  1251. }
  1252. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1253. {
  1254. //works with at most 16 colors, if needed more -> increase values
  1255. assert(howMany < 16);
  1256. SDL_Color palette[16];
  1257. for(int i=0; i<howMany; ++i)
  1258. {
  1259. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1260. }
  1261. SDL_SetColors(img,palette,from,howMany);
  1262. }
  1263. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1264. {
  1265. for(auto & elem : terrainGraphics[7])
  1266. {
  1267. shiftColors(elem,246, 9);
  1268. }
  1269. for(auto & elem : terrainGraphics[8])
  1270. {
  1271. shiftColors(elem,229, 12);
  1272. shiftColors(elem,242, 14);
  1273. }
  1274. for(auto & elem : staticRiverDefs[0]->ourImages)
  1275. {
  1276. shiftColors(elem.bitmap,183, 12);
  1277. shiftColors(elem.bitmap,195, 6);
  1278. }
  1279. for(auto & elem : staticRiverDefs[2]->ourImages)
  1280. {
  1281. shiftColors(elem.bitmap,228, 12);
  1282. shiftColors(elem.bitmap,183, 6);
  1283. shiftColors(elem.bitmap,240, 6);
  1284. }
  1285. for(auto & elem : staticRiverDefs[3]->ourImages)
  1286. {
  1287. shiftColors(elem.bitmap,240, 9);
  1288. }
  1289. }
  1290. CMapHandler::~CMapHandler()
  1291. {
  1292. delete graphics->FoWfullHide;
  1293. delete graphics->FoWpartialHide;
  1294. for(auto & elem : roadDefs)
  1295. delete elem;
  1296. for(auto & elem : staticRiverDefs)
  1297. delete elem;
  1298. for(auto & elem : terrainGraphics)
  1299. {
  1300. for(int j=0; j < elem.size(); ++j)
  1301. SDL_FreeSurface(elem[j]);
  1302. }
  1303. terrainGraphics.clear();
  1304. }
  1305. CMapHandler::CMapHandler()
  1306. {
  1307. frameW = frameH = 0;
  1308. graphics->FoWfullHide = nullptr;
  1309. graphics->FoWpartialHide = nullptr;
  1310. }
  1311. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1312. {
  1313. out.clear();
  1314. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1315. const TerrainTile &t = map->getTile(pos);
  1316. for(auto & elem : tt.objects)
  1317. {
  1318. if(elem.first->ID == Obj::HOLE) //Hole
  1319. {
  1320. out = elem.first->getObjectName();
  1321. return;
  1322. }
  1323. }
  1324. if(t.hasFavourableWinds())
  1325. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1326. else if(terName)
  1327. out = CGI->generaltexth->terrainNames[t.terType];
  1328. }
  1329. ui8 CMapHandler::getPhaseShift(const CGObjectInstance *object) const
  1330. {
  1331. auto i = animationPhase.find(object);
  1332. if(i == animationPhase.end())
  1333. {
  1334. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  1335. animationPhase[object] = ret;
  1336. return ret;
  1337. }
  1338. return i->second;
  1339. }
  1340. void CMapHandler::discardWorldViewCache()
  1341. {
  1342. cache.discardWorldViewCache();
  1343. }
  1344. void CMapHandler::CMapCache::discardWorldViewCache()
  1345. {
  1346. for (auto &cacheDataPair : data)
  1347. {
  1348. for (auto &cacheEntryPair : cacheDataPair.second)
  1349. {
  1350. if (cacheEntryPair.second)
  1351. {
  1352. SDL_FreeSurface(cacheEntryPair.second);
  1353. }
  1354. }
  1355. data[cacheDataPair.first].clear();
  1356. }
  1357. logGlobal->debugStream() << "Discarded world view cache";
  1358. }
  1359. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1360. {
  1361. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1362. discardWorldViewCache();
  1363. worldViewCachedScale = scale;
  1364. }
  1365. void CMapHandler::CMapCache::removeFromWorldViewCache(CMapHandler::EMapCacheType type, int key)
  1366. {
  1367. auto iter = data[type].find(key);
  1368. if (iter != data[type].end())
  1369. {
  1370. SDL_FreeSurface((*iter).second);
  1371. data[type].erase(iter);
  1372. // logGlobal->errorStream() << "Removed world view cache entry: type=" << (int)type << ", key=" << key;
  1373. }
  1374. }
  1375. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCache(CMapHandler::EMapCacheType type, int key)
  1376. {
  1377. auto iter = data[type].find(key);
  1378. if (iter == data[type].end())
  1379. return nullptr;
  1380. // logGlobal->errorStream() << "Returning requested world view cache entry: type=" << (int)type << ", key=" << key << ", ptr=" << (*iter).second;
  1381. return (*iter).second;
  1382. }
  1383. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, int key, SDL_Surface * fullSurface, float scale)
  1384. {
  1385. auto cached = requestWorldViewCache(type, key);
  1386. if (cached)
  1387. return cached;
  1388. auto scaled = CSDL_Ext::scaleSurfaceFast(fullSurface, fullSurface->w * scale, fullSurface->h * scale);
  1389. return cacheWorldViewEntry(type, key, scaled);
  1390. }
  1391. SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, int key, SDL_Surface * entry)
  1392. {
  1393. if (!entry)
  1394. return nullptr;
  1395. if (requestWorldViewCache(type, key)) // valid cache already present, no need to do it again
  1396. return requestWorldViewCache(type, key);
  1397. // logGlobal->errorStream() << "Added world view cache entry: type=" << (int)type << ", key=" << key << ", ptr=" << entry;
  1398. data[type][key] = entry;
  1399. return entry;
  1400. }
  1401. TerrainTile2::TerrainTile2()
  1402. :terbitmap(nullptr)
  1403. {}
  1404. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1405. {
  1406. if (a->appearance.printPriority != b->appearance.printPriority)
  1407. return a->appearance.printPriority > b->appearance.printPriority;
  1408. if(a->pos.y != b->pos.y)
  1409. return a->pos.y < b->pos.y;
  1410. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1411. return true;
  1412. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1413. return false;
  1414. if(!a->isVisitable() && b->isVisitable())
  1415. return true;
  1416. if(!b->isVisitable() && a->isVisitable())
  1417. return false;
  1418. if(a->pos.x < b->pos.x)
  1419. return true;
  1420. return false;
  1421. }