mapHandler.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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. /*
  19. * mapHandler.cpp, part of VCMI engine
  20. *
  21. * Authors: listed in file AUTHORS in main folder
  22. *
  23. * License: GNU General Public License v2.0 or later
  24. * Full text of license available in license.txt file, in main folder
  25. *
  26. */
  27. extern SDL_Surface * screen;
  28. static std::string nameFromType (int typ)
  29. {
  30. switch(static_cast<TerrainTile::EterrainType>(typ))
  31. {
  32. case TerrainTile::dirt:
  33. {
  34. return std::string("DIRTTL.DEF");
  35. break;
  36. }
  37. case TerrainTile::sand:
  38. {
  39. return std::string("SANDTL.DEF");
  40. break;
  41. }
  42. case TerrainTile::grass:
  43. {
  44. return std::string("GRASTL.DEF");
  45. break;
  46. }
  47. case TerrainTile::snow:
  48. {
  49. return std::string("SNOWTL.DEF");
  50. break;
  51. }
  52. case TerrainTile::swamp:
  53. {
  54. return std::string("SWMPTL.DEF");
  55. break;
  56. }
  57. case TerrainTile::rough:
  58. {
  59. return std::string("ROUGTL.DEF");
  60. break;
  61. }
  62. case TerrainTile::subterranean:
  63. {
  64. return std::string("SUBBTL.DEF");
  65. break;
  66. }
  67. case TerrainTile::lava:
  68. {
  69. return std::string("LAVATL.DEF");
  70. break;
  71. }
  72. case TerrainTile::water:
  73. {
  74. return std::string("WATRTL.DEF");
  75. break;
  76. }
  77. case TerrainTile::rock:
  78. {
  79. return std::string("ROCKTL.DEF");
  80. break;
  81. }
  82. case TerrainTile::border:
  83. //TODO use me
  84. break;
  85. default:
  86. //TODO do something here
  87. break;
  88. }
  89. return std::string();
  90. }
  91. struct OCM_HLP
  92. {
  93. bool operator ()(const std::pair<const CGObjectInstance*, SDL_Rect> & a, const std::pair<const CGObjectInstance*, SDL_Rect> & b)
  94. {
  95. return (*a.first)<(*b.first);
  96. }
  97. } ocmptwo ;
  98. static void alphaTransformDef(CGDefInfo * defInfo)
  99. {
  100. SDL_Surface * alphaTransSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 12, 12, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
  101. for(int yy=0;yy<defInfo->handler->ourImages.size();yy++)
  102. {
  103. defInfo->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  104. defInfo->handler->alphaTransformed = true;
  105. }
  106. SDL_FreeSurface(alphaTransSurf);
  107. }
  108. void CMapHandler::prepareFOWDefs()
  109. {
  110. fullHide = CDefHandler::giveDef("TSHRC.DEF");
  111. partialHide = CDefHandler::giveDef("TSHRE.DEF");
  112. //adding necessary rotations
  113. Cimage nw = partialHide->ourImages[22]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  114. partialHide->ourImages.push_back(nw);
  115. nw = partialHide->ourImages[15]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  116. partialHide->ourImages.push_back(nw);
  117. nw = partialHide->ourImages[2]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  118. partialHide->ourImages.push_back(nw);
  119. nw = partialHide->ourImages[13]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  120. partialHide->ourImages.push_back(nw);
  121. nw = partialHide->ourImages[12]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  122. partialHide->ourImages.push_back(nw);
  123. nw = partialHide->ourImages[16]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  124. partialHide->ourImages.push_back(nw);
  125. nw = partialHide->ourImages[18]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  126. partialHide->ourImages.push_back(nw);
  127. nw = partialHide->ourImages[17]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  128. partialHide->ourImages.push_back(nw);
  129. nw = partialHide->ourImages[20]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  130. partialHide->ourImages.push_back(nw);
  131. nw = partialHide->ourImages[19]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  132. partialHide->ourImages.push_back(nw);
  133. nw = partialHide->ourImages[7]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  134. partialHide->ourImages.push_back(nw);
  135. nw = partialHide->ourImages[24]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  136. partialHide->ourImages.push_back(nw);
  137. nw = partialHide->ourImages[26]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  138. partialHide->ourImages.push_back(nw);
  139. nw = partialHide->ourImages[25]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  140. partialHide->ourImages.push_back(nw);
  141. nw = partialHide->ourImages[30]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  142. partialHide->ourImages.push_back(nw);
  143. nw = partialHide->ourImages[32]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  144. partialHide->ourImages.push_back(nw);
  145. nw = partialHide->ourImages[27]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  146. partialHide->ourImages.push_back(nw);
  147. nw = partialHide->ourImages[28]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  148. partialHide->ourImages.push_back(nw);
  149. //necessaary rotations added
  150. for(size_t i=0; i<partialHide->ourImages.size(); ++i)
  151. {
  152. CSDL_Ext::alphaTransform(partialHide->ourImages[i].bitmap);
  153. }
  154. hideBitmap.resize(CGI->mh->map->width);
  155. for (size_t i=0;i<hideBitmap.size();i++)
  156. {
  157. hideBitmap[i].resize(CGI->mh->map->height);
  158. }
  159. for (size_t i=0; i<hideBitmap.size(); ++i)
  160. {
  161. for (int j=0; j < CGI->mh->map->height; ++j)
  162. {
  163. hideBitmap[i][j].resize(CGI->mh->map->twoLevel+1);
  164. for(int k=0; k<CGI->mh->map->twoLevel+1; ++k)
  165. {
  166. hideBitmap[i][j][k] = rand()%fullHide->ourImages.size();
  167. }
  168. }
  169. }
  170. }
  171. void CMapHandler::roadsRiverTerrainInit()
  172. {
  173. //initializing road's and river's DefHandlers
  174. roadDefs.push_back(CDefHandler::giveDef("dirtrd.def"));
  175. roadDefs.push_back(CDefHandler::giveDef("gravrd.def"));
  176. roadDefs.push_back(CDefHandler::giveDef("cobbrd.def"));
  177. staticRiverDefs.push_back(CDefHandler::giveDef("clrrvr.def"));
  178. staticRiverDefs.push_back(CDefHandler::giveDef("icyrvr.def"));
  179. staticRiverDefs.push_back(CDefHandler::giveDef("mudrvr.def"));
  180. staticRiverDefs.push_back(CDefHandler::giveDef("lavrvr.def"));
  181. for(size_t g=0; g<staticRiverDefs.size(); ++g)
  182. {
  183. for(size_t h=0; h < staticRiverDefs[g]->ourImages.size(); ++h)
  184. {
  185. CSDL_Ext::alphaTransform(staticRiverDefs[g]->ourImages[h].bitmap);
  186. }
  187. }
  188. for(size_t g=0; g<roadDefs.size(); ++g)
  189. {
  190. for(size_t h=0; h < roadDefs[g]->ourImages.size(); ++h)
  191. {
  192. CSDL_Ext::alphaTransform(roadDefs[g]->ourImages[h].bitmap);
  193. }
  194. }
  195. sizes.x = CGI->mh->map->width;
  196. sizes.y = CGI->mh->map->height;
  197. sizes.z = CGI->mh->map->twoLevel+1;
  198. ttiles.resize(CGI->mh->map->width,Woff);
  199. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  200. {
  201. ttiles[i].resize(CGI->mh->map->height,Hoff);
  202. }
  203. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  204. {
  205. for (int j=0-Hoff;j<(int)CGI->mh->map->height+Hoff;j++)
  206. ttiles[i][j].resize(CGI->mh->map->twoLevel+1,0);
  207. }
  208. for (int i=0; i<map->width; i++) //jest po szeroko�ci
  209. {
  210. for (int j=0; j<map->height;j++) //po wysoko�ci
  211. {
  212. for (int k=0; k<=map->twoLevel; ++k)
  213. {
  214. TerrainTile2 &pom(ttiles[i][j][k]);
  215. pom.pos = int3(i, j, k);
  216. pom.tileInfo = &(map->terrain[i][j][k]);
  217. if(pom.tileInfo->malle)
  218. {
  219. int cDir;
  220. bool rotV, rotH;
  221. int roadpom = pom.tileInfo->malle-1,
  222. impom = pom.tileInfo->roadDir;
  223. SDL_Surface *pom1 = roadDefs[roadpom]->ourImages[impom].bitmap;
  224. ttiles[i][j][k].roadbitmap.push_back(pom1);
  225. cDir = pom.tileInfo->roadDir;
  226. rotH = (pom.tileInfo->siodmyTajemniczyBajt >> 5) & 1;
  227. rotV = (pom.tileInfo->siodmyTajemniczyBajt >> 4) & 1;
  228. if(rotH)
  229. {
  230. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].roadbitmap[0]);
  231. }
  232. if(rotV)
  233. {
  234. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].roadbitmap[0]);
  235. }
  236. if(rotH || rotV)
  237. {
  238. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].roadbitmap[0]);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. for (int i=0; i<map->width; i++) //jest po szeroko�ci
  245. {
  246. for (int j=0; j<map->height;j++) //po wysoko�ci
  247. {
  248. for(int k=0; k<=map->twoLevel; ++k)
  249. {
  250. if(map->terrain[i][j][k].nuine)
  251. {
  252. int cDir;
  253. bool rotH, rotV;
  254. ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->terrain[i][j][k].nuine-1]->ourImages[map->terrain[i][j][k].rivDir].bitmap);
  255. cDir = map->terrain[i][j][k].rivDir;
  256. rotH = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 3) & 1;
  257. rotV = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 2) & 1;
  258. if(rotH)
  259. {
  260. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].rivbitmap[0]);
  261. }
  262. if(rotV)
  263. {
  264. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].rivbitmap[0]);
  265. }
  266. if(rotH || rotV)
  267. {
  268. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].rivbitmap[0]);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. void CMapHandler::borderAndTerrainBitmapInit()
  276. {
  277. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  278. bord->notFreeImgs = true;
  279. terrainGraphics.resize(10);
  280. for (int i = 0; i < 10 ; i++)
  281. {
  282. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  283. terrainGraphics[i].resize(hlp->ourImages.size());
  284. hlp->notFreeImgs = true;
  285. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  286. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  287. delete hlp;
  288. }
  289. for (int i=0-Woff; i<map->width+Woff; i++) //jest po szeroko�ci
  290. {
  291. for (int j=0-Hoff; j<map->height+Hoff;j++) //po wysoko�ci
  292. {
  293. for(int k=0; k<=map->twoLevel; ++k)
  294. {
  295. if(i < 0 || i > (map->width-1) || j < 0 || j > (map->height-1))
  296. {
  297. if(i==-1 && j==-1)
  298. {
  299. ttiles[i][j][k].terbitmap = bord->ourImages[16].bitmap;
  300. continue;
  301. }
  302. else if(i==-1 && j==(map->height))
  303. {
  304. ttiles[i][j][k].terbitmap = bord->ourImages[19].bitmap;
  305. continue;
  306. }
  307. else if(i==(map->width) && j==-1)
  308. {
  309. ttiles[i][j][k].terbitmap = bord->ourImages[17].bitmap;
  310. continue;
  311. }
  312. else if(i==(map->width) && j==(map->height))
  313. {
  314. ttiles[i][j][k].terbitmap = bord->ourImages[18].bitmap;
  315. continue;
  316. }
  317. else if(j == -1 && i > -1 && i < map->height)
  318. {
  319. ttiles[i][j][k].terbitmap = bord->ourImages[22+rand()%2].bitmap;
  320. continue;
  321. }
  322. else if(i == -1 && j > -1 && j < map->height)
  323. {
  324. ttiles[i][j][k].terbitmap = bord->ourImages[33+rand()%2].bitmap;
  325. continue;
  326. }
  327. else if(j == map->height && i >-1 && i < map->width)
  328. {
  329. ttiles[i][j][k].terbitmap = bord->ourImages[29+rand()%2].bitmap;
  330. continue;
  331. }
  332. else if(i == map->width && j > -1 && j < map->height)
  333. {
  334. ttiles[i][j][k].terbitmap = bord->ourImages[25+rand()%2].bitmap;
  335. continue;
  336. }
  337. else
  338. {
  339. ttiles[i][j][k].terbitmap = bord->ourImages[rand()%16].bitmap;
  340. continue;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. delete bord;
  347. }
  348. void CMapHandler::initObjectRects()
  349. {
  350. //initializing objects / rects
  351. for(size_t f=0; f < map->objects.size(); ++f)
  352. {
  353. if(!map->objects[f]) continue;
  354. if((map->objects[f]->ID==HEROI_TYPE && static_cast<CGHeroInstance*>(map->objects[f])->inTownGarrison)
  355. || !map->objects[f]->defInfo)
  356. {
  357. continue;
  358. }
  359. CDefHandler * curd = map->objects[f]->defInfo->handler;
  360. if(curd)
  361. {
  362. for(int fx=0; fx<curd->ourImages[0].bitmap->w>>5; ++fx) //curd->ourImages[0].bitmap->w/32
  363. {
  364. for(int fy=0; fy<curd->ourImages[0].bitmap->h>>5; ++fy) //curd->ourImages[0].bitmap->h/32
  365. {
  366. SDL_Rect cr;
  367. cr.w = 32;
  368. cr.h = 32;
  369. cr.x = fx<<5; //fx*32
  370. cr.y = fy<<5; //fy*32
  371. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(map->objects[f],cr);
  372. if( (map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1) >= 0
  373. && (map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1) < ttiles.size() - Woff
  374. && (map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1) >= 0
  375. && (map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1) < ttiles[0].size() - Hoff
  376. )
  377. {
  378. //TerrainTile2 & curt =
  379. // ttiles
  380. // [map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  381. //[map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  382. //[map->objects[f]->pos.z];
  383. ttiles[map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1][map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1][map->objects[f]->pos.z].objects.push_back(toAdd);
  384. }
  385. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  386. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  387. }//if curd
  388. } // for(int f=0; f<map->objects.size(); ++f)
  389. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  390. {
  391. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  392. {
  393. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  394. {
  395. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  396. }
  397. }
  398. }
  399. }
  400. static void processDef (CGDefInfo* def)
  401. {
  402. if(def->id == 26)
  403. return;
  404. if(!def->handler) //if object has already set handler (eg. heroes) it should not be overwritten
  405. {
  406. if(def->name.size())
  407. {
  408. def->handler = CDefHandler::giveDef(def->name);
  409. }
  410. else
  411. {
  412. tlog2 << "No def name for " << def->id << " " << def->subid << std::endl;
  413. def->handler = NULL;
  414. return;
  415. }
  416. def->width = def->handler->ourImages[0].bitmap->w/32;
  417. def->height = def->handler->ourImages[0].bitmap->h/32;
  418. }
  419. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  420. if(pom && def->id!=TOWNI_TYPE)
  421. {
  422. pom->handler = def->handler;
  423. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  424. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  425. }
  426. else if(def->id != HEROI_TYPE && def->id != TOWNI_TYPE)
  427. tlog3 << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  428. if(!def->handler->alphaTransformed)
  429. {
  430. for(size_t yy=0; yy < def->handler->ourImages.size(); ++yy)
  431. {
  432. def->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  433. def->handler->alphaTransformed = true;
  434. }
  435. }
  436. }
  437. void CMapHandler::initHeroDef(CGHeroInstance * h)
  438. {
  439. h->defInfo->handler = graphics->flags1[0];
  440. h->defInfo->width = h->defInfo->handler->ourImages[0].bitmap->w/32;
  441. h->defInfo->height = h->defInfo->handler->ourImages[0].bitmap->h/32;
  442. }
  443. void CMapHandler::init()
  444. {
  445. timeHandler th;
  446. th.getDif();
  447. std::ifstream ifs("config/townsDefs.txt");
  448. int ccc;
  449. ifs>>ccc;
  450. for(int i=0;i<ccc*2;i++)
  451. {
  452. CGDefInfo *n;
  453. if(i<ccc)
  454. {
  455. n = CGI->state->villages[i];
  456. map->defy.push_back(CGI->state->forts[i]);
  457. }
  458. else
  459. n = CGI->state->capitols[i%ccc];
  460. ifs >> n->name;
  461. if(!n)
  462. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  463. else
  464. map->defy.push_back(n);
  465. }
  466. tlog0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  467. for(int i=0;i<map->heroes.size();i++)
  468. {
  469. if(!map->heroes[i]->defInfo->handler)
  470. {
  471. initHeroDef(map->heroes[i]);
  472. }
  473. }
  474. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  475. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  476. for(int i=0;i<PLAYER_LIMIT;i++)
  477. {
  478. for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  479. {
  480. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  481. }
  482. }
  483. tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  484. for(size_t h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  485. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  486. tlog0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  487. prepareFOWDefs();
  488. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  489. borderAndTerrainBitmapInit();
  490. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  491. initObjectRects();
  492. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  493. }
  494. SDL_Surface * CMapHandler::terrainRect(int x, int y, int dx, int dy, int level, unsigned char anim, std::vector< std::vector< std::vector<unsigned char> > > * visibilityMap, bool otherHeroAnim, unsigned char heroAnim, SDL_Surface * extSurf, SDL_Rect * extRect, int moveX, int moveY, bool smooth)
  495. {
  496. if(!otherHeroAnim)
  497. heroAnim = anim; //the same, as it should be
  498. //setting surface to blit at
  499. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  500. if(extSurf)
  501. {
  502. su = extSurf;
  503. }
  504. else
  505. {
  506. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  507. }
  508. SDL_Rect prevClip;
  509. SDL_GetClipRect(su, &prevClip);
  510. if(extRect) SDL_SetClipRect(su, extRect); //preventing blitting outside of that rect
  511. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  512. throw std::string("terrainRect: out of range");
  513. dx += smooth?1:0;
  514. dy += smooth?1:0;
  515. ////printing terrain
  516. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  517. {
  518. for (int by=(moveY <= 0 ? 0 : -1); by<dy; by++)
  519. {
  520. const TerrainTile2 & tile = ttiles[x+bx][y+by][level];
  521. SDL_Rect sr;
  522. if(smooth)
  523. {
  524. sr.y=by*32 + moveY + extRect->y;
  525. sr.x=bx*32 + moveX + extRect->x;
  526. sr.h=sr.w=32;
  527. }
  528. else
  529. {
  530. sr.y=by*32;
  531. sr.x=bx*32;
  532. sr.h=sr.w=32;
  533. validateRectTerr(&sr, extRect);
  534. }
  535. if(tile.terbitmap)
  536. {
  537. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  538. }
  539. else
  540. {
  541. if(smooth)
  542. {
  543. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  544. {
  545. case 0:
  546. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  547. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  548. break;
  549. case 1:
  550. CSDL_Ext::blitWithRotate1clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  551. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  552. break;
  553. case 2:
  554. CSDL_Ext::blitWithRotate2clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  555. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  556. break;
  557. default:
  558. CSDL_Ext::blitWithRotate3clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  559. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  566. {
  567. case 0:
  568. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  569. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  570. break;
  571. case 1:
  572. CSDL_Ext::blitWithRotate1(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  573. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  574. break;
  575. case 2:
  576. CSDL_Ext::blitWithRotate2(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  577. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  578. break;
  579. default:
  580. CSDL_Ext::blitWithRotate3(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  581. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. ////terrain printed
  589. ////printing rivers
  590. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  591. {
  592. for (int by=(moveY <= 0 ? 0 : -1); by<dy; by++)
  593. {
  594. SDL_Rect sr;
  595. if(smooth)
  596. {
  597. sr.y=by*32 + moveY + extRect->y;
  598. sr.x=bx*32 + moveX + extRect->x;
  599. sr.h=sr.w=32;
  600. }
  601. else
  602. {
  603. sr.y=by*32;
  604. sr.x=bx*32;
  605. sr.h=sr.w=32;
  606. validateRectTerr(&sr, extRect);
  607. }
  608. const std::vector<SDL_Surface *> &rivbitmap = ttiles[x+bx][y+by][level].rivbitmap;
  609. if(rivbitmap.size())
  610. {
  611. CSDL_Ext::blit8bppAlphaTo24bpp(rivbitmap[anim%rivbitmap.size()],&genRect(sr.h, sr.w, 0, 0),su,&sr);
  612. }
  613. }
  614. }
  615. ////rivers printed
  616. ////printing roads
  617. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  618. {
  619. for (int by=(moveY <= 0 ? 0 : -1) - 1; by<dy; by++)
  620. {
  621. if(y+by<=-4)
  622. continue;
  623. SDL_Rect sr;
  624. if(smooth)
  625. {
  626. sr.y=by*32+16 + moveY + extRect->y;
  627. sr.x=bx*32 + moveX + extRect->x;
  628. sr.h=sr.w=32;
  629. }
  630. else
  631. {
  632. sr.y=by*32+16;
  633. sr.x=bx*32;
  634. sr.h=sr.w=32;
  635. validateRectTerr(&sr, extRect);
  636. }
  637. const std::vector<SDL_Surface *> &roadbitmap = ttiles[x+bx][y+by][level].roadbitmap;
  638. if(roadbitmap.size())
  639. {
  640. CSDL_Ext::blit8bppAlphaTo24bpp(roadbitmap[anim%roadbitmap.size()], &genRect(sr.h, sr.w, 0, (by==-1 && moveY == 0 ? 16 : 0)),su,&sr);
  641. }
  642. }
  643. }
  644. ////roads printed
  645. ////printing objects
  646. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  647. {
  648. for (int by=(moveY <= 0 ? 0 : -1); by<dy; by++)
  649. {
  650. for(int h=0; h < ttiles[x+bx][y+by][level].objects.size(); ++h)
  651. {
  652. SDL_Rect sr;
  653. if(smooth)
  654. {
  655. sr.w = 32;
  656. sr.h = 32;
  657. sr.x = (bx)*32 + moveX + extRect->x;
  658. sr.y = (by)*32 + moveY + extRect->y;
  659. }
  660. else
  661. {
  662. sr.w = 32;
  663. sr.h = 32;
  664. sr.x = (bx)*32;
  665. sr.y = (by)*32;
  666. validateRectTerr(&sr, extRect);
  667. }
  668. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  669. pp.h = sr.h;
  670. pp.w = sr.w;
  671. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  672. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  673. {
  674. int imgVal = 8;
  675. SDL_Surface * tb;
  676. if(themp->type==NULL)
  677. continue;
  678. std::vector<Cimage> & iv = graphics->heroAnims[themp->type->heroType]->ourImages;
  679. size_t gg;
  680. for(gg=0; gg<iv.size(); ++gg)
  681. {
  682. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  683. {
  684. tb = iv[gg+heroAnim%imgVal].bitmap;
  685. break;
  686. }
  687. }
  688. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  689. pp.y+=imgVal*2-32;
  690. sr.y-=16;
  691. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  692. }
  693. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  694. {
  695. int imgVal = 8;
  696. SDL_Surface * tb;
  697. if(themp->type==NULL)
  698. continue;
  699. std::vector<Cimage> & iv = graphics->heroAnims[themp->type->heroType]->ourImages;
  700. size_t gg;
  701. for(gg=0; gg < iv.size(); ++gg)
  702. {
  703. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  704. {
  705. tb = iv[gg].bitmap;
  706. break;
  707. }
  708. }
  709. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  710. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  711. {
  712. SDL_Rect bufr = sr;
  713. bufr.x-=2*32;
  714. bufr.y-=1*32;
  715. bufr.h = 64;
  716. bufr.w = 96;
  717. if(bufr.x-extRect->x>-64)
  718. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  719. }
  720. }
  721. else
  722. {
  723. const CGObjectInstance *obj = ttiles[x+bx][y+by][level].objects[h].first;
  724. const std::vector<Cimage> &ourImages = obj->defInfo->handler->ourImages;
  725. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  726. //setting appropriate flag color
  727. if(obj->tempOwner<8 || obj->tempOwner==255)
  728. CSDL_Ext::setPlayerColor(bitmap, obj->tempOwner);
  729. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,su,&sr);
  730. }
  731. }
  732. }
  733. }
  734. ////objects printed, printing shadow
  735. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  736. {
  737. for (int by=(moveY <= 0 ? 0 : -1); by<dy; by++)
  738. {
  739. SDL_Rect sr;
  740. if(smooth)
  741. {
  742. sr.y=by*32 + moveY + extRect->y;
  743. sr.x=bx*32 + moveX + extRect->x;
  744. sr.h=sr.w=32;
  745. }
  746. else
  747. {
  748. sr.y=by*32;
  749. sr.x=bx*32;
  750. sr.h=sr.w=32;
  751. validateRectTerr(&sr, extRect);
  752. }
  753. if(bx+x>=0 && by+y>=0 && bx+x<CGI->mh->map->width && by+y<CGI->mh->map->height && !(*visibilityMap)[bx+x][by+y][level])
  754. {
  755. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  756. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  757. }
  758. }
  759. }
  760. ////shadow printed
  761. //printing borders
  762. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx; bx++)
  763. {
  764. for (int by=(moveY <= 0 ? 0 : -1); by<dy; by++)
  765. {
  766. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  767. {
  768. SDL_Rect sr;
  769. if(smooth)
  770. {
  771. sr.y=by*32 + moveY + extRect->y;
  772. sr.x=bx*32 + moveX + extRect->x;
  773. sr.h=sr.w=32;
  774. }
  775. else
  776. {
  777. sr.y=by*32;
  778. sr.x=bx*32;
  779. sr.h=sr.w=32;
  780. validateRectTerr(&sr, extRect);
  781. }
  782. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  783. }
  784. else
  785. {
  786. #ifdef MARK_BLOCKED_POSITIONS
  787. if(ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  788. {
  789. SDL_Rect sr;
  790. if(smooth)
  791. {
  792. sr.y=by*32 + moveY;
  793. sr.x=bx*32 + moveX;
  794. sr.h=sr.w=32;
  795. }
  796. else
  797. {
  798. sr.y=by*32;
  799. sr.x=bx*32;
  800. sr.h=sr.w=32;
  801. validateRectTerr(&sr, extRect);
  802. }
  803. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  804. for(int f=0; f<ns->w*ns->h*4; ++f)
  805. {
  806. *((unsigned char*)(ns->pixels) + f) = 128;
  807. }
  808. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  809. SDL_FreeSurface(ns);
  810. }
  811. #endif
  812. #ifdef MARK_VISITABLE_POSITIONS
  813. if(ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  814. {
  815. SDL_Rect sr;
  816. if(smooth)
  817. {
  818. sr.y=by*32 + moveY;
  819. sr.x=bx*32 + moveX;
  820. sr.h=sr.w=32;
  821. }
  822. else
  823. {
  824. sr.y=by*32;
  825. sr.x=bx*32;
  826. sr.h=sr.w=32;
  827. validateRectTerr(&sr, extRect);
  828. }
  829. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  830. for(int f=0; f<ns->w*ns->h*4; ++f)
  831. {
  832. *((unsigned char*)(ns->pixels) + f) = 128;
  833. }
  834. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  835. SDL_FreeSurface(ns);
  836. }
  837. #endif
  838. }
  839. }
  840. }
  841. SDL_SetClipRect(su, &prevClip); //restoring clip_rect
  842. //borders printed
  843. return su;
  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. CDefHandler * curd = obj->defInfo->handler;
  1141. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1142. {
  1143. for(int fy=0; fy<curd->ourImages[0].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 - curd->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  1152. {
  1153. TerrainTile2 & curt = //TODO use me
  1154. ttiles
  1155. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1156. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1157. [obj->pos.z];
  1158. ttiles[obj->pos.x + fx - curd->ourImages[0].bitmap->w/32+1][obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  1159. }
  1160. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1161. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1162. return true;
  1163. }
  1164. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1165. {
  1166. CDefHandler * curd = obj->defInfo->handler;
  1167. if(!curd) return false;
  1168. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1169. {
  1170. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1171. {
  1172. if((obj->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  1173. {
  1174. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & ctile = ttiles[obj->pos.x + fx - curd->ourImages[0].bitmap->w/32+1][obj->pos.y + fy - curd->ourImages[0].bitmap->h/32+1][obj->pos.z].objects;
  1175. for(size_t dd=0; dd < ctile.size(); ++dd)
  1176. {
  1177. if(ctile[dd].first->id==obj->id)
  1178. ctile.erase(ctile.begin() + dd);
  1179. }
  1180. }
  1181. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1182. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1183. return true;
  1184. }
  1185. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1186. {
  1187. hideObject(obj);
  1188. return true;
  1189. }
  1190. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1191. {
  1192. if(isMoving)
  1193. {
  1194. switch(dir)
  1195. {
  1196. case 1:
  1197. return 10;
  1198. case 2:
  1199. return 5;
  1200. case 3:
  1201. return 6;
  1202. case 4:
  1203. return 7;
  1204. case 5:
  1205. return 8;
  1206. case 6:
  1207. return 9;
  1208. case 7:
  1209. return 12;
  1210. case 8:
  1211. return 11;
  1212. default:
  1213. throw std::string("Something very wrong1.");
  1214. }
  1215. }
  1216. else //if(isMoving)
  1217. {
  1218. switch(dir)
  1219. {
  1220. case 1:
  1221. return 13;
  1222. case 2:
  1223. return 0;
  1224. case 3:
  1225. return 1;
  1226. case 4:
  1227. return 2;
  1228. case 5:
  1229. return 3;
  1230. case 6:
  1231. return 4;
  1232. case 7:
  1233. return 15;
  1234. case 8:
  1235. return 14;
  1236. default:
  1237. throw std::string("Something very wrong2.");
  1238. }
  1239. }
  1240. }
  1241. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1242. {
  1243. if(ext)
  1244. {
  1245. if(val->x<0)
  1246. {
  1247. val->w += val->x;
  1248. val->x = ext->x;
  1249. }
  1250. else
  1251. {
  1252. val->x += ext->x;
  1253. }
  1254. if(val->y<0)
  1255. {
  1256. val->h += val->y;
  1257. val->y = ext->y;
  1258. }
  1259. else
  1260. {
  1261. val->y += ext->y;
  1262. }
  1263. if(val->x+val->w > ext->x+ext->w)
  1264. {
  1265. val->w = ext->x+ext->w-val->x;
  1266. }
  1267. if(val->y+val->h > ext->y+ext->h)
  1268. {
  1269. val->h = ext->y+ext->h-val->y;
  1270. }
  1271. //for sign problems
  1272. if(val->h > 20000 || val->w > 20000)
  1273. {
  1274. val->h = val->w = 0;
  1275. }
  1276. }
  1277. }
  1278. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1279. {
  1280. if(a.z!=b.z)
  1281. return -1; //error!
  1282. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1283. {
  1284. return 0;
  1285. }
  1286. else if(a.x==b.x && a.y==b.y+1) //t
  1287. {
  1288. return 1;
  1289. }
  1290. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1291. {
  1292. return 2;
  1293. }
  1294. else if(a.x==b.x-1 && a.y==b.y) //r
  1295. {
  1296. return 3;
  1297. }
  1298. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1299. {
  1300. return 4;
  1301. }
  1302. else if(a.x==b.x && a.y==b.y-1) //b
  1303. {
  1304. return 5;
  1305. }
  1306. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1307. {
  1308. return 6;
  1309. }
  1310. else if(a.x==b.x+1 && a.y==b.y) //l
  1311. {
  1312. return 7;
  1313. }
  1314. return -2; //shouldn't happen
  1315. }
  1316. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1317. {
  1318. SDL_Color palette[14];
  1319. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1320. {
  1321. for(int i=0; i<12; ++i)
  1322. {
  1323. palette[(i+1)%12] = terrainGraphics[8][j]->format->palette->colors[229 + i];
  1324. }
  1325. SDL_SetColors(terrainGraphics[8][j],palette,229,12);
  1326. for(int i=0; i<14; ++i)
  1327. {
  1328. palette[(i+1)%14] = terrainGraphics[8][j]->format->palette->colors[242 + i];
  1329. }
  1330. SDL_SetColors(terrainGraphics[8][j],palette,242,14);
  1331. }
  1332. }
  1333. CMapHandler::~CMapHandler()
  1334. {
  1335. delete fullHide;
  1336. delete partialHide;
  1337. for(int i=0; i < map->defy.size(); i++)
  1338. delete map->defy[i];
  1339. for(int i=0; i < roadDefs.size(); i++)
  1340. delete roadDefs[i];
  1341. for(int i=0; i < staticRiverDefs.size(); i++)
  1342. delete staticRiverDefs[i];
  1343. //TODO: delete border graphics
  1344. }
  1345. CMapHandler::CMapHandler()
  1346. {
  1347. fullHide = NULL;
  1348. partialHide = NULL;
  1349. }
  1350. TerrainTile2::TerrainTile2()
  1351. :tileInfo(0),terbitmap(0)
  1352. {}