mapHandler.cpp 31 KB

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