mapHandler.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 settings["session"]["spectate"].Bool() ? true : 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 = settings["session"]["spectate"].Bool() ? true : (*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. CMapHandler::CMapBlitter::CMapBlitter(CMapHandler * p)
  609. :parent(p), tileSize(0), halfTileSizeCeil(0), info(nullptr)
  610. {
  611. }
  612. CMapHandler::CMapBlitter::~CMapBlitter() = default;
  613. void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
  614. {
  615. Rect destRect(realTileRect);
  616. drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
  617. }
  618. void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  619. {
  620. //nothing to do here
  621. }
  622. void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  623. {
  624. drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
  625. }
  626. void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  627. {
  628. Rect dstRect(realTileRect);
  629. drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
  630. }
  631. void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  632. {
  633. auto & objects = tile.objects;
  634. for(auto & object : objects)
  635. {
  636. if (object.fadeAnimKey >= 0)
  637. {
  638. auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
  639. if (fadeIter != parent->fadeAnims.end())
  640. {
  641. // this object is currently fading, so skip normal drawing
  642. Rect r2(realTileRect);
  643. CFadeAnimation * fade = (*fadeIter).second.second;
  644. fade->draw(targetSurf, nullptr, &r2);
  645. continue;
  646. }
  647. logGlobal->errorStream() << "Fading map object with missing fade anim : " << object.fadeAnimKey;
  648. continue;
  649. }
  650. const CGObjectInstance * obj = object.obj;
  651. if (!obj)
  652. {
  653. logGlobal->errorStream() << "Stray map object that isn't fading";
  654. continue;
  655. }
  656. if (!canDrawObject(obj))
  657. continue;
  658. auto objData = findObjectBitmap(obj, info->anim);
  659. if (objData.objBitmap)
  660. {
  661. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  662. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  663. if (objData.flagBitmap)
  664. {
  665. if (objData.isMoving)
  666. {
  667. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  668. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  669. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  670. }
  671. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  672. {
  673. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  674. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  675. }
  676. }
  677. }
  678. }
  679. }
  680. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  681. {
  682. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  683. {
  684. ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
  685. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  686. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  687. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfoUpper->roadType - 1][tinfoUpper->roadDir][rotation],
  688. &source, targetSurf, &dest);
  689. }
  690. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  691. {
  692. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  693. Rect source(0, 0, tileSize, halfTileSizeCeil);
  694. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  695. drawElement(EMapCacheType::ROADS, parent->roadImages[tinfo.roadType - 1][tinfo.roadDir][rotation],
  696. &source, targetSurf, &dest);
  697. }
  698. }
  699. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  700. {
  701. Rect destRect(realTileRect);
  702. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  703. drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType-1][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
  704. }
  705. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  706. {
  707. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  708. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  709. if (retBitmapID < 0)
  710. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  711. const IImage * image = nullptr;
  712. if (retBitmapID >= 0)
  713. image = parent->FoWpartialHide.at(retBitmapID);
  714. else
  715. image = parent->FoWfullHide.at(-retBitmapID - 1);
  716. Rect destRect(realTileRect);
  717. drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
  718. }
  719. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  720. {
  721. init(info);
  722. auto prevClip = clip(targetSurf);
  723. pos = int3(0, 0, topTile.z);
  724. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  725. {
  726. if (pos.x < 0 || pos.x >= parent->sizes.x)
  727. continue;
  728. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  729. {
  730. if (pos.y < 0 || pos.y >= parent->sizes.y)
  731. continue;
  732. const bool isVisible = canDrawCurrentTile();
  733. realTileRect.x = realPos.x;
  734. realTileRect.y = realPos.y;
  735. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  736. const TerrainTile & tinfo = parent->map->getTile(pos);
  737. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  738. if(isVisible || info->showAllTerrain)
  739. {
  740. drawTileTerrain(targetSurf, tinfo, tile);
  741. if (tinfo.riverType)
  742. drawRiver(targetSurf, tinfo);
  743. drawRoad(targetSurf, tinfo, tinfoUpper);
  744. }
  745. if(isVisible)
  746. drawObjects(targetSurf, tile);
  747. }
  748. }
  749. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  750. {
  751. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  752. {
  753. realTileRect.x = realPos.x;
  754. realTileRect.y = realPos.y;
  755. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  756. pos.y < 0 || pos.y >= parent->sizes.y)
  757. {
  758. drawFrame(targetSurf);
  759. }
  760. else
  761. {
  762. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  763. if(!settings["session"]["spectate"].Bool() && !(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  764. drawFow(targetSurf);
  765. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  766. drawTileOverlay(targetSurf, tile);
  767. // drawDebugVisitables()
  768. if (settings["session"]["showBlock"].Bool())
  769. {
  770. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  771. {
  772. static SDL_Surface * block = nullptr;
  773. if (!block)
  774. block = BitmapHandler::loadBitmap("blocked");
  775. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  776. }
  777. }
  778. if (settings["session"]["showVisit"].Bool())
  779. {
  780. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  781. {
  782. static SDL_Surface * visit = nullptr;
  783. if (!visit)
  784. visit = BitmapHandler::loadBitmap("visitable");
  785. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  786. }
  787. }
  788. }
  789. }
  790. }
  791. drawOverlayEx(targetSurf);
  792. // drawDebugGrid()
  793. if (settings["session"]["showGrid"].Bool())
  794. {
  795. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  796. {
  797. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  798. {
  799. const int3 color(0x555555, 0x555555, 0x555555);
  800. if (realPos.y >= info->drawBounds->y &&
  801. realPos.y < info->drawBounds->y + info->drawBounds->h)
  802. for(int i = 0; i < tileSize; i++)
  803. if (realPos.x + i >= info->drawBounds->x &&
  804. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  805. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  806. if (realPos.x >= info->drawBounds->x &&
  807. realPos.x < info->drawBounds->x + info->drawBounds->w)
  808. for(int i = 0; i < tileSize; i++)
  809. if (realPos.y + i >= info->drawBounds->y &&
  810. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  811. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  812. }
  813. }
  814. }
  815. postProcessing(targetSurf);
  816. SDL_SetClipRect(targetSurf, &prevClip);
  817. }
  818. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  819. {
  820. if(hero && hero->moveDir && hero->type) //it's hero or boat
  821. {
  822. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  823. {
  824. logGlobal->errorStream() << "A neutral hero (" << hero->name << ") at " << hero->pos << ". Should not happen!";
  825. return CMapHandler::AnimBitmapHolder();
  826. }
  827. //pick graphics of hero (or boat if hero is sailing)
  828. std::shared_ptr<CAnimation> animation;
  829. if (hero->boat)
  830. animation = graphics->boatAnimations[hero->boat->subID];
  831. else
  832. animation = graphics->heroAnimations[hero->appearance.animationFile];
  833. bool moving = !hero->isStanding;
  834. int group = getHeroFrameGroup(hero->moveDir, moving);
  835. if(animation->size(group) > 0)
  836. {
  837. int frame = anim % animation->size(group);
  838. IImage * heroImage = animation->getImage(frame, group);
  839. //get flag overlay only if we have main image
  840. IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
  841. return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
  842. }
  843. }
  844. return CMapHandler::AnimBitmapHolder();
  845. }
  846. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  847. {
  848. auto animation = graphics->boatAnimations.at(boat->subID);
  849. int group = getHeroFrameGroup(boat->direction, false);
  850. if(animation->size(group) > 0)
  851. return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
  852. else
  853. return CMapHandler::AnimBitmapHolder();
  854. }
  855. IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  856. {
  857. if (!hero)
  858. return nullptr;
  859. if (hero->boat)
  860. return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
  861. return findHeroFlagBitmap(hero, anim, color, group);
  862. }
  863. IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  864. {
  865. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
  866. }
  867. IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
  868. {
  869. int boatType = boat->subID;
  870. if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
  871. {
  872. logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID;
  873. return nullptr;
  874. }
  875. const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
  876. int colorIndex = color->getNum();
  877. if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
  878. {
  879. logGlobal->errorStream() << "Invalid player color " << colorIndex;
  880. return nullptr;
  881. }
  882. return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
  883. }
  884. IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
  885. {
  886. size_t groupSize = animation->size(group);
  887. if(groupSize == 0)
  888. return nullptr;
  889. if(moving)
  890. return animation->getImage(anim % groupSize, group);
  891. else
  892. return animation->getImage((anim / 4) % groupSize, group);
  893. }
  894. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  895. {
  896. if (!obj)
  897. return CMapHandler::AnimBitmapHolder();
  898. if (obj->ID == Obj::HERO)
  899. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  900. if (obj->ID == Obj::BOAT)
  901. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  902. // normal object
  903. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  904. size_t groupSize = animation->size();
  905. if(groupSize == 0)
  906. return CMapHandler::AnimBitmapHolder();
  907. IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % groupSize);
  908. if(!bitmap)
  909. return CMapHandler::AnimBitmapHolder();
  910. bitmap->setFlagColor(obj->tempOwner);
  911. return CMapHandler::AnimBitmapHolder(bitmap);
  912. }
  913. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  914. {
  915. auto i = parent->animationPhase.find(object);
  916. if(i == parent->animationPhase.end())
  917. {
  918. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  919. parent->animationPhase[object] = ret;
  920. return ret;
  921. }
  922. return i->second;
  923. }
  924. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  925. {
  926. //checking if object has non-empty graphic on this tile
  927. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  928. }
  929. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  930. {
  931. if(settings["session"]["spectate"].Bool())
  932. return true;
  933. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  934. return !neighbors.areAllHidden();
  935. }
  936. ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
  937. {
  938. if(isMoving)
  939. {
  940. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  941. return frame[dir];
  942. }
  943. else //if(isMoving)
  944. {
  945. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  946. return frame[dir];
  947. }
  948. }
  949. bool CMapHandler::updateObjectsFade()
  950. {
  951. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  952. {
  953. int3 pos = (*iter).second.first;
  954. CFadeAnimation * anim = (*iter).second.second;
  955. anim->update();
  956. if (anim->isFading())
  957. ++iter;
  958. else // fade finished
  959. {
  960. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  961. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  962. {
  963. if ((*objIter).fadeAnimKey == (*iter).first)
  964. {
  965. logAnim->traceStream() << "Fade anim finished for obj at " << pos << "; remaining: " << (fadeAnims.size() - 1);
  966. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  967. objs.erase(objIter); // if this was fadeout, remove the object from the map
  968. else
  969. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  970. break;
  971. }
  972. }
  973. delete (*iter).second.second;
  974. iter = fadeAnims.erase(iter);
  975. }
  976. }
  977. return !fadeAnims.empty();
  978. }
  979. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  980. {
  981. SDL_Surface * fadeBitmap;
  982. assert(obj.obj);
  983. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  984. if (objData.objBitmap)
  985. {
  986. if (objData.isMoving) // ignore fading of moving objects (for now?)
  987. {
  988. logAnim->debugStream() << "Ignoring fade of moving object";
  989. return false;
  990. }
  991. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  992. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  993. objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
  994. if (objData.flagBitmap)
  995. {
  996. 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
  997. {
  998. Rect flagSrcRect(32, 0, 32, 32);
  999. objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
  1000. }
  1001. }
  1002. auto anim = new CFadeAnimation();
  1003. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  1004. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  1005. obj.fadeAnimKey = fadeAnimCounter;
  1006. logAnim->traceStream() << "Fade anim started for obj " << obj.obj->ID
  1007. << " at " << pos << "; anim count: " << fadeAnims.size();
  1008. return true;
  1009. }
  1010. return false;
  1011. }
  1012. bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = false */)
  1013. {
  1014. auto animation = graphics->getAnimation(obj);
  1015. if(!animation)
  1016. return false;
  1017. IImage * bitmap = animation->getImage(0);
  1018. if(!bitmap)
  1019. return false;
  1020. const int tilesW = bitmap->width()/32;
  1021. const int tilesH = bitmap->height()/32;
  1022. for(int fx=0; fx<tilesW; ++fx)
  1023. {
  1024. for(int fy=0; fy<tilesH; ++fy)
  1025. {
  1026. SDL_Rect cr;
  1027. cr.w = 32;
  1028. cr.h = 32;
  1029. cr.x = fx*32;
  1030. cr.y = fy*32;
  1031. TerrainTileObject toAdd(obj, cr);
  1032. 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)
  1033. {
  1034. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1035. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1036. if (fadein && ADVOPT.objectFading)
  1037. {
  1038. startObjectFade(toAdd, true, pos);
  1039. }
  1040. auto i = curt.objects.begin();
  1041. for(; i != curt.objects.end(); i++)
  1042. {
  1043. if(objectBlitOrderSorter(toAdd, *i))
  1044. {
  1045. curt.objects.insert(i, toAdd);
  1046. i = curt.objects.begin(); //to validate and avoid adding it second time
  1047. break;
  1048. }
  1049. }
  1050. if(i == curt.objects.end())
  1051. curt.objects.insert(i, toAdd);
  1052. }
  1053. }
  1054. }
  1055. return true;
  1056. }
  1057. bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
  1058. {
  1059. //optimized version which reveals weird bugs with missing def name
  1060. //auto pos = obj->pos;
  1061. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1062. //{
  1063. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1064. // {
  1065. // int3 t(i, j, pos.z);
  1066. // if (!map->isInTheMap(t))
  1067. // continue;
  1068. // auto &objs = ttiles[i][j][pos.z].objects;
  1069. // for (size_t x = 0; x < objs.size(); x++)
  1070. // {
  1071. // auto ourObj = objs[x].obj;
  1072. // if (ourObj && ourObj->id == obj->id)
  1073. // {
  1074. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1075. // {
  1076. // if (startObjectFade(objs[x], false, t))
  1077. // objs[x].obj = nullptr; //set original pointer to null
  1078. // else
  1079. // objs.erase(objs.begin() + x);
  1080. // }
  1081. // else
  1082. // objs.erase(objs.begin() + x);
  1083. // break;
  1084. // }
  1085. // }
  1086. // }
  1087. //}
  1088. for (size_t i = 0; i<map->width; i++)
  1089. {
  1090. for (size_t j = 0; j<map->height; j++)
  1091. {
  1092. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1093. {
  1094. auto &objs = ttiles[i][j][k].objects;
  1095. for (size_t x = 0; x < objs.size(); x++)
  1096. {
  1097. if (objs[x].obj && objs[x].obj->id == obj->id)
  1098. {
  1099. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1100. {
  1101. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1102. objs[x].obj = nullptr;
  1103. else
  1104. objs.erase(objs.begin() + x);
  1105. }
  1106. else
  1107. objs.erase(objs.begin() + x);
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. return true;
  1115. }
  1116. bool CMapHandler::canStartHeroMovement()
  1117. {
  1118. return fadeAnims.empty(); // don't allow movement during fade animation
  1119. }
  1120. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1121. {
  1122. for(auto & elem : terrainImages[7])
  1123. {
  1124. for(IImage * img : elem)
  1125. img->shiftPalette(246, 9);
  1126. }
  1127. for(auto & elem : terrainImages[8])
  1128. {
  1129. for(IImage * img : elem)
  1130. {
  1131. img->shiftPalette(229, 12);
  1132. img->shiftPalette(242, 14);
  1133. }
  1134. }
  1135. for(auto & elem : riverImages[0])
  1136. {
  1137. for(IImage * img : elem)
  1138. {
  1139. img->shiftPalette(183, 12);
  1140. img->shiftPalette(195, 6);
  1141. }
  1142. }
  1143. for(auto & elem : riverImages[2])
  1144. {
  1145. for(IImage * img : elem)
  1146. {
  1147. img->shiftPalette(228, 12);
  1148. img->shiftPalette(183, 6);
  1149. img->shiftPalette(240, 6);
  1150. }
  1151. }
  1152. for(auto & elem : riverImages[3])
  1153. {
  1154. for(IImage * img : elem)
  1155. img->shiftPalette(240, 9);
  1156. }
  1157. }
  1158. CMapHandler::~CMapHandler()
  1159. {
  1160. delete normalBlitter;
  1161. delete worldViewBlitter;
  1162. delete puzzleViewBlitter;
  1163. for (auto & elem : fadeAnims)
  1164. {
  1165. delete elem.second.second;
  1166. }
  1167. }
  1168. CMapHandler::CMapHandler()
  1169. {
  1170. frameW = frameH = 0;
  1171. normalBlitter = new CMapNormalBlitter(this);
  1172. worldViewBlitter = new CMapWorldViewBlitter(this);
  1173. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1174. fadeAnimCounter = 0;
  1175. map = nullptr;
  1176. tilesW = tilesH = 0;
  1177. offsetX = offsetY = 0;
  1178. egdeAnimation = make_unique<CAnimation>("EDG");
  1179. egdeAnimation->preload();
  1180. }
  1181. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1182. {
  1183. out.clear();
  1184. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1185. const TerrainTile &t = map->getTile(pos);
  1186. for(auto & elem : tt.objects)
  1187. {
  1188. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1189. {
  1190. out = elem.obj->getObjectName();
  1191. return;
  1192. }
  1193. }
  1194. if(t.hasFavorableWinds())
  1195. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1196. else if(terName)
  1197. {
  1198. out = CGI->generaltexth->terrainNames[t.terType];
  1199. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1200. {
  1201. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1202. }
  1203. }
  1204. }
  1205. void CMapHandler::discardWorldViewCache()
  1206. {
  1207. cache.discardWorldViewCache();
  1208. }
  1209. CMapHandler::CMapCache::CMapCache()
  1210. {
  1211. worldViewCachedScale = 0;
  1212. }
  1213. void CMapHandler::CMapCache::discardWorldViewCache()
  1214. {
  1215. for(auto & cache : data)
  1216. cache.clear();
  1217. logAnim->debug("Discarded world view cache");
  1218. }
  1219. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1220. {
  1221. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1222. discardWorldViewCache();
  1223. worldViewCachedScale = scale;
  1224. }
  1225. IImage * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, const IImage * fullSurface)
  1226. {
  1227. intptr_t key = (intptr_t) fullSurface;
  1228. auto & cache = data[(ui8)type];
  1229. auto iter = cache.find(key);
  1230. if(iter == cache.end())
  1231. {
  1232. auto scaled = fullSurface->scaleFast(worldViewCachedScale);
  1233. IImage * ret = scaled.get();
  1234. cache[key] = std::move(scaled);
  1235. return ret;
  1236. }
  1237. else
  1238. {
  1239. return (*iter).second.get();
  1240. }
  1241. }
  1242. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1243. {
  1244. if (!a)
  1245. return true;
  1246. if (!b)
  1247. return false;
  1248. if (a->appearance.printPriority != b->appearance.printPriority)
  1249. return a->appearance.printPriority > b->appearance.printPriority;
  1250. if(a->pos.y != b->pos.y)
  1251. return a->pos.y < b->pos.y;
  1252. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1253. return true;
  1254. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1255. return false;
  1256. if(!a->isVisitable() && b->isVisitable())
  1257. return true;
  1258. if(!b->isVisitable() && a->isVisitable())
  1259. return false;
  1260. if(a->pos.x < b->pos.x)
  1261. return true;
  1262. return false;
  1263. }
  1264. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1265. : obj(obj_),
  1266. rect(rect_),
  1267. fadeAnimKey(-1)
  1268. {
  1269. }
  1270. TerrainTileObject::~TerrainTileObject()
  1271. {
  1272. }