mapHandler.cpp 40 KB

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