mapHandler.cpp 37 KB

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