mapHandler.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. def->handler=CDefHandler::giveDef(def->name);
  444. def->width = def->handler->ourImages[0].bitmap->w/32;
  445. def->height = def->handler->ourImages[0].bitmap->h/32;
  446. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  447. if(pom && def->id!=98)
  448. {
  449. pom->handler = def->handler;
  450. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  451. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  452. }
  453. else if(def->id != 34 && def->id != 98)
  454. _log3 << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  455. if(!def->handler->alphaTransformed)
  456. {
  457. for(int yy=0; yy<def->handler->ourImages.size(); ++yy)
  458. {
  459. def->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  460. def->handler->alphaTransformed = true;
  461. }
  462. }
  463. }
  464. void CMapHandler::init()
  465. {
  466. timeHandler th;
  467. th.getDif();
  468. loadDefs(); //loading castles' defs
  469. _log0<<"Reading terrain defs: "<<th.getDif()<<std::endl;
  470. std::ifstream ifs("config/townsDefs.txt");
  471. int ccc;
  472. ifs>>ccc;
  473. for(int i=0;i<ccc*2;i++)
  474. {
  475. CGDefInfo *n;
  476. if(i<ccc)
  477. {
  478. n = CGI->state->villages[i];
  479. map->defs.insert(CGI->state->forts[i]);
  480. }
  481. else
  482. n = CGI->state->capitols[i%ccc];
  483. ifs >> n->name;
  484. if(!n)
  485. _log1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  486. map->defs.insert(n);
  487. }
  488. _log0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  489. for(int i=0;i<map->heroes.size();i++)
  490. {
  491. if(!map->heroes[i]->defInfo->handler)
  492. {
  493. map->heroes[i]->defInfo->handler = graphics->flags1[0];
  494. map->heroes[i]->defInfo->width = map->heroes[i]->defInfo->handler->ourImages[0].bitmap->w/32;
  495. map->heroes[i]->defInfo->height = map->heroes[i]->defInfo->handler->ourImages[0].bitmap->h/32;
  496. }
  497. }
  498. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  499. std::for_each(map->defs.begin(),map->defs.end(),processDef); //and non-h3m defs
  500. _log0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  501. for(int i=0;i<PLAYER_LIMIT;i++)
  502. {
  503. for(int j=0; j<map->players[i].heroesNames.size();j++)
  504. {
  505. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  506. }
  507. }
  508. _log0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  509. for(int h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  510. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  511. _log0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  512. prepareFOWDefs();
  513. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  514. borderAndTerrainBitmapInit();
  515. _log0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  516. initObjectRects();
  517. _log0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  518. }
  519. 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)
  520. {
  521. if(!otherHeroAnim)
  522. heroAnim = anim; //the same, as it should be
  523. //setting surface to blit at
  524. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  525. if(extSurf)
  526. {
  527. su = extSurf;
  528. }
  529. else
  530. {
  531. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  532. }
  533. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  534. throw new std::string("terrainRect: out of range");
  535. ////printing terrain
  536. for (int bx=0; bx<dx; bx++)
  537. {
  538. for (int by=0; by<dy; by++)
  539. {
  540. SDL_Rect sr;
  541. sr.y=by*32;
  542. sr.x=bx*32;
  543. sr.h=sr.w=32;
  544. validateRectTerr(&sr, extRect);
  545. 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);
  546. }
  547. }
  548. ////terrain printed
  549. ////printing rivers
  550. for (int bx=0; bx<dx; bx++)
  551. {
  552. for (int by=0; by<dy; by++)
  553. {
  554. SDL_Rect sr;
  555. sr.y=by*32;
  556. sr.x=bx*32;
  557. sr.h=sr.w=32;
  558. validateRectTerr(&sr, extRect);
  559. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  560. {
  561. 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);
  562. }
  563. }
  564. }
  565. ////rivers printed
  566. ////printing roads
  567. for (int bx=0; bx<dx; bx++)
  568. {
  569. for (int by=-1; by<dy; by++)
  570. {
  571. if(y+by<=-4)
  572. continue;
  573. SDL_Rect sr;
  574. sr.y=by*32+16;
  575. sr.x=bx*32;
  576. sr.h=sr.w=32;
  577. validateRectTerr(&sr, extRect);
  578. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  579. {
  580. 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);
  581. }
  582. }
  583. }
  584. ////roads printed
  585. ////printing objects
  586. for (int bx=0; bx<dx; bx++)
  587. {
  588. for (int by=0; by<dy; by++)
  589. {
  590. for(int h=0; h<ttiles[x+bx][y+by][level].objects.size(); ++h)
  591. {
  592. SDL_Rect sr;
  593. sr.w = 32;
  594. sr.h = 32;
  595. sr.x = (bx)*32;
  596. sr.y = (by)*32;
  597. validateRectTerr(&sr, extRect);
  598. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  599. pp.h = sr.h;
  600. pp.w = sr.w;
  601. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  602. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  603. {
  604. int imgVal = 8;
  605. SDL_Surface * tb;
  606. if(themp->type==NULL)
  607. continue;
  608. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  609. int gg;
  610. for(gg=0; gg<iv.size(); ++gg)
  611. {
  612. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  613. {
  614. tb = iv[gg+heroAnim%imgVal].bitmap;
  615. break;
  616. }
  617. }
  618. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  619. pp.y+=imgVal*2-32;
  620. sr.y-=16;
  621. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  622. }
  623. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  624. {
  625. int imgVal = 8;
  626. SDL_Surface * tb;
  627. if(themp->type==NULL)
  628. continue;
  629. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  630. int gg;
  631. for(gg=0; gg<iv.size(); ++gg)
  632. {
  633. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  634. {
  635. tb = iv[gg].bitmap;
  636. break;
  637. }
  638. }
  639. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  640. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  641. {
  642. SDL_Rect bufr = sr;
  643. bufr.x-=2*32;
  644. bufr.y-=1*32;
  645. bufr.h = 64;
  646. bufr.w = 96;
  647. if(bufr.x-extRect->x>-64)
  648. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  649. }
  650. }
  651. else
  652. {
  653. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  654. int phaseShift = ttiles[x+bx][y+by][level].objects[h].first->animPhaseShift;
  655. //setting appropriate flag color
  656. 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)
  657. 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);
  658. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[(anim+phaseShift)%imgVal].bitmap,&pp,su,&sr);
  659. }
  660. }
  661. }
  662. }
  663. ////objects printed, printing shadow
  664. for (int bx=0; bx<dx; bx++)
  665. {
  666. for (int by=0; by<dy; by++)
  667. {
  668. SDL_Rect sr;
  669. sr.y=by*32;
  670. sr.x=bx*32;
  671. sr.h=sr.w=32;
  672. validateRectTerr(&sr, extRect);
  673. 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])
  674. {
  675. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  676. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  677. }
  678. }
  679. }
  680. ////shadow printed
  681. //printing borders
  682. for (int bx=0; bx<dx; bx++)
  683. {
  684. for (int by=0; by<dy; by++)
  685. {
  686. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  687. {
  688. SDL_Rect sr;
  689. sr.y=by*32;
  690. sr.x=bx*32;
  691. sr.h=sr.w=32;
  692. validateRectTerr(&sr, extRect);
  693. 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);
  694. }
  695. else
  696. {
  697. if(MARK_BLOCKED_POSITIONS && ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  698. {
  699. SDL_Rect sr;
  700. sr.y=by*32;
  701. sr.x=bx*32;
  702. sr.h=sr.w=32;
  703. validateRectTerr(&sr, extRect);
  704. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  705. for(int f=0; f<ns->w*ns->h*4; ++f)
  706. {
  707. *((unsigned char*)(ns->pixels) + f) = 128;
  708. }
  709. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  710. SDL_FreeSurface(ns);
  711. }
  712. if(MARK_VISITABLE_POSITIONS && ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  713. {
  714. SDL_Rect sr;
  715. sr.y=by*32;
  716. sr.x=bx*32;
  717. sr.h=sr.w=32;
  718. validateRectTerr(&sr, extRect);
  719. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  720. for(int f=0; f<ns->w*ns->h*4; ++f)
  721. {
  722. *((unsigned char*)(ns->pixels) + f) = 128;
  723. }
  724. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  725. SDL_FreeSurface(ns);
  726. }
  727. }
  728. }
  729. }
  730. //borders printed
  731. return su;
  732. }
  733. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  734. {
  735. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  736. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  737. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  738. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  739. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  740. d5 = visibilityMap[x][y][lvl],
  741. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  742. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  743. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  744. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  745. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  746. {
  747. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  748. }
  749. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  750. {
  751. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  752. }
  753. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  754. {
  755. return partialHide->ourImages[15].bitmap; //visible right top corner
  756. }
  757. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  758. {
  759. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  760. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  761. }
  762. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  763. {
  764. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  765. return partialHide->ourImages[35].bitmap;
  766. }
  767. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  768. {
  769. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  770. return partialHide->ourImages[0].bitmap; //visible top
  771. }
  772. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  773. {
  774. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  775. return partialHide->ourImages[4].bitmap; //visble bottom
  776. }
  777. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  778. {
  779. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  780. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  781. return partialHide->ourImages[36].bitmap;
  782. }
  783. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  784. {
  785. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  786. return partialHide->ourImages[2].bitmap; //visible right
  787. }
  788. else if(d2 && d6 && !d4 && !d8 && !d7)
  789. {
  790. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  791. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  792. }
  793. else if(!d2 && d6 && !d4 && d8 && !d1)
  794. {
  795. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  796. }
  797. else if(!d2 && !d6 && d4 && d8 && !d3)
  798. {
  799. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  800. return partialHide->ourImages[37].bitmap;
  801. }
  802. else if(d2 && !d6 && d4 && !d8 && !d9)
  803. {
  804. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  805. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  806. return partialHide->ourImages[38].bitmap;
  807. }
  808. else if(d2 && d6 && d4 && d8)
  809. {
  810. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  811. }
  812. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  813. {
  814. return partialHide->ourImages[16].bitmap; //visible right corners
  815. }
  816. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  817. {
  818. return partialHide->ourImages[18].bitmap; //visible top corners
  819. }
  820. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  821. {
  822. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  823. return partialHide->ourImages[39].bitmap;
  824. }
  825. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  826. {
  827. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  828. return partialHide->ourImages[40].bitmap;
  829. }
  830. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  831. {
  832. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  833. }
  834. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  835. {
  836. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  837. return partialHide->ourImages[41].bitmap;
  838. }
  839. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  840. {
  841. return partialHide->ourImages[19].bitmap; //visible corners without left top
  842. }
  843. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  844. {
  845. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  846. }
  847. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  848. {
  849. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  850. return partialHide->ourImages[42].bitmap;
  851. }
  852. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  853. {
  854. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  855. return partialHide->ourImages[43].bitmap;
  856. }
  857. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  858. {
  859. return partialHide->ourImages[21].bitmap; //visible all corners only
  860. }
  861. if(d2 && d6 && d4 && !d8)
  862. {
  863. return partialHide->ourImages[6].bitmap; //hidden top
  864. }
  865. if(d2 && !d6 && d4 && d8)
  866. {
  867. return partialHide->ourImages[7].bitmap; //hidden right
  868. }
  869. if(!d2 && d6 && d4 && d8)
  870. {
  871. return partialHide->ourImages[8].bitmap; //hidden bottom
  872. }
  873. if(d2 && d6 && !d4 && d8)
  874. {
  875. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  876. return partialHide->ourImages[44].bitmap;
  877. }
  878. if(!d2 && d6 && d4 && !d8)
  879. {
  880. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  881. }
  882. if(d2 && !d6 && !d4 && d8)
  883. {
  884. return partialHide->ourImages[29].bitmap; //hidden left and right
  885. }
  886. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  887. {
  888. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  889. }
  890. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  891. {
  892. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  893. return partialHide->ourImages[45].bitmap;
  894. }
  895. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  896. {
  897. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  898. }
  899. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  900. {
  901. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  902. return partialHide->ourImages[46].bitmap;
  903. }
  904. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  905. {
  906. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  907. return partialHide->ourImages[47].bitmap;
  908. }
  909. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  910. {
  911. return partialHide->ourImages[32].bitmap; //visible left and right corners
  912. }
  913. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  914. {
  915. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  916. return partialHide->ourImages[48].bitmap;
  917. }
  918. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  919. {
  920. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  921. }
  922. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  923. {
  924. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  925. }
  926. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  927. {
  928. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  929. }
  930. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  931. {
  932. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  933. }
  934. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  935. {
  936. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  937. return partialHide->ourImages[49].bitmap;
  938. }
  939. if(d2 && d6 && !d4 && !d8 && d7)
  940. {
  941. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  942. }
  943. else if(!d2 && d6 && !d4 && d8 && d1)
  944. {
  945. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  946. }
  947. else if(!d2 && !d6 && d4 && d8 && d3)
  948. {
  949. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  950. return partialHide->ourImages[50].bitmap;
  951. }
  952. else if(d2 && !d6 && d4 && !d8 && d9)
  953. {
  954. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  955. return partialHide->ourImages[51].bitmap;
  956. }
  957. //newly added
  958. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  959. {
  960. return partialHide->ourImages[0].bitmap;
  961. }
  962. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  963. {
  964. return partialHide->ourImages[1].bitmap;
  965. }
  966. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  967. {
  968. return partialHide->ourImages[4].bitmap;
  969. }
  970. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  971. {
  972. return partialHide->ourImages[5].bitmap;
  973. }
  974. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  975. {
  976. return partialHide->ourImages[36].bitmap;
  977. }
  978. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  979. {
  980. return partialHide->ourImages[36].bitmap;
  981. }
  982. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  983. {
  984. return partialHide->ourImages[2].bitmap;
  985. }
  986. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  987. {
  988. return partialHide->ourImages[3].bitmap;
  989. }
  990. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  991. }
  992. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  993. {
  994. int ret=-1;
  995. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  996. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  997. else 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][a.y][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[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1001. else
  1002. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  1003. if(!(a.x==b.x || a.y==b.y))
  1004. ret*=1.41421;
  1005. //TODO: use hero's pathfinding skill during calculating cost
  1006. return ret;
  1007. }
  1008. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1009. //{
  1010. // std::vector < CGObjectInstance * > ret;
  1011. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1012. // {
  1013. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1014. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1015. // ret.push_back(curi);
  1016. // }
  1017. // return ret;
  1018. //}
  1019. std::string CMapHandler::getDefName(int id, int subid)
  1020. {
  1021. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1022. if(temp)
  1023. return temp->name;
  1024. #ifndef __GNUC__
  1025. throw new std::exception("Def not found.");
  1026. #else
  1027. throw new std::exception();
  1028. #endif
  1029. }
  1030. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1031. {
  1032. CDefHandler * curd = obj->defInfo->handler;
  1033. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1034. {
  1035. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1036. {
  1037. SDL_Rect cr;
  1038. cr.w = 32;
  1039. cr.h = 32;
  1040. cr.x = fx*32;
  1041. cr.y = fy*32;
  1042. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1043. 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)
  1044. {
  1045. TerrainTile2 & curt =
  1046. ttiles
  1047. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1048. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1049. [obj->pos.z];
  1050. 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);
  1051. }
  1052. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1053. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1054. return true;
  1055. }
  1056. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1057. {
  1058. CDefHandler * curd = obj->defInfo->handler;
  1059. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1060. {
  1061. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1062. {
  1063. 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)
  1064. {
  1065. 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;
  1066. for(int dd=0; dd<ctile.size(); ++dd)
  1067. {
  1068. if(ctile[dd].first->id==obj->id)
  1069. ctile.erase(ctile.begin() + dd);
  1070. }
  1071. }
  1072. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1073. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1074. return true;
  1075. }
  1076. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1077. {
  1078. hideObject(obj);
  1079. return true;
  1080. }
  1081. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1082. {
  1083. if(isMoving)
  1084. {
  1085. switch(dir)
  1086. {
  1087. case 1:
  1088. return 10;
  1089. case 2:
  1090. return 5;
  1091. case 3:
  1092. return 6;
  1093. case 4:
  1094. return 7;
  1095. case 5:
  1096. return 8;
  1097. case 6:
  1098. return 9;
  1099. case 7:
  1100. return 12;
  1101. case 8:
  1102. return 11;
  1103. default:
  1104. #ifndef __GNUC__
  1105. throw std::exception("Something very wrong1.");
  1106. #else
  1107. throw std::exception();
  1108. #endif
  1109. }
  1110. }
  1111. else //if(isMoving)
  1112. {
  1113. switch(dir)
  1114. {
  1115. case 1:
  1116. return 13;
  1117. case 2:
  1118. return 0;
  1119. case 3:
  1120. return 1;
  1121. case 4:
  1122. return 2;
  1123. case 5:
  1124. return 3;
  1125. case 6:
  1126. return 4;
  1127. case 7:
  1128. return 15;
  1129. case 8:
  1130. return 14;
  1131. default:
  1132. #ifndef __GNUC__
  1133. throw std::exception("Something very wrong2.");
  1134. #else
  1135. throw std::exception();
  1136. #endif
  1137. }
  1138. }
  1139. }
  1140. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1141. {
  1142. if(ext)
  1143. {
  1144. if(val->x<0)
  1145. {
  1146. val->w += val->x;
  1147. val->x = ext->x;
  1148. }
  1149. else
  1150. {
  1151. val->x += ext->x;
  1152. }
  1153. if(val->y<0)
  1154. {
  1155. val->h += val->y;
  1156. val->y = ext->y;
  1157. }
  1158. else
  1159. {
  1160. val->y += ext->y;
  1161. }
  1162. if(val->x+val->w > ext->x+ext->w)
  1163. {
  1164. val->w = ext->x+ext->w-val->x;
  1165. }
  1166. if(val->y+val->h > ext->y+ext->h)
  1167. {
  1168. val->h = ext->y+ext->h-val->y;
  1169. }
  1170. //for sign problems
  1171. if(val->h > 20000 || val->w > 20000)
  1172. {
  1173. val->h = val->w = 0;
  1174. }
  1175. }
  1176. }
  1177. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1178. {
  1179. if(a.z!=b.z)
  1180. return -1; //error!
  1181. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1182. {
  1183. return 0;
  1184. }
  1185. else if(a.x==b.x && a.y==b.y+1) //t
  1186. {
  1187. return 1;
  1188. }
  1189. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1190. {
  1191. return 2;
  1192. }
  1193. else if(a.x==b.x-1 && a.y==b.y) //r
  1194. {
  1195. return 3;
  1196. }
  1197. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1198. {
  1199. return 4;
  1200. }
  1201. else if(a.x==b.x && a.y==b.y-1) //b
  1202. {
  1203. return 5;
  1204. }
  1205. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1206. {
  1207. return 6;
  1208. }
  1209. else if(a.x==b.x+1 && a.y==b.y) //l
  1210. {
  1211. return 7;
  1212. }
  1213. return -2; //shouldn't happen
  1214. }
  1215. void CMapHandler::loadDefs()
  1216. {
  1217. std::set<int> loadedTypes;
  1218. for (int i=0; i<map->width; i++)
  1219. {
  1220. for (int j=0; j<map->width; j++)
  1221. {
  1222. if (loadedTypes.find(map->terrain[i][j][0].tertype)==loadedTypes.end())
  1223. {
  1224. CDefHandler *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j][0].tertype).c_str());
  1225. loadedTypes.insert(map->terrain[i][j][0].tertype);
  1226. defs.push_back(sdh);
  1227. }
  1228. if (map->twoLevel && loadedTypes.find(map->terrain[i][j][1].tertype)==loadedTypes.end())
  1229. {
  1230. CDefHandler *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j][1].tertype).c_str());
  1231. loadedTypes.insert(map->terrain[i][j][1].tertype);
  1232. defs.push_back(sdh);
  1233. }
  1234. }
  1235. }
  1236. }