mapHandler.cpp 30 KB

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