mapHandler.cpp 32 KB

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