mapHandler.cpp 31 KB

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