mapHandler.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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]->ID==34 && static_cast<CGHeroInstance*>(map->objects[f])->inTownGarrison)
  345. || !map->objects[f]->defInfo)
  346. {
  347. continue;
  348. }
  349. CDefHandler * curd = map->objects[f]->defInfo->handler;
  350. if(curd)
  351. {
  352. for(int fx=0; fx<curd->ourImages[0].bitmap->w>>5; ++fx) //curd->ourImages[0].bitmap->w/32
  353. {
  354. for(int fy=0; fy<curd->ourImages[0].bitmap->h>>5; ++fy) //curd->ourImages[0].bitmap->h/32
  355. {
  356. SDL_Rect cr;
  357. cr.w = 32;
  358. cr.h = 32;
  359. cr.x = fx<<5; //fx*32
  360. cr.y = fy<<5; //fy*32
  361. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(map->objects[f],cr);
  362. 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)
  363. {
  364. //TerrainTile2 & curt =
  365. // ttiles
  366. // [map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  367. //[map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  368. //[map->objects[f]->pos.z];
  369. 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);
  370. }
  371. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  372. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  373. }//if curd
  374. } // for(int f=0; f<map->objects.size(); ++f)
  375. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  376. {
  377. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  378. {
  379. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  380. {
  381. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  382. }
  383. }
  384. }
  385. }
  386. void processDef (CGDefInfo* def)
  387. {
  388. if(def->id == 26)
  389. return;
  390. def->handler=CDefHandler::giveDef(def->name);
  391. def->width = def->handler->ourImages[0].bitmap->w/32;
  392. def->height = def->handler->ourImages[0].bitmap->h/32;
  393. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  394. if(pom && def->id!=98)
  395. {
  396. pom->handler = def->handler;
  397. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  398. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  399. }
  400. else if(def->id != 34 && def->id != 98)
  401. tlog3 << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  402. if(!def->handler->alphaTransformed)
  403. {
  404. for(size_t yy=0; yy < def->handler->ourImages.size(); ++yy)
  405. {
  406. def->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  407. def->handler->alphaTransformed = true;
  408. }
  409. }
  410. }
  411. void CMapHandler::initHeroDef(CGHeroInstance * h)
  412. {
  413. h->defInfo->handler = graphics->flags1[0];
  414. h->defInfo->width = h->defInfo->handler->ourImages[0].bitmap->w/32;
  415. h->defInfo->height = h->defInfo->handler->ourImages[0].bitmap->h/32;
  416. }
  417. void CMapHandler::init()
  418. {
  419. timeHandler th;
  420. th.getDif();
  421. std::ifstream ifs("config/townsDefs.txt");
  422. int ccc;
  423. ifs>>ccc;
  424. for(int i=0;i<ccc*2;i++)
  425. {
  426. CGDefInfo *n;
  427. if(i<ccc)
  428. {
  429. n = CGI->state->villages[i];
  430. map->defy.push_back(CGI->state->forts[i]);
  431. }
  432. else
  433. n = CGI->state->capitols[i%ccc];
  434. ifs >> n->name;
  435. if(!n)
  436. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  437. else
  438. map->defy.push_back(n);
  439. }
  440. tlog0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  441. for(int i=0;i<map->heroes.size();i++)
  442. {
  443. if(!map->heroes[i]->defInfo->handler)
  444. {
  445. initHeroDef(map->heroes[i]);
  446. }
  447. }
  448. //for(int i=0; i<map->defy.size(); i++)
  449. //{
  450. // map->defy[i]->serial = i;
  451. // processDef(map->defy[i]);
  452. //}
  453. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  454. //std::for_each(map->defs.begin(),map->defs.end(),processDef); //and non-h3m defs
  455. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  456. for(int i=0;i<PLAYER_LIMIT;i++)
  457. {
  458. for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  459. {
  460. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  461. }
  462. }
  463. tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  464. for(size_t h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  465. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  466. tlog0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  467. prepareFOWDefs();
  468. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  469. borderAndTerrainBitmapInit();
  470. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  471. initObjectRects();
  472. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  473. }
  474. 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)
  475. {
  476. if(!otherHeroAnim)
  477. heroAnim = anim; //the same, as it should be
  478. //setting surface to blit at
  479. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  480. if(extSurf)
  481. {
  482. su = extSurf;
  483. }
  484. else
  485. {
  486. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  487. }
  488. SDL_Rect prevClip;
  489. SDL_GetClipRect(su, &prevClip);
  490. if(extRect) SDL_SetClipRect(su, extRect); //preventing blitting outside of that rect
  491. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  492. throw new std::string("terrainRect: out of range");
  493. ////printing terrain
  494. for (int bx=0; bx<dx; bx++)
  495. {
  496. for (int by=0; by<dy; by++)
  497. {
  498. const TerrainTile2 & tile = ttiles[x+bx][y+by][level];
  499. SDL_Rect sr;
  500. sr.y=by*32;
  501. sr.x=bx*32;
  502. sr.h=sr.w=32;
  503. validateRectTerr(&sr, extRect);
  504. if(tile.terbitmap)
  505. {
  506. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  507. }
  508. else
  509. {
  510. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  511. {
  512. case 0:
  513. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  514. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  515. break;
  516. case 1:
  517. CSDL_Ext::blitWithRotate1(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  518. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  519. break;
  520. case 2:
  521. CSDL_Ext::blitWithRotate2(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  522. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  523. break;
  524. default:
  525. CSDL_Ext::blitWithRotate3(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  526. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  527. break;
  528. }
  529. }
  530. }
  531. }
  532. ////terrain printed
  533. ////printing rivers
  534. for (int bx=0; bx<dx; bx++)
  535. {
  536. for (int by=0; by<dy; by++)
  537. {
  538. SDL_Rect sr;
  539. sr.y=by*32;
  540. sr.x=bx*32;
  541. sr.h=sr.w=32;
  542. validateRectTerr(&sr, extRect);
  543. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  544. {
  545. 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);
  546. }
  547. }
  548. }
  549. ////rivers printed
  550. ////printing roads
  551. for (int bx=0; bx<dx; bx++)
  552. {
  553. for (int by=-1; by<dy; by++)
  554. {
  555. if(y+by<=-4)
  556. continue;
  557. SDL_Rect sr;
  558. sr.y=by*32+16;
  559. sr.x=bx*32;
  560. sr.h=sr.w=32;
  561. validateRectTerr(&sr, extRect);
  562. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  563. {
  564. 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);
  565. }
  566. }
  567. }
  568. ////roads printed
  569. ////printing objects
  570. for (int bx=0; bx<dx; bx++)
  571. {
  572. for (int by=0; by<dy; by++)
  573. {
  574. for(int h=0; h < ttiles[x+bx][y+by][level].objects.size(); ++h)
  575. {
  576. SDL_Rect sr;
  577. sr.w = 32;
  578. sr.h = 32;
  579. sr.x = (bx)*32;
  580. sr.y = (by)*32;
  581. validateRectTerr(&sr, extRect);
  582. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  583. pp.h = sr.h;
  584. pp.w = sr.w;
  585. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  586. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  587. {
  588. int imgVal = 8;
  589. SDL_Surface * tb;
  590. if(themp->type==NULL)
  591. continue;
  592. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  593. size_t gg;
  594. for(gg=0; gg<iv.size(); ++gg)
  595. {
  596. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  597. {
  598. tb = iv[gg+heroAnim%imgVal].bitmap;
  599. break;
  600. }
  601. }
  602. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  603. pp.y+=imgVal*2-32;
  604. sr.y-=16;
  605. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  606. }
  607. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  608. {
  609. int imgVal = 8;
  610. SDL_Surface * tb;
  611. if(themp->type==NULL)
  612. continue;
  613. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  614. size_t gg;
  615. for(gg=0; gg < iv.size(); ++gg)
  616. {
  617. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  618. {
  619. tb = iv[gg].bitmap;
  620. break;
  621. }
  622. }
  623. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  624. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  625. {
  626. SDL_Rect bufr = sr;
  627. bufr.x-=2*32;
  628. bufr.y-=1*32;
  629. bufr.h = 64;
  630. bufr.w = 96;
  631. if(bufr.x-extRect->x>-64)
  632. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  633. }
  634. }
  635. else
  636. {
  637. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  638. int phaseShift = ttiles[x+bx][y+by][level].objects[h].first->animPhaseShift;
  639. //setting appropriate flag color
  640. 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)
  641. 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);
  642. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[(anim+phaseShift)%imgVal].bitmap,&pp,su,&sr);
  643. }
  644. }
  645. }
  646. }
  647. ////objects printed, printing shadow
  648. for (int bx=0; bx<dx; bx++)
  649. {
  650. for (int by=0; by<dy; by++)
  651. {
  652. SDL_Rect sr;
  653. sr.y=by*32;
  654. sr.x=bx*32;
  655. sr.h=sr.w=32;
  656. validateRectTerr(&sr, extRect);
  657. 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])
  658. {
  659. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  660. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  661. }
  662. }
  663. }
  664. ////shadow printed
  665. //printing borders
  666. for (int bx=0; bx<dx; bx++)
  667. {
  668. for (int by=0; by<dy; by++)
  669. {
  670. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  671. {
  672. SDL_Rect sr;
  673. sr.y=by*32;
  674. sr.x=bx*32;
  675. sr.h=sr.w=32;
  676. validateRectTerr(&sr, extRect);
  677. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  678. }
  679. else
  680. {
  681. #ifdef MARK_BLOCKED_POSITIONS
  682. if(ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  683. {
  684. SDL_Rect sr;
  685. sr.y=by*32;
  686. sr.x=bx*32;
  687. sr.h=sr.w=32;
  688. validateRectTerr(&sr, extRect);
  689. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  690. for(int f=0; f<ns->w*ns->h*4; ++f)
  691. {
  692. *((unsigned char*)(ns->pixels) + f) = 128;
  693. }
  694. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  695. SDL_FreeSurface(ns);
  696. }
  697. #endif
  698. #ifdef MARK_VISITABLE_POSITIONS
  699. if(ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  700. {
  701. SDL_Rect sr;
  702. sr.y=by*32;
  703. sr.x=bx*32;
  704. sr.h=sr.w=32;
  705. validateRectTerr(&sr, extRect);
  706. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  707. for(int f=0; f<ns->w*ns->h*4; ++f)
  708. {
  709. *((unsigned char*)(ns->pixels) + f) = 128;
  710. }
  711. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  712. SDL_FreeSurface(ns);
  713. }
  714. #endif
  715. }
  716. }
  717. }
  718. SDL_SetClipRect(su, &prevClip); //restoring clip_rect
  719. //borders printed
  720. return su;
  721. }
  722. SDL_Surface * CMapHandler::terrainRectSmooth(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)
  723. {
  724. if(!otherHeroAnim)
  725. heroAnim = anim; //the same, as it should be
  726. //setting surface to blit at
  727. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  728. if(extSurf)
  729. {
  730. su = extSurf;
  731. }
  732. else
  733. {
  734. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  735. }
  736. SDL_Rect prevClip;
  737. SDL_GetClipRect(su, &prevClip);
  738. if(extRect) SDL_SetClipRect(su, extRect); //preventing blitting outside of that rect
  739. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  740. throw new std::string("terrainRect: out of range");
  741. ////printing terrain
  742. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  743. {
  744. for (int by=(moveY <= 0 ? 0 : -1); by<dy+1; by++)
  745. {
  746. const TerrainTile2 & tile = ttiles[x+bx][y+by][level];
  747. SDL_Rect sr;
  748. sr.y=by*32 + moveY + extRect->y;
  749. sr.x=bx*32 + moveX + extRect->x;
  750. sr.h=sr.w=32;
  751. //validateRectTerr(&sr, extRect);
  752. if(tile.terbitmap)
  753. {
  754. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  755. }
  756. else
  757. {
  758. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  759. {
  760. case 0:
  761. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  762. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  763. break;
  764. case 1:
  765. CSDL_Ext::blitWithRotate1clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  766. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  767. break;
  768. case 2:
  769. CSDL_Ext::blitWithRotate2clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  770. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  771. break;
  772. default:
  773. CSDL_Ext::blitWithRotate3clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  774. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  775. break;
  776. }
  777. }
  778. }
  779. }
  780. ////terrain printed
  781. ////printing rivers
  782. for (int bx=(moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  783. {
  784. for (int by=(moveY <= 0 ? 0 : -1); by<dy+1; by++)
  785. {
  786. SDL_Rect sr;
  787. sr.y=by*32 + moveY + extRect->y;
  788. sr.x=bx*32 + moveX + extRect->x;
  789. sr.h=sr.w=32;
  790. //validateRectTerr(&sr, extRect);
  791. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  792. {
  793. 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);
  794. }
  795. }
  796. }
  797. ////rivers printed
  798. ////printing roads
  799. for (int bx=(moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  800. {
  801. for (int by=(moveY <= 0 ? 0 : -1) - 1; by<dy+1; by++)
  802. {
  803. if(y+by<=-4)
  804. continue;
  805. SDL_Rect sr;
  806. sr.y=by*32+16 + moveY + extRect->y;
  807. sr.x=bx*32 + moveX + extRect->x;
  808. sr.h=sr.w=32;
  809. //validateRectTerr(&sr, extRect);
  810. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  811. {
  812. 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);
  813. }
  814. }
  815. }
  816. ////roads printed
  817. ////printing objects
  818. for (int bx=(moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  819. {
  820. for (int by=(moveY <= 0 ? 0 : -1); by<dy+1; by++)
  821. {
  822. for(int h=0; h < ttiles[x+bx][y+by][level].objects.size(); ++h)
  823. {
  824. SDL_Rect sr;
  825. sr.w = 32;
  826. sr.h = 32;
  827. sr.x = (bx)*32 + moveX + extRect->x;
  828. sr.y = (by)*32 + moveY + extRect->y;
  829. //validateRectTerr(&sr, extRect);
  830. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  831. pp.h = sr.h;
  832. pp.w = sr.w;
  833. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  834. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  835. {
  836. int imgVal = 8;
  837. SDL_Surface * tb;
  838. if(themp->type==NULL)
  839. continue;
  840. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  841. size_t gg;
  842. for(gg=0; gg<iv.size(); ++gg)
  843. {
  844. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  845. {
  846. tb = iv[gg+heroAnim%imgVal].bitmap;
  847. break;
  848. }
  849. }
  850. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  851. pp.y+=imgVal*2-32;
  852. sr.y-=16;
  853. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  854. }
  855. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  856. {
  857. int imgVal = 8;
  858. SDL_Surface * tb;
  859. if(themp->type==NULL)
  860. continue;
  861. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  862. size_t gg;
  863. for(gg=0; gg < iv.size(); ++gg)
  864. {
  865. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  866. {
  867. tb = iv[gg].bitmap;
  868. break;
  869. }
  870. }
  871. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  872. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  873. {
  874. SDL_Rect bufr = sr;
  875. bufr.x-=2*32;
  876. bufr.y-=1*32;
  877. bufr.h = 64;
  878. bufr.w = 96;
  879. if(bufr.x-extRect->x>-64)
  880. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  881. }
  882. }
  883. else
  884. {
  885. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  886. int phaseShift = ttiles[x+bx][y+by][level].objects[h].first->animPhaseShift;
  887. //setting appropriate flag color
  888. 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)
  889. 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);
  890. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[(anim+phaseShift)%imgVal].bitmap,&pp,su,&sr);
  891. }
  892. }
  893. }
  894. }
  895. ////objects printed, printing shadow
  896. for (int bx=(moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  897. {
  898. for (int by=(moveY <= 0 ? 0 : -1); by<dy+1; by++)
  899. {
  900. SDL_Rect sr;
  901. sr.y=by*32 + moveY + extRect->y;
  902. sr.x=bx*32 + moveX + extRect->x;
  903. sr.h=sr.w=32;
  904. //validateRectTerr(&sr, extRect);
  905. 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])
  906. {
  907. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  908. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  909. }
  910. }
  911. }
  912. ////shadow printed
  913. //printing borders
  914. for (int bx=(moveX <= 0 ? 0 : -1); bx<dx+1; bx++)
  915. {
  916. for (int by=(moveY <= 0 ? 0 : -1); by<dy+1; by++)
  917. {
  918. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  919. {
  920. SDL_Rect sr;
  921. sr.y=by*32 + moveY + extRect->y;
  922. sr.x=bx*32 + moveX + extRect->x;
  923. sr.h=sr.w=32;
  924. //validateRectTerr(&sr, extRect);
  925. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  926. }
  927. else
  928. {
  929. #ifdef MARK_BLOCKED_POSITIONS
  930. if(ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  931. {
  932. SDL_Rect sr;
  933. sr.y=by*32 + moveY;
  934. sr.x=bx*32 + moveX;
  935. sr.h=sr.w=32;
  936. //validateRectTerr(&sr, extRect);
  937. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  938. for(int f=0; f<ns->w*ns->h*4; ++f)
  939. {
  940. *((unsigned char*)(ns->pixels) + f) = 128;
  941. }
  942. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  943. SDL_FreeSurface(ns);
  944. }
  945. #endif
  946. #ifdef MARK_VISITABLE_POSITIONS
  947. if(ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  948. {
  949. SDL_Rect sr;
  950. sr.y=by*32 + moveY;
  951. sr.x=bx*32 + moveX;
  952. sr.h=sr.w=32;
  953. //validateRectTerr(&sr, extRect);
  954. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  955. for(int f=0; f<ns->w*ns->h*4; ++f)
  956. {
  957. *((unsigned char*)(ns->pixels) + f) = 128;
  958. }
  959. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  960. SDL_FreeSurface(ns);
  961. }
  962. #endif
  963. }
  964. }
  965. }
  966. //borders printed
  967. SDL_SetClipRect(su, &prevClip); //restoring clip_rect
  968. return su;
  969. }
  970. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  971. {
  972. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  973. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  974. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  975. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  976. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  977. d5 = visibilityMap[x][y][lvl], //TODO use me - OMFG
  978. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  979. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  980. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  981. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  982. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  983. {
  984. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  985. }
  986. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  987. {
  988. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  989. }
  990. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  991. {
  992. return partialHide->ourImages[15].bitmap; //visible right top corner
  993. }
  994. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  995. {
  996. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  997. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  998. }
  999. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  1000. {
  1001. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  1002. return partialHide->ourImages[35].bitmap;
  1003. }
  1004. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  1005. {
  1006. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  1007. return partialHide->ourImages[0].bitmap; //visible top
  1008. }
  1009. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  1010. {
  1011. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  1012. return partialHide->ourImages[4].bitmap; //visble bottom
  1013. }
  1014. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  1015. {
  1016. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  1017. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  1018. return partialHide->ourImages[36].bitmap;
  1019. }
  1020. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  1021. {
  1022. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  1023. return partialHide->ourImages[2].bitmap; //visible right
  1024. }
  1025. else if(d2 && d6 && !d4 && !d8 && !d7)
  1026. {
  1027. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1028. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1029. }
  1030. else if(!d2 && d6 && !d4 && d8 && !d1)
  1031. {
  1032. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  1033. }
  1034. else if(!d2 && !d6 && d4 && d8 && !d3)
  1035. {
  1036. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  1037. return partialHide->ourImages[37].bitmap;
  1038. }
  1039. else if(d2 && !d6 && d4 && !d8 && !d9)
  1040. {
  1041. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1042. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1043. return partialHide->ourImages[38].bitmap;
  1044. }
  1045. else if(d2 && d6 && d4 && d8)
  1046. {
  1047. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  1048. }
  1049. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  1050. {
  1051. return partialHide->ourImages[16].bitmap; //visible right corners
  1052. }
  1053. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  1054. {
  1055. return partialHide->ourImages[18].bitmap; //visible top corners
  1056. }
  1057. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  1058. {
  1059. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  1060. return partialHide->ourImages[39].bitmap;
  1061. }
  1062. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  1063. {
  1064. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  1065. return partialHide->ourImages[40].bitmap;
  1066. }
  1067. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  1068. {
  1069. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  1070. }
  1071. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  1072. {
  1073. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  1074. return partialHide->ourImages[41].bitmap;
  1075. }
  1076. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  1077. {
  1078. return partialHide->ourImages[19].bitmap; //visible corners without left top
  1079. }
  1080. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  1081. {
  1082. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  1083. }
  1084. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  1085. {
  1086. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  1087. return partialHide->ourImages[42].bitmap;
  1088. }
  1089. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  1090. {
  1091. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  1092. return partialHide->ourImages[43].bitmap;
  1093. }
  1094. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  1095. {
  1096. return partialHide->ourImages[21].bitmap; //visible all corners only
  1097. }
  1098. if(d2 && d6 && d4 && !d8)
  1099. {
  1100. return partialHide->ourImages[6].bitmap; //hidden top
  1101. }
  1102. if(d2 && !d6 && d4 && d8)
  1103. {
  1104. return partialHide->ourImages[7].bitmap; //hidden right
  1105. }
  1106. if(!d2 && d6 && d4 && d8)
  1107. {
  1108. return partialHide->ourImages[8].bitmap; //hidden bottom
  1109. }
  1110. if(d2 && d6 && !d4 && d8)
  1111. {
  1112. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  1113. return partialHide->ourImages[44].bitmap;
  1114. }
  1115. if(!d2 && d6 && d4 && !d8)
  1116. {
  1117. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1118. }
  1119. if(d2 && !d6 && !d4 && d8)
  1120. {
  1121. return partialHide->ourImages[29].bitmap; //hidden left and right
  1122. }
  1123. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  1124. {
  1125. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1126. }
  1127. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  1128. {
  1129. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1130. return partialHide->ourImages[45].bitmap;
  1131. }
  1132. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  1133. {
  1134. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1135. }
  1136. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  1137. {
  1138. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1139. return partialHide->ourImages[46].bitmap;
  1140. }
  1141. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  1142. {
  1143. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1144. return partialHide->ourImages[47].bitmap;
  1145. }
  1146. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  1147. {
  1148. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1149. }
  1150. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  1151. {
  1152. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1153. return partialHide->ourImages[48].bitmap;
  1154. }
  1155. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  1156. {
  1157. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1158. }
  1159. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  1160. {
  1161. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1162. }
  1163. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  1164. {
  1165. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1166. }
  1167. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  1168. {
  1169. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1170. }
  1171. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  1172. {
  1173. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1174. return partialHide->ourImages[49].bitmap;
  1175. }
  1176. if(d2 && d6 && !d4 && !d8 && d7)
  1177. {
  1178. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1179. }
  1180. else if(!d2 && d6 && !d4 && d8 && d1)
  1181. {
  1182. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1183. }
  1184. else if(!d2 && !d6 && d4 && d8 && d3)
  1185. {
  1186. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1187. return partialHide->ourImages[50].bitmap;
  1188. }
  1189. else if(d2 && !d6 && d4 && !d8 && d9)
  1190. {
  1191. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1192. return partialHide->ourImages[51].bitmap;
  1193. }
  1194. //newly added
  1195. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  1196. {
  1197. return partialHide->ourImages[0].bitmap;
  1198. }
  1199. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  1200. {
  1201. return partialHide->ourImages[1].bitmap;
  1202. }
  1203. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  1204. {
  1205. return partialHide->ourImages[4].bitmap;
  1206. }
  1207. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  1208. {
  1209. return partialHide->ourImages[5].bitmap;
  1210. }
  1211. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  1212. {
  1213. return partialHide->ourImages[36].bitmap;
  1214. }
  1215. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  1216. {
  1217. return partialHide->ourImages[36].bitmap;
  1218. }
  1219. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  1220. {
  1221. return partialHide->ourImages[2].bitmap;
  1222. }
  1223. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  1224. {
  1225. return partialHide->ourImages[3].bitmap;
  1226. }
  1227. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1228. }
  1229. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1230. {
  1231. int ret=-1;
  1232. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1233. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1234. else if(a.x>=CGI->mh->map->width && a.y<CGI->mh->map->height)
  1235. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][a.y][a.z].tileInfo->malle];
  1236. else if(a.x<CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1237. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1238. else
  1239. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  1240. if(!(a.x==b.x || a.y==b.y))
  1241. ret*=1.41421;
  1242. //TODO: use hero's pathfinding skill during calculating cost
  1243. return ret;
  1244. }
  1245. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1246. //{
  1247. // std::vector < CGObjectInstance * > ret;
  1248. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1249. // {
  1250. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1251. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1252. // ret.push_back(curi);
  1253. // }
  1254. // return ret;
  1255. //}
  1256. std::string CMapHandler::getDefName(int id, int subid)
  1257. {
  1258. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1259. if(temp)
  1260. return temp->name;
  1261. #ifndef __GNUC__
  1262. throw new std::exception("Def not found.");
  1263. #else
  1264. throw new std::exception();
  1265. #endif
  1266. }
  1267. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1268. {
  1269. CDefHandler * curd = obj->defInfo->handler;
  1270. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1271. {
  1272. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1273. {
  1274. SDL_Rect cr;
  1275. cr.w = 32;
  1276. cr.h = 32;
  1277. cr.x = fx*32;
  1278. cr.y = fy*32;
  1279. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1280. 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)
  1281. {
  1282. TerrainTile2 & curt = //TODO use me
  1283. ttiles
  1284. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1285. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1286. [obj->pos.z];
  1287. 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);
  1288. }
  1289. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1290. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1291. return true;
  1292. }
  1293. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1294. {
  1295. CDefHandler * curd = obj->defInfo->handler;
  1296. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1297. {
  1298. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1299. {
  1300. 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)
  1301. {
  1302. 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;
  1303. for(size_t dd=0; dd < ctile.size(); ++dd)
  1304. {
  1305. if(ctile[dd].first->id==obj->id)
  1306. ctile.erase(ctile.begin() + dd);
  1307. }
  1308. }
  1309. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1310. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1311. return true;
  1312. }
  1313. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1314. {
  1315. hideObject(obj);
  1316. return true;
  1317. }
  1318. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1319. {
  1320. if(isMoving)
  1321. {
  1322. switch(dir)
  1323. {
  1324. case 1:
  1325. return 10;
  1326. case 2:
  1327. return 5;
  1328. case 3:
  1329. return 6;
  1330. case 4:
  1331. return 7;
  1332. case 5:
  1333. return 8;
  1334. case 6:
  1335. return 9;
  1336. case 7:
  1337. return 12;
  1338. case 8:
  1339. return 11;
  1340. default:
  1341. #ifndef __GNUC__
  1342. throw std::exception("Something very wrong1.");
  1343. #else
  1344. throw std::exception();
  1345. #endif
  1346. }
  1347. }
  1348. else //if(isMoving)
  1349. {
  1350. switch(dir)
  1351. {
  1352. case 1:
  1353. return 13;
  1354. case 2:
  1355. return 0;
  1356. case 3:
  1357. return 1;
  1358. case 4:
  1359. return 2;
  1360. case 5:
  1361. return 3;
  1362. case 6:
  1363. return 4;
  1364. case 7:
  1365. return 15;
  1366. case 8:
  1367. return 14;
  1368. default:
  1369. #ifndef __GNUC__
  1370. throw std::exception("Something very wrong2.");
  1371. #else
  1372. throw std::exception();
  1373. #endif
  1374. }
  1375. }
  1376. }
  1377. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1378. {
  1379. if(ext)
  1380. {
  1381. if(val->x<0)
  1382. {
  1383. val->w += val->x;
  1384. val->x = ext->x;
  1385. }
  1386. else
  1387. {
  1388. val->x += ext->x;
  1389. }
  1390. if(val->y<0)
  1391. {
  1392. val->h += val->y;
  1393. val->y = ext->y;
  1394. }
  1395. else
  1396. {
  1397. val->y += ext->y;
  1398. }
  1399. if(val->x+val->w > ext->x+ext->w)
  1400. {
  1401. val->w = ext->x+ext->w-val->x;
  1402. }
  1403. if(val->y+val->h > ext->y+ext->h)
  1404. {
  1405. val->h = ext->y+ext->h-val->y;
  1406. }
  1407. //for sign problems
  1408. if(val->h > 20000 || val->w > 20000)
  1409. {
  1410. val->h = val->w = 0;
  1411. }
  1412. }
  1413. }
  1414. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1415. {
  1416. if(a.z!=b.z)
  1417. return -1; //error!
  1418. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1419. {
  1420. return 0;
  1421. }
  1422. else if(a.x==b.x && a.y==b.y+1) //t
  1423. {
  1424. return 1;
  1425. }
  1426. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1427. {
  1428. return 2;
  1429. }
  1430. else if(a.x==b.x-1 && a.y==b.y) //r
  1431. {
  1432. return 3;
  1433. }
  1434. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1435. {
  1436. return 4;
  1437. }
  1438. else if(a.x==b.x && a.y==b.y-1) //b
  1439. {
  1440. return 5;
  1441. }
  1442. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1443. {
  1444. return 6;
  1445. }
  1446. else if(a.x==b.x+1 && a.y==b.y) //l
  1447. {
  1448. return 7;
  1449. }
  1450. return -2; //shouldn't happen
  1451. }
  1452. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1453. {
  1454. SDL_Color palette[14];
  1455. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1456. {
  1457. for(int i=0; i<12; ++i)
  1458. {
  1459. palette[(i+1)%12] = terrainGraphics[8][j]->format->palette->colors[229 + i];
  1460. }
  1461. SDL_SetColors(terrainGraphics[8][j],palette,229,12);
  1462. for(int i=0; i<14; ++i)
  1463. {
  1464. palette[(i+1)%14] = terrainGraphics[8][j]->format->palette->colors[242 + i];
  1465. }
  1466. SDL_SetColors(terrainGraphics[8][j],palette,242,14);
  1467. }
  1468. }
  1469. TerrainTile2::TerrainTile2()
  1470. :terbitmap(0),tileInfo(0)
  1471. {}