mapHandler.cpp 31 KB

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