mapHandler.cpp 32 KB

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