mapHandler.cpp 43 KB

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