mapHandler.cpp 44 KB

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