mapHandler.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. #include "stdafx.h"
  2. #include "mapHandler.h"
  3. #include "client/SDL_Extensions.h"
  4. #include "client/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 "client/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 "client/CConfigHandler.h"
  19. #include <boost/assign/list_of.hpp>
  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. extern SDL_Surface * screen;
  30. #define ADVOPT (conf.go()->ac)
  31. std::string nameFromType (int typ)
  32. {
  33. switch(static_cast<TerrainTile::EterrainType>(typ))
  34. {
  35. case TerrainTile::dirt:
  36. return std::string("DIRTTL.DEF");
  37. case TerrainTile::sand:
  38. return std::string("SANDTL.DEF");
  39. case TerrainTile::grass:
  40. return std::string("GRASTL.DEF");
  41. case TerrainTile::snow:
  42. return std::string("SNOWTL.DEF");
  43. case TerrainTile::swamp:
  44. return std::string("SWMPTL.DEF");
  45. case TerrainTile::rough:
  46. return std::string("ROUGTL.DEF");
  47. case TerrainTile::subterranean:
  48. return std::string("SUBBTL.DEF");
  49. case TerrainTile::lava:
  50. return std::string("LAVATL.DEF");
  51. case TerrainTile::water:
  52. return std::string("WATRTL.DEF");
  53. case TerrainTile::rock:
  54. return std::string("ROCKTL.DEF");
  55. case TerrainTile::border:
  56. //TODO use me
  57. break;
  58. default:
  59. //TODO do something here
  60. break;
  61. }
  62. return std::string();
  63. }
  64. struct OCM_HLP
  65. {
  66. bool operator ()(const std::pair<const CGObjectInstance*, SDL_Rect> & a, const std::pair<const CGObjectInstance*, SDL_Rect> & b)
  67. {
  68. return (*a.first)<(*b.first);
  69. }
  70. } ocmptwo ;
  71. void alphaTransformDef(CGDefInfo * defInfo)
  72. {
  73. for(int yy=0; yy<defInfo->handler->ourImages.size(); ++yy)
  74. {
  75. CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  76. }
  77. }
  78. void CMapHandler::prepareFOWDefs()
  79. {
  80. fullHide = CDefHandler::giveDef("TSHRC.DEF");
  81. partialHide = CDefHandler::giveDef("TSHRE.DEF");
  82. //adding necessary rotations
  83. int missRot [] = {22, 15, 2, 13, 12, 16, 18, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27, 28};
  84. Cimage nw;
  85. for(int g=0; g<ARRAY_COUNT(missRot); ++g)
  86. {
  87. nw = partialHide->ourImages[missRot[g]];
  88. nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  89. partialHide->ourImages.push_back(nw);
  90. }
  91. //necessaary rotations added
  92. //alpha - transformation
  93. for(size_t i=0; i<partialHide->ourImages.size(); ++i)
  94. {
  95. CSDL_Ext::alphaTransform(partialHide->ourImages[i].bitmap);
  96. }
  97. //initialization of type of full-hide image
  98. hideBitmap.resize(CGI->mh->map->width);
  99. for (size_t i=0;i<hideBitmap.size();i++)
  100. {
  101. hideBitmap[i].resize(CGI->mh->map->height);
  102. }
  103. for (size_t i=0; i<hideBitmap.size(); ++i)
  104. {
  105. for (int j=0; j < CGI->mh->map->height; ++j)
  106. {
  107. hideBitmap[i][j].resize(CGI->mh->map->twoLevel+1);
  108. for(int k=0; k<CGI->mh->map->twoLevel+1; ++k)
  109. {
  110. hideBitmap[i][j][k] = rand()%fullHide->ourImages.size();
  111. }
  112. }
  113. }
  114. }
  115. void CMapHandler::roadsRiverTerrainInit()
  116. {
  117. //initializing road's and river's DefHandlers
  118. roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
  119. roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
  120. roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
  121. staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
  122. staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
  123. staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
  124. staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
  125. for(size_t g=0; g<staticRiverDefs.size(); ++g)
  126. {
  127. for(size_t h=0; h < staticRiverDefs[g]->ourImages.size(); ++h)
  128. {
  129. CSDL_Ext::alphaTransform(staticRiverDefs[g]->ourImages[h].bitmap);
  130. }
  131. }
  132. for(size_t g=0; g<roadDefs.size(); ++g)
  133. {
  134. for(size_t h=0; h < roadDefs[g]->ourImages.size(); ++h)
  135. {
  136. CSDL_Ext::alphaTransform(roadDefs[g]->ourImages[h].bitmap);
  137. }
  138. }
  139. sizes.x = CGI->mh->map->width;
  140. sizes.y = CGI->mh->map->height;
  141. sizes.z = CGI->mh->map->twoLevel+1;
  142. // Create enough room for the whole map and its frame
  143. ttiles.resize(CGI->mh->map->width, frameW, frameW);
  144. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  145. {
  146. ttiles[i].resize(CGI->mh->map->height, frameH, frameH);
  147. }
  148. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  149. {
  150. for (int j=0-frameH;j<(int)CGI->mh->map->height+frameH;j++)
  151. ttiles[i][j].resize(CGI->mh->map->twoLevel+1, 0, 0);
  152. }
  153. // prepare the map
  154. for (int i=0; i<map->width; i++) //by width
  155. {
  156. for (int j=0; j<map->height;j++) //by height
  157. {
  158. for (int k=0; k<=map->twoLevel; ++k) //by levels
  159. {
  160. TerrainTile2 &pom(ttiles[i][j][k]);
  161. pom.pos = int3(i, j, k);
  162. pom.tileInfo = &(map->terrain[i][j][k]);
  163. }
  164. }
  165. }
  166. }
  167. void CMapHandler::borderAndTerrainBitmapInit()
  168. {
  169. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  170. bord->notFreeImgs = true;
  171. terrainGraphics.resize(10);
  172. for (int i = 0; i < 10 ; i++)
  173. {
  174. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  175. terrainGraphics[i].resize(hlp->ourImages.size());
  176. hlp->notFreeImgs = true;
  177. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  178. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  179. delete hlp;
  180. }
  181. for (int i=0-frameW; i<map->width+frameW; i++) //by width
  182. {
  183. for (int j=0-frameH; j<map->height+frameH;j++) //by height
  184. {
  185. for(int k=0; k<=map->twoLevel; ++k) //by levles
  186. {
  187. if(i < 0 || i > (map->width-1) || j < 0 || j > (map->height-1))
  188. {
  189. int terBitmapNum = -1;
  190. if(i==-1 && j==-1)
  191. terBitmapNum = 16;
  192. else if(i==-1 && j==(map->height))
  193. terBitmapNum = 19;
  194. else if(i==(map->width) && j==-1)
  195. terBitmapNum = 17;
  196. else if(i==(map->width) && j==(map->height))
  197. terBitmapNum = 18;
  198. else if(j == -1 && i > -1 && i < map->height)
  199. terBitmapNum = 22+rand()%2;
  200. else if(i == -1 && j > -1 && j < map->height)
  201. terBitmapNum = 33+rand()%2;
  202. else if(j == map->height && i >-1 && i < map->width)
  203. terBitmapNum = 29+rand()%2;
  204. else if(i == map->width && j > -1 && j < map->height)
  205. terBitmapNum = 25+rand()%2;
  206. else
  207. terBitmapNum = rand()%16;
  208. if(terBitmapNum != -1)
  209. {
  210. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  211. continue;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. delete bord;
  218. }
  219. void CMapHandler::initObjectRects()
  220. {
  221. //initializing objects / rects
  222. for(size_t f=0; f < map->objects.size(); ++f)
  223. {
  224. const CGObjectInstance *obj = map->objects[f];
  225. if( !obj
  226. || obj->ID==HEROI_TYPE && static_cast<const CGHeroInstance*>(obj)->inTownGarrison //garrisoned hero
  227. || obj->ID==8 && static_cast<const CGBoat*>(obj)->hero //boat wih hero (hero graphics is used)
  228. || !obj->defInfo
  229. || !obj->defInfo->handler) //no graphic...
  230. {
  231. continue;
  232. }
  233. const SDL_Surface *bitmap = obj->defInfo->handler->ourImages[0].bitmap;
  234. for(int fx=0; fx<bitmap->w>>5; ++fx) //bitmap->w/32
  235. {
  236. for(int fy=0; fy<bitmap->h>>5; ++fy) //bitmap->h/32
  237. {
  238. SDL_Rect cr;
  239. cr.w = 32;
  240. cr.h = 32;
  241. cr.x = fx<<5; //fx*32
  242. cr.y = fy<<5; //fy*32
  243. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  244. if( (obj->pos.x + fx - bitmap->w/32+1) >= 0
  245. && (obj->pos.x + fx - bitmap->w/32+1) < ttiles.size() - frameW
  246. && (obj->pos.y + fy - bitmap->h/32+1) >= 0
  247. && (obj->pos.y + fy - bitmap->h/32+1) < ttiles[0].size() - frameH
  248. )
  249. {
  250. //TerrainTile2 & curt =
  251. // ttiles
  252. // [obj->pos.x + fx - bitmap->w/32]
  253. //[obj->pos.y + fy - bitmap->h/32]
  254. //[obj->pos.z];
  255. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  256. }
  257. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  258. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  259. } // for(int f=0; f<map->objects.size(); ++f)
  260. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  261. {
  262. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  263. {
  264. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  265. {
  266. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  267. }
  268. }
  269. }
  270. }
  271. static void processDef (CGDefInfo* def)
  272. {
  273. if(def->id == EVENTI_TYPE)
  274. return;
  275. if(!def->handler) //if object has already set handler (eg. heroes) it should not be overwritten
  276. {
  277. if(def->name.size())
  278. {
  279. def->handler = CDefHandler::giveDefEss(def->name);
  280. }
  281. else
  282. {
  283. tlog2 << "No def name for " << def->id << " " << def->subid << std::endl;
  284. def->handler = NULL;
  285. return;
  286. }
  287. def->width = def->handler->ourImages[0].bitmap->w/32;
  288. def->height = def->handler->ourImages[0].bitmap->h/32;
  289. }
  290. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  291. if(pom && def->id!=TOWNI_TYPE)
  292. {
  293. pom->handler = def->handler;
  294. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  295. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  296. }
  297. else if(def->id != HEROI_TYPE && def->id != TOWNI_TYPE)
  298. tlog3 << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  299. //alpha transformation
  300. for(size_t yy=0; yy < def->handler->ourImages.size(); ++yy)
  301. {
  302. CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  303. }
  304. }
  305. void CMapHandler::initHeroDef(CGHeroInstance * h)
  306. {
  307. h->defInfo->handler = graphics->flags1[0];
  308. h->defInfo->width = h->defInfo->handler->ourImages[0].bitmap->w/32;
  309. h->defInfo->height = h->defInfo->handler->ourImages[0].bitmap->h/32;
  310. }
  311. void CMapHandler::init()
  312. {
  313. timeHandler th;
  314. th.getDif();
  315. CGI->dobjinfo->gobjs[8][0]->handler = graphics->boatAnims[0];
  316. CGI->dobjinfo->gobjs[8][1]->handler = graphics->boatAnims[1];
  317. CGI->dobjinfo->gobjs[8][2]->handler = graphics->boatAnims[2];
  318. // Size of visible terrain.
  319. mapW = conf.go()->ac.advmapW;
  320. mapH = conf.go()->ac.advmapH;
  321. // Total number of visible tiles. Substract the center tile, then
  322. // compute the number of tiles on each side, and reassemble.
  323. int t1, t2;
  324. t1 = (mapW-32)/2;
  325. t2 = mapW - 32 - t1;
  326. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  327. t1 = (mapH-32)/2;
  328. t2 = mapH - 32 - t1;
  329. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  330. // Size of the frame around the map. In extremes positions, the
  331. // frame must not be on the center of the map, but right on the
  332. // edge of the center tile.
  333. frameW = (mapW+31) /32 / 2;
  334. frameH = (mapH+31) /32 / 2;
  335. offsetX = (mapW - (2*frameW+1)*32)/2;
  336. offsetY = (mapH - (2*frameH+1)*32)/2;
  337. std::ifstream ifs(DATA_DIR "/config/townsDefs.txt");
  338. int ccc;
  339. ifs>>ccc;
  340. for(int i=0;i<ccc*2;i++)
  341. {
  342. CGDefInfo *n;
  343. if(i<ccc)
  344. {
  345. n = CGI->state->villages[i];
  346. map->defy.push_back(CGI->state->forts[i]);
  347. }
  348. else
  349. n = CGI->state->capitols[i%ccc];
  350. ifs >> n->name;
  351. if(!n)
  352. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  353. else
  354. map->defy.push_back(n);
  355. }
  356. tlog0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  357. for(int i=0;i<map->heroes.size();i++)
  358. {
  359. if(!map->heroes[i]->defInfo->handler)
  360. {
  361. initHeroDef(map->heroes[i]);
  362. }
  363. }
  364. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  365. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  366. for(int i=0;i<PLAYER_LIMIT;i++)
  367. {
  368. for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  369. {
  370. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  371. }
  372. }
  373. tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  374. for(size_t h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  375. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  376. tlog0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  377. prepareFOWDefs();
  378. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  379. borderAndTerrainBitmapInit();
  380. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  381. initObjectRects();
  382. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  383. }
  384. // Update map window screen
  385. // top_tile top left tile to draw. Not necessarily visible.
  386. // extRect, extRect = map window on screen
  387. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  388. 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)
  389. {
  390. // Width and height of the portion of the map to process. Units in tiles.
  391. unsigned int dx = tilesW;
  392. unsigned int dy = tilesH;
  393. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  394. SDL_Rect rtile = { 0, 0, 32, 32 };
  395. // Absolute coords of the first pixel in the top left corner
  396. int srx_init = offsetX + extRect->x;
  397. int sry_init = offsetY + extRect->y;
  398. int srx, sry; // absolute screen coordinates in pixels
  399. // If moving, we need to add an extra column/line
  400. if (moveX != 0)
  401. {
  402. dx++;
  403. srx_init += moveX;
  404. if (moveX > 0)
  405. {
  406. // Moving right. We still need to draw the old tile on the
  407. // left, so adjust our referential
  408. top_tile.x --;
  409. srx_init -= 32;
  410. }
  411. }
  412. if (moveY != 0)
  413. {
  414. dy++;
  415. sry_init += moveY;
  416. if (moveY > 0)
  417. {
  418. // Moving down. We still need to draw the tile on the top,
  419. // so adjust our referential.
  420. top_tile.y --;
  421. sry_init -= 32;
  422. }
  423. }
  424. // Reduce sizes if we go out of the full map.
  425. if (top_tile.x < -frameW)
  426. top_tile.x = -frameW;
  427. if (top_tile.y < -frameH)
  428. top_tile.y = -frameH;
  429. if (top_tile.x + dx > map->width + frameW)
  430. dx = map->width + frameW - top_tile.x;
  431. if (top_tile.y + dy > map->height + frameH)
  432. dy = map->height + frameH - top_tile.y;
  433. if(!otherHeroAnim)
  434. heroAnim = anim; //the same, as it should be
  435. SDL_Rect prevClip;
  436. SDL_GetClipRect(extSurf, &prevClip);
  437. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  438. // printing terrain
  439. srx = srx_init;
  440. for (int bx = 0; bx < dx; bx++, srx+=32)
  441. {
  442. // Skip column if not in map
  443. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  444. continue;
  445. sry = sry_init;
  446. for (int by=0; by < dy; by++, sry+=32)
  447. {
  448. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  449. // Skip tile if not in map
  450. if (pos.y < 0 || pos.y >= map->height)
  451. continue;
  452. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  453. const TerrainTile &tinfo = *tile.tileInfo;
  454. SDL_Rect sr;
  455. sr.x=srx;
  456. sr.y=sry;
  457. sr.h=sr.w=32;
  458. //blit terrain with river/road
  459. if(tile.terbitmap) //if custom terrain graphic - use it
  460. SDL_BlitSurface(tile.terbitmap, &genRect(sr.h, sr.w, 0, 0), extSurf, &sr);
  461. else //use default terrain graphic
  462. CSDL_Ext::blitWithRotateClipVal(terrainGraphics[tinfo.tertype][tinfo.terview],rtile, extSurf, sr, tinfo.siodmyTajemniczyBajt%4);
  463. if(tinfo.nuine) //print river if present
  464. CSDL_Ext::blitWithRotateClipValWithAlpha(staticRiverDefs[tinfo.nuine-1]->ourImages[tinfo.rivDir].bitmap,rtile, extSurf, sr, (tinfo.siodmyTajemniczyBajt>>2)%4);
  465. if(tinfo.malle) //print road if present
  466. CSDL_Ext::blitWithRotateClipValWithAlpha(roadDefs[tinfo.malle-1]->ourImages[tinfo.roadDir].bitmap,rtile, extSurf, sr, (tinfo.siodmyTajemniczyBajt>>4)%4);
  467. //blit objects
  468. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  469. for(int h=0; h < objects.size(); ++h)
  470. {
  471. const CGObjectInstance *obj = objects[h].first;
  472. ui8 color = obj->tempOwner;
  473. //checking if object has non-empty graphic on this tile
  474. if(obj->ID != HEROI_TYPE && !obj->coveringAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5))
  475. continue;
  476. //don't print flaggable objects in puzzle mode
  477. if(puzzleMode && obj->tempOwner != 254)
  478. continue;
  479. SDL_Rect sr2(sr);
  480. SDL_Rect pp = objects[h].second;
  481. pp.h = sr.h;
  482. pp.w = sr.w;
  483. const CGHeroInstance * themp = (obj->ID != HEROI_TYPE
  484. ? NULL
  485. : static_cast<const CGHeroInstance*>(obj));
  486. //print hero / boat and flag
  487. if(themp && themp->moveDir && themp->type || obj->ID == 8) //it's hero or boat
  488. {
  489. const int IMGVAL = 8; //frames per group of movement animation
  490. ui8 dir;
  491. std::vector<Cimage> * iv = NULL;
  492. std::vector<CDefEssential *> Graphics::*flg = NULL;
  493. SDL_Surface * tb; //surface to blitted
  494. if(themp) //hero
  495. {
  496. dir = themp->moveDir;
  497. //pick graphics of hero (or boat if hero is sailing)
  498. iv = (themp->boat)
  499. ? &graphics->boatAnims[themp->boat->subID]->ourImages
  500. : &graphics->heroAnims[themp->type->heroType]->ourImages;
  501. //pick appropriate flag set
  502. if(themp->boat)
  503. {
  504. switch (themp->boat->subID)
  505. {
  506. case 0: flg = &Graphics::flags1; break;
  507. case 1: flg = &Graphics::flags2; break;
  508. case 2: flg = &Graphics::flags3; break;
  509. default: tlog1 << "Not supported boat subtype: " << themp->boat->subID << std::endl;
  510. }
  511. }
  512. else
  513. {
  514. flg = &Graphics::flags4;
  515. }
  516. }
  517. else //boat
  518. {
  519. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  520. dir = boat->direction;
  521. iv = &graphics->boatAnims[boat->subID]->ourImages;
  522. }
  523. if(themp && !themp->isStanding) //hero is moving
  524. {
  525. size_t gg;
  526. for(gg=0; gg<iv->size(); ++gg)
  527. {
  528. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  529. {
  530. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  531. break;
  532. }
  533. }
  534. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  535. //printing flag
  536. pp.y+=IMGVAL*2-32;
  537. sr2.y-=16;
  538. SDL_BlitSurface((graphics->*flg)[color]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  539. }
  540. else //hero / boat stands still
  541. {
  542. size_t gg;
  543. for(gg=0; gg < iv->size(); ++gg)
  544. {
  545. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  546. {
  547. tb = (*iv)[gg].bitmap;
  548. break;
  549. }
  550. }
  551. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  552. //printing flag
  553. if(flg
  554. && obj->pos.x == top_tile.x + bx
  555. && obj->pos.y == top_tile.y + by)
  556. {
  557. SDL_Rect bufr = sr2;
  558. bufr.x-=2*32;
  559. bufr.y-=1*32;
  560. bufr.h = 64;
  561. bufr.w = 96;
  562. if(bufr.x-extRect->x>-64)
  563. SDL_BlitSurface((graphics->*flg)[color]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, NULL, extSurf, &bufr);
  564. }
  565. }
  566. }
  567. else //blit normal object
  568. {
  569. const std::vector<Cimage> &ourImages = obj->defInfo->handler->ourImages;
  570. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  571. //setting appropriate flag color
  572. if(color < 8 || color==255)
  573. CSDL_Ext::setPlayerColor(bitmap, color);
  574. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  575. }
  576. }
  577. //objects blitted
  578. }
  579. }
  580. // terrain printed
  581. // printing borders
  582. srx = srx_init;
  583. for (int bx = 0; bx < dx; bx++, srx+=32)
  584. {
  585. sry = sry_init;
  586. for (int by = 0; by<dy; by++, sry+=32)
  587. {
  588. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  589. SDL_Rect sr;
  590. sr.x=srx;
  591. sr.y=sry;
  592. sr.h=sr.w=32;
  593. if (pos.x < 0 || pos.x >= map->width ||
  594. pos.y < 0 || pos.y >= map->height)
  595. {
  596. SDL_BlitSurface(ttiles[pos.x][pos.y][top_tile.z].terbitmap,
  597. &genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  598. }
  599. else
  600. {
  601. //blitting Fog of War
  602. if (pos.x >= 0 &&
  603. pos.y >= 0 &&
  604. pos.x < CGI->mh->map->width &&
  605. pos.y < CGI->mh->map->height &&
  606. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  607. {
  608. SDL_Surface * hide = getVisBitmap(pos.x, pos.y, *visibilityMap, top_tile.z);
  609. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &rtile, extSurf, &sr);
  610. }
  611. //FoW blitted
  612. // TODO: these should be activable by the console
  613. #ifdef MARK_BLOCKED_POSITIONS
  614. if(ttiles[pos.x][pos.y][top_tile.z].tileInfo->blocked) //temporary hiding blocked positions
  615. {
  616. SDL_Rect sr;
  617. sr.x=srx;
  618. sr.y=sry;
  619. sr.h=sr.w=32;
  620. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  621. SDL_BlitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  622. }
  623. #endif
  624. #ifdef MARK_VISITABLE_POSITIONS
  625. if(ttiles[pos.x][pos.y][top_tile.z].tileInfo->visitable) //temporary hiding visitable positions
  626. {
  627. SDL_Rect sr;
  628. sr.x=srx;
  629. sr.y=sry;
  630. sr.h=sr.w=32;
  631. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  632. SDL_BlitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  633. }
  634. #endif
  635. }
  636. }
  637. }
  638. // borders printed
  639. #ifdef MARK_GRID_POSITIONS
  640. // print grid
  641. // TODO: This option should be activated by the console.
  642. srx = srx_init;
  643. for (int bx = 0; bx < dx; bx++, srx+=32)
  644. {
  645. sry = sry_init;
  646. for (int by = 0; by<dy; by++, sry+=32)
  647. {
  648. SDL_Rect sr;
  649. sr.x=srx;
  650. sr.y=sry;
  651. sr.h=sr.w=32;
  652. const int3 color(0x555555, 0x555555, 0x555555);
  653. if (sr.y >= extRect->y &&
  654. sr.y < extRect->y+extRect->h)
  655. for(int i=0;i<sr.w;i++)
  656. if (sr.x+i >= extRect->x &&
  657. sr.x+i < extRect->x+extRect->w)
  658. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  659. if (sr.x >= extRect->x &&
  660. sr.x < extRect->x+extRect->w)
  661. for(int i=0; i<sr.h;i++)
  662. if (sr.y+i >= extRect->y &&
  663. sr.y+i < extRect->y+extRect->h)
  664. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  665. }
  666. }
  667. // grid
  668. #endif
  669. //applying sepia / gray effect
  670. if(puzzleMode)
  671. {
  672. if(ADVOPT.puzzleSepia)
  673. {
  674. const int sepiaDepth = 20;
  675. const int sepiaIntensity = 30;
  676. for(int xp = extRect->x; xp < extRect->x + extRect->w; ++xp)
  677. {
  678. for(int yp = extRect->y; yp < extRect->y + extRect->h; ++yp)
  679. {
  680. unsigned char * pixels = (unsigned char*)extSurf->pixels + yp * extSurf->pitch + xp * extSurf->format->BytesPerPixel;
  681. int b = pixels[0];
  682. int g = pixels[1];
  683. int r = pixels[2];
  684. int gry = (r + g + b) / 3;
  685. r = g = b = gry;
  686. r = r + (sepiaDepth * 2);
  687. g = g + sepiaDepth;
  688. if (r>255) r=255;
  689. if (g>255) g=255;
  690. if (b>255) b=255;
  691. // Darken blue color to increase sepia effect
  692. b -= sepiaIntensity;
  693. // normalize if out of bounds
  694. if (b<0) b=0;
  695. if (b>255) b=255;
  696. pixels[0] = b;
  697. pixels[1] = g;
  698. pixels[2] = r;
  699. }
  700. }
  701. }
  702. else
  703. {
  704. for(int xp = extRect->x; xp < extRect->x + extRect->w; ++xp)
  705. {
  706. for(int yp = extRect->y; yp < extRect->y + extRect->h; ++yp)
  707. {
  708. unsigned char * pixels = (unsigned char*)extSurf->pixels + yp * extSurf->pitch + xp * extSurf->format->BytesPerPixel;
  709. int b = pixels[0];
  710. int g = pixels[1];
  711. int r = pixels[2];
  712. int gry = (r + g + b) / 3;
  713. pixels[0] = pixels[1] = pixels[2] = gry;
  714. }
  715. }
  716. }
  717. }
  718. //sepia / gray effect applied
  719. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  720. }
  721. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  722. {
  723. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  724. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  725. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  726. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  727. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  728. d5 = visibilityMap[x][y][lvl], //TODO use me - OMFG
  729. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  730. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  731. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  732. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  733. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  734. {
  735. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  736. }
  737. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  738. {
  739. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  740. }
  741. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  742. {
  743. return partialHide->ourImages[15].bitmap; //visible right top corner
  744. }
  745. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  746. {
  747. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  748. }
  749. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  750. {
  751. return partialHide->ourImages[35].bitmap; //visible left top corner
  752. }
  753. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  754. {
  755. return partialHide->ourImages[0].bitmap; //visible top
  756. }
  757. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  758. {
  759. return partialHide->ourImages[4].bitmap; //visble bottom
  760. }
  761. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  762. {
  763. return partialHide->ourImages[36].bitmap; //visible left
  764. }
  765. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  766. {
  767. return partialHide->ourImages[2].bitmap; //visible right
  768. }
  769. else if(d2 && d6 && !d4 && !d8 && !d7)
  770. {
  771. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  772. }
  773. else if(!d2 && d6 && !d4 && d8 && !d1)
  774. {
  775. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  776. }
  777. else if(!d2 && !d6 && d4 && d8 && !d3)
  778. {
  779. return partialHide->ourImages[37].bitmap; //visible top, top - left, left; right bottom corner hidden
  780. }
  781. else if(d2 && !d6 && d4 && !d8 && !d9)
  782. {
  783. return partialHide->ourImages[38].bitmap; //visible left, left - bottom, bottom; right top corner hidden
  784. }
  785. else if(d2 && d6 && d4 && d8)
  786. {
  787. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  788. }
  789. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  790. {
  791. return partialHide->ourImages[16].bitmap; //visible right corners
  792. }
  793. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  794. {
  795. return partialHide->ourImages[18].bitmap; //visible top corners
  796. }
  797. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  798. {
  799. return partialHide->ourImages[39].bitmap; //visible left corners
  800. }
  801. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  802. {
  803. return partialHide->ourImages[40].bitmap; //visible bottom corners
  804. }
  805. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  806. {
  807. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  808. }
  809. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  810. {
  811. return partialHide->ourImages[41].bitmap; //visible top - left and bottom - right corners
  812. }
  813. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  814. {
  815. return partialHide->ourImages[19].bitmap; //visible corners without left top
  816. }
  817. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  818. {
  819. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  820. }
  821. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  822. {
  823. return partialHide->ourImages[42].bitmap; //visible corners without right bottom
  824. }
  825. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  826. {
  827. return partialHide->ourImages[43].bitmap; //visible corners without right top
  828. }
  829. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  830. {
  831. return partialHide->ourImages[21].bitmap; //visible all corners only
  832. }
  833. if(d2 && d6 && d4 && !d8)
  834. {
  835. return partialHide->ourImages[6].bitmap; //hidden top
  836. }
  837. if(d2 && !d6 && d4 && d8)
  838. {
  839. return partialHide->ourImages[7].bitmap; //hidden right
  840. }
  841. if(!d2 && d6 && d4 && d8)
  842. {
  843. return partialHide->ourImages[8].bitmap; //hidden bottom
  844. }
  845. if(d2 && d6 && !d4 && d8)
  846. {
  847. return partialHide->ourImages[44].bitmap; //hidden left
  848. }
  849. if(!d2 && d6 && d4 && !d8)
  850. {
  851. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  852. }
  853. if(d2 && !d6 && !d4 && d8)
  854. {
  855. return partialHide->ourImages[29].bitmap; //hidden left and right
  856. }
  857. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  858. {
  859. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  860. }
  861. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  862. {
  863. return partialHide->ourImages[45].bitmap; //visible top and left bottom corner
  864. }
  865. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  866. {
  867. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  868. }
  869. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  870. {
  871. return partialHide->ourImages[46].bitmap; //visible left and right top corner
  872. }
  873. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  874. {
  875. return partialHide->ourImages[47].bitmap; //visible left and right bottom corner
  876. }
  877. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  878. {
  879. return partialHide->ourImages[32].bitmap; //visible left and right corners
  880. }
  881. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  882. {
  883. return partialHide->ourImages[48].bitmap; //visible bottom and left top corner
  884. }
  885. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  886. {
  887. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  888. }
  889. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  890. {
  891. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  892. }
  893. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  894. {
  895. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  896. }
  897. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  898. {
  899. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  900. }
  901. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  902. {
  903. return partialHide->ourImages[49].bitmap; //visible right and left cornres
  904. }
  905. if(d2 && d6 && !d4 && !d8 && d7)
  906. {
  907. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  908. }
  909. else if(!d2 && d6 && !d4 && d8 && d1)
  910. {
  911. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  912. }
  913. else if(!d2 && !d6 && d4 && d8 && d3)
  914. {
  915. return partialHide->ourImages[50].bitmap; //visible top, top - left, left; right bottom corner visible
  916. }
  917. else if(d2 && !d6 && d4 && !d8 && d9)
  918. {
  919. return partialHide->ourImages[51].bitmap; //visible left, left - bottom, bottom; right top corner visible
  920. }
  921. //newly added
  922. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  923. {
  924. return partialHide->ourImages[0].bitmap;
  925. }
  926. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  927. {
  928. return partialHide->ourImages[1].bitmap;
  929. }
  930. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  931. {
  932. return partialHide->ourImages[4].bitmap;
  933. }
  934. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  935. {
  936. return partialHide->ourImages[5].bitmap;
  937. }
  938. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  939. {
  940. return partialHide->ourImages[36].bitmap;
  941. }
  942. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  943. {
  944. return partialHide->ourImages[36].bitmap;
  945. }
  946. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  947. {
  948. return partialHide->ourImages[2].bitmap;
  949. }
  950. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  951. {
  952. return partialHide->ourImages[3].bitmap;
  953. }
  954. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  955. }
  956. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  957. {
  958. int ret=-1;
  959. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  960. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  961. else if(a.x>=CGI->mh->map->width && a.y<CGI->mh->map->height)
  962. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][a.y][a.z].tileInfo->malle];
  963. else if(a.x<CGI->mh->map->width && a.y>=CGI->mh->map->height)
  964. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  965. else
  966. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  967. if(!(a.x==b.x || a.y==b.y))
  968. ret*=1.41421;
  969. //TODO: use hero's pathfinding skill during calculating cost
  970. return ret;
  971. }
  972. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  973. //{
  974. // std::vector < CGObjectInstance * > ret;
  975. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  976. // {
  977. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  978. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  979. // ret.push_back(curi);
  980. // }
  981. // return ret;
  982. //}
  983. std::string CMapHandler::getDefName(int id, int subid)
  984. {
  985. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  986. if(temp)
  987. return temp->name;
  988. throw std::string("Def not found.");
  989. }
  990. bool CMapHandler::printObject(const CGObjectInstance *obj)
  991. {
  992. const SDL_Surface *bitmap = obj->defInfo->handler->ourImages[0].bitmap;
  993. for(int fx=0; fx<bitmap->w/32; ++fx)
  994. {
  995. for(int fy=0; fy<bitmap->h/32; ++fy)
  996. {
  997. SDL_Rect cr;
  998. cr.w = 32;
  999. cr.h = 32;
  1000. cr.x = fx*32;
  1001. cr.y = fy*32;
  1002. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1003. 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)
  1004. {
  1005. TerrainTile2 & curt = //TODO use me
  1006. ttiles
  1007. [obj->pos.x + fx - bitmap->w/32]
  1008. [obj->pos.y + fy - bitmap->h/32]
  1009. [obj->pos.z];
  1010. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  1011. }
  1012. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1013. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1014. return true;
  1015. }
  1016. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1017. {
  1018. CDefEssential * curd = obj->defInfo->handler;
  1019. if(!curd) return false;
  1020. const SDL_Surface *bitmap = curd->ourImages[0].bitmap;
  1021. for(int fx=0; fx<bitmap->w/32; ++fx)
  1022. {
  1023. for(int fy=0; fy<bitmap->h/32; ++fy)
  1024. {
  1025. 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)
  1026. {
  1027. 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;
  1028. for(size_t dd=0; dd < ctile.size(); ++dd)
  1029. {
  1030. if(ctile[dd].first->id==obj->id)
  1031. ctile.erase(ctile.begin() + dd);
  1032. }
  1033. }
  1034. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1035. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1036. return true;
  1037. }
  1038. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1039. {
  1040. hideObject(obj);
  1041. return true;
  1042. }
  1043. unsigned char CMapHandler::getHeroFrameNum(unsigned char dir, bool isMoving) const
  1044. {
  1045. if(isMoving)
  1046. {
  1047. switch(dir)
  1048. {
  1049. case 1:
  1050. return 10;
  1051. case 2:
  1052. return 5;
  1053. case 3:
  1054. return 6;
  1055. case 4:
  1056. return 7;
  1057. case 5:
  1058. return 8;
  1059. case 6:
  1060. return 9;
  1061. case 7:
  1062. return 12;
  1063. case 8:
  1064. return 11;
  1065. default:
  1066. throw std::string("Something very wrong1.");
  1067. }
  1068. }
  1069. else //if(isMoving)
  1070. {
  1071. switch(dir)
  1072. {
  1073. case 1:
  1074. return 13;
  1075. case 2:
  1076. return 0;
  1077. case 3:
  1078. return 1;
  1079. case 4:
  1080. return 2;
  1081. case 5:
  1082. return 3;
  1083. case 6:
  1084. return 4;
  1085. case 7:
  1086. return 15;
  1087. case 8:
  1088. return 14;
  1089. default:
  1090. throw std::string("Something very wrong2.");
  1091. }
  1092. }
  1093. }
  1094. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1095. {
  1096. if(ext)
  1097. {
  1098. if(val->x<0)
  1099. {
  1100. val->w += val->x;
  1101. val->x = ext->x;
  1102. }
  1103. else
  1104. {
  1105. val->x += ext->x;
  1106. }
  1107. if(val->y<0)
  1108. {
  1109. val->h += val->y;
  1110. val->y = ext->y;
  1111. }
  1112. else
  1113. {
  1114. val->y += ext->y;
  1115. }
  1116. if(val->x+val->w > ext->x+ext->w)
  1117. {
  1118. val->w = ext->x+ext->w-val->x;
  1119. }
  1120. if(val->y+val->h > ext->y+ext->h)
  1121. {
  1122. val->h = ext->y+ext->h-val->y;
  1123. }
  1124. //for sign problems
  1125. if(val->h > 20000 || val->w > 20000)
  1126. {
  1127. val->h = val->w = 0;
  1128. }
  1129. }
  1130. }
  1131. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1132. {
  1133. if(a.z!=b.z)
  1134. return -1; //error!
  1135. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1136. return 0;
  1137. else if(a.x==b.x && a.y==b.y+1) //t
  1138. return 1;
  1139. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1140. return 2;
  1141. else if(a.x==b.x-1 && a.y==b.y) //r
  1142. return 3;
  1143. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1144. return 4;
  1145. else if(a.x==b.x && a.y==b.y-1) //b
  1146. return 5;
  1147. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1148. return 6;
  1149. else if(a.x==b.x+1 && a.y==b.y) //l
  1150. return 7;
  1151. return -2; //shouldn't happen
  1152. }
  1153. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1154. {
  1155. //works with at most 16 colors, if needed more -> increase values
  1156. assert(howMany < 16);
  1157. SDL_Color palette[16];
  1158. for(int i=0; i<howMany; ++i)
  1159. {
  1160. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1161. }
  1162. SDL_SetColors(img,palette,from,howMany);
  1163. }
  1164. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1165. {
  1166. for(size_t j=0; j < terrainGraphics[7].size(); ++j)
  1167. {
  1168. shiftColors(terrainGraphics[7][j],246, 9);
  1169. }
  1170. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1171. {
  1172. shiftColors(terrainGraphics[8][j],229, 12);
  1173. shiftColors(terrainGraphics[8][j],242, 14);
  1174. }
  1175. for(size_t j=0; j < staticRiverDefs[0]->ourImages.size(); ++j)
  1176. {
  1177. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,183, 12);
  1178. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,195, 6);
  1179. }
  1180. for(size_t j=0; j < staticRiverDefs[2]->ourImages.size(); ++j)
  1181. {
  1182. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,228, 12);
  1183. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,183, 6);
  1184. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,240, 6);
  1185. }
  1186. for(size_t j=0; j < staticRiverDefs[3]->ourImages.size(); ++j)
  1187. {
  1188. shiftColors(staticRiverDefs[3]->ourImages[j].bitmap,240, 9);
  1189. }
  1190. }
  1191. CMapHandler::~CMapHandler()
  1192. {
  1193. delete fullHide;
  1194. delete partialHide;
  1195. for(int i=0; i < roadDefs.size(); i++)
  1196. delete roadDefs[i];
  1197. for(int i=0; i < staticRiverDefs.size(); i++)
  1198. delete staticRiverDefs[i];
  1199. //TODO: why this code makes VCMI crash?
  1200. /*for(int i=0; i < terrainGraphics.size(); ++i)
  1201. {
  1202. for(int j=0; j < terrainGraphics[i].size(); ++j)
  1203. SDL_FreeSurface(terrainGraphics[i][j]);
  1204. }
  1205. terrainGraphics.clear();*/
  1206. }
  1207. CMapHandler::CMapHandler()
  1208. {
  1209. mapW = mapH = 0;
  1210. frameW = frameH = 0;
  1211. fullHide = NULL;
  1212. partialHide = NULL;
  1213. }
  1214. TerrainTile2::TerrainTile2()
  1215. :tileInfo(0),terbitmap(0)
  1216. {}