mapHandler.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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/SDL_Extensions.h"
  14. #include "CGameInfo.h"
  15. #include "../lib/mapObjects/CGHeroInstance.h"
  16. #include "../lib/mapObjects/CObjectClassesHandler.h"
  17. #include "../lib/CGameState.h"
  18. #include "../lib/CHeroHandler.h"
  19. #include "../lib/CTownHandler.h"
  20. #include "Graphics.h"
  21. #include "../lib/mapping/CMap.h"
  22. #include "CDefHandler.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. std::string nameFromType (int typ)
  31. {
  32. switch(ETerrainType(typ))
  33. {
  34. case ETerrainType::DIRT:
  35. return std::string("DIRTTL.DEF");
  36. case ETerrainType::SAND:
  37. return std::string("SANDTL.DEF");
  38. case ETerrainType::GRASS:
  39. return std::string("GRASTL.DEF");
  40. case ETerrainType::SNOW:
  41. return std::string("SNOWTL.DEF");
  42. case ETerrainType::SWAMP:
  43. return std::string("SWMPTL.DEF");
  44. case ETerrainType::ROUGH:
  45. return std::string("ROUGTL.DEF");
  46. case ETerrainType::SUBTERRANEAN:
  47. return std::string("SUBBTL.DEF");
  48. case ETerrainType::LAVA:
  49. return std::string("LAVATL.DEF");
  50. case ETerrainType::WATER:
  51. return std::string("WATRTL.DEF");
  52. case ETerrainType::ROCK:
  53. return std::string("ROCKTL.DEF");
  54. case ETerrainType::BORDER:
  55. //TODO use me
  56. break;
  57. default:
  58. //TODO do something here
  59. break;
  60. }
  61. return std::string();
  62. }
  63. static bool objectBlitOrderSorter(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b)
  64. {
  65. return CMapHandler::compareObjectBlitOrder(a.first, b.first);
  66. }
  67. struct NeighborTilesInfo
  68. {
  69. bool d7, //789
  70. d8, //456
  71. d9, //123
  72. d4,
  73. d5,
  74. d6,
  75. d1,
  76. d2,
  77. d3;
  78. NeighborTilesInfo(const int3 & pos, const int3 & sizes, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap)
  79. {
  80. auto getTile = [&](int dx, int dy)->bool
  81. {
  82. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  83. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  84. return false;
  85. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  86. };
  87. d7 = getTile(-1, -1); //789
  88. d8 = getTile( 0, -1); //456
  89. d9 = getTile(+1, -1); //123
  90. d4 = getTile(-1, 0);
  91. d5 = visibilityMap[pos.x][pos.y][pos.z];
  92. d6 = getTile(+1, 0);
  93. d1 = getTile(-1, +1);
  94. d2 = getTile( 0, +1);
  95. d3 = getTile(+1, +1);
  96. }
  97. bool areAllHidden() const
  98. {
  99. return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d8 );
  100. }
  101. int getBitmapID() const
  102. {
  103. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  104. // 0-1, 2-3, 4-5, 11-13, 12-14
  105. static const int visBitmaps[256] = {
  106. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  107. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  108. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  109. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  110. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  111. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  112. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  113. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  114. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  115. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  116. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  117. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  118. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  119. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  120. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  121. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  122. };
  123. return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  124. }
  125. };
  126. void CMapHandler::prepareFOWDefs()
  127. {
  128. graphics->FoWfullHide = CDefHandler::giveDef("TSHRC.DEF");
  129. graphics->FoWpartialHide = CDefHandler::giveDef("TSHRE.DEF");
  130. //adding necessary rotations
  131. static const int missRot [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  132. Cimage nw;
  133. for(auto & elem : missRot)
  134. {
  135. nw = graphics->FoWpartialHide->ourImages[elem];
  136. nw.bitmap = CSDL_Ext::verticalFlip(nw.bitmap);
  137. graphics->FoWpartialHide->ourImages.push_back(nw);
  138. }
  139. //necessaary rotations added
  140. //alpha - transformation
  141. for(auto & elem : graphics->FoWpartialHide->ourImages)
  142. {
  143. CSDL_Ext::alphaTransform(elem.bitmap);
  144. }
  145. //initialization of type of full-hide image
  146. hideBitmap.resize(sizes.x);
  147. for (auto & elem : hideBitmap)
  148. {
  149. elem.resize(sizes.y);
  150. }
  151. for (auto & elem : hideBitmap)
  152. {
  153. for (int j = 0; j < sizes.y; ++j)
  154. {
  155. elem[j].resize(sizes.z);
  156. for(int k = 0; k < sizes.z; ++k)
  157. {
  158. elem[j][k] = CRandomGenerator::getDefault().nextInt(graphics->FoWfullHide->ourImages.size() - 1);
  159. }
  160. }
  161. }
  162. }
  163. void CMapHandler::roadsRiverTerrainInit()
  164. {
  165. //initializing road's and river's DefHandlers
  166. roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
  167. roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
  168. roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
  169. staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
  170. staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
  171. staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
  172. staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
  173. for(auto & elem : staticRiverDefs)
  174. {
  175. for(size_t h=0; h < elem->ourImages.size(); ++h)
  176. {
  177. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  178. }
  179. }
  180. for(auto & elem : roadDefs)
  181. {
  182. for(size_t h=0; h < elem->ourImages.size(); ++h)
  183. {
  184. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  185. }
  186. }
  187. // Create enough room for the whole map and its frame
  188. ttiles.resize(sizes.x, frameW, frameW);
  189. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  190. {
  191. ttiles[i].resize(sizes.y, frameH, frameH);
  192. }
  193. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  194. {
  195. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  196. ttiles[i][j].resize(sizes.z, 0, 0);
  197. }
  198. }
  199. void CMapHandler::borderAndTerrainBitmapInit()
  200. {
  201. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  202. bord->notFreeImgs = true;
  203. terrainGraphics.resize(10);
  204. for (int i = 0; i < 10 ; i++)
  205. {
  206. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  207. terrainGraphics[i].resize(hlp->ourImages.size());
  208. hlp->notFreeImgs = true;
  209. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  210. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  211. delete hlp;
  212. }
  213. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  214. {
  215. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  216. {
  217. for(int k=0; k<sizes.z; ++k) //by levles
  218. {
  219. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  220. {
  221. int terBitmapNum = -1;
  222. auto & rand = CRandomGenerator::getDefault();
  223. if(i==-1 && j==-1)
  224. terBitmapNum = 16;
  225. else if(i==-1 && j==(sizes.y))
  226. terBitmapNum = 19;
  227. else if(i==(sizes.x) && j==-1)
  228. terBitmapNum = 17;
  229. else if(i==(sizes.x) && j==(sizes.y))
  230. terBitmapNum = 18;
  231. else if(j == -1 && i > -1 && i < sizes.x)
  232. terBitmapNum = rand.nextInt(22, 23);
  233. else if(i == -1 && j > -1 && j < sizes.y)
  234. terBitmapNum = rand.nextInt(33, 34);
  235. else if(j == sizes.y && i >-1 && i < sizes.x)
  236. terBitmapNum = rand.nextInt(29, 30);
  237. else if(i == sizes.x && j > -1 && j < sizes.y)
  238. terBitmapNum = rand.nextInt(25, 26);
  239. else
  240. terBitmapNum = rand.nextInt(15);
  241. if(terBitmapNum != -1)
  242. {
  243. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  244. continue;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. delete bord;
  251. }
  252. static void processDef (const ObjectTemplate & objTempl)
  253. {
  254. if(objTempl.id == Obj::EVENT)
  255. {
  256. graphics->advmapobjGraphics[objTempl.animationFile] = nullptr;
  257. return;
  258. }
  259. CDefEssential * ourDef = graphics->getDef(objTempl);
  260. if(!ourDef) //if object has already set handler (eg. heroes) it should not be overwritten
  261. {
  262. if(objTempl.animationFile.size())
  263. {
  264. graphics->advmapobjGraphics[objTempl.animationFile] = CDefHandler::giveDefEss(objTempl.animationFile);
  265. }
  266. else
  267. {
  268. logGlobal->warnStream() << "No def name for " << objTempl.id << " " << objTempl.subid;
  269. return;
  270. }
  271. ourDef = graphics->getDef(objTempl);
  272. }
  273. //alpha transformation
  274. for(auto & elem : ourDef->ourImages)
  275. {
  276. CSDL_Ext::alphaTransform(elem.bitmap);
  277. }
  278. }
  279. void CMapHandler::initObjectRects()
  280. {
  281. //initializing objects / rects
  282. for(auto & elem : map->objects)
  283. {
  284. const CGObjectInstance *obj = elem;
  285. if( !obj
  286. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  287. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  288. {
  289. continue;
  290. }
  291. if (!graphics->getDef(obj)) //try to load it
  292. processDef(obj->appearance);
  293. if (!graphics->getDef(obj)) // stil no graphics? exit
  294. continue;
  295. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  296. for(int fx=0; fx < obj->getWidth(); ++fx)
  297. {
  298. for(int fy=0; fy < obj->getHeight(); ++fy)
  299. {
  300. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  301. SDL_Rect cr;
  302. cr.w = 32;
  303. cr.h = 32;
  304. cr.x = bitmap->w - fx * 32 - 32;
  305. cr.y = bitmap->h - fy * 32 - 32;
  306. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  307. if( map->isInTheMap(currTile) && // within map
  308. cr.x + cr.w > 0 && // image has data on this tile
  309. cr.y + cr.h > 0 &&
  310. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  311. )
  312. {
  313. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  314. }
  315. }
  316. }
  317. }
  318. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  319. {
  320. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  321. {
  322. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  323. {
  324. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
  325. }
  326. }
  327. }
  328. }
  329. void CMapHandler::init()
  330. {
  331. CStopWatch th;
  332. th.getDiff();
  333. graphics->advmapobjGraphics["AB01_.DEF"] = graphics->boatAnims[0];
  334. graphics->advmapobjGraphics["AB02_.DEF"] = graphics->boatAnims[1];
  335. graphics->advmapobjGraphics["AB03_.DEF"] = graphics->boatAnims[2];
  336. // Size of visible terrain.
  337. int mapW = conf.go()->ac.advmapW;
  338. int mapH = conf.go()->ac.advmapH;
  339. //sizes of terrain
  340. sizes.x = map->width;
  341. sizes.y = map->height;
  342. sizes.z = map->twoLevel ? 2 : 1;
  343. // Total number of visible tiles. Subtract the center tile, then
  344. // compute the number of tiles on each side, and reassemble.
  345. int t1, t2;
  346. t1 = (mapW-32)/2;
  347. t2 = mapW - 32 - t1;
  348. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  349. t1 = (mapH-32)/2;
  350. t2 = mapH - 32 - t1;
  351. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  352. // Size of the frame around the map. In extremes positions, the
  353. // frame must not be on the center of the map, but right on the
  354. // edge of the center tile.
  355. frameW = (mapW+31) /32 / 2;
  356. frameH = (mapH+31) /32 / 2;
  357. offsetX = (mapW - (2*frameW+1)*32)/2;
  358. offsetY = (mapH - (2*frameH+1)*32)/2;
  359. prepareFOWDefs();
  360. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  361. borderAndTerrainBitmapInit();
  362. logGlobal->infoStream()<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDiff();
  363. initObjectRects();
  364. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  365. }
  366. void CMapHandler::drawWorldViewOverlay(int targetTilesX, int targetTilesY, int srx_init, int sry_init, CDefHandler * iconsDef, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, float scale, int targetTileSize, int3 top_tile, SDL_Surface * extSurf)
  367. {
  368. int srx = srx_init;
  369. int bxStart = top_tile.x;
  370. int byStart = top_tile.y;
  371. for (int bx = bxStart; bx < top_tile.x + targetTilesX; bx++, srx+=targetTileSize)
  372. {
  373. if (bx < 0 || bx >= sizes.x)
  374. {
  375. continue;
  376. }
  377. int sry = sry_init;
  378. for (int by = byStart; by < top_tile.y + targetTilesY; by++, sry+=targetTileSize)
  379. {
  380. if (by < 0 || by >= sizes.y)
  381. {
  382. continue;
  383. }
  384. int3 pos(bx, by, top_tile.z);
  385. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  386. auto &objects = tile.objects;
  387. for(auto & object : objects)
  388. {
  389. const CGObjectInstance *obj = object.first;
  390. if (obj->pos.z != top_tile.z)
  391. continue;
  392. if (!(*visibilityMap)[pos.x][pos.y][top_tile.z])
  393. continue;
  394. if (!obj->visitableAt(pos.x, pos.y))
  395. continue;
  396. auto &ownerRaw = obj->tempOwner;
  397. int ownerIndex = 0;
  398. if (ownerRaw < PlayerColor::PLAYER_LIMIT)
  399. {
  400. ownerIndex = ownerRaw.getNum() * 19;
  401. }
  402. else if (ownerRaw == PlayerColor::NEUTRAL)
  403. {
  404. ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
  405. }
  406. SDL_Surface * wvIcon = nullptr;
  407. switch (obj->ID)
  408. {
  409. default:
  410. continue;
  411. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  412. case Obj::MONOLITH_ONE_WAY_EXIT:
  413. case Obj::MONOLITH_TWO_WAY:
  414. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::TELEPORT].bitmap;
  415. break;
  416. case Obj::SUBTERRANEAN_GATE:
  417. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::GATE].bitmap;
  418. break;
  419. case Obj::ARTIFACT:
  420. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::ARTIFACT].bitmap;
  421. break;
  422. case Obj::TOWN:
  423. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::TOWN + ownerIndex].bitmap;
  424. break;
  425. case Obj::HERO:
  426. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::HERO + ownerIndex].bitmap;
  427. break;
  428. case Obj::MINE:
  429. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::MINE_WOOD + obj->subID + ownerIndex].bitmap;
  430. break;
  431. case Obj::RESOURCE:
  432. wvIcon = iconsDef->ourImages[(int)EWorldViewIcon::RES_WOOD + obj->subID + ownerIndex].bitmap;
  433. break;
  434. }
  435. if (wvIcon)
  436. {
  437. Rect tempDst;
  438. tempDst.x = srx + targetTileSize / 2 - wvIcon->w / 2;
  439. tempDst.y = sry + targetTileSize / 2 - wvIcon->h / 2;
  440. tempDst.w = wvIcon->w;
  441. tempDst.h = wvIcon->h;
  442. CSDL_Ext::blitSurface(wvIcon, nullptr, extSurf, &tempDst);
  443. }
  444. }
  445. }
  446. }
  447. }
  448. void CMapHandler::calculateWorldViewCameraPos(int targetTilesX, int targetTilesY, int3 &top_tile)
  449. {
  450. bool outsideLeft = top_tile.x < 0;
  451. bool outsideTop = top_tile.y < 0;
  452. bool outsideRight = std::max(0, top_tile.x) + targetTilesX > sizes.x;
  453. bool outsideBottom = std::max(0, top_tile.y) + targetTilesY > sizes.y;
  454. if (targetTilesX > sizes.x)
  455. top_tile.x = sizes.x / 2 - targetTilesX / 2; // center viewport if the whole map can fit into the screen at once
  456. else if (outsideLeft)
  457. {
  458. if (outsideRight)
  459. {
  460. top_tile.x = sizes.x / 2 - targetTilesX / 2;
  461. }
  462. else
  463. top_tile.x = 0;
  464. }
  465. else if (outsideRight)
  466. top_tile.x = sizes.x - targetTilesX;
  467. if (targetTilesY > sizes.y)
  468. top_tile.y = sizes.y / 2 - targetTilesY / 2;
  469. else if (outsideTop)
  470. {
  471. if (outsideBottom)
  472. {
  473. top_tile.y = sizes.y / 2 - targetTilesY / 2;
  474. }
  475. else
  476. top_tile.y = 0;
  477. }
  478. else if (outsideBottom)
  479. top_tile.y = sizes.y - targetTilesY;
  480. }
  481. void::CMapHandler::drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  482. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect /*= nullptr*/)
  483. {
  484. auto key = cache.genKey((intptr_t)baseSurf, rotation);
  485. auto scaledSurf = cache.requestWorldViewCache(type, key);
  486. if (scaledSurf) // blitting from cache
  487. {
  488. if (srcRect)
  489. {
  490. dstRect->w = srcRect->w;
  491. dstRect->h = srcRect->h;
  492. }
  493. CSDL_Ext::blitSurface(scaledSurf, srcRect, targetSurf, dstRect);
  494. }
  495. else // creating new
  496. {
  497. auto baseSurfRotated = CSDL_Ext::newSurface(baseSurf->w, baseSurf->h);
  498. if (!baseSurfRotated)
  499. return;
  500. Rect baseRect(0, 0, baseSurf->w, baseSurf->h);
  501. CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(baseSurf, baseRect, baseSurfRotated, baseRect, rotation);
  502. SDL_Surface * scaledSurf2 = CSDL_Ext::scaleSurfaceFast(baseSurfRotated, baseSurf->w * scale, baseSurf->h * scale);
  503. CSDL_Ext::blitSurface(scaledSurf2, srcRect, targetSurf, dstRect);
  504. cache.cacheWorldViewEntry(type, key, scaledSurf2);
  505. }
  506. }
  507. // updates map terrain when adventure map is in world view mode;
  508. // this method was copied from terrainRect(), so some parts are the same -- that probably should be refactored slightly
  509. void CMapHandler::terrainRectScaled(int3 topTile, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, SDL_Surface * extSurf, const SDL_Rect * extRect, float scale, CDefHandler * iconsDef)
  510. {
  511. cache.updateWorldViewScale(scale);
  512. // size of a map tile in pixels given the world view scale
  513. int targetTileSize = (int) floorf(32.0f * scale);
  514. int halfTargetTileSizeHigh = (int)ceilf(targetTileSize / 2.0f);
  515. // number of tiles that fit in the viewport
  516. int targetTilesX = (int) ceilf((float)extRect->w / targetTileSize) + 1;
  517. int targetTilesY = (int) ceilf((float)extRect->h / targetTileSize) + 1;
  518. SDL_Rect rtile = { 0, 0, targetTileSize, targetTileSize };
  519. // Absolute coords of the first pixel in the top left corner
  520. int srx_init = offsetX + extRect->x;
  521. int sry_init = offsetY + extRect->y;
  522. int srx, sry; // absolute screen coordinates in pixels
  523. calculateWorldViewCameraPos(targetTilesX, targetTilesY, topTile);
  524. SDL_FillRect(extSurf, extRect, SDL_MapRGB(extSurf->format, 0, 0, 0));
  525. // makes the clip area smaller if the map is smaller than the screen frame
  526. Rect clipRect(std::max(extRect->x, -topTile.x * targetTileSize),
  527. std::max(extRect->y, -topTile.y * targetTileSize),
  528. std::min(extRect->w, sizes.x * targetTileSize),
  529. std::min(extRect->h, sizes.y * targetTileSize));
  530. SDL_Rect prevClip;
  531. SDL_GetClipRect(extSurf, &prevClip);
  532. SDL_SetClipRect(extSurf, &clipRect); //preventing blitting outside of that rect
  533. // printing terrain
  534. srx = srx_init;
  535. for (int bx = topTile.x; bx < topTile.x + targetTilesX; bx++, srx+=targetTileSize)
  536. {
  537. // Skip column if not in map
  538. if (bx < 0 || bx >= sizes.x)
  539. {
  540. continue;
  541. }
  542. sry = sry_init;
  543. for (int by = topTile.y; by < topTile.y + targetTilesY; by++, sry+=targetTileSize)
  544. {
  545. int3 pos(bx, by, topTile.z); //blitted tile position
  546. // Skip tile if not in map
  547. if (by < 0 || by >= sizes.y)
  548. {
  549. continue;
  550. }
  551. const NeighborTilesInfo info(pos,sizes,*visibilityMap);
  552. if(info.areAllHidden())
  553. continue;
  554. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  555. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  556. SDL_Rect sr;
  557. sr.x=srx;
  558. sr.y=sry;
  559. sr.h=sr.w=targetTileSize;
  560. //blit terrain with river/road
  561. if(tile.terbitmap)
  562. { //if custom terrain graphic - use it
  563. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::TERRAIN_CUSTOM, (intptr_t)tile.terbitmap, tile.terbitmap, scale);
  564. Rect tempDst = Rect(sr.x, sr.y, scaledSurf->w, scaledSurf->h);
  565. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, nullptr, extSurf, &tempDst);
  566. }
  567. else //use default terrain graphic
  568. {
  569. auto baseSurf = terrainGraphics[tinfo.terType][tinfo.terView];
  570. drawScaledRotatedElement(EMapCacheType::TERRAIN, baseSurf, extSurf, tinfo.extTileFlags % 4, scale, &sr);
  571. }
  572. if(tinfo.riverType) //print river if present
  573. {
  574. auto baseSurf = staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap;
  575. drawScaledRotatedElement(EMapCacheType::RIVERS, baseSurf, extSurf, (tinfo.extTileFlags >> 2) % 4, scale, &sr);
  576. }
  577. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  578. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  579. {
  580. const TerrainTile &topPathTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  581. Rect source(0, targetTileSize / 2, targetTileSize, targetTileSize / 2);
  582. Rect dest(sr.x, sr.y, targetTileSize, targetTileSize / 2);
  583. auto baseSurf = roadDefs[topPathTile.roadType - 1]->ourImages[topPathTile.roadDir].bitmap;
  584. drawScaledRotatedElement(EMapCacheType::ROADS, baseSurf, extSurf, (topPathTile.extTileFlags >> 4) % 4, scale, &dest, &source);
  585. }
  586. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  587. {
  588. Rect source(0, 0, targetTileSize, halfTargetTileSizeHigh);
  589. Rect dest(sr.x, sr.y + targetTileSize / 2, targetTileSize, halfTargetTileSizeHigh);
  590. auto baseSurf = roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap;
  591. drawScaledRotatedElement(EMapCacheType::ROADS, baseSurf, extSurf, (tinfo.extTileFlags >> 4) % 4, scale, &dest, &source);
  592. }
  593. //blit objects
  594. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  595. for(auto & object : objects)
  596. {
  597. const CGObjectInstance *obj = object.first;
  598. if (!graphics->getDef(obj))
  599. processDef(obj->appearance);
  600. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  601. {
  602. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  603. }
  604. PlayerColor color = obj->tempOwner;
  605. //checking if object has non-empty graphic on this tile
  606. if(obj->ID != Obj::HERO && !obj->coveringAt(bx, by))
  607. continue;
  608. SDL_Rect sr2(sr);
  609. SDL_Rect pp = object.second;
  610. pp.h = sr.h;
  611. pp.w = sr.w;
  612. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  613. ? nullptr
  614. : static_cast<const CGHeroInstance*>(obj));
  615. //print hero / boat and flag
  616. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  617. {
  618. ui8 dir;
  619. std::vector<Cimage> * iv = nullptr;
  620. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  621. SDL_Surface * tb = nullptr; //surface to blitted
  622. if(themp) //hero
  623. {
  624. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  625. {
  626. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  627. continue;
  628. }
  629. dir = themp->moveDir;
  630. //pick graphics of hero (or boat if hero is sailing)
  631. if (themp->boat)
  632. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  633. else
  634. iv = &graphics->heroAnims[themp->appearance.animationFile]->ourImages;
  635. //pick appropriate flag set
  636. if(themp->boat)
  637. {
  638. switch (themp->boat->subID)
  639. {
  640. case 0: flg = &Graphics::flags1; break;
  641. case 1: flg = &Graphics::flags2; break;
  642. case 2: flg = &Graphics::flags3; break;
  643. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  644. }
  645. }
  646. else
  647. {
  648. flg = &Graphics::flags4;
  649. }
  650. }
  651. else //boat
  652. {
  653. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  654. dir = boat->direction;
  655. iv = &graphics->boatAnims[boat->subID]->ourImages;
  656. }
  657. if(themp && !themp->isStanding) //hero is moving
  658. {
  659. // not the case for world view
  660. }
  661. else //hero / boat stands still
  662. {
  663. size_t gg;
  664. for(gg=0; gg < iv->size(); ++gg)
  665. {
  666. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  667. {
  668. tb = (*iv)[gg].bitmap;
  669. break;
  670. }
  671. }
  672. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::HEROES, (intptr_t)tb, tb, scale);
  673. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf,&pp,extSurf,&sr2);
  674. //printing flag
  675. if(flg
  676. && obj->pos.x == pos.x
  677. && obj->pos.y == pos.y)
  678. {
  679. SDL_Rect bufr = sr2;
  680. bufr.x-=2*targetTileSize;
  681. bufr.y-=1*targetTileSize;
  682. bufr.h = 2 * targetTileSize;
  683. bufr.w = 3 * targetTileSize;
  684. if(bufr.x-extRect-> x > -targetTileSize * 2)
  685. {
  686. auto baseSurf = (graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) * 8].bitmap;
  687. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::HERO_FLAGS, (int)baseSurf, baseSurf, scale);
  688. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, nullptr, extSurf, &bufr);
  689. }
  690. }
  691. }
  692. }
  693. else //blit normal object
  694. {
  695. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  696. SDL_Surface *bitmap = ourImages[0].bitmap;
  697. //setting appropriate flag color
  698. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  699. CSDL_Ext::setPlayerColor(bitmap, color);
  700. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::OBJECTS, (intptr_t)obj->id.getNum(), bitmap, scale);
  701. Rect tempSrc = Rect(pp.x * scale, pp.y * scale, pp.w, pp.h);
  702. Rect tempDst = Rect(sr2.x, sr2.y, pp.w, pp.h);
  703. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf,&tempSrc,extSurf,&tempDst);
  704. }
  705. }
  706. //objects blitted
  707. }
  708. }
  709. // terrain printed
  710. drawWorldViewOverlay(targetTilesX, targetTilesY, srx_init, sry_init, iconsDef, visibilityMap, scale, targetTileSize, topTile, extSurf);
  711. //blitting Fog of War
  712. srx = srx_init;
  713. for (int bx = topTile.x; bx < topTile.x + targetTilesX; bx++, srx+=targetTileSize)
  714. {
  715. sry = sry_init;
  716. for (int by = topTile.y; by < topTile.y + targetTilesY; by++, sry+=targetTileSize)
  717. {
  718. int3 pos(bx, by, topTile.z); //blitted tile position
  719. SDL_Rect sr;
  720. sr.x=srx;
  721. sr.y=sry;
  722. sr.h=sr.w=targetTileSize;
  723. if (pos.x < 0 || pos.x >= sizes.x ||
  724. pos.y < 0 || pos.y >= sizes.y)
  725. {
  726. // no borders in world view
  727. }
  728. else
  729. {
  730. if (pos.x >= 0 &&
  731. pos.y >= 0 &&
  732. pos.x < sizes.x &&
  733. pos.y < sizes.y &&
  734. !(*visibilityMap)[pos.x][pos.y][topTile.z])
  735. {
  736. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  737. auto scaledSurf = cache.requestWorldViewCacheOrCreate(EMapCacheType::FOW, (intptr_t)hide.first, hide.first, scale);
  738. if(hide.second)
  739. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, &rtile, extSurf, &sr);
  740. else
  741. CSDL_Ext::blitSurface(scaledSurf, &rtile, extSurf, &sr);
  742. }
  743. }
  744. }
  745. }
  746. //FoW blitted
  747. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  748. }
  749. // Update map window screen
  750. // top_tile top left tile to draw. Not necessarily visible.
  751. // extRect, extRect = map window on screen
  752. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  753. void CMapHandler::terrainRect( int3 top_tile, ui8 anim, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, bool otherHeroAnim, ui8 heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode, int3 grailPosRel ) const
  754. {
  755. // Width and height of the portion of the map to process. Units in tiles.
  756. ui32 dx = tilesW;
  757. ui32 dy = tilesH;
  758. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  759. SDL_Rect rtile = { 0, 0, 32, 32 };
  760. // Absolute coords of the first pixel in the top left corner
  761. int srx_init = offsetX + extRect->x;
  762. int sry_init = offsetY + extRect->y;
  763. int srx, sry; // absolute screen coordinates in pixels
  764. // If moving, we need to add an extra column/line
  765. if (moveX != 0)
  766. {
  767. dx++;
  768. srx_init += moveX;
  769. if (moveX > 0)
  770. {
  771. // Moving right. We still need to draw the old tile on the
  772. // left, so adjust our referential
  773. top_tile.x --;
  774. srx_init -= 32;
  775. }
  776. }
  777. if (moveY != 0)
  778. {
  779. dy++;
  780. sry_init += moveY;
  781. if (moveY > 0)
  782. {
  783. // Moving down. We still need to draw the tile on the top,
  784. // so adjust our referential.
  785. top_tile.y --;
  786. sry_init -= 32;
  787. }
  788. }
  789. // Reduce sizes if we go out of the full map.
  790. if (top_tile.x < -frameW)
  791. top_tile.x = -frameW;
  792. if (top_tile.y < -frameH)
  793. top_tile.y = -frameH;
  794. if (top_tile.x + dx > sizes.x + frameW)
  795. dx = sizes.x + frameW - top_tile.x;
  796. if (top_tile.y + dy > sizes.y + frameH)
  797. dy = sizes.y + frameH - top_tile.y;
  798. if(!otherHeroAnim)
  799. heroAnim = anim; //the same, as it should be
  800. SDL_Rect prevClip;
  801. SDL_GetClipRect(extSurf, &prevClip);
  802. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  803. const BlitterWithRotationVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  804. const BlitterWithRotationVal blitterWithRotationAndAlpha = CSDL_Ext::getBlitterWithRotationAndAlpha(extSurf);
  805. //const BlitterWithRotationAndAlphaVal blitterWithRotation = CSDL_Ext::getBlitterWithRotation(extSurf);
  806. // printing terrain
  807. srx = srx_init;
  808. for (int bx = 0; bx < dx; bx++, srx+=32)
  809. {
  810. // Skip column if not in map
  811. if (top_tile.x+bx < 0 || top_tile.x+bx >= sizes.x)
  812. continue;
  813. sry = sry_init;
  814. for (int by=0; by < dy; by++, sry+=32)
  815. {
  816. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  817. // Skip tile if not in map
  818. if (pos.y < 0 || pos.y >= sizes.y)
  819. continue;
  820. //we should not render fully hidden tiles
  821. if(!puzzleMode)
  822. {
  823. const NeighborTilesInfo info(pos,sizes,*visibilityMap);
  824. if(info.areAllHidden())
  825. continue;
  826. }
  827. const TerrainTile2 & tile = ttiles[pos.x][pos.y][pos.z];
  828. const TerrainTile &tinfo = map->getTile(int3(pos.x, pos.y, pos.z));
  829. SDL_Rect sr;
  830. sr.x=srx;
  831. sr.y=sry;
  832. sr.h=sr.w=32;
  833. //blit terrain with river/road
  834. if(tile.terbitmap)
  835. { //if custom terrain graphic - use it
  836. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  837. CSDL_Ext::blitSurface(tile.terbitmap, &temp_rect, extSurf, &sr);
  838. }
  839. else //use default terrain graphic
  840. {
  841. blitterWithRotation(terrainGraphics[tinfo.terType][tinfo.terView],rtile, extSurf, sr, tinfo.extTileFlags%4);
  842. }
  843. if(tinfo.riverType) //print river if present
  844. {
  845. blitterWithRotationAndAlpha(staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,rtile, extSurf, sr, (tinfo.extTileFlags>>2)%4);
  846. }
  847. //Roads are shifted by 16 pixels to bottom. We have to draw both parts separately
  848. if (pos.y > 0 && map->getTile(int3(pos.x, pos.y-1, pos.z)).roadType != ERoadType::NO_ROAD)
  849. { //part from top tile
  850. const TerrainTile &topTile = map->getTile(int3(pos.x, pos.y-1, pos.z));
  851. Rect source(0, 16, 32, 16);
  852. Rect dest(sr.x, sr.y, sr.w, sr.h/2);
  853. blitterWithRotationAndAlpha(roadDefs[topTile.roadType - 1]->ourImages[topTile.roadDir].bitmap, source, extSurf, dest, (topTile.extTileFlags>>4)%4);
  854. }
  855. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  856. {
  857. Rect source(0, 0, 32, 32);
  858. Rect dest(sr.x, sr.y+16, sr.w, sr.h/2);
  859. blitterWithRotationAndAlpha(roadDefs[tinfo.roadType-1]->ourImages[tinfo.roadDir].bitmap, source, extSurf, dest, (tinfo.extTileFlags>>4)%4);
  860. }
  861. //blit objects
  862. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects = tile.objects;
  863. for(auto & object : objects)
  864. {
  865. const CGObjectInstance *obj = object.first;
  866. if (!graphics->getDef(obj))
  867. processDef(obj->appearance);
  868. if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
  869. {
  870. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  871. }
  872. PlayerColor color = obj->tempOwner;
  873. //checking if object has non-empty graphic on this tile
  874. if(obj->ID != Obj::HERO && !obj->coveringAt(top_tile.x + bx, top_tile.y + by))
  875. continue;
  876. static const int notBlittedInPuzzleMode[] = {Obj::HOLE};
  877. //don't print flaggable objects in puzzle mode
  878. if(puzzleMode && (obj->isVisitable() || std::find(notBlittedInPuzzleMode, notBlittedInPuzzleMode+1, obj->ID) != notBlittedInPuzzleMode+1)) //?
  879. continue;
  880. SDL_Rect sr2(sr);
  881. SDL_Rect pp = object.second;
  882. pp.h = sr.h;
  883. pp.w = sr.w;
  884. const CGHeroInstance * themp = (obj->ID != Obj::HERO
  885. ? nullptr
  886. : static_cast<const CGHeroInstance*>(obj));
  887. //print hero / boat and flag
  888. if((themp && themp->moveDir && themp->type) || (obj->ID == Obj::BOAT)) //it's hero or boat
  889. {
  890. const int IMGVAL = 8; //frames per group of movement animation
  891. ui8 dir;
  892. std::vector<Cimage> * iv = nullptr;
  893. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  894. SDL_Surface * tb = nullptr; //surface to blitted
  895. if(themp) //hero
  896. {
  897. if(themp->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  898. {
  899. logGlobal->errorStream() << "A neutral hero (" << themp->name << ") at " << themp->pos << ". Should not happen!";
  900. continue;
  901. }
  902. dir = themp->moveDir;
  903. //pick graphics of hero (or boat if hero is sailing)
  904. if (themp->boat)
  905. iv = &graphics->boatAnims[themp->boat->subID]->ourImages;
  906. else
  907. iv = &graphics->heroAnims[themp->appearance.animationFile]->ourImages;
  908. //pick appropriate flag set
  909. if(themp->boat)
  910. {
  911. switch (themp->boat->subID)
  912. {
  913. case 0: flg = &Graphics::flags1; break;
  914. case 1: flg = &Graphics::flags2; break;
  915. case 2: flg = &Graphics::flags3; break;
  916. default: logGlobal->errorStream() << "Not supported boat subtype: " << themp->boat->subID;
  917. }
  918. }
  919. else
  920. {
  921. flg = &Graphics::flags4;
  922. }
  923. }
  924. else //boat
  925. {
  926. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  927. dir = boat->direction;
  928. iv = &graphics->boatAnims[boat->subID]->ourImages;
  929. }
  930. if(themp && !themp->isStanding) //hero is moving
  931. {
  932. size_t gg;
  933. for(gg=0; gg<iv->size(); ++gg)
  934. {
  935. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  936. {
  937. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  938. break;
  939. }
  940. }
  941. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  942. //printing flag
  943. pp.y+=IMGVAL*2-32;
  944. sr2.y-=16;
  945. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr2);
  946. }
  947. else //hero / boat stands still
  948. {
  949. size_t gg;
  950. for(gg=0; gg < iv->size(); ++gg)
  951. {
  952. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  953. {
  954. tb = (*iv)[gg].bitmap;
  955. break;
  956. }
  957. }
  958. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr2);
  959. //printing flag
  960. if(flg
  961. && obj->pos.x == top_tile.x + bx
  962. && obj->pos.y == top_tile.y + by)
  963. {
  964. SDL_Rect bufr = sr2;
  965. bufr.x-=2*32;
  966. bufr.y-=1*32;
  967. bufr.h = 64;
  968. bufr.w = 96;
  969. if(bufr.x-extRect->x>-64)
  970. CSDL_Ext::blitSurface((graphics->*flg)[color.getNum()]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, nullptr, extSurf, &bufr);
  971. }
  972. }
  973. }
  974. else //blit normal object
  975. {
  976. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  977. SDL_Surface *bitmap = ourImages[(anim+getPhaseShift(obj))%ourImages.size()].bitmap;
  978. //setting appropriate flag color
  979. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  980. CSDL_Ext::setPlayerColor(bitmap, color);
  981. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
  982. }
  983. }
  984. //objects blitted
  985. //X sign
  986. if(puzzleMode)
  987. {
  988. if(bx == grailPosRel.x && by == grailPosRel.y)
  989. {
  990. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, extSurf, &sr);
  991. }
  992. }
  993. }
  994. }
  995. // terrain printed
  996. // printing borders
  997. srx = srx_init;
  998. for (int bx = 0; bx < dx; bx++, srx+=32)
  999. {
  1000. sry = sry_init;
  1001. for (int by = 0; by<dy; by++, sry+=32)
  1002. {
  1003. int3 pos(top_tile.x+bx, top_tile.y+by, top_tile.z); //blitted tile position
  1004. SDL_Rect sr;
  1005. sr.x=srx;
  1006. sr.y=sry;
  1007. sr.h=sr.w=32;
  1008. if (pos.x < 0 || pos.x >= sizes.x ||
  1009. pos.y < 0 || pos.y >= sizes.y)
  1010. {
  1011. // outside of the map - print borders
  1012. SDL_Rect temp_rect = genRect(sr.h, sr.w, 0, 0);
  1013. SDL_Surface * src = ttiles[pos.x][pos.y][top_tile.z].terbitmap;
  1014. assert(src);
  1015. CSDL_Ext::blitSurface(src, &temp_rect,extSurf,&sr);
  1016. }
  1017. else
  1018. {
  1019. //blitting Fog of War
  1020. if (!puzzleMode)
  1021. {
  1022. if (pos.x >= 0 &&
  1023. pos.y >= 0 &&
  1024. pos.x < sizes.x &&
  1025. pos.y < sizes.y &&
  1026. !(*visibilityMap)[pos.x][pos.y][top_tile.z])
  1027. {
  1028. std::pair<SDL_Surface *, bool> hide = getVisBitmap(pos, *visibilityMap);
  1029. if(hide.second)
  1030. CSDL_Ext::blit8bppAlphaTo24bpp(hide.first, &rtile, extSurf, &sr);
  1031. else
  1032. CSDL_Ext::blitSurface(hide.first, &rtile, extSurf, &sr);
  1033. }
  1034. }
  1035. //FoW blitted
  1036. SDL_Rect tileRect = genRect(sr.h, sr.w, 0, 0);
  1037. if (settings["session"]["showBlock"].Bool())
  1038. {
  1039. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).blocked) //temporary hiding blocked positions
  1040. {
  1041. static SDL_Surface * block = nullptr;
  1042. if (!block)
  1043. block = BitmapHandler::loadBitmap("blocked");
  1044. SDL_Rect sr;
  1045. sr.x=srx;
  1046. sr.y=sry;
  1047. sr.h=sr.w=32;
  1048. CSDL_Ext::blitSurface(block, &tileRect, extSurf, &sr);
  1049. }
  1050. }
  1051. if (settings["session"]["showVisit"].Bool())
  1052. {
  1053. if(map->getTile(int3(pos.x, pos.y, top_tile.z)).visitable) //temporary hiding visitable positions
  1054. {
  1055. static SDL_Surface * visit = nullptr;
  1056. if (!visit)
  1057. visit = BitmapHandler::loadBitmap("visitable");
  1058. SDL_Rect sr;
  1059. sr.x=srx;
  1060. sr.y=sry;
  1061. sr.h=sr.w=32;
  1062. CSDL_Ext::blitSurface(visit, &tileRect, extSurf, &sr);
  1063. }
  1064. }
  1065. }
  1066. }
  1067. }
  1068. // borders printed
  1069. // print grid
  1070. if (settings["session"]["showGrid"].Bool())
  1071. {
  1072. srx = srx_init;
  1073. for (int bx = 0; bx < dx; bx++, srx+=32)
  1074. {
  1075. sry = sry_init;
  1076. for (int by = 0; by<dy; by++, sry+=32)
  1077. {
  1078. SDL_Rect sr;
  1079. sr.x=srx;
  1080. sr.y=sry;
  1081. sr.h=sr.w=32;
  1082. const int3 color(0x555555, 0x555555, 0x555555);
  1083. if (sr.y >= extRect->y &&
  1084. sr.y < extRect->y+extRect->h)
  1085. for(int i=0;i<sr.w;i++)
  1086. if (sr.x+i >= extRect->x &&
  1087. sr.x+i < extRect->x+extRect->w)
  1088. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  1089. if (sr.x >= extRect->x &&
  1090. sr.x < extRect->x+extRect->w)
  1091. for(int i=0; i<sr.h;i++)
  1092. if (sr.y+i >= extRect->y &&
  1093. sr.y+i < extRect->y+extRect->h)
  1094. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  1095. }
  1096. }
  1097. }
  1098. // grid
  1099. //applying sepia / gray effect
  1100. if(puzzleMode)
  1101. {
  1102. CSDL_Ext::applyEffect(extSurf, extRect, static_cast<int>(!ADVOPT.puzzleSepia));
  1103. }
  1104. //sepia / gray effect applied
  1105. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  1106. }
  1107. std::pair<SDL_Surface *, bool> CMapHandler::getVisBitmap( const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap ) const
  1108. {
  1109. const NeighborTilesInfo info(pos,sizes,visibilityMap);
  1110. int retBitmapID = info.getBitmapID();// >=0 -> partial hide, <0 - full hide
  1111. if (retBitmapID < 0)
  1112. {
  1113. retBitmapID = - hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  1114. }
  1115. if (retBitmapID >= 0)
  1116. {
  1117. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  1118. }
  1119. else
  1120. {
  1121. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  1122. }
  1123. }
  1124. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1125. {
  1126. if (!graphics->getDef(obj))
  1127. processDef(obj->appearance);
  1128. const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  1129. const int tilesW = bitmap->w/32;
  1130. const int tilesH = bitmap->h/32;
  1131. for(int fx=0; fx<tilesW; ++fx)
  1132. {
  1133. for(int fy=0; fy<tilesH; ++fy)
  1134. {
  1135. SDL_Rect cr;
  1136. cr.w = 32;
  1137. cr.h = 32;
  1138. cr.x = fx*32;
  1139. cr.y = fy*32;
  1140. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1141. 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)
  1142. {
  1143. TerrainTile2 & curt = ttiles[obj->pos.x + fx - tilesW+1][obj->pos.y + fy - tilesH+1][obj->pos.z];
  1144. auto i = curt.objects.begin();
  1145. for(; i != curt.objects.end(); i++)
  1146. {
  1147. if(objectBlitOrderSorter(toAdd, *i))
  1148. {
  1149. curt.objects.insert(i, toAdd);
  1150. i = curt.objects.begin(); //to validate and avoid adding it second time
  1151. break;
  1152. }
  1153. }
  1154. if(i == curt.objects.end())
  1155. curt.objects.insert(i, toAdd);
  1156. }
  1157. } // for(int fy=0; fy<tilesH; ++fy)
  1158. } //for(int fx=0; fx<tilesW; ++fx)
  1159. return true;
  1160. }
  1161. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1162. {
  1163. for (size_t i=0; i<map->width; i++)
  1164. {
  1165. for (size_t j=0; j<map->height; j++)
  1166. {
  1167. for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
  1168. {
  1169. for(size_t x=0; x < ttiles[i][j][k].objects.size(); x++)
  1170. {
  1171. if (ttiles[i][j][k].objects[x].first->id == obj->id)
  1172. {
  1173. ttiles[i][j][k].objects.erase(ttiles[i][j][k].objects.begin() + x);
  1174. break;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. return true;
  1181. }
  1182. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1183. {
  1184. hideObject(obj);
  1185. return true;
  1186. }
  1187. ui8 CMapHandler::getHeroFrameNum(ui8 dir, bool isMoving) const
  1188. {
  1189. if(isMoving)
  1190. {
  1191. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  1192. return frame[dir];
  1193. }
  1194. else //if(isMoving)
  1195. {
  1196. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  1197. return frame[dir];
  1198. }
  1199. }
  1200. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1201. {
  1202. if(ext)
  1203. {
  1204. if(val->x<0)
  1205. {
  1206. val->w += val->x;
  1207. val->x = ext->x;
  1208. }
  1209. else
  1210. {
  1211. val->x += ext->x;
  1212. }
  1213. if(val->y<0)
  1214. {
  1215. val->h += val->y;
  1216. val->y = ext->y;
  1217. }
  1218. else
  1219. {
  1220. val->y += ext->y;
  1221. }
  1222. if(val->x+val->w > ext->x+ext->w)
  1223. {
  1224. val->w = ext->x+ext->w-val->x;
  1225. }
  1226. if(val->y+val->h > ext->y+ext->h)
  1227. {
  1228. val->h = ext->y+ext->h-val->y;
  1229. }
  1230. //for sign problems
  1231. if(val->h > 20000 || val->w > 20000)
  1232. {
  1233. val->h = val->w = 0;
  1234. }
  1235. }
  1236. }
  1237. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  1238. {
  1239. if(a.z!=b.z)
  1240. return -1; //error!
  1241. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1242. return 0;
  1243. else if(a.x==b.x && a.y==b.y+1) //t
  1244. return 1;
  1245. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1246. return 2;
  1247. else if(a.x==b.x-1 && a.y==b.y) //r
  1248. return 3;
  1249. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1250. return 4;
  1251. else if(a.x==b.x && a.y==b.y-1) //b
  1252. return 5;
  1253. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1254. return 6;
  1255. else if(a.x==b.x+1 && a.y==b.y) //l
  1256. return 7;
  1257. return -2; //shouldn't happen
  1258. }
  1259. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1260. {
  1261. //works with at most 16 colors, if needed more -> increase values
  1262. assert(howMany < 16);
  1263. SDL_Color palette[16];
  1264. for(int i=0; i<howMany; ++i)
  1265. {
  1266. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1267. }
  1268. SDL_SetColors(img,palette,from,howMany);
  1269. }
  1270. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1271. {
  1272. for(auto & elem : terrainGraphics[7])
  1273. {
  1274. shiftColors(elem,246, 9);
  1275. }
  1276. for(auto & elem : terrainGraphics[8])
  1277. {
  1278. shiftColors(elem,229, 12);
  1279. shiftColors(elem,242, 14);
  1280. }
  1281. for(auto & elem : staticRiverDefs[0]->ourImages)
  1282. {
  1283. shiftColors(elem.bitmap,183, 12);
  1284. shiftColors(elem.bitmap,195, 6);
  1285. }
  1286. for(auto & elem : staticRiverDefs[2]->ourImages)
  1287. {
  1288. shiftColors(elem.bitmap,228, 12);
  1289. shiftColors(elem.bitmap,183, 6);
  1290. shiftColors(elem.bitmap,240, 6);
  1291. }
  1292. for(auto & elem : staticRiverDefs[3]->ourImages)
  1293. {
  1294. shiftColors(elem.bitmap,240, 9);
  1295. }
  1296. }
  1297. CMapHandler::~CMapHandler()
  1298. {
  1299. delete graphics->FoWfullHide;
  1300. delete graphics->FoWpartialHide;
  1301. for(auto & elem : roadDefs)
  1302. delete elem;
  1303. for(auto & elem : staticRiverDefs)
  1304. delete elem;
  1305. for(auto & elem : terrainGraphics)
  1306. {
  1307. for(int j=0; j < elem.size(); ++j)
  1308. SDL_FreeSurface(elem[j]);
  1309. }
  1310. terrainGraphics.clear();
  1311. }
  1312. CMapHandler::CMapHandler()
  1313. {
  1314. frameW = frameH = 0;
  1315. graphics->FoWfullHide = nullptr;
  1316. graphics->FoWpartialHide = nullptr;
  1317. }
  1318. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1319. {
  1320. out.clear();
  1321. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1322. const TerrainTile &t = map->getTile(pos);
  1323. for(auto & elem : tt.objects)
  1324. {
  1325. if(elem.first->ID == Obj::HOLE) //Hole
  1326. {
  1327. out = elem.first->getObjectName();
  1328. return;
  1329. }
  1330. }
  1331. if(t.hasFavourableWinds())
  1332. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1333. else if(terName)
  1334. out = CGI->generaltexth->terrainNames[t.terType];
  1335. }
  1336. ui8 CMapHandler::getPhaseShift(const CGObjectInstance *object) const
  1337. {
  1338. auto i = animationPhase.find(object);
  1339. if(i == animationPhase.end())
  1340. {
  1341. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  1342. animationPhase[object] = ret;
  1343. return ret;
  1344. }
  1345. return i->second;
  1346. }
  1347. void CMapHandler::discardWorldViewCache()
  1348. {
  1349. cache.discardWorldViewCache();
  1350. }
  1351. void CMapHandler::CMapCache::discardWorldViewCache()
  1352. {
  1353. for (auto &cacheDataPair : data)
  1354. {
  1355. for (auto &cacheEntryPair : cacheDataPair.second)
  1356. {
  1357. if (cacheEntryPair.second)
  1358. {
  1359. SDL_FreeSurface(cacheEntryPair.second);
  1360. }
  1361. }
  1362. data[cacheDataPair.first].clear();
  1363. }
  1364. logGlobal->debugStream() << "Discarded world view cache";
  1365. }
  1366. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1367. {
  1368. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1369. discardWorldViewCache();
  1370. worldViewCachedScale = scale;
  1371. }
  1372. void CMapHandler::CMapCache::removeFromWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1373. {
  1374. auto iter = data[type].find(key);
  1375. if (iter != data[type].end())
  1376. {
  1377. SDL_FreeSurface((*iter).second);
  1378. data[type].erase(iter);
  1379. }
  1380. }
  1381. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1382. {
  1383. auto iter = data[type].find(key);
  1384. if (iter == data[type].end())
  1385. return nullptr;
  1386. return (*iter).second;
  1387. }
  1388. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale)
  1389. {
  1390. auto cached = requestWorldViewCache(type, key);
  1391. if (cached)
  1392. return cached;
  1393. auto scaled = CSDL_Ext::scaleSurfaceFast(fullSurface, fullSurface->w * scale, fullSurface->h * scale);
  1394. return cacheWorldViewEntry(type, key, scaled);
  1395. }
  1396. SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * entry)
  1397. {
  1398. if (!entry)
  1399. return nullptr;
  1400. if (requestWorldViewCache(type, key)) // valid cache already present, no need to do it again
  1401. return requestWorldViewCache(type, key);
  1402. data[type][key] = entry;
  1403. return entry;
  1404. }
  1405. intptr_t CMapHandler::CMapCache::genKey(intptr_t realPtr, ui8 mod)
  1406. {
  1407. return (intptr_t)(realPtr ^ (mod << (sizeof(intptr_t) - 2))); // maybe some cleaner method to pack rotation into cache key?
  1408. }
  1409. TerrainTile2::TerrainTile2()
  1410. :terbitmap(nullptr)
  1411. {}
  1412. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1413. {
  1414. if (a->appearance.printPriority != b->appearance.printPriority)
  1415. return a->appearance.printPriority > b->appearance.printPriority;
  1416. if(a->pos.y != b->pos.y)
  1417. return a->pos.y < b->pos.y;
  1418. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1419. return true;
  1420. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1421. return false;
  1422. if(!a->isVisitable() && b->isVisitable())
  1423. return true;
  1424. if(!b->isVisitable() && a->isVisitable())
  1425. return false;
  1426. if(a->pos.x < b->pos.x)
  1427. return true;
  1428. return false;
  1429. }