mapHandler.cpp 43 KB

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