mapHandler.cpp 49 KB

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