mapHandler.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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/CObjectClassesHandler.h"
  16. #include "../lib/CGameState.h"
  17. #include "../lib/CHeroHandler.h"
  18. #include "../lib/CTownHandler.h"
  19. #include "Graphics.h"
  20. #include "../lib/mapObjects/CObjectHandler.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. struct OCM_HLP
  64. {
  65. bool operator ()(const std::pair<const CGObjectInstance*, SDL_Rect> & a, const std::pair<const CGObjectInstance*, SDL_Rect> & b)
  66. {
  67. return (*a.first)<(*b.first);
  68. }
  69. } ocmptwo ;
  70. // void alphaTransformDef(CGDefInfo * defInfo)
  71. // {
  72. // for(int yy=0; yy<defInfo->handler->ourImages.size(); ++yy)
  73. // {
  74. // CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  75. // }
  76. // }
  77. void CMapHandler::prepareFOWDefs()
  78. {
  79. graphics->FoWfullHide = CDefHandler::giveDef("TSHRC.DEF");
  80. graphics->FoWpartialHide = CDefHandler::giveDef("TSHRE.DEF");
  81. //adding necessary rotations
  82. static const int missRot [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  83. Cimage nw;
  84. for(auto & elem : missRot)
  85. {
  86. nw = graphics->FoWpartialHide->ourImages[elem];
  87. nw.bitmap = CSDL_Ext::verticalFlip(nw.bitmap);
  88. graphics->FoWpartialHide->ourImages.push_back(nw);
  89. }
  90. //necessaary rotations added
  91. //alpha - transformation
  92. for(auto & elem : graphics->FoWpartialHide->ourImages)
  93. {
  94. CSDL_Ext::alphaTransform(elem.bitmap);
  95. }
  96. //initialization of type of full-hide image
  97. hideBitmap.resize(sizes.x);
  98. for (auto & elem : hideBitmap)
  99. {
  100. elem.resize(sizes.y);
  101. }
  102. for (auto & elem : hideBitmap)
  103. {
  104. for (int j = 0; j < sizes.y; ++j)
  105. {
  106. elem[j].resize(sizes.z);
  107. for(int k = 0; k < sizes.z; ++k)
  108. {
  109. elem[j][k] = CRandomGenerator::getDefault().nextInt(graphics->FoWfullHide->ourImages.size() - 1);
  110. }
  111. }
  112. }
  113. }
  114. void CMapHandler::roadsRiverTerrainInit()
  115. {
  116. //initializing road's and river's DefHandlers
  117. roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
  118. roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
  119. roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
  120. staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
  121. staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
  122. staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
  123. staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
  124. for(auto & elem : staticRiverDefs)
  125. {
  126. for(size_t h=0; h < elem->ourImages.size(); ++h)
  127. {
  128. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  129. }
  130. }
  131. for(auto & elem : roadDefs)
  132. {
  133. for(size_t h=0; h < elem->ourImages.size(); ++h)
  134. {
  135. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  136. }
  137. }
  138. // Create enough room for the whole map and its frame
  139. ttiles.resize(sizes.x, frameW, frameW);
  140. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  141. {
  142. ttiles[i].resize(sizes.y, frameH, frameH);
  143. }
  144. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  145. {
  146. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  147. ttiles[i][j].resize(sizes.z, 0, 0);
  148. }
  149. }
  150. void CMapHandler::borderAndTerrainBitmapInit()
  151. {
  152. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  153. bord->notFreeImgs = true;
  154. terrainGraphics.resize(10);
  155. for (int i = 0; i < 10 ; i++)
  156. {
  157. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  158. terrainGraphics[i].resize(hlp->ourImages.size());
  159. hlp->notFreeImgs = true;
  160. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  161. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  162. delete hlp;
  163. }
  164. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  165. {
  166. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  167. {
  168. for(int k=0; k<sizes.z; ++k) //by levles
  169. {
  170. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  171. {
  172. int terBitmapNum = -1;
  173. auto & rand = CRandomGenerator::getDefault();
  174. if(i==-1 && j==-1)
  175. terBitmapNum = 16;
  176. else if(i==-1 && j==(sizes.y))
  177. terBitmapNum = 19;
  178. else if(i==(sizes.x) && j==-1)
  179. terBitmapNum = 17;
  180. else if(i==(sizes.x) && j==(sizes.y))
  181. terBitmapNum = 18;
  182. else if(j == -1 && i > -1 && i < sizes.y)
  183. terBitmapNum = rand.nextInt(22, 23);
  184. else if(i == -1 && j > -1 && j < sizes.y)
  185. terBitmapNum = rand.nextInt(33, 34);
  186. else if(j == sizes.y && i >-1 && i < sizes.x)
  187. terBitmapNum = rand.nextInt(29, 30);
  188. else if(i == sizes.x && j > -1 && j < sizes.y)
  189. terBitmapNum = rand.nextInt(25, 26);
  190. else
  191. terBitmapNum = rand.nextInt(15);
  192. if(terBitmapNum != -1)
  193. {
  194. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  195. continue;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. delete bord;
  202. }
  203. static void processDef (const ObjectTemplate & objTempl)
  204. {
  205. if(objTempl.id == Obj::EVENT)
  206. {
  207. graphics->advmapobjGraphics[objTempl.animationFile] = nullptr;
  208. return;
  209. }
  210. CDefEssential * ourDef = graphics->getDef(objTempl);
  211. if(!ourDef) //if object has already set handler (eg. heroes) it should not be overwritten
  212. {
  213. if(objTempl.animationFile.size())
  214. {
  215. graphics->advmapobjGraphics[objTempl.animationFile] = CDefHandler::giveDefEss(objTempl.animationFile);
  216. }
  217. else
  218. {
  219. logGlobal->warnStream() << "No def name for " << objTempl.id << " " << objTempl.subid;
  220. return;
  221. }
  222. ourDef = graphics->getDef(objTempl);
  223. }
  224. //alpha transformation
  225. for(auto & elem : ourDef->ourImages)
  226. {
  227. CSDL_Ext::alphaTransform(elem.bitmap);
  228. }
  229. }
  230. void CMapHandler::initObjectRects()
  231. {
  232. //initializing objects / rects
  233. for(auto & elem : map->objects)
  234. {
  235. const CGObjectInstance *obj = elem;
  236. if( !obj
  237. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  238. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  239. {
  240. continue;
  241. }
  242. if (!graphics->getDef(obj)) //try to load it
  243. processDef(obj->appearance);
  244. if (!graphics->getDef(obj)) // stil no graphics? exit
  245. continue;
  246. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  247. for(int fx=0; fx < obj->getWidth(); ++fx)
  248. {
  249. for(int fy=0; fy < obj->getHeight(); ++fy)
  250. {
  251. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  252. SDL_Rect cr;
  253. cr.w = 32;
  254. cr.h = 32;
  255. cr.x = bitmap->w - fx * 32 - 32;
  256. cr.y = bitmap->h - fy * 32 - 32;
  257. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  258. if( map->isInTheMap(currTile) && // within map
  259. cr.x + cr.w > 0 && // image has data on this tile
  260. cr.y + cr.h > 0 &&
  261. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  262. )
  263. {
  264. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  265. }
  266. }
  267. }
  268. }
  269. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  270. {
  271. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  272. {
  273. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  274. {
  275. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  276. }
  277. }
  278. }
  279. }
  280. void CMapHandler::init()
  281. {
  282. CStopWatch th;
  283. th.getDiff();
  284. graphics->advmapobjGraphics["AB01_.DEF"] = graphics->boatAnims[0];
  285. graphics->advmapobjGraphics["AB02_.DEF"] = graphics->boatAnims[1];
  286. graphics->advmapobjGraphics["AB03_.DEF"] = graphics->boatAnims[2];
  287. // Size of visible terrain.
  288. int mapW = conf.go()->ac.advmapW;
  289. int mapH = conf.go()->ac.advmapH;
  290. //sizes of terrain
  291. sizes.x = map->width;
  292. sizes.y = map->height;
  293. sizes.z = map->twoLevel ? 2 : 1;
  294. // Total number of visible tiles. Subtract the center tile, then
  295. // compute the number of tiles on each side, and reassemble.
  296. int t1, t2;
  297. t1 = (mapW-32)/2;
  298. t2 = mapW - 32 - t1;
  299. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  300. t1 = (mapH-32)/2;
  301. t2 = mapH - 32 - t1;
  302. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  303. // Size of the frame around the map. In extremes positions, the
  304. // frame must not be on the center of the map, but right on the
  305. // edge of the center tile.
  306. frameW = (mapW+31) /32 / 2;
  307. frameH = (mapH+31) /32 / 2;
  308. offsetX = (mapW - (2*frameW+1)*32)/2;
  309. offsetY = (mapH - (2*frameH+1)*32)/2;
  310. prepareFOWDefs();
  311. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  312. borderAndTerrainBitmapInit();
  313. logGlobal->infoStream()<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDiff();
  314. initObjectRects();
  315. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  316. }
  317. // Update map window screen
  318. // top_tile top left tile to draw. Not necessarily visible.
  319. // extRect, extRect = map window on screen
  320. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  321. 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
  322. {
  323. // Width and height of the portion of the map to process. Units in tiles.
  324. ui32 dx = tilesW;
  325. ui32 dy = tilesH;
  326. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  327. SDL_Rect rtile = { 0, 0, 32, 32 };
  328. // Absolute coords of the first pixel in the top left corner
  329. int srx_init = offsetX + extRect->x;
  330. int sry_init = offsetY + extRect->y;
  331. int srx, sry; // absolute screen coordinates in pixels
  332. // If moving, we need to add an extra column/line
  333. if (moveX != 0)
  334. {
  335. dx++;
  336. srx_init += moveX;
  337. if (moveX > 0)
  338. {
  339. // Moving right. We still need to draw the old tile on the
  340. // left, so adjust our referential
  341. top_tile.x --;
  342. srx_init -= 32;
  343. }
  344. }
  345. if (moveY != 0)
  346. {
  347. dy++;
  348. sry_init += moveY;
  349. if (moveY > 0)
  350. {
  351. // Moving down. We still need to draw the tile on the top,
  352. // so adjust our referential.
  353. top_tile.y --;
  354. sry_init -= 32;
  355. }
  356. }
  357. // Reduce sizes if we go out of the full map.
  358. if (top_tile.x < -frameW)
  359. top_tile.x = -frameW;
  360. if (top_tile.y < -frameH)
  361. top_tile.y = -frameH;
  362. if (top_tile.x + dx > sizes.x + frameW)
  363. dx = sizes.x + frameW - top_tile.x;
  364. if (top_tile.y + dy > sizes.y + frameH)
  365. dy = sizes.y + frameH - top_tile.y;
  366. if(!otherHeroAnim)
  367. heroAnim = anim; //the same, as it should be
  368. SDL_Rect prevClip;
  369. SDL_GetClipRect(extSurf, &prevClip);
  370. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  371. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  372. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  373. //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  374. // printing terrain
  375. srx = srx_init;
  376. for (int bx = 0; bx < dx; bx++, srx+=32)
  377. {
  378. // Skip column if not in map
  379. if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x)
  380. continue;
  381. sry = sry_init;
  382. for (int by=0; by < dy; by++, sry+=32)
  383. {
  384. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  385. // Skip tile if not in map
  386. if (pos.y < 0 || pos.y >= sizes.y)
  387. continue;
  388. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  389. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  390. SDL_Rect sr;
  391. sr.x=srx;
  392. sr.y=sry;
  393. sr.h=sr.w=32;
  394. //blit terrain with river/road
  395. if(tile.terbitmap)
  396. { //if custom terrain graphic - use it
  397. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  398. CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr);
  399. }
  400. else //use default terrain graphic
  401. {
  402. blitterWithRotation(terrainGraphics[tinfo.terType][tinfo.terView],rtile, extSurf, sr, tinfo.extTileFlags%4);
  403. }
  404. if(tinfo.riverType) //print river if present
  405. {
  406. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4);
  407. }
  408. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  409. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  410. { //part from top tile
  411. const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  412. Rect source(0, 16, 32, 16);
  413. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  414. blitterWithRotationAndAlpha(roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.extTileFlags>>4)%4);
  415. }
  416. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  417. {
  418. Rect source(0, 0, 32, 32);
  419. Rect dest(sr.x, sr.y+16, sr.w, sr.h/2);
  420. blitterWithRotationAndAlpha(roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.extTileFlags>>4)%4);
  421. }
  422. //blit objects
  423. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  424. for(auto & object : objects)
  425. {
  426. const CGObjectInstance *obj = object.first;
  427. if (!graphics->getDef(obj))
  428. processDef(obj->appearance);
  429. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  430. {
  431. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  432. }
  433. PlayerColor color = obj->tempOwner;
  434. //checking if object has non-empty graphic on this tile
  435. if(obj->ID != Obj::HERO && !obj->coveringAt(top_tile.x + bx, top_tile.y + by))
  436. continue;
  437. static const int notBlittedInPuzzleMode[] = {124};
  438. //don't print flaggable objects in puzzle mode
  439. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  440. continue;
  441. SDL_Rect sr2(sr);
  442. SDL_Rect pp = object.second;
  443. pp.h = sr.h;
  444. pp.w = sr.w;
  445. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  446. ? nullptr
  447. : static_cast<const CGHeroInstance*>(obj));
  448. //print hero / boat and flag
  449. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  450. {
  451. const int IMGVAL = 8; //frames per group of movement animation
  452. ui8 dir;
  453. std::vector<Cimage> * iv = nullptr;
  454. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  455. SDL_Surface * tb = nullptr; //surface to blitted
  456. if(themp) //hero
  457. {
  458. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  459. {
  460. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  461. continue;
  462. }
  463. dir = themp->moveDir;
  464. //pick graphics of hero (or boat if hero is sailing)
  465. if (themp->boat)
  466. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  467. else if (themp->sex)
  468. iv = &graphics->heroAnims[themp->type->heroClass->imageMapFemale]->ourImages;
  469. else
  470. iv = &graphics->heroAnims[themp->type->heroClass->imageMapMale]->ourImages;
  471. //pick appropriate flag set
  472. if(themp->boat)
  473. {
  474. switch (themp->boat->subID)
  475. {
  476. case 0: flg = &Graphics::flags1; break;
  477. case 1: flg = &Graphics::flags2; break;
  478. case 2: flg = &Graphics::flags3; break;
  479. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  480. }
  481. }
  482. else
  483. {
  484. flg = &Graphics::flags4;
  485. }
  486. }
  487. else //boat
  488. {
  489. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  490. dir = boat->direction;
  491. iv = &graphics->boatAnims[boat->subID]->ourImages;
  492. }
  493. if(themp && !themp->isStanding) //hero is moving
  494. {
  495. size_t gg;
  496. for(gg=0; gg<iv->size(); ++gg)
  497. {
  498. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  499. {
  500. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  501. break;
  502. }
  503. }
  504. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  505. //printing flag
  506. pp.y+=IMGVAL*2-32;
  507. sr2.y-=16;
  508. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  509. }
  510. else //hero / boat stands still
  511. {
  512. size_t gg;
  513. for(gg=0; gg < iv->size(); ++gg)
  514. {
  515. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  516. {
  517. tb = (*iv)[gg].bitmap;
  518. break;
  519. }
  520. }
  521. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  522. //printing flag
  523. if(flg
  524. && obj->pos.x == top_tile.x + bx
  525. && obj->pos.y == top_tile.y + by)
  526. {
  527. SDL_Rect bufr = sr2;
  528. bufr.x-=2*32;
  529. bufr.y-=1*32;
  530. bufr.h = 64;
  531. bufr.w = 96;
  532. if(bufr.x-extRect->x>-64)
  533. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, nullptr, extSurf, &bufr);
  534. }
  535. }
  536. }
  537. else //blit normal object
  538. {
  539. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  540. SDL_Surface *bitmap = ourImages[(anim+getPhaseShift(obj))%ourImages.size()].bitmap;
  541. //setting appropriate flag color
  542. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  543. CSDL_Ext::setPlayerColor(bitmap, color);
  544. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  545. }
  546. }
  547. //objects blitted
  548. //X sign
  549. if(puzzleMode)
  550. {
  551. if(bx == grailPosRel.x && by == grailPosRel.y)
  552. {
  553. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, extSurf, &sr);
  554. }
  555. }
  556. }
  557. }
  558. // terrain printed
  559. // printing borders
  560. srx = srx_init;
  561. for (int bx = 0; bx < dx; bx++, srx+=32)
  562. {
  563. sry = sry_init;
  564. for (int by = 0; by<dy; by++, sry+=32)
  565. {
  566. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  567. SDL_Rect sr;
  568. sr.x=srx;
  569. sr.y=sry;
  570. sr.h=sr.w=32;
  571. if (pos.x < 0 || pos.x >= sizes.x ||
  572. pos.y < 0 || pos.y >= sizes.y)
  573. {
  574. // outside of the map - print borders
  575. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  576. SDL_Surface * src = ttiles[pos.x][pos.y][top_tile.z].terbitmap;
  577. assert(src);
  578. CSDL_Ext::blitSurface(src, &temp_rect,extSurf,&sr);
  579. }
  580. else
  581. {
  582. //blitting Fog of War
  583. if (!puzzleMode)
  584. {
  585. if (pos.x >= 0 &&
  586. pos.y >= 0 &&
  587. pos.x < sizes.x &&
  588. pos.y < sizes.y &&
  589. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  590. {
  591. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  592. if(hide.second)
  593. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  594. else
  595. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  596. }
  597. }
  598. //FoW blitted
  599. SDL_Rect tileRect = genRect(sr.h, sr.w, 0, 0);
  600. if (settings["session"]["showBlock"].Bool())
  601. {
  602. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).blocked) //temporary hiding blocked positions
  603. {
  604. static SDL_Surface * block = nullptr;
  605. if (!block)
  606. block = BitmapHandler::loadBitmap("blocked");
  607. SDL_Rect sr;
  608. sr.x=srx;
  609. sr.y=sry;
  610. sr.h=sr.w=32;
  611. CSDL_Ext::blitSurface(block, &tileRect, extSurf, &sr);
  612. }
  613. }
  614. if (settings["session"]["showVisit"].Bool())
  615. {
  616. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).visitable) //temporary hiding visitable positions
  617. {
  618. static SDL_Surface * visit = nullptr;
  619. if (!visit)
  620. visit = BitmapHandler::loadBitmap("visitable");
  621. SDL_Rect sr;
  622. sr.x=srx;
  623. sr.y=sry;
  624. sr.h=sr.w=32;
  625. CSDL_Ext::blitSurface(visit, &tileRect, extSurf, &sr);
  626. }
  627. }
  628. }
  629. }
  630. }
  631. // borders printed
  632. // print grid
  633. if (settings["session"]["showGrid"].Bool())
  634. {
  635. srx = srx_init;
  636. for (int bx = 0; bx < dx; bx++, srx+=32)
  637. {
  638. sry = sry_init;
  639. for (int by = 0; by<dy; by++, sry+=32)
  640. {
  641. SDL_Rect sr;
  642. sr.x=srx;
  643. sr.y=sry;
  644. sr.h=sr.w=32;
  645. const int3 color(0x555555, 0x555555, 0x555555);
  646. if (sr.y >= extRect->y &&
  647. sr.y < extRect->y+extRect->h)
  648. for(int i=0;i<sr.w;i++)
  649. if (sr.x+i >= extRect->x &&
  650. sr.x+i < extRect->x+extRect->w)
  651. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  652. if (sr.x >= extRect->x &&
  653. sr.x < extRect->x+extRect->w)
  654. for(int i=0; i<sr.h;i++)
  655. if (sr.y+i >= extRect->y &&
  656. sr.y+i < extRect->y+extRect->h)
  657. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  658. }
  659. }
  660. }
  661. // grid
  662. //applying sepia / gray effect
  663. if(puzzleMode)
  664. {
  665. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  666. }
  667. //sepia / gray effect applied
  668. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  669. }
  670. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap ) const
  671. {
  672. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  673. // 0-1, 2-3, 4-5, 11-13, 12-14
  674. static const int visBitmaps[256] = {
  675. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  676. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  677. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  678. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  679. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  680. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  681. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  682. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  683. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  684. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  685. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  686. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  687. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  688. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  689. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  690. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  691. };
  692. auto getTile = [&](int dx, int dy)->bool
  693. {
  694. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  695. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  696. return false;
  697. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  698. };
  699. //is tile visible. arrangement: (like num keyboard)
  700. bool d7 = getTile(-1, -1), //789
  701. d8 = getTile( 0, -1), //456
  702. d9 = getTile(+1, -1), //123
  703. d4 = getTile(-1, 0),
  704. d6 = getTile(+1, 0),
  705. d1 = getTile(-1, +1),
  706. d2 = getTile( 0, +1),
  707. d3 = getTile(+1, +1);
  708. int retBitmapID = visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  709. if (retBitmapID < 0)
  710. {
  711. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  712. }
  713. if (retBitmapID >= 0)
  714. {
  715. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  716. }
  717. else
  718. {
  719. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  720. }
  721. }
  722. bool CMapHandler::printObject(const CGObjectInstance *obj)
  723. {
  724. if (!graphics->getDef(obj))
  725. processDef(obj->appearance);
  726. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  727. const int tilesW = bitmap->w/32;
  728. const int tilesH = bitmap->h/32;
  729. for(int fx=0; fx<tilesW; ++fx)
  730. {
  731. for(int fy=0; fy<tilesH; ++fy)
  732. {
  733. SDL_Rect cr;
  734. cr.w = 32;
  735. cr.h = 32;
  736. cr.x = fx*32;
  737. cr.y = fy*32;
  738. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  739. 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)
  740. {
  741. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  742. auto i = curt.objects.begin();
  743. for(; i != curt.objects.end(); i++)
  744. {
  745. if(ocmptwo(toAdd, *i))
  746. {
  747. curt.objects.insert(i, toAdd);
  748. i = curt.objects.begin(); //to validate and avoid adding it second time
  749. break;
  750. }
  751. }
  752. if(i == curt.objects.end())
  753. curt.objects.insert(i, toAdd);
  754. }
  755. } // for(int fy=0; fy<tilesH; ++fy)
  756. } //for(int fx=0; fx<tilesW; ++fx)
  757. return true;
  758. }
  759. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  760. {
  761. for (size_t i=0; i<map->width; i++)
  762. {
  763. for (size_t j=0; j<map->height; j++)
  764. {
  765. for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
  766. {
  767. for(size_t x=0; x < ttiles[i][j][k].objects.size(); x++)
  768. {
  769. if (ttiles[i][j][k].objects[x].first->id == obj->id)
  770. {
  771. ttiles[i][j][k].objects.erase(ttiles[i][j][k].objects.begin() + x);
  772. break;
  773. }
  774. }
  775. }
  776. }
  777. }
  778. return true;
  779. }
  780. bool CMapHandler::removeObject(CGObjectInstance *obj)
  781. {
  782. hideObject(obj);
  783. return true;
  784. }
  785. ui8 CMapHandler::getHeroFrameNum(ui8 dir, bool isMoving) const
  786. {
  787. if(isMoving)
  788. {
  789. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  790. return frame[dir];
  791. }
  792. else //if(isMoving)
  793. {
  794. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  795. return frame[dir];
  796. }
  797. }
  798. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  799. {
  800. if(ext)
  801. {
  802. if(val->x<0)
  803. {
  804. val->w += val->x;
  805. val->x = ext->x;
  806. }
  807. else
  808. {
  809. val->x += ext->x;
  810. }
  811. if(val->y<0)
  812. {
  813. val->h += val->y;
  814. val->y = ext->y;
  815. }
  816. else
  817. {
  818. val->y += ext->y;
  819. }
  820. if(val->x+val->w > ext->x+ext->w)
  821. {
  822. val->w = ext->x+ext->w-val->x;
  823. }
  824. if(val->y+val->h > ext->y+ext->h)
  825. {
  826. val->h = ext->y+ext->h-val->y;
  827. }
  828. //for sign problems
  829. if(val->h > 20000 || val->w > 20000)
  830. {
  831. val->h = val->w = 0;
  832. }
  833. }
  834. }
  835. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  836. {
  837. if(a.z!=b.z)
  838. return -1; //error!
  839. if(a.x==b.x+1 && a.y==b.y+1) //lt
  840. return 0;
  841. else if(a.x==b.x && a.y==b.y+1) //t
  842. return 1;
  843. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  844. return 2;
  845. else if(a.x==b.x-1 && a.y==b.y) //r
  846. return 3;
  847. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  848. return 4;
  849. else if(a.x==b.x && a.y==b.y-1) //b
  850. return 5;
  851. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  852. return 6;
  853. else if(a.x==b.x+1 && a.y==b.y) //l
  854. return 7;
  855. return -2; //shouldn't happen
  856. }
  857. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  858. {
  859. //works with at most 16 colors, if needed more -> increase values
  860. assert(howMany < 16);
  861. SDL_Color palette[16];
  862. for(int i=0; i<howMany; ++i)
  863. {
  864. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  865. }
  866. SDL_SetColors(img,palette,from,howMany);
  867. }
  868. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  869. {
  870. for(auto & elem : terrainGraphics[7])
  871. {
  872. shiftColors(elem,246, 9);
  873. }
  874. for(auto & elem : terrainGraphics[8])
  875. {
  876. shiftColors(elem,229, 12);
  877. shiftColors(elem,242, 14);
  878. }
  879. for(auto & elem : staticRiverDefs[0]->ourImages)
  880. {
  881. shiftColors(elem.bitmap,183, 12);
  882. shiftColors(elem.bitmap,195, 6);
  883. }
  884. for(auto & elem : staticRiverDefs[2]->ourImages)
  885. {
  886. shiftColors(elem.bitmap,228, 12);
  887. shiftColors(elem.bitmap,183, 6);
  888. shiftColors(elem.bitmap,240, 6);
  889. }
  890. for(auto & elem : staticRiverDefs[3]->ourImages)
  891. {
  892. shiftColors(elem.bitmap,240, 9);
  893. }
  894. }
  895. CMapHandler::~CMapHandler()
  896. {
  897. delete graphics->FoWfullHide;
  898. delete graphics->FoWpartialHide;
  899. for(auto & elem : roadDefs)
  900. delete elem;
  901. for(auto & elem : staticRiverDefs)
  902. delete elem;
  903. for(auto & elem : terrainGraphics)
  904. {
  905. for(int j=0; j < elem.size(); ++j)
  906. SDL_FreeSurface(elem[j]);
  907. }
  908. terrainGraphics.clear();
  909. }
  910. CMapHandler::CMapHandler()
  911. {
  912. frameW = frameH = 0;
  913. graphics->FoWfullHide = nullptr;
  914. graphics->FoWpartialHide = nullptr;
  915. }
  916. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  917. {
  918. out.clear();
  919. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  920. const TerrainTile &t = map->getTile(pos);
  921. for(auto & elem : tt.objects)
  922. {
  923. if(elem.first->ID == Obj::HOLE) //Hole
  924. {
  925. out = elem.first->hoverName;
  926. return;
  927. }
  928. }
  929. if(t.hasFavourableWinds())
  930. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  931. else if(terName)
  932. out = CGI->generaltexth->terrainNames[t.terType];
  933. }
  934. ui8 CMapHandler::getPhaseShift(const CGObjectInstance *object) const
  935. {
  936. auto i = animationPhase.find(object);
  937. if(i == animationPhase.end())
  938. {
  939. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  940. animationPhase[object] = ret;
  941. return ret;
  942. }
  943. return i->second;
  944. }
  945. TerrainTile2::TerrainTile2()
  946. :terbitmap(nullptr)
  947. {}