mapHandler.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. /*
  152. //FIXME: unused?
  153. // prepare the map
  154. for (int i=0; i<sizes.x; i++) //by width
  155. {
  156. for (int j=0; j<sizes.y;j++) //by height
  157. {
  158. for (int k=0; k<sizes.z; ++k) //by levels
  159. {
  160. TerrainTile2 &pom(ttiles[i][j][k]);
  161. }
  162. }
  163. }*/
  164. }
  165. void CMapHandler::borderAndTerrainBitmapInit()
  166. {
  167. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  168. bord->notFreeImgs = true;
  169. terrainGraphics.resize(10);
  170. for (int i = 0; i < 10 ; i++)
  171. {
  172. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  173. terrainGraphics[i].resize(hlp->ourImages.size());
  174. hlp->notFreeImgs = true;
  175. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  176. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  177. delete hlp;
  178. }
  179. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  180. {
  181. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  182. {
  183. for(int k=0; k<sizes.z; ++k) //by levles
  184. {
  185. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  186. {
  187. int terBitmapNum = -1;
  188. if(i==-1 && j==-1)
  189. terBitmapNum = 16;
  190. else if(i==-1 && j==(sizes.y))
  191. terBitmapNum = 19;
  192. else if(i==(sizes.x) && j==-1)
  193. terBitmapNum = 17;
  194. else if(i==(sizes.x) && j==(sizes.y))
  195. terBitmapNum = 18;
  196. else if(j == -1 && i > -1 && i < sizes.y)
  197. terBitmapNum = 22+rand()%2;
  198. else if(i == -1 && j > -1 && j < sizes.y)
  199. terBitmapNum = 33+rand()%2;
  200. else if(j == sizes.y && i >-1 && i < sizes.x)
  201. terBitmapNum = 29+rand()%2;
  202. else if(i == sizes.x && j > -1 && j < sizes.y)
  203. terBitmapNum = 25+rand()%2;
  204. else
  205. terBitmapNum = rand()%16;
  206. if(terBitmapNum != -1)
  207. {
  208. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  209. continue;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. delete bord;
  216. }
  217. void CMapHandler::initObjectRects()
  218. {
  219. //initializing objects / rects
  220. for(size_t f=0; f < map->objects.size(); ++f)
  221. {
  222. const CGObjectInstance *obj = map->objects[f];
  223. if( !obj
  224. || (obj->ID==HEROI_TYPE && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  225. || (obj->ID==8 && static_cast<const CGBoat*>(obj)->hero) //boat with hero (hero graphics is used)
  226. || !obj->defInfo
  227. || !graphics->getDef(obj)) //no graphic...
  228. {
  229. continue;
  230. }
  231. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  232. for(int fx=0; fx<bitmap->w>>5; ++fx) //bitmap->w/32
  233. {
  234. for(int fy=0; fy<bitmap->h>>5; ++fy) //bitmap->h/32
  235. {
  236. SDL_Rect cr;
  237. cr.w = 32;
  238. cr.h = 32;
  239. cr.x = fx<<5; //fx*32
  240. cr.y = fy<<5; //fy*32
  241. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  242. if( (obj->pos.x + fx - bitmap->w/32+1) >= 0
  243. && (obj->pos.x + fx - bitmap->w/32+1) < ttiles.size() - frameW
  244. && (obj->pos.y + fy - bitmap->h/32+1) >= 0
  245. && (obj->pos.y + fy - bitmap->h/32+1) < ttiles[0].size() - frameH
  246. )
  247. {
  248. //TerrainTile2 & curt =
  249. // ttiles
  250. // [obj->pos.x + fx - bitmap->w/32]
  251. //[obj->pos.y + fy - bitmap->h/32]
  252. //[obj->pos.z];
  253. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  254. }
  255. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  256. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  257. } // for(int f=0; f<map->objects.size(); ++f)
  258. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  259. {
  260. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  261. {
  262. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  263. {
  264. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  265. }
  266. }
  267. }
  268. }
  269. static void processDef (const CGDefInfo* def)
  270. {
  271. if(def->id == EVENTI_TYPE)
  272. {
  273. graphics->advmapobjGraphics[def->id][def->subid][def->name] = NULL;
  274. return;
  275. }
  276. CDefEssential * ourDef = graphics->getDef(def);
  277. if(!ourDef) //if object has already set handler (eg. heroes) it should not be overwritten
  278. {
  279. if(def->name.size())
  280. {
  281. if(vstd::contains(graphics->mapObjectDefs, def->name))
  282. {
  283. graphics->advmapobjGraphics[def->id][def->subid][def->name] = graphics->mapObjectDefs[def->name];
  284. }
  285. else
  286. {
  287. graphics->mapObjectDefs[def->name] = graphics->advmapobjGraphics[def->id][def->subid][def->name] = CDefHandler::giveDefEss(def->name);
  288. }
  289. }
  290. else
  291. {
  292. tlog2 << "No def name for " << def->id << " " << def->subid << std::endl;
  293. return;
  294. }
  295. ourDef = graphics->getDef(def);
  296. }
  297. //alpha transformation
  298. for(size_t yy=0; yy < ourDef->ourImages.size(); ++yy)
  299. {
  300. CSDL_Ext::alphaTransform(ourDef->ourImages[yy].bitmap);
  301. }
  302. }
  303. void CMapHandler::initHeroDef(const CGHeroInstance * h)
  304. {
  305. graphics->advmapobjGraphics[h->defInfo->id][h->defInfo->subid][h->defInfo->name] = graphics->flags1[0];
  306. }
  307. void CMapHandler::init()
  308. {
  309. timeHandler th;
  310. th.getDif();
  311. graphics->advmapobjGraphics[8][0]["AB01_.DEF"] = graphics->boatAnims[0];
  312. graphics->advmapobjGraphics[8][1]["AB02_.DEF"] = graphics->boatAnims[1];
  313. graphics->advmapobjGraphics[8][2]["AB03_.DEF"] = graphics->boatAnims[2];
  314. // Size of visible terrain.
  315. int mapW = conf.go()->ac.advmapW;
  316. int mapH = conf.go()->ac.advmapH;
  317. //sizes of terrain
  318. sizes.x = map->width;
  319. sizes.y = map->height;
  320. sizes.z = map->twoLevel+1;
  321. // Total number of visible tiles. Subtract 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. for(int i=0;i<map->heroes.size();i++)
  338. {
  339. if( !graphics->getDef(map->heroes[i]) )
  340. {
  341. initHeroDef(map->heroes[i]);
  342. }
  343. }
  344. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  345. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  346. //it seems to be completely unnecessary and useless
  347. // for(int i=0;i<PLAYER_LIMIT;i++)
  348. // {
  349. // for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  350. // {
  351. // usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  352. // }
  353. // }
  354. // tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  355. prepareFOWDefs();
  356. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  357. borderAndTerrainBitmapInit();
  358. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  359. initObjectRects();
  360. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  361. }
  362. // Update map window screen
  363. // top_tile top left tile to draw. Not necessarily visible.
  364. // extRect, extRect = map window on screen
  365. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  366. 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
  367. {
  368. // Width and height of the portion of the map to process. Units in tiles.
  369. unsigned int dx = tilesW;
  370. unsigned int dy = tilesH;
  371. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  372. SDL_Rect rtile = { 0, 0, 32, 32 };
  373. // Absolute coords of the first pixel in the top left corner
  374. int srx_init = offsetX + extRect->x;
  375. int sry_init = offsetY + extRect->y;
  376. int srx, sry; // absolute screen coordinates in pixels
  377. // If moving, we need to add an extra column/line
  378. if (moveX != 0)
  379. {
  380. dx++;
  381. srx_init += moveX;
  382. if (moveX > 0)
  383. {
  384. // Moving right. We still need to draw the old tile on the
  385. // left, so adjust our referential
  386. top_tile.x --;
  387. srx_init -= 32;
  388. }
  389. }
  390. if (moveY != 0)
  391. {
  392. dy++;
  393. sry_init += moveY;
  394. if (moveY > 0)
  395. {
  396. // Moving down. We still need to draw the tile on the top,
  397. // so adjust our referential.
  398. top_tile.y --;
  399. sry_init -= 32;
  400. }
  401. }
  402. // Reduce sizes if we go out of the full map.
  403. if (top_tile.x < -frameW)
  404. top_tile.x = -frameW;
  405. if (top_tile.y < -frameH)
  406. top_tile.y = -frameH;
  407. if (top_tile.x + dx > sizes.x + frameW)
  408. dx = sizes.x + frameW - top_tile.x;
  409. if (top_tile.y + dy > sizes.y + frameH)
  410. dy = sizes.y + frameH - top_tile.y;
  411. if(!otherHeroAnim)
  412. heroAnim = anim; //the same, as it should be
  413. SDL_Rect prevClip;
  414. SDL_GetClipRect(extSurf, &prevClip);
  415. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  416. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  417. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  418. //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  419. // printing terrain
  420. srx = srx_init;
  421. for (int bx = 0; bx < dx; bx++, srx+=32)
  422. {
  423. // Skip column if not in map
  424. if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x)
  425. continue;
  426. sry = sry_init;
  427. for (int by=0; by < dy; by++, sry+=32)
  428. {
  429. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  430. // Skip tile if not in map
  431. if (pos.y < 0 || pos.y >= sizes.y)
  432. continue;
  433. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  434. const TerrainTile &tinfo = map->terrain[pos.x][pos.y][pos.z];
  435. SDL_Rect sr;
  436. sr.x=srx;
  437. sr.y=sry;
  438. sr.h=sr.w=32;
  439. //blit terrain with river/road
  440. if(tile.terbitmap) { //if custom terrain graphic - use it
  441. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  442. CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr);
  443. } else //use default terrain graphic
  444. blitterWithRotation(terrainGraphics[tinfo.tertype][tinfo.terview],rtile, extSurf, sr, tinfo.siodmyTajemniczyBajt%4);
  445. if(tinfo.nuine) //print river if present
  446. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.nuine-1]->ourImages[tinfo.rivDir].bitmap,rtile, extSurf, sr, (tinfo.siodmyTajemniczyBajt>>2)%4);
  447. if(tinfo.malle) //print road if present
  448. blitterWithRotationAndAlpha(roadDefs[tinfo.malle-1]->ourImages[tinfo.roadDir].bitmap,rtile, extSurf, sr, (tinfo.siodmyTajemniczyBajt>>4)%4);
  449. //blit objects
  450. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  451. for(int h=0; h < objects.size(); ++h)
  452. {
  453. const CGObjectInstance *obj = objects[h].first;
  454. if (!graphics->getDef(obj))
  455. processDef(obj->defInfo);
  456. ui8 color = obj->tempOwner;
  457. //checking if object has non-empty graphic on this tile
  458. if(obj->ID != HEROI_TYPE && !obj->coveringAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5))
  459. continue;
  460. static const int notBlittedInPuzzleMode[] = {124};
  461. //don't print flaggable objects in puzzle mode
  462. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  463. continue;
  464. SDL_Rect sr2(sr);
  465. SDL_Rect pp = objects[h].second;
  466. pp.h = sr.h;
  467. pp.w = sr.w;
  468. const CGHeroInstance * themp = (obj->ID != HEROI_TYPE
  469. ? NULL
  470. : static_cast<const CGHeroInstance*>(obj));
  471. //print hero / boat and flag
  472. if((themp && themp->moveDir && themp->type) || (obj->ID == 8)) //it's hero or boat
  473. {
  474. const int IMGVAL = 8; //frames per group of movement animation
  475. ui8 dir;
  476. std::vector<Cimage> * iv = NULL;
  477. std::vector<CDefEssential *> Graphics::*flg = NULL;
  478. SDL_Surface * tb; //surface to blitted
  479. if(themp) //hero
  480. {
  481. dir = themp->moveDir;
  482. //pick graphics of hero (or boat if hero is sailing)
  483. iv = (themp->boat)
  484. ? &graphics->boatAnims[themp->boat->subID]->ourImages
  485. : &graphics->heroAnims[themp->type->heroType]->ourImages;
  486. //pick appropriate flag set
  487. if(themp->boat)
  488. {
  489. switch (themp->boat->subID)
  490. {
  491. case 0: flg = &Graphics::flags1; break;
  492. case 1: flg = &Graphics::flags2; break;
  493. case 2: flg = &Graphics::flags3; break;
  494. default: tlog1 << "Not supported boat subtype: " << themp->boat->subID << std::endl;
  495. }
  496. }
  497. else
  498. {
  499. flg = &Graphics::flags4;
  500. }
  501. }
  502. else //boat
  503. {
  504. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  505. dir = boat->direction;
  506. iv = &graphics->boatAnims[boat->subID]->ourImages;
  507. }
  508. if(themp && !themp->isStanding) //hero is moving
  509. {
  510. size_t gg;
  511. for(gg=0; gg<iv->size(); ++gg)
  512. {
  513. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  514. {
  515. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  516. break;
  517. }
  518. }
  519. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  520. //printing flag
  521. pp.y+=IMGVAL*2-32;
  522. sr2.y-=16;
  523. CSDL_Ext::blitSurface((graphics->*flg)[color]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  524. }
  525. else //hero / boat stands still
  526. {
  527. size_t gg;
  528. for(gg=0; gg < iv->size(); ++gg)
  529. {
  530. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  531. {
  532. tb = (*iv)[gg].bitmap;
  533. break;
  534. }
  535. }
  536. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  537. //printing flag
  538. if(flg
  539. && obj->pos.x == top_tile.x + bx
  540. && obj->pos.y == top_tile.y + by)
  541. {
  542. SDL_Rect bufr = sr2;
  543. bufr.x-=2*32;
  544. bufr.y-=1*32;
  545. bufr.h = 64;
  546. bufr.w = 96;
  547. if(bufr.x-extRect->x>-64)
  548. CSDL_Ext::blitSurface((graphics->*flg)[color]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, NULL, extSurf, &bufr);
  549. }
  550. }
  551. }
  552. else //blit normal object
  553. {
  554. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  555. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  556. //setting appropriate flag color
  557. if(color < 8 || color==255)
  558. CSDL_Ext::setPlayerColor(bitmap, color);
  559. if( obj->hasShadowAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5) )
  560. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  561. else
  562. CSDL_Ext::blitSurface(bitmap,&pp,extSurf,&sr2);
  563. }
  564. }
  565. //objects blitted
  566. //X sign
  567. if(puzzleMode)
  568. {
  569. if(bx == grailPosRel.x && by == grailPosRel.y)
  570. {
  571. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, NULL, extSurf, &sr);
  572. }
  573. }
  574. }
  575. }
  576. // terrain printed
  577. // printing borders
  578. srx = srx_init;
  579. for (int bx = 0; bx < dx; bx++, srx+=32)
  580. {
  581. sry = sry_init;
  582. for (int by = 0; by<dy; by++, sry+=32)
  583. {
  584. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  585. SDL_Rect sr;
  586. sr.x=srx;
  587. sr.y=sry;
  588. sr.h=sr.w=32;
  589. if (pos.x < 0 || pos.x >= sizes.x ||
  590. pos.y < 0 || pos.y >= sizes.y)
  591. {
  592. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  593. CSDL_Ext::blitSurface(ttiles[pos.x][pos.y][top_tile.z].terbitmap,
  594. &temp_rect,extSurf,&sr);
  595. }
  596. else
  597. {
  598. //blitting Fog of War
  599. if (!puzzleMode)
  600. {
  601. if (pos.x >= 0 &&
  602. pos.y >= 0 &&
  603. pos.x < sizes.x &&
  604. pos.y < sizes.y &&
  605. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  606. {
  607. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  608. if(hide.second)
  609. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  610. else
  611. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  612. }
  613. }
  614. //FoW blitted
  615. // TODO: these should be activable by the console
  616. #ifdef MARK_BLOCKED_POSITIONS
  617. if(map->terrain[pos.x][pos.y][top_tile.z].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. CSDL_Ext::blitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  625. }
  626. #endif
  627. #ifdef MARK_VISITABLE_POSITIONS
  628. if(map->terrain[pos.x][pos.y][top_tile.z].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. CSDL_Ext::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. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  676. }
  677. //sepia / gray effect applied
  678. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  679. }
  680. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap ) const
  681. {
  682. 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,
  683. 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,
  684. 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,
  685. 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,
  686. 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,
  687. 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,
  688. 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,
  689. 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,
  690. 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10};
  691. //is tile visible. arrangement: (like num keyboard)
  692. bool d7 = (pos.x>0 && pos.y>0) ? visibilityMap[pos.x-1][pos.y-1][pos.z] : 0, //789
  693. d8 = (pos.y>0) ? visibilityMap[pos.x][pos.y-1][pos.z] : 0, //456
  694. d9 = (pos.y>0 && pos.x<sizes.x-1) ? visibilityMap[pos.x+1][pos.y-1][pos.z] : 0, //123
  695. d4 = (pos.x>0) ? visibilityMap[pos.x-1][pos.y][pos.z] : 0,
  696. //d5 = visibilityMap[pos.x][y][pos.z], //TODO use me - OMFG
  697. d6 = (pos.x<sizes.x-1) ? visibilityMap[pos.x+1][pos.y][pos.z] : 0,
  698. d1 = (pos.x>0 && pos.y<sizes.y-1) ? visibilityMap[pos.x-1][pos.y+1][pos.z] : 0,
  699. d2 = (pos.y<sizes.y-1) ? visibilityMap[pos.x][pos.y+1][pos.z] : 0,
  700. d3 = (pos.x<sizes.x-1 && pos.y<sizes.y-1) ? visibilityMap[pos.x+1][pos.y+1][pos.z] : 0;
  701. int retBitmapID = visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  702. if (retBitmapID < 0)
  703. {
  704. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  705. }
  706. if (retBitmapID >= 0)
  707. {
  708. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  709. }
  710. else
  711. {
  712. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  713. }
  714. }
  715. bool CMapHandler::printObject(const CGObjectInstance *obj)
  716. {
  717. if (!graphics->getDef(obj))
  718. processDef(obj->defInfo);
  719. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  720. const int tilesW = bitmap->w/32;
  721. const int tilesH = bitmap->h/32;
  722. for(int fx=0; fx<tilesW; ++fx)
  723. {
  724. for(int fy=0; fy<tilesH; ++fy)
  725. {
  726. SDL_Rect cr;
  727. cr.w = 32;
  728. cr.h = 32;
  729. cr.x = fx*32;
  730. cr.y = fy*32;
  731. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  732. 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)
  733. {
  734. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  735. std::vector< std::pair<const CGObjectInstance*,SDL_Rect> >::iterator i = curt.objects.begin();
  736. for(; i != curt.objects.end(); i++)
  737. {
  738. OCM_HLP cmp;
  739. if(cmp(toAdd, *i))
  740. {
  741. curt.objects.insert(i, toAdd);
  742. i = curt.objects.begin(); //to validate and avoid adding it second time
  743. break;
  744. }
  745. }
  746. if(i == curt.objects.end())
  747. curt.objects.insert(i, toAdd);
  748. }
  749. } // for(int fy=0; fy<tilesH; ++fy)
  750. } //for(int fx=0; fx<tilesW; ++fx)
  751. return true;
  752. }
  753. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  754. {
  755. CDefEssential * curd = graphics->getDef(obj);
  756. if(!curd) return false;
  757. const SDL_Surface *bitmap = curd->ourImages[0].bitmap;
  758. for(int fx=0; fx<bitmap->w/32; ++fx)
  759. {
  760. for(int fy=0; fy<bitmap->h/32; ++fy)
  761. {
  762. 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)
  763. {
  764. 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;
  765. for(size_t dd=0; dd < ctile.size(); ++dd)
  766. {
  767. if(ctile[dd].first->id==obj->id)
  768. ctile.erase(ctile.begin() + dd);
  769. }
  770. }
  771. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  772. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  773. return true;
  774. }
  775. bool CMapHandler::removeObject(CGObjectInstance *obj)
  776. {
  777. hideObject(obj);
  778. return true;
  779. }
  780. unsigned char CMapHandler::getHeroFrameNum(unsigned char dir, bool isMoving) const
  781. {
  782. if(isMoving)
  783. {
  784. static const unsigned char frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  785. return frame[dir];
  786. }
  787. else //if(isMoving)
  788. {
  789. static const unsigned char frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  790. return frame[dir];
  791. }
  792. }
  793. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  794. {
  795. if(ext)
  796. {
  797. if(val->x<0)
  798. {
  799. val->w += val->x;
  800. val->x = ext->x;
  801. }
  802. else
  803. {
  804. val->x += ext->x;
  805. }
  806. if(val->y<0)
  807. {
  808. val->h += val->y;
  809. val->y = ext->y;
  810. }
  811. else
  812. {
  813. val->y += ext->y;
  814. }
  815. if(val->x+val->w > ext->x+ext->w)
  816. {
  817. val->w = ext->x+ext->w-val->x;
  818. }
  819. if(val->y+val->h > ext->y+ext->h)
  820. {
  821. val->h = ext->y+ext->h-val->y;
  822. }
  823. //for sign problems
  824. if(val->h > 20000 || val->w > 20000)
  825. {
  826. val->h = val->w = 0;
  827. }
  828. }
  829. }
  830. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  831. {
  832. if(a.z!=b.z)
  833. return -1; //error!
  834. if(a.x==b.x+1 && a.y==b.y+1) //lt
  835. return 0;
  836. else if(a.x==b.x && a.y==b.y+1) //t
  837. return 1;
  838. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  839. return 2;
  840. else if(a.x==b.x-1 && a.y==b.y) //r
  841. return 3;
  842. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  843. return 4;
  844. else if(a.x==b.x && a.y==b.y-1) //b
  845. return 5;
  846. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  847. return 6;
  848. else if(a.x==b.x+1 && a.y==b.y) //l
  849. return 7;
  850. return -2; //shouldn't happen
  851. }
  852. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  853. {
  854. //works with at most 16 colors, if needed more -> increase values
  855. assert(howMany < 16);
  856. SDL_Color palette[16];
  857. for(int i=0; i<howMany; ++i)
  858. {
  859. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  860. }
  861. SDL_SetColors(img,palette,from,howMany);
  862. }
  863. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  864. {
  865. for(size_t j=0; j < terrainGraphics[7].size(); ++j)
  866. {
  867. shiftColors(terrainGraphics[7][j],246, 9);
  868. }
  869. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  870. {
  871. shiftColors(terrainGraphics[8][j],229, 12);
  872. shiftColors(terrainGraphics[8][j],242, 14);
  873. }
  874. for(size_t j=0; j < staticRiverDefs[0]->ourImages.size(); ++j)
  875. {
  876. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,183, 12);
  877. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,195, 6);
  878. }
  879. for(size_t j=0; j < staticRiverDefs[2]->ourImages.size(); ++j)
  880. {
  881. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,228, 12);
  882. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,183, 6);
  883. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,240, 6);
  884. }
  885. for(size_t j=0; j < staticRiverDefs[3]->ourImages.size(); ++j)
  886. {
  887. shiftColors(staticRiverDefs[3]->ourImages[j].bitmap,240, 9);
  888. }
  889. }
  890. CMapHandler::~CMapHandler()
  891. {
  892. delete graphics->FoWfullHide;
  893. delete graphics->FoWpartialHide;
  894. for(int i=0; i < roadDefs.size(); i++)
  895. delete roadDefs[i];
  896. for(int i=0; i < staticRiverDefs.size(); i++)
  897. delete staticRiverDefs[i];
  898. for(int i=0; i < terrainGraphics.size(); ++i)
  899. {
  900. for(int j=0; j < terrainGraphics[i].size(); ++j)
  901. SDL_FreeSurface(terrainGraphics[i][j]);
  902. }
  903. terrainGraphics.clear();
  904. }
  905. CMapHandler::CMapHandler()
  906. {
  907. frameW = frameH = 0;
  908. graphics->FoWfullHide = NULL;
  909. graphics->FoWpartialHide = NULL;
  910. }
  911. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  912. {
  913. out.clear();
  914. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  915. const TerrainTile &t = map->terrain[pos.x][pos.y][pos.z];
  916. for(std::vector < std::pair<const CGObjectInstance*,SDL_Rect> >::const_iterator i = tt.objects.begin(); i != tt.objects.end(); i++)
  917. {
  918. if(i->first->ID == 124) //Hole
  919. {
  920. out = i->first->hoverName;
  921. return;
  922. }
  923. }
  924. if(t.hasFavourableWinds())
  925. out = CGI->generaltexth->names[225]; //Favourable Winds
  926. else if(terName)
  927. out = CGI->generaltexth->terrainNames[t.tertype];
  928. }
  929. TerrainTile2::TerrainTile2()
  930. :terbitmap(0)
  931. {}