mapHandler.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  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)
  440. { //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. }
  444. else //use default terrain graphic
  445. {
  446. blitterWithRotation(terrainGraphics[tinfo.tertype][tinfo.terview],rtile, extSurf, sr, tinfo.siodmyTajemniczyBajt%4);
  447. }
  448. if(tinfo.nuine) //print river if present
  449. {
  450. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.nuine-1]->ourImages[tinfo.rivDir].bitmap,rtile, extSurf, sr, (tinfo.siodmyTajemniczyBajt>>2)%4);
  451. }
  452. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  453. if (pos.y > 0 && map->terrain[pos.x][pos.y-1][pos.z].malle)
  454. { //part from top tile
  455. const TerrainTile &topTile = map->terrain[pos.x][pos.y-1][pos.z];
  456. Rect source(0, 16, 32, 16);
  457. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  458. blitterWithRotationAndAlpha(roadDefs[topTile.malle-1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.siodmyTajemniczyBajt>>4)%4);
  459. }
  460. if(tinfo.malle) //print road from this tile
  461. {
  462. Rect source(0, 0, 32, 32);
  463. Rect dest(sr.x, sr.y+16, sr.w, sr.h/2);
  464. blitterWithRotationAndAlpha(roadDefs[tinfo.malle-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.siodmyTajemniczyBajt>>4)%4);
  465. }
  466. //blit objects
  467. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  468. for(int h=0; h < objects.size(); ++h)
  469. {
  470. const CGObjectInstance *obj = objects[h].first;
  471. if (!graphics->getDef(obj))
  472. processDef(obj->defInfo);
  473. ui8 color = obj->tempOwner;
  474. //checking if object has non-empty graphic on this tile
  475. if(obj->ID != GameConstants::HEROI_TYPE && !obj->coveringAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5))
  476. continue;
  477. static const int notBlittedInPuzzleMode[] = {124};
  478. //don't print flaggable objects in puzzle mode
  479. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  480. continue;
  481. SDL_Rect sr2(sr);
  482. SDL_Rect pp = objects[h].second;
  483. pp.h = sr.h;
  484. pp.w = sr.w;
  485. const CGHeroInstance * themp = (obj->ID != GameConstants::HEROI_TYPE
  486. ? NULL
  487. : static_cast<const CGHeroInstance*>(obj));
  488. //print hero / boat and flag
  489. if((themp && themp->moveDir && themp->type) || (obj->ID == 8)) //it's hero or boat
  490. {
  491. const int IMGVAL = 8; //frames per group of movement animation
  492. ui8 dir;
  493. std::vector<Cimage> * iv = NULL;
  494. std::vector<CDefEssential *> Graphics::*flg = NULL;
  495. SDL_Surface * tb; //surface to blitted
  496. if(themp) //hero
  497. {
  498. dir = themp->moveDir;
  499. //pick graphics of hero (or boat if hero is sailing)
  500. iv = (themp->boat)
  501. ? &graphics->boatAnims[themp->boat->subID]->ourImages
  502. : &graphics->heroAnims[themp->type->heroType]->ourImages;
  503. //pick appropriate flag set
  504. if(themp->boat)
  505. {
  506. switch (themp->boat->subID)
  507. {
  508. case 0: flg = &Graphics::flags1; break;
  509. case 1: flg = &Graphics::flags2; break;
  510. case 2: flg = &Graphics::flags3; break;
  511. default: tlog1 << "Not supported boat subtype: " << themp->boat->subID << std::endl;
  512. }
  513. }
  514. else
  515. {
  516. flg = &Graphics::flags4;
  517. }
  518. }
  519. else //boat
  520. {
  521. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  522. dir = boat->direction;
  523. iv = &graphics->boatAnims[boat->subID]->ourImages;
  524. }
  525. if(themp && !themp->isStanding) //hero is moving
  526. {
  527. size_t gg;
  528. for(gg=0; gg<iv->size(); ++gg)
  529. {
  530. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  531. {
  532. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  533. break;
  534. }
  535. }
  536. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  537. //printing flag
  538. pp.y+=IMGVAL*2-32;
  539. sr2.y-=16;
  540. CSDL_Ext::blitSurface((graphics->*flg)[color]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  541. }
  542. else //hero / boat stands still
  543. {
  544. size_t gg;
  545. for(gg=0; gg < iv->size(); ++gg)
  546. {
  547. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  548. {
  549. tb = (*iv)[gg].bitmap;
  550. break;
  551. }
  552. }
  553. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  554. //printing flag
  555. if(flg
  556. && obj->pos.x == top_tile.x + bx
  557. && obj->pos.y == top_tile.y + by)
  558. {
  559. SDL_Rect bufr = sr2;
  560. bufr.x-=2*32;
  561. bufr.y-=1*32;
  562. bufr.h = 64;
  563. bufr.w = 96;
  564. if(bufr.x-extRect->x>-64)
  565. CSDL_Ext::blitSurface((graphics->*flg)[color]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, NULL, extSurf, &bufr);
  566. }
  567. }
  568. }
  569. else //blit normal object
  570. {
  571. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  572. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  573. //setting appropriate flag color
  574. if(color < 8 || color==255)
  575. CSDL_Ext::setPlayerColor(bitmap, color);
  576. if( obj->hasShadowAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5) )
  577. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  578. else
  579. CSDL_Ext::blitSurface(bitmap,&pp,extSurf,&sr2);
  580. }
  581. }
  582. //objects blitted
  583. //X sign
  584. if(puzzleMode)
  585. {
  586. if(bx == grailPosRel.x && by == grailPosRel.y)
  587. {
  588. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, NULL, extSurf, &sr);
  589. }
  590. }
  591. }
  592. }
  593. // terrain printed
  594. // printing borders
  595. srx = srx_init;
  596. for (int bx = 0; bx < dx; bx++, srx+=32)
  597. {
  598. sry = sry_init;
  599. for (int by = 0; by<dy; by++, sry+=32)
  600. {
  601. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  602. SDL_Rect sr;
  603. sr.x=srx;
  604. sr.y=sry;
  605. sr.h=sr.w=32;
  606. if (pos.x < 0 || pos.x >= sizes.x ||
  607. pos.y < 0 || pos.y >= sizes.y)
  608. {
  609. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  610. CSDL_Ext::blitSurface(ttiles[pos.x][pos.y][top_tile.z].terbitmap,
  611. &temp_rect,extSurf,&sr);
  612. }
  613. else
  614. {
  615. //blitting Fog of War
  616. if (!puzzleMode)
  617. {
  618. if (pos.x >= 0 &&
  619. pos.y >= 0 &&
  620. pos.x < sizes.x &&
  621. pos.y < sizes.y &&
  622. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  623. {
  624. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  625. if(hide.second)
  626. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  627. else
  628. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  629. }
  630. }
  631. //FoW blitted
  632. // TODO: these should be activable by the console
  633. #ifdef MARK_BLOCKED_POSITIONS
  634. if(map->terrain[pos.x][pos.y][top_tile.z].blocked) //temporary hiding blocked positions
  635. {
  636. SDL_Rect sr;
  637. sr.x=srx;
  638. sr.y=sry;
  639. sr.h=sr.w=32;
  640. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  641. CSDL_Ext::blitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  642. }
  643. #endif
  644. #ifdef MARK_VISITABLE_POSITIONS
  645. if(map->terrain[pos.x][pos.y][top_tile.z].visitable) //temporary hiding visitable positions
  646. {
  647. SDL_Rect sr;
  648. sr.x=srx;
  649. sr.y=sry;
  650. sr.h=sr.w=32;
  651. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  652. CSDL_Ext::blitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  653. }
  654. #endif
  655. }
  656. }
  657. }
  658. // borders printed
  659. // print grid
  660. if (settings["session"]["showGrid"].Bool())
  661. {
  662. srx = srx_init;
  663. for (int bx = 0; bx < dx; bx++, srx+=32)
  664. {
  665. sry = sry_init;
  666. for (int by = 0; by<dy; by++, sry+=32)
  667. {
  668. SDL_Rect sr;
  669. sr.x=srx;
  670. sr.y=sry;
  671. sr.h=sr.w=32;
  672. const int3 color(0x555555, 0x555555, 0x555555);
  673. if (sr.y >= extRect->y &&
  674. sr.y < extRect->y+extRect->h)
  675. for(int i=0;i<sr.w;i++)
  676. if (sr.x+i >= extRect->x &&
  677. sr.x+i < extRect->x+extRect->w)
  678. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  679. if (sr.x >= extRect->x &&
  680. sr.x < extRect->x+extRect->w)
  681. for(int i=0; i<sr.h;i++)
  682. if (sr.y+i >= extRect->y &&
  683. sr.y+i < extRect->y+extRect->h)
  684. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  685. }
  686. }
  687. }
  688. // grid
  689. //applying sepia / gray effect
  690. if(puzzleMode)
  691. {
  692. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  693. }
  694. //sepia / gray effect applied
  695. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  696. }
  697. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap ) const
  698. {
  699. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  700. // 0-1, 2-3, 4-5, 11-13, 13-14
  701. static const int visBitmaps[256] = {
  702. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  703. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  704. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  705. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  706. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  707. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  708. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  709. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  710. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  711. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  712. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  713. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  714. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  715. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  716. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  717. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  718. };
  719. auto getTile = [&](int dx, int dy)->bool
  720. {
  721. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  722. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  723. return false;
  724. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  725. };
  726. //is tile visible. arrangement: (like num keyboard)
  727. bool d7 = getTile(-1, -1), //789
  728. d8 = getTile( 0, -1), //456
  729. d9 = getTile(+1, -1), //123
  730. d4 = getTile(-1, 0),
  731. d6 = getTile(+1, 0),
  732. d1 = getTile(-1, +1),
  733. d2 = getTile( 0, +1),
  734. d3 = getTile(+1, +1);
  735. int retBitmapID = visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  736. if (retBitmapID < 0)
  737. {
  738. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  739. }
  740. if (retBitmapID >= 0)
  741. {
  742. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  743. }
  744. else
  745. {
  746. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  747. }
  748. }
  749. bool CMapHandler::printObject(const CGObjectInstance *obj)
  750. {
  751. if (!graphics->getDef(obj))
  752. processDef(obj->defInfo);
  753. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  754. const int tilesW = bitmap->w/32;
  755. const int tilesH = bitmap->h/32;
  756. for(int fx=0; fx<tilesW; ++fx)
  757. {
  758. for(int fy=0; fy<tilesH; ++fy)
  759. {
  760. SDL_Rect cr;
  761. cr.w = 32;
  762. cr.h = 32;
  763. cr.x = fx*32;
  764. cr.y = fy*32;
  765. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  766. 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)
  767. {
  768. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  769. std::vector< std::pair<const CGObjectInstance*,SDL_Rect> >::iterator i = curt.objects.begin();
  770. for(; i != curt.objects.end(); i++)
  771. {
  772. OCM_HLP cmp;
  773. if(cmp(toAdd, *i))
  774. {
  775. curt.objects.insert(i, toAdd);
  776. i = curt.objects.begin(); //to validate and avoid adding it second time
  777. break;
  778. }
  779. }
  780. if(i == curt.objects.end())
  781. curt.objects.insert(i, toAdd);
  782. }
  783. } // for(int fy=0; fy<tilesH; ++fy)
  784. } //for(int fx=0; fx<tilesW; ++fx)
  785. return true;
  786. }
  787. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  788. {
  789. CDefEssential * curd = graphics->getDef(obj);
  790. if(!curd) return false;
  791. const SDL_Surface *bitmap = curd->ourImages[0].bitmap;
  792. for(int fx=0; fx<bitmap->w/32; ++fx)
  793. {
  794. for(int fy=0; fy<bitmap->h/32; ++fy)
  795. {
  796. 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)
  797. {
  798. 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;
  799. for(size_t dd=0; dd < ctile.size(); ++dd)
  800. {
  801. if(ctile[dd].first->id==obj->id)
  802. ctile.erase(ctile.begin() + dd);
  803. }
  804. }
  805. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  806. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  807. return true;
  808. }
  809. bool CMapHandler::removeObject(CGObjectInstance *obj)
  810. {
  811. hideObject(obj);
  812. return true;
  813. }
  814. ui8 CMapHandler::getHeroFrameNum(ui8 dir, bool isMoving) const
  815. {
  816. if(isMoving)
  817. {
  818. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  819. return frame[dir];
  820. }
  821. else //if(isMoving)
  822. {
  823. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  824. return frame[dir];
  825. }
  826. }
  827. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  828. {
  829. if(ext)
  830. {
  831. if(val->x<0)
  832. {
  833. val->w += val->x;
  834. val->x = ext->x;
  835. }
  836. else
  837. {
  838. val->x += ext->x;
  839. }
  840. if(val->y<0)
  841. {
  842. val->h += val->y;
  843. val->y = ext->y;
  844. }
  845. else
  846. {
  847. val->y += ext->y;
  848. }
  849. if(val->x+val->w > ext->x+ext->w)
  850. {
  851. val->w = ext->x+ext->w-val->x;
  852. }
  853. if(val->y+val->h > ext->y+ext->h)
  854. {
  855. val->h = ext->y+ext->h-val->y;
  856. }
  857. //for sign problems
  858. if(val->h > 20000 || val->w > 20000)
  859. {
  860. val->h = val->w = 0;
  861. }
  862. }
  863. }
  864. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  865. {
  866. if(a.z!=b.z)
  867. return -1; //error!
  868. if(a.x==b.x+1 && a.y==b.y+1) //lt
  869. return 0;
  870. else if(a.x==b.x && a.y==b.y+1) //t
  871. return 1;
  872. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  873. return 2;
  874. else if(a.x==b.x-1 && a.y==b.y) //r
  875. return 3;
  876. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  877. return 4;
  878. else if(a.x==b.x && a.y==b.y-1) //b
  879. return 5;
  880. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  881. return 6;
  882. else if(a.x==b.x+1 && a.y==b.y) //l
  883. return 7;
  884. return -2; //shouldn't happen
  885. }
  886. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  887. {
  888. //works with at most 16 colors, if needed more -> increase values
  889. assert(howMany < 16);
  890. SDL_Color palette[16];
  891. for(int i=0; i<howMany; ++i)
  892. {
  893. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  894. }
  895. SDL_SetColors(img,palette,from,howMany);
  896. }
  897. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  898. {
  899. for(size_t j=0; j < terrainGraphics[7].size(); ++j)
  900. {
  901. shiftColors(terrainGraphics[7][j],246, 9);
  902. }
  903. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  904. {
  905. shiftColors(terrainGraphics[8][j],229, 12);
  906. shiftColors(terrainGraphics[8][j],242, 14);
  907. }
  908. for(size_t j=0; j < staticRiverDefs[0]->ourImages.size(); ++j)
  909. {
  910. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,183, 12);
  911. shiftColors(staticRiverDefs[0]->ourImages[j].bitmap,195, 6);
  912. }
  913. for(size_t j=0; j < staticRiverDefs[2]->ourImages.size(); ++j)
  914. {
  915. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,228, 12);
  916. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,183, 6);
  917. shiftColors(staticRiverDefs[2]->ourImages[j].bitmap,240, 6);
  918. }
  919. for(size_t j=0; j < staticRiverDefs[3]->ourImages.size(); ++j)
  920. {
  921. shiftColors(staticRiverDefs[3]->ourImages[j].bitmap,240, 9);
  922. }
  923. }
  924. CMapHandler::~CMapHandler()
  925. {
  926. delete graphics->FoWfullHide;
  927. delete graphics->FoWpartialHide;
  928. for(int i=0; i < roadDefs.size(); i++)
  929. delete roadDefs[i];
  930. for(int i=0; i < staticRiverDefs.size(); i++)
  931. delete staticRiverDefs[i];
  932. for(int i=0; i < terrainGraphics.size(); ++i)
  933. {
  934. for(int j=0; j < terrainGraphics[i].size(); ++j)
  935. SDL_FreeSurface(terrainGraphics[i][j]);
  936. }
  937. terrainGraphics.clear();
  938. }
  939. CMapHandler::CMapHandler()
  940. {
  941. frameW = frameH = 0;
  942. graphics->FoWfullHide = NULL;
  943. graphics->FoWpartialHide = NULL;
  944. }
  945. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  946. {
  947. out.clear();
  948. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  949. const TerrainTile &t = map->terrain[pos.x][pos.y][pos.z];
  950. for(std::vector < std::pair<const CGObjectInstance*,SDL_Rect> >::const_iterator i = tt.objects.begin(); i != tt.objects.end(); i++)
  951. {
  952. if(i->first->ID == 124) //Hole
  953. {
  954. out = i->first->hoverName;
  955. return;
  956. }
  957. }
  958. if(t.hasFavourableWinds())
  959. out = CGI->generaltexth->names[225]; //Favourable Winds
  960. else if(terName)
  961. out = CGI->generaltexth->terrainNames[t.tertype];
  962. }
  963. TerrainTile2::TerrainTile2()
  964. :terbitmap(0)
  965. {}