mapHandler.cpp 43 KB

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