mapHandler.cpp 31 KB

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