mapHandler.cpp 37 KB

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