mapHandler.cpp 40 KB

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