mapHandler.cpp 41 KB

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