mapHandler.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. // Update map window screen
  367. // top_tile top left tile to draw. Not necessarily visible.
  368. // extRect, extRect = map window on screen
  369. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  370. 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
  371. {
  372. // Width and height of the portion of the map to process. Units in tiles.
  373. ui32 dx = tilesW;
  374. ui32 dy = tilesH;
  375. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  376. SDL_Rect rtile = { 0, 0, 32, 32 };
  377. // Absolute coords of the first pixel in the top left corner
  378. int srx_init = offsetX + extRect->x;
  379. int sry_init = offsetY + extRect->y;
  380. int srx, sry; // absolute screen coordinates in pixels
  381. // If moving, we need to add an extra column/line
  382. if (moveX != 0)
  383. {
  384. dx++;
  385. srx_init += moveX;
  386. if (moveX > 0)
  387. {
  388. // Moving right. We still need to draw the old tile on the
  389. // left, so adjust our referential
  390. top_tile.x --;
  391. srx_init -= 32;
  392. }
  393. }
  394. if (moveY != 0)
  395. {
  396. dy++;
  397. sry_init += moveY;
  398. if (moveY > 0)
  399. {
  400. // Moving down. We still need to draw the tile on the top,
  401. // so adjust our referential.
  402. top_tile.y --;
  403. sry_init -= 32;
  404. }
  405. }
  406. // Reduce sizes if we go out of the full map.
  407. if (top_tile.x < -frameW)
  408. top_tile.x = -frameW;
  409. if (top_tile.y < -frameH)
  410. top_tile.y = -frameH;
  411. if (top_tile.x + dx > sizes.x + frameW)
  412. dx = sizes.x + frameW - top_tile.x;
  413. if (top_tile.y + dy > sizes.y + frameH)
  414. dy = sizes.y + frameH - top_tile.y;
  415. if(!otherHeroAnim)
  416. heroAnim = anim; //the same, as it should be
  417. SDL_Rect prevClip;
  418. SDL_GetClipRect(extSurf, &prevClip);
  419. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  420. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  421. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  422. //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  423. // printing terrain
  424. srx = srx_init;
  425. for (int bx = 0; bx < dx; bx++, srx+=32)
  426. {
  427. // Skip column if not in map
  428. if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x)
  429. continue;
  430. sry = sry_init;
  431. for (int by=0; by < dy; by++, sry+=32)
  432. {
  433. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  434. // Skip tile if not in map
  435. if (pos.y < 0 || pos.y >= sizes.y)
  436. continue;
  437. //we should not render fully hidden tiles
  438. if(!puzzleMode)
  439. {
  440. const NeighborTilesInfo info(pos,sizes,*visibilityMap);
  441. if(info.areAllHidden())
  442. continue;
  443. }
  444. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  445. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  446. SDL_Rect sr;
  447. sr.x=srx;
  448. sr.y=sry;
  449. sr.h=sr.w=32;
  450. //blit terrain with river/road
  451. if(tile.terbitmap)
  452. { //if custom terrain graphic - use it
  453. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  454. CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr);
  455. }
  456. else //use default terrain graphic
  457. {
  458. blitterWithRotation(terrainGraphics[tinfo.terType][tinfo.terView],rtile, extSurf, sr, tinfo.extTileFlags%4);
  459. }
  460. if(tinfo.riverType) //print river if present
  461. {
  462. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4);
  463. }
  464. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  465. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  466. { //part from top tile
  467. const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  468. Rect source(0, 16, 32, 16);
  469. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  470. blitterWithRotationAndAlpha(roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.extTileFlags>>4)%4);
  471. }
  472. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  473. {
  474. Rect source(0, 0, 32, 32);
  475. Rect dest(sr.x, sr.y+16, sr.w, sr.h/2);
  476. blitterWithRotationAndAlpha(roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.extTileFlags>>4)%4);
  477. }
  478. //blit objects
  479. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  480. for(auto & object : objects)
  481. {
  482. const CGObjectInstance *obj = object.first;
  483. if (!graphics->getDef(obj))
  484. processDef(obj->appearance);
  485. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  486. {
  487. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  488. }
  489. PlayerColor color = obj->tempOwner;
  490. //checking if object has non-empty graphic on this tile
  491. if(obj->ID != Obj::HERO && !obj->coveringAt(top_tile.x + bx, top_tile.y + by))
  492. continue;
  493. static const int notBlittedInPuzzleMode[] = {Obj::HOLE};
  494. //don't print flaggable objects in puzzle mode
  495. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  496. continue;
  497. SDL_Rect sr2(sr);
  498. SDL_Rect pp = object.second;
  499. pp.h = sr.h;
  500. pp.w = sr.w;
  501. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  502. ? nullptr
  503. : static_cast<const CGHeroInstance*>(obj));
  504. //print hero / boat and flag
  505. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  506. {
  507. const int IMGVAL = 8; //frames per group of movement animation
  508. ui8 dir;
  509. std::vector<Cimage> * iv = nullptr;
  510. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  511. SDL_Surface * tb = nullptr; //surface to blitted
  512. if(themp) //hero
  513. {
  514. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  515. {
  516. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  517. continue;
  518. }
  519. dir = themp->moveDir;
  520. //pick graphics of hero (or boat if hero is sailing)
  521. if (themp->boat)
  522. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  523. else
  524. iv = &graphics->heroAnims[themp->appearance.animationFile]->ourImages;
  525. //pick appropriate flag set
  526. if(themp->boat)
  527. {
  528. switch (themp->boat->subID)
  529. {
  530. case 0: flg = &Graphics::flags1; break;
  531. case 1: flg = &Graphics::flags2; break;
  532. case 2: flg = &Graphics::flags3; break;
  533. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  534. }
  535. }
  536. else
  537. {
  538. flg = &Graphics::flags4;
  539. }
  540. }
  541. else //boat
  542. {
  543. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  544. dir = boat->direction;
  545. iv = &graphics->boatAnims[boat->subID]->ourImages;
  546. }
  547. if(themp && !themp->isStanding) //hero is moving
  548. {
  549. size_t gg;
  550. for(gg=0; gg<iv->size(); ++gg)
  551. {
  552. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  553. {
  554. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  555. break;
  556. }
  557. }
  558. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  559. //printing flag
  560. pp.y+=IMGVAL*2-32;
  561. sr2.y-=16;
  562. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  563. }
  564. else //hero / boat stands still
  565. {
  566. size_t gg;
  567. for(gg=0; gg < iv->size(); ++gg)
  568. {
  569. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  570. {
  571. tb = (*iv)[gg].bitmap;
  572. break;
  573. }
  574. }
  575. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  576. //printing flag
  577. if(flg
  578. && obj->pos.x == top_tile.x + bx
  579. && obj->pos.y == top_tile.y + by)
  580. {
  581. SDL_Rect bufr = sr2;
  582. bufr.x-=2*32;
  583. bufr.y-=1*32;
  584. bufr.h = 64;
  585. bufr.w = 96;
  586. if(bufr.x-extRect->x>-64)
  587. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, nullptr, extSurf, &bufr);
  588. }
  589. }
  590. }
  591. else //blit normal object
  592. {
  593. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  594. SDL_Surface *bitmap = ourImages[(anim+getPhaseShift(obj))%ourImages.size()].bitmap;
  595. //setting appropriate flag color
  596. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  597. CSDL_Ext::setPlayerColor(bitmap, color);
  598. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  599. }
  600. }
  601. //objects blitted
  602. //X sign
  603. if(puzzleMode)
  604. {
  605. if(bx == grailPosRel.x && by == grailPosRel.y)
  606. {
  607. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, extSurf, &sr);
  608. }
  609. }
  610. }
  611. }
  612. // terrain printed
  613. // printing borders
  614. srx = srx_init;
  615. for (int bx = 0; bx < dx; bx++, srx+=32)
  616. {
  617. sry = sry_init;
  618. for (int by = 0; by<dy; by++, sry+=32)
  619. {
  620. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  621. SDL_Rect sr;
  622. sr.x=srx;
  623. sr.y=sry;
  624. sr.h=sr.w=32;
  625. if (pos.x < 0 || pos.x >= sizes.x ||
  626. pos.y < 0 || pos.y >= sizes.y)
  627. {
  628. // outside of the map - print borders
  629. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  630. SDL_Surface * src = ttiles[pos.x][pos.y][top_tile.z].terbitmap;
  631. assert(src);
  632. CSDL_Ext::blitSurface(src, &temp_rect,extSurf,&sr);
  633. }
  634. else
  635. {
  636. //blitting Fog of War
  637. if (!puzzleMode)
  638. {
  639. if (pos.x >= 0 &&
  640. pos.y >= 0 &&
  641. pos.x < sizes.x &&
  642. pos.y < sizes.y &&
  643. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  644. {
  645. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  646. if(hide.second)
  647. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  648. else
  649. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  650. }
  651. }
  652. //FoW blitted
  653. SDL_Rect tileRect = genRect(sr.h, sr.w, 0, 0);
  654. if (settings["session"]["showBlock"].Bool())
  655. {
  656. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).blocked) //temporary hiding blocked positions
  657. {
  658. static SDL_Surface * block = nullptr;
  659. if (!block)
  660. block = BitmapHandler::loadBitmap("blocked");
  661. SDL_Rect sr;
  662. sr.x=srx;
  663. sr.y=sry;
  664. sr.h=sr.w=32;
  665. CSDL_Ext::blitSurface(block, &tileRect, extSurf, &sr);
  666. }
  667. }
  668. if (settings["session"]["showVisit"].Bool())
  669. {
  670. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).visitable) //temporary hiding visitable positions
  671. {
  672. static SDL_Surface * visit = nullptr;
  673. if (!visit)
  674. visit = BitmapHandler::loadBitmap("visitable");
  675. SDL_Rect sr;
  676. sr.x=srx;
  677. sr.y=sry;
  678. sr.h=sr.w=32;
  679. CSDL_Ext::blitSurface(visit, &tileRect, extSurf, &sr);
  680. }
  681. }
  682. }
  683. }
  684. }
  685. // borders printed
  686. // print grid
  687. if (settings["session"]["showGrid"].Bool())
  688. {
  689. srx = srx_init;
  690. for (int bx = 0; bx < dx; bx++, srx+=32)
  691. {
  692. sry = sry_init;
  693. for (int by = 0; by<dy; by++, sry+=32)
  694. {
  695. SDL_Rect sr;
  696. sr.x=srx;
  697. sr.y=sry;
  698. sr.h=sr.w=32;
  699. const int3 color(0x555555, 0x555555, 0x555555);
  700. if (sr.y >= extRect->y &&
  701. sr.y < extRect->y+extRect->h)
  702. for(int i=0;i<sr.w;i++)
  703. if (sr.x+i >= extRect->x &&
  704. sr.x+i < extRect->x+extRect->w)
  705. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  706. if (sr.x >= extRect->x &&
  707. sr.x < extRect->x+extRect->w)
  708. for(int i=0; i<sr.h;i++)
  709. if (sr.y+i >= extRect->y &&
  710. sr.y+i < extRect->y+extRect->h)
  711. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  712. }
  713. }
  714. }
  715. // grid
  716. //applying sepia / gray effect
  717. if(puzzleMode)
  718. {
  719. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  720. }
  721. //sepia / gray effect applied
  722. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  723. }
  724. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap ) const
  725. {
  726. const NeighborTilesInfo info(pos,sizes,visibilityMap);
  727. int retBitmapID = info.getBitmapID();// >=0 -> partial hide, <0 - full hide
  728. if (retBitmapID < 0)
  729. {
  730. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  731. }
  732. if (retBitmapID >= 0)
  733. {
  734. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  735. }
  736. else
  737. {
  738. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  739. }
  740. }
  741. bool CMapHandler::printObject(const CGObjectInstance *obj)
  742. {
  743. if (!graphics->getDef(obj))
  744. processDef(obj->appearance);
  745. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  746. const int tilesW = bitmap->w/32;
  747. const int tilesH = bitmap->h/32;
  748. for(int fx=0; fx<tilesW; ++fx)
  749. {
  750. for(int fy=0; fy<tilesH; ++fy)
  751. {
  752. SDL_Rect cr;
  753. cr.w = 32;
  754. cr.h = 32;
  755. cr.x = fx*32;
  756. cr.y = fy*32;
  757. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  758. 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)
  759. {
  760. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  761. auto i = curt.objects.begin();
  762. for(; i != curt.objects.end(); i++)
  763. {
  764. if(objectBlitOrderSorter(toAdd, *i))
  765. {
  766. curt.objects.insert(i, toAdd);
  767. i = curt.objects.begin(); //to validate and avoid adding it second time
  768. break;
  769. }
  770. }
  771. if(i == curt.objects.end())
  772. curt.objects.insert(i, toAdd);
  773. }
  774. } // for(int fy=0; fy<tilesH; ++fy)
  775. } //for(int fx=0; fx<tilesW; ++fx)
  776. return true;
  777. }
  778. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  779. {
  780. for (size_t i=0; i<map->width; i++)
  781. {
  782. for (size_t j=0; j<map->height; j++)
  783. {
  784. for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
  785. {
  786. for(size_t x=0; x < ttiles[i][j][k].objects.size(); x++)
  787. {
  788. if (ttiles[i][j][k].objects[x].first->id == obj->id)
  789. {
  790. ttiles[i][j][k].objects.erase(ttiles[i][j][k].objects.begin() + x);
  791. break;
  792. }
  793. }
  794. }
  795. }
  796. }
  797. return true;
  798. }
  799. bool CMapHandler::removeObject(CGObjectInstance *obj)
  800. {
  801. hideObject(obj);
  802. return true;
  803. }
  804. ui8 CMapHandler::getHeroFrameNum(ui8 dir, bool isMoving) const
  805. {
  806. if(isMoving)
  807. {
  808. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  809. return frame[dir];
  810. }
  811. else //if(isMoving)
  812. {
  813. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  814. return frame[dir];
  815. }
  816. }
  817. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  818. {
  819. if(ext)
  820. {
  821. if(val->x<0)
  822. {
  823. val->w += val->x;
  824. val->x = ext->x;
  825. }
  826. else
  827. {
  828. val->x += ext->x;
  829. }
  830. if(val->y<0)
  831. {
  832. val->h += val->y;
  833. val->y = ext->y;
  834. }
  835. else
  836. {
  837. val->y += ext->y;
  838. }
  839. if(val->x+val->w > ext->x+ext->w)
  840. {
  841. val->w = ext->x+ext->w-val->x;
  842. }
  843. if(val->y+val->h > ext->y+ext->h)
  844. {
  845. val->h = ext->y+ext->h-val->y;
  846. }
  847. //for sign problems
  848. if(val->h > 20000 || val->w > 20000)
  849. {
  850. val->h = val->w = 0;
  851. }
  852. }
  853. }
  854. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  855. {
  856. if(a.z!=b.z)
  857. return -1; //error!
  858. if(a.x==b.x+1 && a.y==b.y+1) //lt
  859. return 0;
  860. else if(a.x==b.x && a.y==b.y+1) //t
  861. return 1;
  862. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  863. return 2;
  864. else if(a.x==b.x-1 && a.y==b.y) //r
  865. return 3;
  866. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  867. return 4;
  868. else if(a.x==b.x && a.y==b.y-1) //b
  869. return 5;
  870. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  871. return 6;
  872. else if(a.x==b.x+1 && a.y==b.y) //l
  873. return 7;
  874. return -2; //shouldn't happen
  875. }
  876. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  877. {
  878. //works with at most 16 colors, if needed more -> increase values
  879. assert(howMany < 16);
  880. SDL_Color palette[16];
  881. for(int i=0; i<howMany; ++i)
  882. {
  883. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  884. }
  885. SDL_SetColors(img,palette,from,howMany);
  886. }
  887. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  888. {
  889. for(auto & elem : terrainGraphics[7])
  890. {
  891. shiftColors(elem,246, 9);
  892. }
  893. for(auto & elem : terrainGraphics[8])
  894. {
  895. shiftColors(elem,229, 12);
  896. shiftColors(elem,242, 14);
  897. }
  898. for(auto & elem : staticRiverDefs[0]->ourImages)
  899. {
  900. shiftColors(elem.bitmap,183, 12);
  901. shiftColors(elem.bitmap,195, 6);
  902. }
  903. for(auto & elem : staticRiverDefs[2]->ourImages)
  904. {
  905. shiftColors(elem.bitmap,228, 12);
  906. shiftColors(elem.bitmap,183, 6);
  907. shiftColors(elem.bitmap,240, 6);
  908. }
  909. for(auto & elem : staticRiverDefs[3]->ourImages)
  910. {
  911. shiftColors(elem.bitmap,240, 9);
  912. }
  913. }
  914. CMapHandler::~CMapHandler()
  915. {
  916. delete graphics->FoWfullHide;
  917. delete graphics->FoWpartialHide;
  918. for(auto & elem : roadDefs)
  919. delete elem;
  920. for(auto & elem : staticRiverDefs)
  921. delete elem;
  922. for(auto & elem : terrainGraphics)
  923. {
  924. for(int j=0; j < elem.size(); ++j)
  925. SDL_FreeSurface(elem[j]);
  926. }
  927. terrainGraphics.clear();
  928. }
  929. CMapHandler::CMapHandler()
  930. {
  931. frameW = frameH = 0;
  932. graphics->FoWfullHide = nullptr;
  933. graphics->FoWpartialHide = nullptr;
  934. }
  935. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  936. {
  937. out.clear();
  938. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  939. const TerrainTile &t = map->getTile(pos);
  940. for(auto & elem : tt.objects)
  941. {
  942. if(elem.first->ID == Obj::HOLE) //Hole
  943. {
  944. out = elem.first->getObjectName();
  945. return;
  946. }
  947. }
  948. if(t.hasFavourableWinds())
  949. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  950. else if(terName)
  951. out = CGI->generaltexth->terrainNames[t.terType];
  952. }
  953. ui8 CMapHandler::getPhaseShift(const CGObjectInstance *object) const
  954. {
  955. auto i = animationPhase.find(object);
  956. if(i == animationPhase.end())
  957. {
  958. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  959. animationPhase[object] = ret;
  960. return ret;
  961. }
  962. return i->second;
  963. }
  964. TerrainTile2::TerrainTile2()
  965. :terbitmap(nullptr)
  966. {}
  967. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  968. {
  969. if (a->appearance.printPriority != b->appearance.printPriority)
  970. return a->appearance.printPriority > b->appearance.printPriority;
  971. if(a->pos.y != b->pos.y)
  972. return a->pos.y < b->pos.y;
  973. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  974. return true;
  975. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  976. return false;
  977. if(!a->isVisitable() && b->isVisitable())
  978. return true;
  979. if(!b->isVisitable() && a->isVisitable())
  980. return false;
  981. if(a->pos.x < b->pos.x)
  982. return true;
  983. return false;
  984. }