mapHandler.cpp 47 KB

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