mapHandler.cpp 31 KB

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