mapHandler.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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, frameW, frameW);
  201. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  202. {
  203. ttiles[i].resize(CGI->mh->map->height, frameH, frameH);
  204. }
  205. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  206. {
  207. for (int j=0-frameH;j<(int)CGI->mh->map->height+frameH;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-frameW; i<map->width+frameW; i++) //jest po szeroko�ci
  294. {
  295. for (int j=0-frameH; j<map->height+frameH;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() - frameW
  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() - frameH
  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()-frameW; ++ix)
  395. {
  396. for(int iy=0; iy<ttiles[0].size()-frameH; ++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 visible terrain. TODO: change settings.txt to add that
  453. // infor and remove the old stuff in conf file (tilesW, tileH,
  454. // TrimX, TrimY)
  455. mapW = conf.go()->ac.tilesW * 32 - conf.go()->ac.advmapTrimX;
  456. mapH = conf.go()->ac.tilesH * 32 - conf.go()->ac.advmapTrimY;
  457. // Total number of visible tiles. Substract the center tile, then
  458. // compute the number of tiles on each side, and reassemble.
  459. int t1, t2;
  460. t1 = (mapW-32)/2;
  461. t2 = mapW - 32 - t1;
  462. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  463. t1 = (mapH-32)/2;
  464. t2 = mapH - 32 - t1;
  465. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  466. // Size of the frame around the map. In extremes positions, the
  467. // frame must not be on the center of the map, but right on the
  468. // edge of the center tile.
  469. frameW = (mapW+31) /32 / 2;
  470. frameH = (mapH+31) /32 / 2;
  471. offsetX = (mapW - (2*frameW+1)*32)/2;
  472. offsetY = (mapH - (2*frameH+1)*32)/2;
  473. std::ifstream ifs("config/townsDefs.txt");
  474. int ccc;
  475. ifs>>ccc;
  476. for(int i=0;i<ccc*2;i++)
  477. {
  478. CGDefInfo *n;
  479. if(i<ccc)
  480. {
  481. n = CGI->state->villages[i];
  482. map->defy.push_back(CGI->state->forts[i]);
  483. }
  484. else
  485. n = CGI->state->capitols[i%ccc];
  486. ifs >> n->name;
  487. if(!n)
  488. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  489. else
  490. map->defy.push_back(n);
  491. }
  492. tlog0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  493. for(int i=0;i<map->heroes.size();i++)
  494. {
  495. if(!map->heroes[i]->defInfo->handler)
  496. {
  497. initHeroDef(map->heroes[i]);
  498. }
  499. }
  500. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  501. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  502. for(int i=0;i<PLAYER_LIMIT;i++)
  503. {
  504. for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  505. {
  506. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  507. }
  508. }
  509. tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  510. for(size_t h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  511. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  512. tlog0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  513. prepareFOWDefs();
  514. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  515. borderAndTerrainBitmapInit();
  516. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  517. initObjectRects();
  518. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  519. }
  520. // Update map window screen
  521. // top_tile top left tile to draw. Not necessarily visible.
  522. // extRect, extRect = map window on screen
  523. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  524. void CMapHandler::terrainRect(int3 top_tile, 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)
  525. {
  526. // Width and height of the portion of the map to process. Units in tiles.
  527. unsigned int dx = tilesW;
  528. unsigned int dy = tilesH;
  529. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  530. SDL_Rect rtile = { 0, 0, 32, 32 };
  531. // Absolute coords of the first pixel in the top left corner
  532. int srx_init = offsetX + extRect->x;
  533. int sry_init = offsetY + extRect->y;
  534. int srx, sry; // absolute screen coordinates in pixels
  535. // If moving, we need to add an extra column/line
  536. if (moveX != 0) {
  537. dx++;
  538. srx_init += moveX;
  539. if (moveX < 0) {
  540. // Moving left, so adjust our referential
  541. top_tile.x --;
  542. srx_init -= 32;
  543. }
  544. }
  545. if (moveY != 0) {
  546. dy++;
  547. sry_init += moveY;
  548. if (moveY < 0) {
  549. // Moving up, so adjust our referential
  550. top_tile.y --;
  551. sry_init -= 32;
  552. }
  553. }
  554. // Reduce sizes if we go out of the full map.
  555. if (top_tile.x < -frameW)
  556. top_tile.x = -frameW;
  557. if (top_tile.y < -frameH)
  558. top_tile.y = -frameH;
  559. if (top_tile.x + dx > map->width + frameW)
  560. dx = map->width + frameW - top_tile.x;
  561. if (top_tile.y + dy > map->height + frameH)
  562. dy = map->height + frameH - top_tile.y;
  563. if(!otherHeroAnim)
  564. heroAnim = anim; //the same, as it should be
  565. SDL_Rect prevClip;
  566. SDL_GetClipRect(extSurf, &prevClip);
  567. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  568. // Temp surface for rotating tile and then properly blit. The
  569. // problem is that blitWithRotate1clip & co do not respect the
  570. // previous SDL_SetClipRect.
  571. // TODO: optimize by pre-rotating those surfaces
  572. SDL_Surface *rSurf = CSDL_Ext::newSurface(32, 32, extSurf);
  573. // printing terrain
  574. srx = srx_init;
  575. for (int bx = 0; bx < dx; bx++, srx+=32)
  576. {
  577. // Skip column if not in map
  578. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  579. continue;
  580. sry = sry_init;
  581. for (int by=0; by < dy; by++, sry+=32)
  582. {
  583. // Skip tile if not in map
  584. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  585. continue;
  586. const TerrainTile2 & tile = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z];
  587. SDL_Rect sr;
  588. sr.x=srx;
  589. sr.y=sry;
  590. sr.h=sr.w=32;
  591. if(tile.terbitmap)
  592. {
  593. SDL_BlitSurface(tile.terbitmap, &genRect(sr.h, sr.w, 0, 0), extSurf, &sr);
  594. }
  595. else
  596. {
  597. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  598. {
  599. case 0:
  600. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  601. &rtile, extSurf, &sr);
  602. break;
  603. case 1:
  604. CSDL_Ext::blitWithRotate1clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  605. &rtile, rSurf, &rtile);
  606. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  607. break;
  608. case 2:
  609. CSDL_Ext::blitWithRotate2clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  610. &rtile, rSurf, &rtile);
  611. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  612. break;
  613. default:
  614. CSDL_Ext::blitWithRotate3clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  615. &rtile, rSurf, &rtile);
  616. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  617. break;
  618. }
  619. }
  620. }
  621. }
  622. // terrain printed
  623. // printing rivers
  624. srx = srx_init;
  625. for (int bx = 0; bx<dx; bx++, srx+=32)
  626. {
  627. // Skip column if not in map
  628. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  629. continue;
  630. sry = sry_init;
  631. for (int by= 0; by<dy; by++, sry+=32)
  632. {
  633. // Skip tile if not in map
  634. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  635. continue;
  636. SDL_Rect sr;
  637. sr.x=srx;
  638. sr.y=sry;
  639. sr.h=sr.w=32;
  640. const std::vector<SDL_Surface *> &rivbitmap = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].rivbitmap;
  641. if(rivbitmap.size())
  642. {
  643. CSDL_Ext::blit8bppAlphaTo24bpp(rivbitmap[anim%rivbitmap.size()],&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  644. }
  645. }
  646. }
  647. // rivers printed
  648. // printing roads
  649. srx = srx_init;
  650. for (int bx = 0; bx < dx; bx++, srx+=32)
  651. {
  652. // Skip column if not in map
  653. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  654. continue;
  655. sry = sry_init + 16;
  656. for (int by = 0; by < dy; by++, sry+=32)
  657. {
  658. // Skip tile if not in map
  659. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  660. continue;
  661. SDL_Rect sr;
  662. sr.x = srx;
  663. sr.y = sry;
  664. sr.h=sr.w=32;
  665. const std::vector<SDL_Surface *> &roadbitmap = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].roadbitmap;
  666. if(roadbitmap.size())
  667. {
  668. CSDL_Ext::blit8bppAlphaTo24bpp(roadbitmap[anim%roadbitmap.size()], &genRect(sr.h, sr.w, 0, (by==-1 && moveY == 0 ? 16 : 0)),extSurf,&sr);
  669. }
  670. }
  671. }
  672. // roads printed
  673. // printing objects
  674. srx = srx_init;
  675. for (int bx = 0; bx<dx; bx++, srx+=32)
  676. {
  677. // Skip column if not in map
  678. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  679. continue;
  680. sry = sry_init;
  681. for (int by = 0; by<dy; by++, sry+=32)
  682. {
  683. // Skip tile if not in map
  684. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  685. continue;
  686. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects =
  687. ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].objects;
  688. for(int h=0; h < objects.size(); ++h)
  689. {
  690. SDL_Rect sr;
  691. sr.x = srx;
  692. sr.y = sry;
  693. sr.w = sr.h = 32;
  694. SDL_Rect pp = objects[h].second;
  695. pp.h = sr.h;
  696. pp.w = sr.w;
  697. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(objects[h].first));
  698. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  699. {
  700. int imgVal = 8;
  701. SDL_Surface * tb;
  702. if(themp->type==NULL)
  703. continue;
  704. std::vector<Cimage> & iv = graphics->heroAnims[themp->type->heroType]->ourImages;
  705. size_t gg;
  706. for(gg=0; gg<iv.size(); ++gg)
  707. {
  708. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  709. {
  710. tb = iv[gg+heroAnim%imgVal].bitmap;
  711. break;
  712. }
  713. }
  714. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr);
  715. pp.y+=imgVal*2-32;
  716. sr.y-=16;
  717. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, extSurf, &sr);
  718. }
  719. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  720. {
  721. int imgVal = 8;
  722. SDL_Surface * tb;
  723. if(themp->type==NULL)
  724. continue;
  725. std::vector<Cimage> & iv = graphics->heroAnims[themp->type->heroType]->ourImages;
  726. size_t gg;
  727. for(gg=0; gg < iv.size(); ++gg)
  728. {
  729. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  730. {
  731. tb = iv[gg].bitmap;
  732. break;
  733. }
  734. }
  735. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr);
  736. if(themp->pos.x==top_tile.x+bx && themp->pos.y==top_tile.y+by)
  737. {
  738. SDL_Rect bufr = sr;
  739. bufr.x-=2*32;
  740. bufr.y-=1*32;
  741. bufr.h = 64;
  742. bufr.w = 96;
  743. if(bufr.x-extRect->x>-64)
  744. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, extSurf, &bufr);
  745. }
  746. }
  747. else
  748. {
  749. const CGObjectInstance *obj = objects[h].first;
  750. const std::vector<Cimage> &ourImages = obj->defInfo->handler->ourImages;
  751. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  752. //setting appropriate flag color
  753. if(obj->tempOwner<8 || obj->tempOwner==255)
  754. CSDL_Ext::setPlayerColor(bitmap, obj->tempOwner);
  755. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr);
  756. }
  757. }
  758. }
  759. }
  760. // objects printed
  761. // printing shadow
  762. srx = srx_init;
  763. for (int bx = 0; bx<dx; bx++, srx+=32)
  764. {
  765. // Skip column if not in map
  766. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  767. continue;
  768. sry = sry_init;
  769. for (int by = 0; by<dy; by++, sry+=32)
  770. {
  771. // Skip tile if not in map
  772. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  773. continue;
  774. SDL_Rect sr;
  775. sr.x = srx;
  776. sr.y = sry;
  777. sr.h = sr.w = 32;
  778. if (top_tile.x+bx >= 0 &&
  779. top_tile.y+by >= 0 &&
  780. top_tile.x+bx < CGI->mh->map->width &&
  781. top_tile.y+by < CGI->mh->map->height &&
  782. !(*visibilityMap)[top_tile.x+bx][top_tile.y+by][top_tile.z])
  783. {
  784. SDL_Surface * hide = getVisBitmap(top_tile.x+bx, top_tile.y+by, *visibilityMap, top_tile.z);
  785. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &rtile, extSurf, &sr);
  786. }
  787. }
  788. }
  789. // shadow printed
  790. // printing borders
  791. srx = srx_init;
  792. for (int bx = 0; bx < dx; bx++, srx+=32)
  793. {
  794. sry = sry_init;
  795. for (int by = 0; by<dy; by++, sry+=32)
  796. {
  797. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width ||
  798. top_tile.y+by < 0 || top_tile.y+by >= map->height) {
  799. SDL_Rect sr;
  800. sr.x=srx;
  801. sr.y=sry;
  802. sr.h=sr.w=32;
  803. SDL_BlitSurface(ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].terbitmap,
  804. &genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  805. } else {
  806. // TODO: these should be activable by the console
  807. #ifdef MARK_BLOCKED_POSITIONS
  808. if(ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  809. {
  810. SDL_Rect sr;
  811. if(smooth)
  812. {
  813. sr.y=by*32 + moveY;
  814. sr.x=bx*32 + moveX;
  815. sr.h=sr.w=32;
  816. }
  817. else
  818. {
  819. sr.y=by*32;
  820. sr.x=bx*32;
  821. sr.h=sr.w=32;
  822. validateRectTerr(&sr, extRect);
  823. }
  824. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  825. for(int f=0; f<ns->w*ns->h*4; ++f)
  826. {
  827. *((unsigned char*)(ns->pixels) + f) = 128;
  828. }
  829. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  830. SDL_FreeSurface(ns);
  831. }
  832. #endif
  833. #ifdef MARK_VISITABLE_POSITIONS
  834. if(ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  835. {
  836. SDL_Rect sr;
  837. if(smooth)
  838. {
  839. sr.y=by*32 + moveY;
  840. sr.x=bx*32 + moveX;
  841. sr.h=sr.w=32;
  842. }
  843. else
  844. {
  845. sr.y=by*32;
  846. sr.x=bx*32;
  847. sr.h=sr.w=32;
  848. validateRectTerr(&sr, extRect);
  849. }
  850. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  851. for(int f=0; f<ns->w*ns->h*4; ++f)
  852. {
  853. *((unsigned char*)(ns->pixels) + f) = 128;
  854. }
  855. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  856. SDL_FreeSurface(ns);
  857. }
  858. #endif
  859. }
  860. }
  861. }
  862. // borders printed
  863. #if 1
  864. // print grid
  865. // TODO: This option should be activated by the console.
  866. srx = srx_init;
  867. for (int bx = 0; bx < dx; bx++, srx+=32)
  868. {
  869. sry = sry_init;
  870. for (int by = 0; by<dy; by++, sry+=32)
  871. {
  872. SDL_Rect sr;
  873. sr.x=srx;
  874. sr.y=sry;
  875. sr.h=sr.w=32;
  876. const int3 color(0x555555, 0x555555, 0x555555);
  877. if (sr.y >= extRect->y &&
  878. sr.y < extRect->y+extRect->h)
  879. for(int i=0;i<sr.w;i++)
  880. if (sr.x+i >= extRect->x &&
  881. sr.x+i < extRect->x+extRect->w)
  882. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  883. if (sr.x >= extRect->x &&
  884. sr.x < extRect->x+extRect->w)
  885. for(int i=0; i<sr.h;i++)
  886. if (sr.y+i >= extRect->y &&
  887. sr.y+i < extRect->y+extRect->h)
  888. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  889. }
  890. }
  891. // grid
  892. #endif
  893. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  894. delete rSurf;
  895. }
  896. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  897. {
  898. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  899. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  900. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  901. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  902. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  903. d5 = visibilityMap[x][y][lvl], //TODO use me - OMFG
  904. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  905. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  906. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  907. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  908. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  909. {
  910. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  911. }
  912. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  913. {
  914. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  915. }
  916. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  917. {
  918. return partialHide->ourImages[15].bitmap; //visible right top corner
  919. }
  920. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  921. {
  922. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  923. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  924. }
  925. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  926. {
  927. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  928. return partialHide->ourImages[35].bitmap;
  929. }
  930. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  931. {
  932. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  933. return partialHide->ourImages[0].bitmap; //visible top
  934. }
  935. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  936. {
  937. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  938. return partialHide->ourImages[4].bitmap; //visble bottom
  939. }
  940. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  941. {
  942. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  943. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  944. return partialHide->ourImages[36].bitmap;
  945. }
  946. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  947. {
  948. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  949. return partialHide->ourImages[2].bitmap; //visible right
  950. }
  951. else if(d2 && d6 && !d4 && !d8 && !d7)
  952. {
  953. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  954. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  955. }
  956. else if(!d2 && d6 && !d4 && d8 && !d1)
  957. {
  958. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  959. }
  960. else if(!d2 && !d6 && d4 && d8 && !d3)
  961. {
  962. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  963. return partialHide->ourImages[37].bitmap;
  964. }
  965. else if(d2 && !d6 && d4 && !d8 && !d9)
  966. {
  967. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  968. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  969. return partialHide->ourImages[38].bitmap;
  970. }
  971. else if(d2 && d6 && d4 && d8)
  972. {
  973. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  974. }
  975. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  976. {
  977. return partialHide->ourImages[16].bitmap; //visible right corners
  978. }
  979. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  980. {
  981. return partialHide->ourImages[18].bitmap; //visible top corners
  982. }
  983. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  984. {
  985. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  986. return partialHide->ourImages[39].bitmap;
  987. }
  988. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  989. {
  990. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  991. return partialHide->ourImages[40].bitmap;
  992. }
  993. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  994. {
  995. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  996. }
  997. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  998. {
  999. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  1000. return partialHide->ourImages[41].bitmap;
  1001. }
  1002. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  1003. {
  1004. return partialHide->ourImages[19].bitmap; //visible corners without left top
  1005. }
  1006. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  1007. {
  1008. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  1009. }
  1010. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  1011. {
  1012. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  1013. return partialHide->ourImages[42].bitmap;
  1014. }
  1015. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  1016. {
  1017. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  1018. return partialHide->ourImages[43].bitmap;
  1019. }
  1020. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  1021. {
  1022. return partialHide->ourImages[21].bitmap; //visible all corners only
  1023. }
  1024. if(d2 && d6 && d4 && !d8)
  1025. {
  1026. return partialHide->ourImages[6].bitmap; //hidden top
  1027. }
  1028. if(d2 && !d6 && d4 && d8)
  1029. {
  1030. return partialHide->ourImages[7].bitmap; //hidden right
  1031. }
  1032. if(!d2 && d6 && d4 && d8)
  1033. {
  1034. return partialHide->ourImages[8].bitmap; //hidden bottom
  1035. }
  1036. if(d2 && d6 && !d4 && d8)
  1037. {
  1038. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  1039. return partialHide->ourImages[44].bitmap;
  1040. }
  1041. if(!d2 && d6 && d4 && !d8)
  1042. {
  1043. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1044. }
  1045. if(d2 && !d6 && !d4 && d8)
  1046. {
  1047. return partialHide->ourImages[29].bitmap; //hidden left and right
  1048. }
  1049. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  1050. {
  1051. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1052. }
  1053. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  1054. {
  1055. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1056. return partialHide->ourImages[45].bitmap;
  1057. }
  1058. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  1059. {
  1060. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1061. }
  1062. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  1063. {
  1064. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1065. return partialHide->ourImages[46].bitmap;
  1066. }
  1067. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  1068. {
  1069. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1070. return partialHide->ourImages[47].bitmap;
  1071. }
  1072. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  1073. {
  1074. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1075. }
  1076. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  1077. {
  1078. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1079. return partialHide->ourImages[48].bitmap;
  1080. }
  1081. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  1082. {
  1083. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1084. }
  1085. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  1086. {
  1087. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1088. }
  1089. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  1090. {
  1091. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1092. }
  1093. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  1094. {
  1095. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1096. }
  1097. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  1098. {
  1099. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1100. return partialHide->ourImages[49].bitmap;
  1101. }
  1102. if(d2 && d6 && !d4 && !d8 && d7)
  1103. {
  1104. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1105. }
  1106. else if(!d2 && d6 && !d4 && d8 && d1)
  1107. {
  1108. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1109. }
  1110. else if(!d2 && !d6 && d4 && d8 && d3)
  1111. {
  1112. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1113. return partialHide->ourImages[50].bitmap;
  1114. }
  1115. else if(d2 && !d6 && d4 && !d8 && d9)
  1116. {
  1117. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1118. return partialHide->ourImages[51].bitmap;
  1119. }
  1120. //newly added
  1121. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  1122. {
  1123. return partialHide->ourImages[0].bitmap;
  1124. }
  1125. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  1126. {
  1127. return partialHide->ourImages[1].bitmap;
  1128. }
  1129. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  1130. {
  1131. return partialHide->ourImages[4].bitmap;
  1132. }
  1133. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  1134. {
  1135. return partialHide->ourImages[5].bitmap;
  1136. }
  1137. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  1138. {
  1139. return partialHide->ourImages[36].bitmap;
  1140. }
  1141. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  1142. {
  1143. return partialHide->ourImages[36].bitmap;
  1144. }
  1145. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  1146. {
  1147. return partialHide->ourImages[2].bitmap;
  1148. }
  1149. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  1150. {
  1151. return partialHide->ourImages[3].bitmap;
  1152. }
  1153. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1154. }
  1155. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1156. {
  1157. int ret=-1;
  1158. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1159. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1160. else if(a.x>=CGI->mh->map->width && a.y<CGI->mh->map->height)
  1161. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][a.y][a.z].tileInfo->malle];
  1162. else if(a.x<CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1163. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1164. else
  1165. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  1166. if(!(a.x==b.x || a.y==b.y))
  1167. ret*=1.41421;
  1168. //TODO: use hero's pathfinding skill during calculating cost
  1169. return ret;
  1170. }
  1171. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1172. //{
  1173. // std::vector < CGObjectInstance * > ret;
  1174. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1175. // {
  1176. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1177. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1178. // ret.push_back(curi);
  1179. // }
  1180. // return ret;
  1181. //}
  1182. std::string CMapHandler::getDefName(int id, int subid)
  1183. {
  1184. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1185. if(temp)
  1186. return temp->name;
  1187. throw std::string("Def not found.");
  1188. }
  1189. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1190. {
  1191. const SDL_Surface *bitmap = obj->defInfo->handler->ourImages[0].bitmap;
  1192. for(int fx=0; fx<bitmap->w/32; ++fx)
  1193. {
  1194. for(int fy=0; fy<bitmap->h/32; ++fy)
  1195. {
  1196. SDL_Rect cr;
  1197. cr.w = 32;
  1198. cr.h = 32;
  1199. cr.x = fx*32;
  1200. cr.y = fy*32;
  1201. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1202. if((obj->pos.x + fx - bitmap->w/32+1)>=0 && (obj->pos.x + fx - bitmap->w/32+1)<ttiles.size()-frameW && (obj->pos.y + fy - bitmap->h/32+1)>=0 && (obj->pos.y + fy - bitmap->h/32+1)<ttiles[0].size()-frameH)
  1203. {
  1204. TerrainTile2 & curt = //TODO use me
  1205. ttiles
  1206. [obj->pos.x + fx - bitmap->w/32]
  1207. [obj->pos.y + fy - bitmap->h/32]
  1208. [obj->pos.z];
  1209. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  1210. }
  1211. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1212. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1213. return true;
  1214. }
  1215. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1216. {
  1217. CDefHandler * curd = obj->defInfo->handler;
  1218. if(!curd) return false;
  1219. const SDL_Surface *bitmap = curd->ourImages[0].bitmap;
  1220. for(int fx=0; fx<bitmap->w/32; ++fx)
  1221. {
  1222. for(int fy=0; fy<bitmap->h/32; ++fy)
  1223. {
  1224. if((obj->pos.x + fx - bitmap->w/32+1)>=0 && (obj->pos.x + fx - bitmap->w/32+1)<ttiles.size()-frameW && (obj->pos.y + fy - bitmap->h/32+1)>=0 && (obj->pos.y + fy - bitmap->h/32+1)<ttiles[0].size()-frameH)
  1225. {
  1226. 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;
  1227. for(size_t dd=0; dd < ctile.size(); ++dd)
  1228. {
  1229. if(ctile[dd].first->id==obj->id)
  1230. ctile.erase(ctile.begin() + dd);
  1231. }
  1232. }
  1233. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1234. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1235. return true;
  1236. }
  1237. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1238. {
  1239. hideObject(obj);
  1240. return true;
  1241. }
  1242. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1243. {
  1244. if(isMoving)
  1245. {
  1246. switch(dir)
  1247. {
  1248. case 1:
  1249. return 10;
  1250. case 2:
  1251. return 5;
  1252. case 3:
  1253. return 6;
  1254. case 4:
  1255. return 7;
  1256. case 5:
  1257. return 8;
  1258. case 6:
  1259. return 9;
  1260. case 7:
  1261. return 12;
  1262. case 8:
  1263. return 11;
  1264. default:
  1265. throw std::string("Something very wrong1.");
  1266. }
  1267. }
  1268. else //if(isMoving)
  1269. {
  1270. switch(dir)
  1271. {
  1272. case 1:
  1273. return 13;
  1274. case 2:
  1275. return 0;
  1276. case 3:
  1277. return 1;
  1278. case 4:
  1279. return 2;
  1280. case 5:
  1281. return 3;
  1282. case 6:
  1283. return 4;
  1284. case 7:
  1285. return 15;
  1286. case 8:
  1287. return 14;
  1288. default:
  1289. throw std::string("Something very wrong2.");
  1290. }
  1291. }
  1292. }
  1293. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1294. {
  1295. if(ext)
  1296. {
  1297. if(val->x<0)
  1298. {
  1299. val->w += val->x;
  1300. val->x = ext->x;
  1301. }
  1302. else
  1303. {
  1304. val->x += ext->x;
  1305. }
  1306. if(val->y<0)
  1307. {
  1308. val->h += val->y;
  1309. val->y = ext->y;
  1310. }
  1311. else
  1312. {
  1313. val->y += ext->y;
  1314. }
  1315. if(val->x+val->w > ext->x+ext->w)
  1316. {
  1317. val->w = ext->x+ext->w-val->x;
  1318. }
  1319. if(val->y+val->h > ext->y+ext->h)
  1320. {
  1321. val->h = ext->y+ext->h-val->y;
  1322. }
  1323. //for sign problems
  1324. if(val->h > 20000 || val->w > 20000)
  1325. {
  1326. val->h = val->w = 0;
  1327. }
  1328. }
  1329. }
  1330. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1331. {
  1332. if(a.z!=b.z)
  1333. return -1; //error!
  1334. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1335. {
  1336. return 0;
  1337. }
  1338. else if(a.x==b.x && a.y==b.y+1) //t
  1339. {
  1340. return 1;
  1341. }
  1342. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1343. {
  1344. return 2;
  1345. }
  1346. else if(a.x==b.x-1 && a.y==b.y) //r
  1347. {
  1348. return 3;
  1349. }
  1350. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1351. {
  1352. return 4;
  1353. }
  1354. else if(a.x==b.x && a.y==b.y-1) //b
  1355. {
  1356. return 5;
  1357. }
  1358. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1359. {
  1360. return 6;
  1361. }
  1362. else if(a.x==b.x+1 && a.y==b.y) //l
  1363. {
  1364. return 7;
  1365. }
  1366. return -2; //shouldn't happen
  1367. }
  1368. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1369. {
  1370. SDL_Color palette[14];
  1371. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1372. {
  1373. for(int i=0; i<12; ++i)
  1374. {
  1375. palette[(i+1)%12] = terrainGraphics[8][j]->format->palette->colors[229 + i];
  1376. }
  1377. SDL_SetColors(terrainGraphics[8][j],palette,229,12);
  1378. for(int i=0; i<14; ++i)
  1379. {
  1380. palette[(i+1)%14] = terrainGraphics[8][j]->format->palette->colors[242 + i];
  1381. }
  1382. SDL_SetColors(terrainGraphics[8][j],palette,242,14);
  1383. }
  1384. }
  1385. CMapHandler::~CMapHandler()
  1386. {
  1387. delete fullHide;
  1388. delete partialHide;
  1389. for(int i=0; i < map->defy.size(); i++)
  1390. delete map->defy[i];
  1391. for(int i=0; i < roadDefs.size(); i++)
  1392. delete roadDefs[i];
  1393. for(int i=0; i < staticRiverDefs.size(); i++)
  1394. delete staticRiverDefs[i];
  1395. //TODO: delete border graphics
  1396. }
  1397. CMapHandler::CMapHandler()
  1398. {
  1399. mapW = mapH = 0;
  1400. frameW = frameH = 0;
  1401. fullHide = NULL;
  1402. partialHide = NULL;
  1403. }
  1404. TerrainTile2::TerrainTile2()
  1405. :tileInfo(0),terbitmap(0)
  1406. {}