mapHandler.cpp 30 KB

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