mapHandler.cpp 46 KB

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