mapHandler.cpp 43 KB

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