mapHandler.cpp 32 KB

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