mapHandler.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * mapHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "mapHandler.h"
  12. #include "CBitmapHandler.h"
  13. #include "gui/CAnimation.h"
  14. #include "gui/SDL_Extensions.h"
  15. #include "CGameInfo.h"
  16. #include "../lib/mapObjects/CGHeroInstance.h"
  17. #include "../lib/mapObjects/CObjectClassesHandler.h"
  18. #include "../lib/CGameState.h"
  19. #include "../lib/CHeroHandler.h"
  20. #include "../lib/CTownHandler.h"
  21. #include "Graphics.h"
  22. #include "../lib/mapping/CMap.h"
  23. #include "../lib/CConfigHandler.h"
  24. #include "../lib/CGeneralTextHandler.h"
  25. #include "../lib/GameConstants.h"
  26. #include "../lib/CStopWatch.h"
  27. #include "CMT.h"
  28. #include "../lib/CRandomGenerator.h"
  29. #define ADVOPT (conf.go()->ac)
  30. static bool objectBlitOrderSorter(const TerrainTileObject & a, const TerrainTileObject & b)
  31. {
  32. return CMapHandler::compareObjectBlitOrder(a.obj, b.obj);
  33. }
  34. struct NeighborTilesInfo
  35. {
  36. bool d7, //789
  37. d8, //456
  38. d9, //123
  39. d4,
  40. d5,
  41. d6,
  42. d1,
  43. d2,
  44. d3;
  45. NeighborTilesInfo(const int3 & pos, const int3 & sizes, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap)
  46. {
  47. auto getTile = [&](int dx, int dy)->bool
  48. {
  49. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  50. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  51. return false;
  52. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  53. };
  54. d7 = getTile(-1, -1); //789
  55. d8 = getTile( 0, -1); //456
  56. d9 = getTile(+1, -1); //123
  57. d4 = getTile(-1, 0);
  58. d5 = visibilityMap[pos.x][pos.y][pos.z];
  59. d6 = getTile(+1, 0);
  60. d1 = getTile(-1, +1);
  61. d2 = getTile( 0, +1);
  62. d3 = getTile(+1, +1);
  63. }
  64. bool areAllHidden() const
  65. {
  66. return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d8 );
  67. }
  68. int getBitmapID() const
  69. {
  70. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  71. // 0-1, 2-3, 4-5, 11-13, 12-14
  72. static const int visBitmaps[256] = {
  73. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  74. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  75. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  76. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  77. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  78. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  79. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  80. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  81. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  82. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  83. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  84. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  85. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  86. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  87. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  88. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  89. };
  90. return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  91. }
  92. };
  93. void CMapHandler::prepareFOWDefs()
  94. {
  95. //assume all frames in group 0
  96. size_t size = graphics->fogOfWarFullHide->size(0);
  97. FoWfullHide.resize(size);
  98. for(size_t frame = 0; frame < size; frame++)
  99. FoWfullHide[frame] = graphics->fogOfWarFullHide->getImage(frame);
  100. //initialization of type of full-hide image
  101. hideBitmap.resize(sizes.x);
  102. for (auto & elem : hideBitmap)
  103. {
  104. elem.resize(sizes.y);
  105. }
  106. for (auto & elem : hideBitmap)
  107. {
  108. for (int j = 0; j < sizes.y; ++j)
  109. {
  110. elem[j].resize(sizes.z);
  111. for(int k = 0; k < sizes.z; ++k)
  112. {
  113. elem[j][k] = CRandomGenerator::getDefault().nextInt(size - 1);
  114. }
  115. }
  116. }
  117. size = graphics->fogOfWarPartialHide->size(0);
  118. FoWpartialHide.resize(size);
  119. for(size_t frame = 0; frame < size; frame++)
  120. FoWpartialHide[frame] = graphics->fogOfWarPartialHide->getImage(frame);
  121. }
  122. EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim /* = false */)
  123. {
  124. assert(info);
  125. bool hasActiveFade = updateObjectsFade();
  126. resolveBlitter(info)->blit(targetSurface, info);
  127. return hasActiveFade ? EMapAnimRedrawStatus::REDRAW_REQUESTED : EMapAnimRedrawStatus::OK;
  128. }
  129. void CMapHandler::initTerrainGraphics()
  130. {
  131. static const std::vector<std::string> TERRAIN_FILES =
  132. {
  133. "DIRTTL",
  134. "SANDTL",
  135. "GRASTL",
  136. "SNOWTL",
  137. "SWMPTL",
  138. "ROUGTL",
  139. "SUBBTL",
  140. "LAVATL",
  141. "WATRTL",
  142. "ROCKTL"
  143. };
  144. static const std::vector<std::string> ROAD_FILES =
  145. {
  146. "dirtrd",
  147. "gravrd",
  148. "cobbrd"
  149. };
  150. static const std::vector<std::string> RIVER_FILES =
  151. {
  152. "clrrvr",
  153. "icyrvr",
  154. "mudrvr",
  155. "lavrvr"
  156. };
  157. auto loadFlipped = [this](int types, TFlippedAnimations & animation, TFlippedCache & cache, const std::vector<std::string> & files)
  158. {
  159. animation.resize(types);
  160. cache.resize(types);
  161. //no rotation and basic setup
  162. for(int i = 0; i < types; i++)
  163. {
  164. animation[i][0] = make_unique<CAnimation>(files[i]);
  165. animation[i][0]->preload();
  166. const size_t views = animation[i][0]->size(0);
  167. cache[i].resize(views);
  168. for(int j = 0; j < views; j++)
  169. cache[i][j][0] = animation[i][0]->getImage(j);
  170. }
  171. for(int rotation = 1; rotation < 4; rotation++)
  172. {
  173. for(int i = 0; i < types; i++)
  174. {
  175. animation[i][rotation] = make_unique<CAnimation>(files[i]);
  176. animation[i][rotation]->preload();
  177. const size_t views = animation[i][rotation]->size(0);
  178. for(int j = 0; j < views; j++)
  179. {
  180. IImage * image = animation[i][rotation]->getImage(j);
  181. if(rotation == 2 || rotation == 3)
  182. image->horizontalFlip();
  183. if(rotation == 1 || rotation == 3)
  184. image->verticalFlip();
  185. cache[i][j][rotation] = image;
  186. }
  187. }
  188. }
  189. };
  190. loadFlipped(GameConstants::TERRAIN_TYPES, terrainAnimations, terrainImages, TERRAIN_FILES);
  191. loadFlipped(3, roadAnimations, roadImages, ROAD_FILES);
  192. loadFlipped(4, riverAnimations, riverImages, RIVER_FILES);
  193. // Create enough room for the whole map and its frame
  194. ttiles.resize(sizes.x, frameW, frameW);
  195. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  196. {
  197. ttiles[i].resize(sizes.y, frameH, frameH);
  198. }
  199. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  200. {
  201. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  202. ttiles[i][j].resize(sizes.z, 0, 0);
  203. }
  204. }
  205. void CMapHandler::initBorderGraphics()
  206. {
  207. egdeImages.resize(egdeAnimation->size(0));
  208. for(size_t i = 0; i < egdeImages.size(); i++)
  209. egdeImages[i] = egdeAnimation->getImage(i);
  210. edgeFrames.resize(sizes.x, frameW, frameW);
  211. for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
  212. {
  213. edgeFrames[i].resize(sizes.y, frameH, frameH);
  214. }
  215. for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
  216. {
  217. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  218. edgeFrames[i][j].resize(sizes.z, 0, 0);
  219. }
  220. auto & rand = CRandomGenerator::getDefault();
  221. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  222. {
  223. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  224. {
  225. for(int k=0; k<sizes.z; ++k) //by levels
  226. {
  227. ui8 terBitmapNum = 0;
  228. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  229. {
  230. if(i==-1 && j==-1)
  231. terBitmapNum = 16;
  232. else if(i==-1 && j==(sizes.y))
  233. terBitmapNum = 19;
  234. else if(i==(sizes.x) && j==-1)
  235. terBitmapNum = 17;
  236. else if(i==(sizes.x) && j==(sizes.y))
  237. terBitmapNum = 18;
  238. else if(j == -1 && i > -1 && i < sizes.x)
  239. terBitmapNum = rand.nextInt(22, 23);
  240. else if(i == -1 && j > -1 && j < sizes.y)
  241. terBitmapNum = rand.nextInt(33, 34);
  242. else if(j == sizes.y && i >-1 && i < sizes.x)
  243. terBitmapNum = rand.nextInt(29, 30);
  244. else if(i == sizes.x && j > -1 && j < sizes.y)
  245. terBitmapNum = rand.nextInt(25, 26);
  246. else
  247. terBitmapNum = rand.nextInt(15);
  248. }
  249. edgeFrames[i][j][k] = terBitmapNum;
  250. }
  251. }
  252. }
  253. }
  254. void CMapHandler::initObjectRects()
  255. {
  256. //initializing objects / rects
  257. for(auto & elem : map->objects)
  258. {
  259. const CGObjectInstance *obj = elem;
  260. if( !obj
  261. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  262. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  263. {
  264. continue;
  265. }
  266. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  267. //no animation at all
  268. if(!animation)
  269. continue;
  270. //empty animation
  271. if(animation->size(0) == 0)
  272. continue;
  273. IImage * image = animation->getImage(0,0);
  274. for(int fx=0; fx < obj->getWidth(); ++fx)
  275. {
  276. for(int fy=0; fy < obj->getHeight(); ++fy)
  277. {
  278. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  279. SDL_Rect cr;
  280. cr.w = 32;
  281. cr.h = 32;
  282. cr.x = image->width() - fx * 32 - 32;
  283. cr.y = image->height() - fy * 32 - 32;
  284. TerrainTileObject toAdd(obj,cr);
  285. if( map->isInTheMap(currTile) && // within map
  286. cr.x + cr.w > 0 && // image has data on this tile
  287. cr.y + cr.h > 0 &&
  288. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  289. )
  290. {
  291. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  292. }
  293. }
  294. }
  295. }
  296. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  297. {
  298. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  299. {
  300. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  301. {
  302. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
  303. }
  304. }
  305. }
  306. }
  307. void CMapHandler::init()
  308. {
  309. CStopWatch th;
  310. th.getDiff();
  311. // Size of visible terrain.
  312. int mapW = conf.go()->ac.advmapW;
  313. int mapH = conf.go()->ac.advmapH;
  314. //sizes of terrain
  315. sizes.x = map->width;
  316. sizes.y = map->height;
  317. sizes.z = map->twoLevel ? 2 : 1;
  318. // Total number of visible tiles. Subtract the center tile, then
  319. // compute the number of tiles on each side, and reassemble.
  320. int t1, t2;
  321. t1 = (mapW-32)/2;
  322. t2 = mapW - 32 - t1;
  323. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  324. t1 = (mapH-32)/2;
  325. t2 = mapH - 32 - t1;
  326. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  327. // Size of the frame around the map. In extremes positions, the
  328. // frame must not be on the center of the map, but right on the
  329. // edge of the center tile.
  330. frameW = (mapW+31) /32 / 2;
  331. frameH = (mapH+31) /32 / 2;
  332. offsetX = (mapW - (2*frameW+1)*32)/2;
  333. offsetY = (mapH - (2*frameH+1)*32)/2;
  334. prepareFOWDefs();
  335. initTerrainGraphics();
  336. initBorderGraphics();
  337. logGlobal->infoStream()<<"\tPreparing FoW, terrain, roads, rivers, borders: "<<th.getDiff();
  338. initObjectRects();
  339. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  340. }
  341. CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
  342. {
  343. if (info->scaled)
  344. return worldViewBlitter;
  345. if (info->puzzleMode)
  346. return puzzleViewBlitter;
  347. return normalBlitter;
  348. }
  349. void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  350. {
  351. source->draw(targetSurf, destRect, sourceRect);
  352. }
  353. void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
  354. {
  355. info = drawingInfo;
  356. // Width and height of the portion of the map to process. Units in tiles.
  357. tileCount.x = parent->tilesW;
  358. tileCount.y = parent->tilesH;
  359. topTile = info->topTile;
  360. initPos.x = parent->offsetX + info->drawBounds->x;
  361. initPos.y = parent->offsetY + info->drawBounds->y;
  362. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  363. // If moving, we need to add an extra column/line
  364. if (info->movement.x != 0)
  365. {
  366. tileCount.x++;
  367. initPos.x += info->movement.x;
  368. if (info->movement.x > 0)
  369. {
  370. // Moving right. We still need to draw the old tile on the
  371. // left, so adjust our referential
  372. topTile.x--;
  373. initPos.x -= tileSize;
  374. }
  375. }
  376. if (info->movement.y != 0)
  377. {
  378. tileCount.y++;
  379. initPos.y += info->movement.y;
  380. if (info->movement.y > 0)
  381. {
  382. // Moving down. We still need to draw the tile on the top,
  383. // so adjust our referential.
  384. topTile.y--;
  385. initPos.y -= tileSize;
  386. }
  387. }
  388. // Reduce sizes if we go out of the full map.
  389. if (topTile.x < -parent->frameW)
  390. topTile.x = -parent->frameW;
  391. if (topTile.y < -parent->frameH)
  392. topTile.y = -parent->frameH;
  393. if (topTile.x + tileCount.x > parent->sizes.x + parent->frameW)
  394. tileCount.x = parent->sizes.x + parent->frameW - topTile.x;
  395. if (topTile.y + tileCount.y > parent->sizes.y + parent->frameH)
  396. tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
  397. }
  398. SDL_Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
  399. {
  400. SDL_Rect prevClip;
  401. SDL_GetClipRect(targetSurf, &prevClip);
  402. SDL_SetClipRect(targetSurf, info->drawBounds);
  403. return prevClip;
  404. }
  405. CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
  406. : CMapBlitter(parent)
  407. {
  408. tileSize = 32;
  409. halfTileSizeCeil = 16;
  410. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  411. }
  412. IImage * CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
  413. {
  414. int ownerIndex = 0;
  415. if(owner < PlayerColor::PLAYER_LIMIT)
  416. {
  417. ownerIndex = owner.getNum() * 19;
  418. }
  419. else if (owner == PlayerColor::NEUTRAL)
  420. {
  421. ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
  422. }
  423. switch(id)
  424. {
  425. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  426. case Obj::MONOLITH_ONE_WAY_EXIT:
  427. case Obj::MONOLITH_TWO_WAY:
  428. return info->icons->getImage((int)EWorldViewIcon::TELEPORT);
  429. case Obj::SUBTERRANEAN_GATE:
  430. return info->icons->getImage((int)EWorldViewIcon::GATE);
  431. case Obj::ARTIFACT:
  432. return info->icons->getImage((int)EWorldViewIcon::ARTIFACT);
  433. case Obj::TOWN:
  434. return info->icons->getImage((int)EWorldViewIcon::TOWN + ownerIndex);
  435. case Obj::HERO:
  436. return info->icons->getImage((int)EWorldViewIcon::HERO + ownerIndex);
  437. case Obj::MINE:
  438. return info->icons->getImage((int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex);
  439. case Obj::RESOURCE:
  440. return info->icons->getImage((int)EWorldViewIcon::RES_WOOD + subId + ownerIndex);
  441. }
  442. return nullptr;
  443. }
  444. void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
  445. {
  446. bool outsideLeft = topTile.x < 0;
  447. bool outsideTop = topTile.y < 0;
  448. bool outsideRight = std::max(0, topTile.x) + tileCount.x > parent->sizes.x;
  449. bool outsideBottom = std::max(0, topTile.y) + tileCount.y > parent->sizes.y;
  450. if (tileCount.x > parent->sizes.x)
  451. topTile.x = parent->sizes.x / 2 - tileCount.x / 2; // center viewport if the whole map can fit into the screen at once
  452. else if (outsideLeft)
  453. {
  454. if (outsideRight)
  455. topTile.x = parent->sizes.x / 2 - tileCount.x / 2;
  456. else
  457. topTile.x = 0;
  458. }
  459. else if (outsideRight)
  460. topTile.x = parent->sizes.x - tileCount.x;
  461. if (tileCount.y > parent->sizes.y)
  462. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  463. else if (outsideTop)
  464. {
  465. if (outsideBottom)
  466. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  467. else
  468. topTile.y = 0;
  469. }
  470. else if (outsideBottom)
  471. topTile.y = parent->sizes.y - tileCount.y;
  472. }
  473. void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  474. {
  475. IImage * scaled = parent->cache.requestWorldViewCacheOrCreate(cacheType, source);
  476. if(scaled)
  477. scaled->draw(targetSurf, destRect, sourceRect);
  478. }
  479. void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  480. {
  481. auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
  482. {
  483. IImage * wvIcon = this->objectToIcon(id, subId, owner);
  484. if(nullptr != wvIcon)
  485. {
  486. // centering icon on the object
  487. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  488. wvIcon->draw(targetSurf, dest.x, dest.y);
  489. }
  490. };
  491. auto & objects = tile.objects;
  492. for(auto & object : objects)
  493. {
  494. const CGObjectInstance * obj = object.obj;
  495. const bool sameLevel = obj->pos.z == pos.z;
  496. const bool isVisible = (*info->visibilityMap)[pos.x][pos.y][pos.z];
  497. const bool isVisitable = obj->visitableAt(pos.x, pos.y);
  498. if(sameLevel && isVisible && isVisitable)
  499. drawIcon(obj->ID, obj->subID, obj->tempOwner);
  500. }
  501. }
  502. void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  503. {
  504. if(nullptr == info->additionalIcons)
  505. return;
  506. const int3 bottomRight = pos + tileCount;
  507. for(const ObjectPosInfo & iconInfo : *(info->additionalIcons))
  508. {
  509. if( iconInfo.pos.z != pos.z)
  510. continue;
  511. if((iconInfo.pos.x < topTile.x) || (iconInfo.pos.y < topTile.y))
  512. continue;
  513. if((iconInfo.pos.x > bottomRight.x) || (iconInfo.pos.y > bottomRight.y))
  514. continue;
  515. realPos.x = initPos.x + (iconInfo.pos.x - topTile.x) * tileSize;
  516. realPos.y = initPos.x + (iconInfo.pos.y - topTile.y) * tileSize;
  517. IImage * wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
  518. if(nullptr != wvIcon)
  519. {
  520. // centering icon on the object
  521. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  522. wvIcon->draw(targetSurf, dest.x, dest.y);
  523. }
  524. }
  525. }
  526. void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  527. {
  528. if (moving)
  529. return;
  530. CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
  531. }
  532. void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  533. {
  534. if (moving)
  535. return;
  536. Rect scaledSourceRect(sourceRect->x * info->scale, sourceRect->y * info->scale, sourceRect->w, sourceRect->h);
  537. CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
  538. }
  539. void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
  540. {
  541. Rect destRect(realTileRect);
  542. ui8 rotation = tinfo.extTileFlags % 4;
  543. drawElement(EMapCacheType::TERRAIN, parent->terrainImages[tinfo.terType][tinfo.terView][rotation], nullptr, targetSurf, &destRect);
  544. }
  545. void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
  546. {
  547. info = drawingInfo;
  548. parent->cache.updateWorldViewScale(info->scale);
  549. topTile = info->topTile;
  550. tileSize = (int) floorf(32.0f * info->scale);
  551. halfTileSizeCeil = (int)ceilf(tileSize / 2.0f);
  552. tileCount.x = (int) ceilf((float)info->drawBounds->w / tileSize);
  553. tileCount.y = (int) ceilf((float)info->drawBounds->h / tileSize);
  554. initPos.x = info->drawBounds->x;
  555. initPos.y = info->drawBounds->y;
  556. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  557. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  558. calculateWorldViewCameraPos();
  559. }
  560. SDL_Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
  561. {
  562. SDL_Rect prevClip;
  563. SDL_FillRect(targetSurf, info->drawBounds, SDL_MapRGB(targetSurf->format, 0, 0, 0));
  564. // makes the clip area smaller if the map is smaller than the screen frame
  565. // (actually, it could be made 1 tile bigger so that overlay icons on edge tiles could be drawn partly outside)
  566. Rect clipRect(std::max<int>(info->drawBounds->x, info->drawBounds->x - topTile.x * tileSize),
  567. std::max<int>(info->drawBounds->y, info->drawBounds->y - topTile.y * tileSize),
  568. std::min<int>(info->drawBounds->w, parent->sizes.x * tileSize),
  569. std::min<int>(info->drawBounds->h, parent->sizes.y * tileSize));
  570. SDL_GetClipRect(targetSurf, &prevClip);
  571. SDL_SetClipRect(targetSurf, &clipRect); //preventing blitting outside of that rect
  572. return prevClip;
  573. }
  574. CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
  575. : CMapBlitter(parent)
  576. {
  577. }
  578. void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  579. {
  580. CMapBlitter::drawObjects(targetSurf, tile);
  581. // grail X mark
  582. if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
  583. {
  584. const IImage * mark = graphics->heroMoveArrows->getImage(0);
  585. mark->draw(targetSurf,realTileRect.x,realTileRect.y);
  586. }
  587. }
  588. void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
  589. {
  590. CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
  591. }
  592. bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
  593. {
  594. if (!CMapBlitter::canDrawObject(obj))
  595. return false;
  596. //don't print flaggable objects in puzzle mode
  597. if (obj->isVisitable())
  598. return false;
  599. if(std::find(unblittableObjects.begin(), unblittableObjects.end(), obj->ID) != unblittableObjects.end())
  600. return false;
  601. return true;
  602. }
  603. CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
  604. : CMapNormalBlitter(parent)
  605. {
  606. unblittableObjects.push_back(Obj::HOLE);
  607. }
  608. void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
  609. {
  610. Rect destRect(realTileRect);
  611. drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
  612. }
  613. void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  614. {
  615. //nothing to do here
  616. }
  617. void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  618. {
  619. drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
  620. }
  621. void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  622. {
  623. Rect dstRect(realTileRect);
  624. drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
  625. }
  626. void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  627. {
  628. auto & objects = tile.objects;
  629. for(auto & object : objects)
  630. {
  631. if (object.fadeAnimKey >= 0)
  632. {
  633. auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
  634. if (fadeIter != parent->fadeAnims.end())
  635. {
  636. // this object is currently fading, so skip normal drawing
  637. Rect r2(realTileRect);
  638. CFadeAnimation * fade = (*fadeIter).second.second;
  639. fade->draw(targetSurf, nullptr, &r2);
  640. continue;
  641. }
  642. logGlobal->errorStream() << "Fading map object with missing fade anim : " << object.fadeAnimKey;
  643. continue;
  644. }
  645. const CGObjectInstance * obj = object.obj;
  646. if (!obj)
  647. {
  648. logGlobal->errorStream() << "Stray map object that isn't fading";
  649. continue;
  650. }
  651. if (!canDrawObject(obj))
  652. continue;
  653. auto objData = findObjectBitmap(obj, info->anim);
  654. if (objData.objBitmap)
  655. {
  656. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  657. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  658. if (objData.flagBitmap)
  659. {
  660. if (objData.isMoving)
  661. {
  662. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  663. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  664. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  665. }
  666. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  667. {
  668. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  669. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  676. {
  677. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  678. {
  679. ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
  680. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  681. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  682. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfoUpper->roadType - 1][tinfoUpper->roadDir][rotation],
  683. &source, targetSurf, &dest);
  684. }
  685. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  686. {
  687. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  688. Rect source(0, 0, tileSize, halfTileSizeCeil);
  689. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  690. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfo.roadType - 1][tinfo.roadDir][rotation],
  691. &source, targetSurf, &dest);
  692. }
  693. }
  694. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  695. {
  696. Rect destRect(realTileRect);
  697. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  698. drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType-1][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
  699. }
  700. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  701. {
  702. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  703. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  704. if (retBitmapID < 0)
  705. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  706. const IImage * image = nullptr;
  707. if (retBitmapID >= 0)
  708. image = parent->FoWpartialHide.at(retBitmapID);
  709. else
  710. image = parent->FoWfullHide.at(-retBitmapID - 1);
  711. Rect destRect(realTileRect);
  712. drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
  713. }
  714. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  715. {
  716. init(info);
  717. auto prevClip = clip(targetSurf);
  718. pos = int3(0, 0, topTile.z);
  719. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  720. {
  721. if (pos.x < 0 || pos.x >= parent->sizes.x)
  722. continue;
  723. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  724. {
  725. if (pos.y < 0 || pos.y >= parent->sizes.y)
  726. continue;
  727. const bool isVisible = canDrawCurrentTile();
  728. realTileRect.x = realPos.x;
  729. realTileRect.y = realPos.y;
  730. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  731. const TerrainTile & tinfo = parent->map->getTile(pos);
  732. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  733. if(isVisible || info->showAllTerrain)
  734. {
  735. drawTileTerrain(targetSurf, tinfo, tile);
  736. if (tinfo.riverType)
  737. drawRiver(targetSurf, tinfo);
  738. drawRoad(targetSurf, tinfo, tinfoUpper);
  739. }
  740. if(isVisible)
  741. drawObjects(targetSurf, tile);
  742. }
  743. }
  744. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  745. {
  746. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  747. {
  748. realTileRect.x = realPos.x;
  749. realTileRect.y = realPos.y;
  750. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  751. pos.y < 0 || pos.y >= parent->sizes.y)
  752. {
  753. drawFrame(targetSurf);
  754. }
  755. else
  756. {
  757. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  758. if (!(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  759. drawFow(targetSurf);
  760. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  761. drawTileOverlay(targetSurf, tile);
  762. // drawDebugVisitables()
  763. if (settings["session"]["showBlock"].Bool())
  764. {
  765. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  766. {
  767. static SDL_Surface * block = nullptr;
  768. if (!block)
  769. block = BitmapHandler::loadBitmap("blocked");
  770. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  771. }
  772. }
  773. if (settings["session"]["showVisit"].Bool())
  774. {
  775. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  776. {
  777. static SDL_Surface * visit = nullptr;
  778. if (!visit)
  779. visit = BitmapHandler::loadBitmap("visitable");
  780. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. drawOverlayEx(targetSurf);
  787. // drawDebugGrid()
  788. if (settings["session"]["showGrid"].Bool())
  789. {
  790. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  791. {
  792. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  793. {
  794. const int3 color(0x555555, 0x555555, 0x555555);
  795. if (realPos.y >= info->drawBounds->y &&
  796. realPos.y < info->drawBounds->y + info->drawBounds->h)
  797. for(int i = 0; i < tileSize; i++)
  798. if (realPos.x + i >= info->drawBounds->x &&
  799. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  800. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  801. if (realPos.x >= info->drawBounds->x &&
  802. realPos.x < info->drawBounds->x + info->drawBounds->w)
  803. for(int i = 0; i < tileSize; i++)
  804. if (realPos.y + i >= info->drawBounds->y &&
  805. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  806. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  807. }
  808. }
  809. }
  810. postProcessing(targetSurf);
  811. SDL_SetClipRect(targetSurf, &prevClip);
  812. }
  813. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  814. {
  815. if(hero && hero->moveDir && hero->type) //it's hero or boat
  816. {
  817. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  818. {
  819. logGlobal->errorStream() << "A neutral hero (" << hero->name << ") at " << hero->pos << ". Should not happen!";
  820. return CMapHandler::AnimBitmapHolder();
  821. }
  822. //pick graphics of hero (or boat if hero is sailing)
  823. std::shared_ptr<CAnimation> animation;
  824. if (hero->boat)
  825. animation = graphics->boatAnimations[hero->boat->subID];
  826. else
  827. animation = graphics->heroAnimations[hero->appearance.animationFile];
  828. bool moving = !hero->isStanding;
  829. int group = getHeroFrameGroup(hero->moveDir, moving);
  830. if(animation->size(group) > 0)
  831. {
  832. int frame = anim % animation->size(group);
  833. IImage * heroImage = animation->getImage(frame, group);
  834. //get flag overlay only if we have main image
  835. IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
  836. return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
  837. }
  838. }
  839. return CMapHandler::AnimBitmapHolder();
  840. }
  841. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  842. {
  843. auto animation = graphics->boatAnimations.at(boat->subID);
  844. int group = getHeroFrameGroup(boat->direction, false);
  845. if(animation->size(group) > 0)
  846. return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
  847. else
  848. return CMapHandler::AnimBitmapHolder();
  849. }
  850. IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  851. {
  852. if (!hero)
  853. return nullptr;
  854. if (hero->boat)
  855. return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
  856. return findHeroFlagBitmap(hero, anim, color, group);
  857. }
  858. IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  859. {
  860. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
  861. }
  862. IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
  863. {
  864. int boatType = boat->subID;
  865. if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
  866. {
  867. logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID;
  868. return nullptr;
  869. }
  870. const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
  871. int colorIndex = color->getNum();
  872. if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
  873. {
  874. logGlobal->errorStream() << "Invalid player color " << colorIndex;
  875. return nullptr;
  876. }
  877. return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
  878. }
  879. IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
  880. {
  881. if(moving)
  882. return animation->getImage(anim % animation->size(group), group);
  883. else
  884. return animation->getImage((anim / 4) % animation->size(group), group);
  885. }
  886. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  887. {
  888. if (!obj)
  889. return CMapHandler::AnimBitmapHolder();
  890. if (obj->ID == Obj::HERO)
  891. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  892. if (obj->ID == Obj::BOAT)
  893. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  894. // normal object
  895. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  896. IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % animation->size());
  897. bitmap->setFlagColor(obj->tempOwner);
  898. return CMapHandler::AnimBitmapHolder(bitmap);
  899. }
  900. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  901. {
  902. auto i = parent->animationPhase.find(object);
  903. if(i == parent->animationPhase.end())
  904. {
  905. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  906. parent->animationPhase[object] = ret;
  907. return ret;
  908. }
  909. return i->second;
  910. }
  911. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  912. {
  913. //checking if object has non-empty graphic on this tile
  914. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  915. }
  916. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  917. {
  918. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  919. return !neighbors.areAllHidden();
  920. }
  921. ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
  922. {
  923. if(isMoving)
  924. {
  925. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  926. return frame[dir];
  927. }
  928. else //if(isMoving)
  929. {
  930. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  931. return frame[dir];
  932. }
  933. }
  934. bool CMapHandler::updateObjectsFade()
  935. {
  936. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  937. {
  938. int3 pos = (*iter).second.first;
  939. CFadeAnimation * anim = (*iter).second.second;
  940. anim->update();
  941. if (anim->isFading())
  942. ++iter;
  943. else // fade finished
  944. {
  945. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  946. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  947. {
  948. if ((*objIter).fadeAnimKey == (*iter).first)
  949. {
  950. logAnim->traceStream() << "Fade anim finished for obj at " << pos << "; remaining: " << (fadeAnims.size() - 1);
  951. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  952. objs.erase(objIter); // if this was fadeout, remove the object from the map
  953. else
  954. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  955. break;
  956. }
  957. }
  958. delete (*iter).second.second;
  959. iter = fadeAnims.erase(iter);
  960. }
  961. }
  962. return !fadeAnims.empty();
  963. }
  964. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  965. {
  966. SDL_Surface * fadeBitmap;
  967. assert(obj.obj);
  968. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  969. if (objData.objBitmap)
  970. {
  971. if (objData.isMoving) // ignore fading of moving objects (for now?)
  972. {
  973. logAnim->debugStream() << "Ignoring fade of moving object";
  974. return false;
  975. }
  976. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  977. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  978. objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
  979. if (objData.flagBitmap)
  980. {
  981. if (obj.obj->pos.x - 1 == pos.x && obj.obj->pos.y - 1 == pos.y) // -1 to draw flag in top-center instead of right-bottom; kind of a hack
  982. {
  983. Rect flagSrcRect(32, 0, 32, 32);
  984. objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
  985. }
  986. }
  987. auto anim = new CFadeAnimation();
  988. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  989. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  990. obj.fadeAnimKey = fadeAnimCounter;
  991. logAnim->traceStream() << "Fade anim started for obj " << obj.obj->ID
  992. << " at " << pos << "; anim count: " << fadeAnims.size();
  993. return true;
  994. }
  995. return false;
  996. }
  997. bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = false */)
  998. {
  999. auto animation = graphics->getAnimation(obj);
  1000. if(!animation)
  1001. return false;
  1002. IImage * bitmap = animation->getImage(0);
  1003. if(!bitmap)
  1004. return false;
  1005. const int tilesW = bitmap->width()/32;
  1006. const int tilesH = bitmap->height()/32;
  1007. for(int fx=0; fx<tilesW; ++fx)
  1008. {
  1009. for(int fy=0; fy<tilesH; ++fy)
  1010. {
  1011. SDL_Rect cr;
  1012. cr.w = 32;
  1013. cr.h = 32;
  1014. cr.x = fx*32;
  1015. cr.y = fy*32;
  1016. TerrainTileObject toAdd(obj, cr);
  1017. 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)
  1018. {
  1019. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1020. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1021. if (fadein && ADVOPT.objectFading)
  1022. {
  1023. startObjectFade(toAdd, true, pos);
  1024. }
  1025. auto i = curt.objects.begin();
  1026. for(; i != curt.objects.end(); i++)
  1027. {
  1028. if(objectBlitOrderSorter(toAdd, *i))
  1029. {
  1030. curt.objects.insert(i, toAdd);
  1031. i = curt.objects.begin(); //to validate and avoid adding it second time
  1032. break;
  1033. }
  1034. }
  1035. if(i == curt.objects.end())
  1036. curt.objects.insert(i, toAdd);
  1037. }
  1038. }
  1039. }
  1040. return true;
  1041. }
  1042. bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
  1043. {
  1044. //optimized version which reveals weird bugs with missing def name
  1045. //auto pos = obj->pos;
  1046. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1047. //{
  1048. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1049. // {
  1050. // int3 t(i, j, pos.z);
  1051. // if (!map->isInTheMap(t))
  1052. // continue;
  1053. // auto &objs = ttiles[i][j][pos.z].objects;
  1054. // for (size_t x = 0; x < objs.size(); x++)
  1055. // {
  1056. // auto ourObj = objs[x].obj;
  1057. // if (ourObj && ourObj->id == obj->id)
  1058. // {
  1059. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1060. // {
  1061. // if (startObjectFade(objs[x], false, t))
  1062. // objs[x].obj = nullptr; //set original pointer to null
  1063. // else
  1064. // objs.erase(objs.begin() + x);
  1065. // }
  1066. // else
  1067. // objs.erase(objs.begin() + x);
  1068. // break;
  1069. // }
  1070. // }
  1071. // }
  1072. //}
  1073. for (size_t i = 0; i<map->width; i++)
  1074. {
  1075. for (size_t j = 0; j<map->height; j++)
  1076. {
  1077. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1078. {
  1079. auto &objs = ttiles[i][j][k].objects;
  1080. for (size_t x = 0; x < objs.size(); x++)
  1081. {
  1082. if (objs[x].obj && objs[x].obj->id == obj->id)
  1083. {
  1084. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1085. {
  1086. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1087. objs[x].obj = nullptr;
  1088. else
  1089. objs.erase(objs.begin() + x);
  1090. }
  1091. else
  1092. objs.erase(objs.begin() + x);
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. }
  1099. return true;
  1100. }
  1101. bool CMapHandler::canStartHeroMovement()
  1102. {
  1103. return fadeAnims.empty(); // don't allow movement during fade animation
  1104. }
  1105. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1106. {
  1107. for(auto & elem : terrainImages[7])
  1108. {
  1109. for(IImage * img : elem)
  1110. img->shiftPalette(246, 9);
  1111. }
  1112. for(auto & elem : terrainImages[8])
  1113. {
  1114. for(IImage * img : elem)
  1115. {
  1116. img->shiftPalette(229, 12);
  1117. img->shiftPalette(242, 14);
  1118. }
  1119. }
  1120. for(auto & elem : riverImages[0])
  1121. {
  1122. for(IImage * img : elem)
  1123. {
  1124. img->shiftPalette(183, 12);
  1125. img->shiftPalette(195, 6);
  1126. }
  1127. }
  1128. for(auto & elem : riverImages[2])
  1129. {
  1130. for(IImage * img : elem)
  1131. {
  1132. img->shiftPalette(228, 12);
  1133. img->shiftPalette(183, 6);
  1134. img->shiftPalette(240, 6);
  1135. }
  1136. }
  1137. for(auto & elem : riverImages[3])
  1138. {
  1139. for(IImage * img : elem)
  1140. img->shiftPalette(240, 9);
  1141. }
  1142. }
  1143. CMapHandler::~CMapHandler()
  1144. {
  1145. delete normalBlitter;
  1146. delete worldViewBlitter;
  1147. delete puzzleViewBlitter;
  1148. for (auto & elem : fadeAnims)
  1149. {
  1150. delete elem.second.second;
  1151. }
  1152. }
  1153. CMapHandler::CMapHandler()
  1154. {
  1155. frameW = frameH = 0;
  1156. normalBlitter = new CMapNormalBlitter(this);
  1157. worldViewBlitter = new CMapWorldViewBlitter(this);
  1158. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1159. fadeAnimCounter = 0;
  1160. egdeAnimation = make_unique<CAnimation>("EDG");
  1161. egdeAnimation->preload();
  1162. }
  1163. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1164. {
  1165. out.clear();
  1166. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1167. const TerrainTile &t = map->getTile(pos);
  1168. for(auto & elem : tt.objects)
  1169. {
  1170. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1171. {
  1172. out = elem.obj->getObjectName();
  1173. return;
  1174. }
  1175. }
  1176. if(t.hasFavorableWinds())
  1177. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1178. else if(terName)
  1179. {
  1180. out = CGI->generaltexth->terrainNames[t.terType];
  1181. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1182. {
  1183. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1184. }
  1185. }
  1186. }
  1187. void CMapHandler::discardWorldViewCache()
  1188. {
  1189. cache.discardWorldViewCache();
  1190. }
  1191. void CMapHandler::CMapCache::discardWorldViewCache()
  1192. {
  1193. for(auto & cache : data)
  1194. cache.clear();
  1195. logAnim->debug("Discarded world view cache");
  1196. }
  1197. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1198. {
  1199. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1200. discardWorldViewCache();
  1201. worldViewCachedScale = scale;
  1202. }
  1203. IImage * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, const IImage * fullSurface)
  1204. {
  1205. intptr_t key = (intptr_t) fullSurface;
  1206. auto & cache = data[(ui8)type];
  1207. auto iter = cache.find(key);
  1208. if(iter == cache.end())
  1209. {
  1210. auto scaled = fullSurface->scaleFast(worldViewCachedScale);
  1211. IImage * ret = scaled.get();
  1212. cache[key] = std::move(scaled);
  1213. return ret;
  1214. }
  1215. else
  1216. {
  1217. return (*iter).second.get();
  1218. }
  1219. }
  1220. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1221. {
  1222. if (!a)
  1223. return true;
  1224. if (!b)
  1225. return false;
  1226. if (a->appearance.printPriority != b->appearance.printPriority)
  1227. return a->appearance.printPriority > b->appearance.printPriority;
  1228. if(a->pos.y != b->pos.y)
  1229. return a->pos.y < b->pos.y;
  1230. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1231. return true;
  1232. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1233. return false;
  1234. if(!a->isVisitable() && b->isVisitable())
  1235. return true;
  1236. if(!b->isVisitable() && a->isVisitable())
  1237. return false;
  1238. if(a->pos.x < b->pos.x)
  1239. return true;
  1240. return false;
  1241. }
  1242. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1243. : obj(obj_),
  1244. rect(rect_),
  1245. fadeAnimKey(-1)
  1246. {
  1247. }
  1248. TerrainTileObject::~TerrainTileObject()
  1249. {
  1250. }