mapHandler.cpp 39 KB

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