mapHandler.cpp 42 KB

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