CAdvmapInterface.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. #include "StdInc.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CCastleInterface.h"
  4. #include "CHeroWindow.h"
  5. #include "CKingdomInterface.h"
  6. #include "CSpellWindow.h"
  7. #include "GUIClasses.h"
  8. #include "CTradeWindow.h"
  9. #include "../CBitmapHandler.h"
  10. #include "../CDefHandler.h"
  11. #include "../CGameInfo.h"
  12. #include "../CMessage.h"
  13. #include "../CMusicHandler.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CPreGame.h"
  16. #include "../Graphics.h"
  17. #include "../mapHandler.h"
  18. #include "../gui/CCursorHandler.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../gui/SDL_Extensions.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../windows/InfoWindows.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/CGameState.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/CHeroHandler.h"
  28. #include "../../lib/CSoundBase.h"
  29. #include "../../lib/CSpellHandler.h"
  30. #include "../../lib/CTownHandler.h"
  31. #include "../../lib/JsonNode.h"
  32. #include "../../lib/mapObjects/CGHeroInstance.h"
  33. #include "../../lib/mapping/CMap.h"
  34. #include "../../lib/UnlockGuard.h"
  35. #include "../../lib/VCMI_Lib.h"
  36. #ifdef _MSC_VER
  37. #pragma warning (disable : 4355)
  38. #endif
  39. /*
  40. * CAdvMapInterface.cpp, part of VCMI engine
  41. *
  42. * Authors: listed in file AUTHORS in main folder
  43. *
  44. * License: GNU General Public License v2.0 or later
  45. * Full text of license available in license.txt file, in main folder
  46. *
  47. */
  48. #define ADVOPT (conf.go()->ac)
  49. using namespace boost::logic;
  50. using namespace CSDL_Ext;
  51. CAdvMapInt *adventureInt;
  52. CTerrainRect::CTerrainRect()
  53. : curHoveredTile(-1,-1,-1), currentPath(nullptr)
  54. {
  55. tilesw=(ADVOPT.advmapW+31)/32;
  56. tilesh=(ADVOPT.advmapH+31)/32;
  57. pos.x=ADVOPT.advmapX;
  58. pos.y=ADVOPT.advmapY;
  59. pos.w=ADVOPT.advmapW;
  60. pos.h=ADVOPT.advmapH;
  61. moveX = moveY = 0;
  62. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  63. }
  64. void CTerrainRect::deactivate()
  65. {
  66. CIntObject::deactivate();
  67. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  68. }
  69. void CTerrainRect::clickLeft(tribool down, bool previousState)
  70. {
  71. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  72. return;
  73. if ((down==false) || indeterminate(down))
  74. return;
  75. int3 mp = whichTileIsIt();
  76. if (mp.x<0 || mp.y<0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  77. return;
  78. adventureInt->tileLClicked(mp);
  79. }
  80. void CTerrainRect::clickRight(tribool down, bool previousState)
  81. {
  82. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  83. return;
  84. int3 mp = whichTileIsIt();
  85. if (CGI->mh->map->isInTheMap(mp) && down)
  86. adventureInt->tileRClicked(mp);
  87. }
  88. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  89. {
  90. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  91. int3 pom = adventureInt->verifyPos(tHovered);
  92. if(tHovered != pom) //tile outside the map
  93. {
  94. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  95. return;
  96. }
  97. if (pom != curHoveredTile)
  98. curHoveredTile=pom;
  99. else
  100. return;
  101. adventureInt->tileHovered(curHoveredTile);
  102. }
  103. void CTerrainRect::hover(bool on)
  104. {
  105. if (!on)
  106. {
  107. adventureInt->statusbar.clear();
  108. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  109. }
  110. //Hoverable::hover(on);
  111. }
  112. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  113. {
  114. const static int pns[9][9] = {
  115. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  116. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  117. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  118. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  119. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  120. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  121. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  122. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  123. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  124. }; //table of magic values TODO meaning, change variable name
  125. for (int i=0; i < (int)currentPath->nodes.size()-1; ++i)
  126. {
  127. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  128. if(curPos.z != adventureInt->position.z)
  129. continue;
  130. int pn=-1;//number of picture
  131. if (i==0) //last tile
  132. {
  133. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  134. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  135. if (x<0 || y<0 || x>pos.w || y>pos.h)
  136. continue;
  137. pn=0;
  138. }
  139. else
  140. {
  141. const int3 &prevPos = currentPath->nodes[i-1].coord;
  142. std::vector<CGPathNode> & cv = currentPath->nodes;
  143. /* Vector directions
  144. * 0 1 2
  145. * \ | /
  146. * 3 - 4 - 5
  147. * / | \
  148. * 6 7 8
  149. *For example:
  150. * |
  151. * |__\
  152. * /
  153. * is id1=7, id2=5 (pns[7][5])
  154. */
  155. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  156. if(pathContinuous && cv[i].land == cv[i+1].land)
  157. {
  158. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  159. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  160. pn=pns[id1][id2];
  161. }
  162. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  163. {
  164. pn = 0;
  165. }
  166. }
  167. if (currentPath->nodes[i].turns)
  168. pn+=25;
  169. if (pn>=0)
  170. {
  171. CDefEssential * arrows = graphics->heroMoveArrows;
  172. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  173. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  174. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  175. continue;
  176. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  177. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  178. SDL_Rect prevClip;
  179. SDL_GetClipRect(to, &prevClip);
  180. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  181. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  182. {
  183. if (hvx<0 && hvy<0)
  184. {
  185. Rect dstRect = genRect(32, 32, x + moveX, y + moveY);
  186. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, nullptr, to, &dstRect);
  187. }
  188. else if(hvx<0)
  189. {
  190. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  191. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY);
  192. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  193. }
  194. else if (hvy<0)
  195. {
  196. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  197. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  198. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  199. }
  200. else
  201. {
  202. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  203. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  204. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  205. }
  206. }
  207. else //standard version
  208. {
  209. if (hvx<0 && hvy<0)
  210. {
  211. Rect dstRect = genRect(32, 32, x, y);
  212. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, nullptr, to, &dstRect);
  213. }
  214. else if(hvx<0)
  215. {
  216. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  217. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y);
  218. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  219. }
  220. else if (hvy<0)
  221. {
  222. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  223. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  224. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  225. }
  226. else
  227. {
  228. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  229. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  230. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  231. }
  232. }
  233. SDL_SetClipRect(to, &prevClip);
  234. }
  235. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  236. }
  237. void CTerrainRect::show(SDL_Surface * to)
  238. {
  239. if (adventureInt->mode == EAdvMapMode::NORMAL)
  240. {
  241. MapDrawingInfo info(adventureInt->position, &LOCPLINT->cb->getVisibilityMap(), &pos);
  242. info.otherheroAnim = true;
  243. info.anim = adventureInt->anim;
  244. info.heroAnim = adventureInt->heroAnim;
  245. if (ADVOPT.smoothMove)
  246. info.movement = int3(moveX, moveY, 0);
  247. CGI->mh->drawTerrainRectNew(to, &info);
  248. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  249. {
  250. showPath(&pos, to);
  251. }
  252. }
  253. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  254. //SDL_FreeSurface(teren);
  255. }
  256. void CTerrainRect::showAll(SDL_Surface * to)
  257. {
  258. // world view map is static and doesn't need redraw every frame
  259. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  260. {
  261. MapDrawingInfo info(adventureInt->position, &LOCPLINT->cb->getVisibilityMap(), &pos, adventureInt->worldViewIconsDef);
  262. info.scaled = true;
  263. info.scale = adventureInt->worldViewScale;
  264. CGI->mh->drawTerrainRectNew(to, &info);
  265. }
  266. }
  267. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  268. {
  269. int3 ret;
  270. ret.x = adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  271. ret.y = adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  272. ret.z = adventureInt->position.z;
  273. return ret;
  274. }
  275. int3 CTerrainRect::whichTileIsIt()
  276. {
  277. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  278. }
  279. int3 CTerrainRect::tileCountOnScreen()
  280. {
  281. switch (adventureInt->mode)
  282. {
  283. default:
  284. logGlobal->errorStream() << "Unhandled map mode " << (int)adventureInt->mode;
  285. return int3();
  286. case EAdvMapMode::NORMAL:
  287. return int3(tilesw, tilesh, 1);
  288. case EAdvMapMode::WORLD_VIEW:
  289. return int3(tilesw / adventureInt->worldViewScale, tilesh / adventureInt->worldViewScale, 1);
  290. }
  291. }
  292. void CResDataBar::clickRight(tribool down, bool previousState)
  293. {
  294. }
  295. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  296. {
  297. bg = BitmapHandler::loadBitmap(defname);
  298. CSDL_Ext::setDefaultColorKey(bg);
  299. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  300. pos = genRect(bg->h, bg->w, pos.x+x, pos.y+y);
  301. txtpos.resize(8);
  302. for (int i = 0; i < 8 ; i++)
  303. {
  304. txtpos[i].first = pos.x + offx + resdist*i;
  305. txtpos[i].second = pos.y + offy;
  306. }
  307. txtpos[7].first = txtpos[6].first + datedist;
  308. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  309. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  310. addUsedEvents(RCLICK);
  311. }
  312. CResDataBar::CResDataBar()
  313. {
  314. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  315. CSDL_Ext::setDefaultColorKey(bg);
  316. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  317. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  318. txtpos.resize(8);
  319. for (int i = 0; i < 8 ; i++)
  320. {
  321. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  322. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  323. }
  324. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  325. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  326. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  327. }
  328. CResDataBar::~CResDataBar()
  329. {
  330. SDL_FreeSurface(bg);
  331. }
  332. void CResDataBar::draw(SDL_Surface * to)
  333. {
  334. blitAt(bg,pos.x,pos.y,to);
  335. for (auto i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  336. {
  337. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  338. graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
  339. }
  340. std::vector<std::string> temp;
  341. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  342. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  343. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  344. graphics->fonts[FONT_SMALL]->renderTextLeft(to, processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
  345. }
  346. void CResDataBar::show(SDL_Surface * to)
  347. {
  348. }
  349. void CResDataBar::showAll(SDL_Surface * to)
  350. {
  351. draw(to);
  352. }
  353. CAdvMapInt::CAdvMapInt():
  354. mode(EAdvMapMode::NORMAL),
  355. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  356. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  357. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  358. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  359. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192) )
  360. {
  361. duringAITurn = false;
  362. state = NA;
  363. spellBeingCasted = nullptr;
  364. pos.x = pos.y = 0;
  365. pos.w = screen->w;
  366. pos.h = screen->h;
  367. position = int3(0,0,0);
  368. selection = nullptr;
  369. townList.onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  370. adventureInt=this;
  371. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  372. bgWorldView = BitmapHandler::loadBitmap(ADVOPT.worldViewGraphic);
  373. scrollingDir = 0;
  374. updateScreen = false;
  375. anim=0;
  376. animValHitCount=0; //animation frame
  377. heroAnim=0;
  378. heroAnimValHitCount=0; // hero animation frame
  379. if (!bgWorldView)
  380. {
  381. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  382. bgWorldView = BitmapHandler::loadBitmap("VWorld.bmp");
  383. }
  384. worldViewIconsDef = CDefHandler::giveDef("VwSymbol.def");
  385. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  386. {
  387. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  388. }
  389. auto makeButton = [&] (int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> CButton *
  390. {
  391. auto button = new CButton(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  392. for (auto image : info.additionalDefs)
  393. button->addImage(image);
  394. return button;
  395. };
  396. kingOverview = makeButton(293, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  397. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  398. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  399. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  400. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  401. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  402. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  403. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  404. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  405. endTurn = makeButton(302, std::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  406. panelMain = new CAdvMapPanel(nullptr, Point(0, 0));
  407. panelWorldView = new CAdvMapWorldViewPanel(bgWorldView, Point(heroList.pos.x - 2, 195)); // TODO correct drawing position
  408. panelMain->addChildColorableButton(kingOverview);
  409. panelMain->addChildColorableButton(underground);
  410. panelMain->addChildColorableButton(questlog);
  411. panelMain->addChildColorableButton(sleepWake);
  412. panelMain->addChildColorableButton(moveHero);
  413. panelMain->addChildColorableButton(spellbook);
  414. panelMain->addChildColorableButton(advOptions);
  415. panelMain->addChildColorableButton(sysOptions);
  416. panelMain->addChildColorableButton(nextHero);
  417. panelMain->addChildColorableButton(endTurn);
  418. // TODO move configs to resolutions.json, similarly to previous buttons
  419. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  420. worldViewBackConfig.defName = "IOK6432.DEF";
  421. worldViewBackConfig.x = screen->w - 73;
  422. worldViewBackConfig.y = 343 + 195;
  423. worldViewBackConfig.playerColoured = false;
  424. panelWorldView->addChildToPanel(
  425. makeButton(288, std::bind(&CAdvMapInt::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  426. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  427. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  428. worldViewScale1xConfig.x = screen->w - 191;
  429. worldViewScale1xConfig.y = 23 + 195;
  430. worldViewScale1xConfig.playerColoured = false;
  431. panelWorldView->addChildToPanel(
  432. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  433. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  434. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  435. worldViewScale2xConfig.x = screen->w - 191 + 63;
  436. worldViewScale2xConfig.y = 23 + 195;
  437. worldViewScale2xConfig.playerColoured = false;
  438. panelWorldView->addChildToPanel(
  439. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  440. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  441. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  442. worldViewScale4xConfig.x = screen->w - 191 + 126;
  443. worldViewScale4xConfig.y = 23 + 195;
  444. worldViewScale4xConfig.playerColoured = false;
  445. panelWorldView->addChildToPanel(
  446. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  447. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  448. worldViewUndergroundConfig.defName = "IAM010.DEF";
  449. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  450. worldViewUndergroundConfig.x = screen->w - 115;
  451. worldViewUndergroundConfig.y = 343 + 195;
  452. worldViewUndergroundConfig.playerColoured = true;
  453. worldViewUnderground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  454. panelWorldView->addChildColorableButton(worldViewUnderground);
  455. setPlayer(LOCPLINT->playerID);
  456. int iconColorMultiplier = player.getNum() * 19;
  457. int wvLeft = heroList.pos.x - 2; // TODO correct drawing position
  458. for (int i = 0; i < 5; ++i)
  459. {
  460. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(wvLeft + 5, 253 + i * 20)), worldViewIconsDef, iconColorMultiplier);
  461. panelWorldView->addChildToPanel(new CLabel(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  462. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  463. }
  464. for (int i = 0; i < 7; ++i)
  465. {
  466. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(wvLeft + 5, 377 + i * 20)), worldViewIconsDef, iconColorMultiplier);
  467. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(wvLeft + 160, 377 + i * 20)), worldViewIconsDef, iconColorMultiplier);
  468. panelWorldView->addChildToPanel(new CLabel(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  469. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  470. }
  471. panelWorldView->addChildToPanel(new CLabel(wvLeft + 5, 367, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  472. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  473. panelWorldView->addChildToPanel(new CLabel(wvLeft + 185, 387, EFonts::FONT_SMALL, EAlignment::BOTTOMRIGHT,
  474. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  475. activeMapPanel = panelMain;
  476. underground->block(!CGI->mh->map->twoLevel);
  477. worldViewUnderground->block(!CGI->mh->map->twoLevel);
  478. addUsedEvents(MOVE);
  479. }
  480. CAdvMapInt::~CAdvMapInt()
  481. {
  482. SDL_FreeSurface(bg);
  483. for(int i=0; i<gems.size(); i++)
  484. delete gems[i];
  485. delete worldViewIconsDef;
  486. }
  487. void CAdvMapInt::fshowOverview()
  488. {
  489. GH.pushInt(new CKingdomInterface);
  490. }
  491. void CAdvMapInt::fworldViewBack()
  492. {
  493. changeMode(EAdvMapMode::NORMAL);
  494. CGI->mh->discardWorldViewCache();
  495. auto hero = curHero();
  496. if (hero)
  497. centerOn(hero);
  498. }
  499. void CAdvMapInt::fworldViewScale1x()
  500. {
  501. // TODO set corresponding scale button to "selected" mode
  502. changeMode(EAdvMapMode::WORLD_VIEW, 0.22f);
  503. }
  504. void CAdvMapInt::fworldViewScale2x()
  505. {
  506. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f);
  507. }
  508. void CAdvMapInt::fworldViewScale4x()
  509. {
  510. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f);
  511. }
  512. void CAdvMapInt::fswitchLevel()
  513. {
  514. // with support for future multi-level maps :)
  515. int maxLevels = CGI->mh->map->twoLevel ? 2 : 1;
  516. if (maxLevels < 2)
  517. return;
  518. position.z = (position.z + 1) % maxLevels;
  519. underground->setIndex(position.z, true);
  520. underground->redraw();
  521. worldViewUnderground->setIndex(position.z, true);
  522. worldViewUnderground->redraw();
  523. updateScreen = true;
  524. minimap.setLevel(position.z);
  525. if (mode == EAdvMapMode::WORLD_VIEW)
  526. terrain.redraw();
  527. }
  528. void CAdvMapInt::fshowQuestlog()
  529. {
  530. LOCPLINT->showQuestLog();
  531. }
  532. void CAdvMapInt::fsleepWake()
  533. {
  534. const CGHeroInstance *h = curHero();
  535. if (!h)
  536. return;
  537. bool newSleep = !isHeroSleeping(h);
  538. setHeroSleeping(h, newSleep);
  539. updateSleepWake(h);
  540. if (newSleep)
  541. {
  542. fnextHero();
  543. //moveHero.block(true);
  544. //uncomment to enable original HoMM3 behaviour:
  545. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  546. }
  547. }
  548. void CAdvMapInt::fmoveHero()
  549. {
  550. const CGHeroInstance *h = curHero();
  551. if (!h || !terrain.currentPath)
  552. return;
  553. LOCPLINT->moveHero(h, *terrain.currentPath);
  554. }
  555. void CAdvMapInt::fshowSpellbok()
  556. {
  557. if (!curHero()) //checking necessary values
  558. return;
  559. centerOn(selection);
  560. auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  561. GH.pushInt(spellWindow);
  562. }
  563. void CAdvMapInt::fadventureOPtions()
  564. {
  565. GH.pushInt(new CAdventureOptions);
  566. }
  567. void CAdvMapInt::fsystemOptions()
  568. {
  569. GH.pushInt(new CSystemOptionsWindow());
  570. }
  571. void CAdvMapInt::fnextHero()
  572. {
  573. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  574. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  575. if (next < 0)
  576. return;
  577. select(LOCPLINT->wanderingHeroes[next], true);
  578. }
  579. void CAdvMapInt::fendTurn()
  580. {
  581. if(!LOCPLINT->makingTurn)
  582. return;
  583. if ( settings["adventure"]["heroReminder"].Bool())
  584. {
  585. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  586. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  587. {
  588. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), 0, false);
  589. return;
  590. }
  591. }
  592. endingTurn();
  593. }
  594. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  595. {
  596. sleepWake->block(!h);
  597. if (!h)
  598. return;
  599. bool state = isHeroSleeping(h);
  600. sleepWake->setIndex(state ? 1 : 0, true);
  601. sleepWake->assignedKeys.clear();
  602. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  603. }
  604. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  605. {
  606. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  607. if (hasPath == boost::indeterminate)
  608. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  609. if (!h)
  610. {
  611. moveHero->block(true);
  612. return;
  613. }
  614. moveHero->block(!hasPath || (h->movement == 0));
  615. }
  616. int CAdvMapInt::getNextHeroIndex(int startIndex)
  617. {
  618. if (LOCPLINT->wanderingHeroes.size() == 0)
  619. return -1;
  620. if (startIndex < 0)
  621. startIndex = 0;
  622. int i = startIndex;
  623. do
  624. {
  625. i++;
  626. if (i >= LOCPLINT->wanderingHeroes.size())
  627. i = 0;
  628. }
  629. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  630. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  631. return i;
  632. else
  633. return -1;
  634. }
  635. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  636. {
  637. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  638. int next = getNextHeroIndex(start);
  639. if (next < 0)
  640. {
  641. nextHero->block(true);
  642. return;
  643. }
  644. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  645. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  646. nextHero->block(noActiveHeroes);
  647. }
  648. void CAdvMapInt::activate()
  649. {
  650. CIntObject::activate();
  651. if (!(active & KEYBOARD))
  652. CIntObject::activate(KEYBOARD);
  653. screenBuf = screen;
  654. GH.statusbar = &statusbar;
  655. if(!duringAITurn)
  656. {
  657. activeMapPanel->activate();
  658. if (mode == EAdvMapMode::NORMAL)
  659. {
  660. heroList.activate();
  661. townList.activate();
  662. infoBar.activate();
  663. }
  664. minimap.activate();
  665. terrain.activate();
  666. LOCPLINT->cingconsole->activate();
  667. GH.fakeMouseMove(); //to restore the cursor
  668. }
  669. }
  670. void CAdvMapInt::deactivate()
  671. {
  672. CIntObject::deactivate();
  673. if(!duringAITurn)
  674. {
  675. scrollingDir = 0;
  676. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  677. activeMapPanel->deactivate();
  678. if (mode == EAdvMapMode::NORMAL)
  679. {
  680. heroList.deactivate();
  681. townList.deactivate();
  682. infoBar.deactivate();
  683. }
  684. minimap.deactivate();
  685. terrain.deactivate();
  686. if(LOCPLINT)
  687. LOCPLINT->cingconsole->deactivate();
  688. }
  689. }
  690. void CAdvMapInt::showAll(SDL_Surface * to)
  691. {
  692. blitAt(bg,0,0,to);
  693. if(state != INGAME)
  694. return;
  695. switch (mode)
  696. {
  697. case EAdvMapMode::NORMAL:
  698. heroList.showAll(to);
  699. townList.showAll(to);
  700. infoBar.showAll(to);
  701. break;
  702. case EAdvMapMode::WORLD_VIEW:
  703. terrain.showAll(to);
  704. break;
  705. }
  706. activeMapPanel->showAll(to);
  707. updateScreen = true;
  708. minimap.showAll(to);
  709. show(to);
  710. resdatabar.draw(to);
  711. statusbar.show(to);
  712. LOCPLINT->cingconsole->showAll(to);
  713. }
  714. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  715. {
  716. if (!hero)
  717. return false;
  718. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  719. }
  720. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  721. {
  722. if (sleep)
  723. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  724. else
  725. LOCPLINT->sleepingHeroes -= hero;
  726. updateNextHero(nullptr);
  727. }
  728. void CAdvMapInt::show(SDL_Surface * to)
  729. {
  730. if(state != INGAME)
  731. return;
  732. ++animValHitCount; //for animations
  733. if(animValHitCount == 8)
  734. {
  735. CGI->mh->updateWater();
  736. animValHitCount = 0;
  737. ++anim;
  738. updateScreen = true;
  739. }
  740. ++heroAnim;
  741. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  742. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  743. if((animValHitCount % (4/scrollSpeed)) == 0
  744. && (
  745. (GH.topInt() == this)
  746. || isCtrlKeyDown()
  747. )
  748. )
  749. {
  750. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  751. position.x--;
  752. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  753. position.x++;
  754. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  755. position.y--;
  756. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  757. position.y++;
  758. if(scrollingDir)
  759. {
  760. updateScreen = true;
  761. minimap.redraw();
  762. if (mode == EAdvMapMode::WORLD_VIEW)
  763. terrain.redraw();
  764. }
  765. }
  766. if(updateScreen)
  767. {
  768. int3 betterPos = LOCPLINT->repairScreenPos(position);
  769. if (betterPos != position)
  770. {
  771. logGlobal->warnStream() << "Incorrect position for adventure map!";
  772. position = betterPos;
  773. }
  774. terrain.show(to);
  775. for(int i=0;i<4;i++)
  776. blitAt(gems[i]->ourImages[LOCPLINT->playerID.getNum()].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  777. updateScreen=false;
  778. LOCPLINT->cingconsole->showAll(to);
  779. }
  780. infoBar.show(to);
  781. statusbar.showAll(to);
  782. }
  783. void CAdvMapInt::selectionChanged()
  784. {
  785. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  786. if (selection != to)
  787. select(to);
  788. }
  789. void CAdvMapInt::centerOn(int3 on)
  790. {
  791. bool switchedLevels = on.z != position.z;
  792. switch (mode)
  793. {
  794. default:
  795. case EAdvMapMode::NORMAL:
  796. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  797. on.y -= CGI->mh->frameH;
  798. break;
  799. case EAdvMapMode::WORLD_VIEW:
  800. on.x -= CGI->mh->tilesW / 2 / worldViewScale;
  801. on.y -= CGI->mh->tilesH / 2 / worldViewScale;
  802. break;
  803. }
  804. on = LOCPLINT->repairScreenPos(on);
  805. position = on;
  806. updateScreen=true;
  807. underground->setIndex(on.z,true); //change underground switch button image
  808. underground->redraw();
  809. worldViewUnderground->setIndex(on.z, true);
  810. worldViewUnderground->redraw();
  811. if (switchedLevels)
  812. minimap.setLevel(position.z);
  813. minimap.redraw();
  814. if (mode == EAdvMapMode::WORLD_VIEW)
  815. terrain.redraw();
  816. }
  817. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  818. {
  819. centerOn(obj->getSightCenter());
  820. }
  821. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  822. {
  823. if (mode == EAdvMapMode::WORLD_VIEW)
  824. return;
  825. ui8 Dir = 0;
  826. int k = key.keysym.sym;
  827. const CGHeroInstance *h = curHero(); //selected hero
  828. const CGTownInstance *t = curTown(); //selected town
  829. switch(k)
  830. {
  831. case SDLK_g:
  832. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  833. return;
  834. {
  835. //find first town with tavern
  836. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  837. {
  838. return town->hasBuilt(BuildingID::TAVERN);
  839. });
  840. if(itr != LOCPLINT->towns.end())
  841. LOCPLINT->showThievesGuildWindow(*itr);
  842. else
  843. LOCPLINT->showInfoDialog("No available town with tavern!");
  844. }
  845. return;
  846. case SDLK_i:
  847. if(isActive())
  848. CAdventureOptions::showScenarioInfo();
  849. return;
  850. case SDLK_l:
  851. if(isActive())
  852. LOCPLINT->proposeLoadingGame();
  853. return;
  854. case SDLK_s:
  855. if(isActive())
  856. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  857. return;
  858. case SDLK_d:
  859. {
  860. if(h && isActive() && key.state == SDL_PRESSED)
  861. LOCPLINT->tryDiggging(h);
  862. return;
  863. }
  864. case SDLK_p:
  865. if(isActive())
  866. LOCPLINT->showPuzzleMap();
  867. return;
  868. case SDLK_r:
  869. if(isActive() && LOCPLINT->ctrlPressed())
  870. {
  871. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  872. []{ LOCPLINT->sendCustomEvent(RESTART_GAME); },
  873. []{}, true);
  874. }
  875. return;
  876. case SDLK_SPACE: //space - try to revisit current object with selected hero
  877. {
  878. if(!isActive())
  879. return;
  880. if(h && key.state == SDL_PRESSED)
  881. {
  882. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  883. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  884. //this thread leaves scope and tries to lock pim while holding gs,
  885. //network thread tries to lock gs (appluy cl) while holding pim
  886. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  887. LOCPLINT->cb->moveHero(h,h->pos);
  888. }
  889. }
  890. return;
  891. case SDLK_RETURN:
  892. {
  893. if(!isActive() || !selection || key.state != SDL_PRESSED)
  894. return;
  895. if(h)
  896. LOCPLINT->openHeroWindow(h);
  897. else if(t)
  898. LOCPLINT->openTownWindow(t);
  899. return;
  900. }
  901. case SDLK_ESCAPE:
  902. {
  903. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  904. return;
  905. leaveCastingMode();
  906. return;
  907. }
  908. case SDLK_t:
  909. {
  910. //act on key down if marketplace windows is not already opened
  911. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  912. return;
  913. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  914. {
  915. //check if we have any marketplace
  916. const CGTownInstance *townWithMarket = nullptr;
  917. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  918. {
  919. if(t->hasBuilt(BuildingID::MARKETPLACE))
  920. {
  921. townWithMarket = t;
  922. break;
  923. }
  924. }
  925. if(townWithMarket) //if any town has marketplace, open window
  926. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  927. else //if not - complain
  928. LOCPLINT->showInfoDialog("No available marketplace!");
  929. }
  930. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  931. {
  932. townList.selectNext();
  933. }
  934. return;
  935. }
  936. default:
  937. {
  938. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  939. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  940. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  941. //numpad arrow
  942. if(CGuiHandler::isArrowKey(SDLKey(k)))
  943. k = CGuiHandler::arrowToNum(SDLKey(k));
  944. #ifdef VCMI_SDL1
  945. k -= SDLK_KP0 + 1;
  946. #else
  947. k -= SDLK_KP_1;
  948. #endif // VCMI_SDL1
  949. if(k < 0 || k > 8)
  950. return;
  951. int3 dir = directions[k];
  952. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  953. {
  954. Dir = (dir.x<0 ? LEFT : 0) |
  955. (dir.x>0 ? RIGHT : 0) |
  956. (dir.y<0 ? UP : 0) |
  957. (dir.y>0 ? DOWN : 0) ;
  958. break;
  959. }
  960. if(!h || key.state != SDL_PRESSED)
  961. break;
  962. if(k == 4)
  963. {
  964. centerOn(h);
  965. return;
  966. }
  967. CGPath &path = LOCPLINT->paths[h];
  968. terrain.currentPath = &path;
  969. if(!LOCPLINT->cb->getPathsInfo(h)->getPath(h->getPosition(false) + dir, path))
  970. {
  971. terrain.currentPath = nullptr;
  972. return;
  973. }
  974. if (path.nodes.size() > 2)
  975. updateMoveHero(h);
  976. else
  977. if(!path.nodes[0].turns)
  978. LOCPLINT->moveHero(h, path);
  979. }
  980. return;
  981. }
  982. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  983. && LOCPLINT->ctrlPressed()
  984. )
  985. scrollingDir |= Dir;
  986. else
  987. scrollingDir &= ~Dir;
  988. }
  989. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  990. {
  991. if(down)
  992. {
  993. CRClickPopup::createAndPush(text);
  994. }
  995. }
  996. int3 CAdvMapInt::verifyPos(int3 ver)
  997. {
  998. if (ver.x<0)
  999. ver.x=0;
  1000. if (ver.y<0)
  1001. ver.y=0;
  1002. if (ver.z<0)
  1003. ver.z=0;
  1004. if (ver.x>=CGI->mh->sizes.x)
  1005. ver.x=CGI->mh->sizes.x-1;
  1006. if (ver.y>=CGI->mh->sizes.y)
  1007. ver.y=CGI->mh->sizes.y-1;
  1008. if (ver.z>=CGI->mh->sizes.z)
  1009. ver.z=CGI->mh->sizes.z-1;
  1010. return ver;
  1011. }
  1012. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  1013. {
  1014. assert(sel);
  1015. LOCPLINT->setSelection(sel);
  1016. selection = sel;
  1017. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1018. {
  1019. auto pos = sel->visitablePos();
  1020. auto tile = LOCPLINT->cb->getTile(pos);
  1021. if(tile)
  1022. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  1023. }
  1024. if(centerView)
  1025. centerOn(sel);
  1026. terrain.currentPath = nullptr;
  1027. if(sel->ID==Obj::TOWN)
  1028. {
  1029. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1030. infoBar.showTownSelection(town);
  1031. townList.select(town);
  1032. heroList.select(nullptr);
  1033. updateSleepWake(nullptr);
  1034. updateMoveHero(nullptr);
  1035. }
  1036. else //hero selected
  1037. {
  1038. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1039. infoBar.showHeroSelection(hero);
  1040. heroList.select(hero);
  1041. townList.select(nullptr);
  1042. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1043. updateSleepWake(hero);
  1044. updateMoveHero(hero);
  1045. }
  1046. townList.redraw();
  1047. heroList.redraw();
  1048. }
  1049. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1050. {
  1051. // adventure map scrolling with mouse
  1052. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1053. if(!isCtrlKeyDown() && isActive() && mode == EAdvMapMode::NORMAL)
  1054. {
  1055. if(sEvent.x<15)
  1056. {
  1057. scrollingDir |= LEFT;
  1058. }
  1059. else
  1060. {
  1061. scrollingDir &= ~LEFT;
  1062. }
  1063. if(sEvent.x>screen->w-15)
  1064. {
  1065. scrollingDir |= RIGHT;
  1066. }
  1067. else
  1068. {
  1069. scrollingDir &= ~RIGHT;
  1070. }
  1071. if(sEvent.y<15)
  1072. {
  1073. scrollingDir |= UP;
  1074. }
  1075. else
  1076. {
  1077. scrollingDir &= ~UP;
  1078. }
  1079. if(sEvent.y>screen->h-15)
  1080. {
  1081. scrollingDir |= DOWN;
  1082. }
  1083. else
  1084. {
  1085. scrollingDir &= ~DOWN;
  1086. }
  1087. }
  1088. }
  1089. bool CAdvMapInt::isActive()
  1090. {
  1091. return active & ~CIntObject::KEYBOARD;
  1092. }
  1093. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1094. {
  1095. state = WAITING;
  1096. }
  1097. void CAdvMapInt::setPlayer(PlayerColor Player)
  1098. {
  1099. player = Player;
  1100. graphics->blueToPlayersAdv(bg,player);
  1101. panelMain->setPlayerColor(player);
  1102. panelWorldView->setPlayerColor(player);
  1103. panelWorldView->recolorIcons(worldViewIconsDef, player.getNum() * 19);
  1104. graphics->blueToPlayersAdv(resdatabar.bg,player);
  1105. //heroList.updateHList();
  1106. //townList.genList();
  1107. }
  1108. void CAdvMapInt::startTurn()
  1109. {
  1110. state = INGAME;
  1111. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  1112. {
  1113. adjustActiveness(false);
  1114. minimap.setAIRadar(false);
  1115. }
  1116. }
  1117. void CAdvMapInt::endingTurn()
  1118. {
  1119. if(LOCPLINT->cingconsole->active)
  1120. LOCPLINT->cingconsole->deactivate();
  1121. LOCPLINT->makingTurn = false;
  1122. LOCPLINT->cb->endTurn();
  1123. }
  1124. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1125. {
  1126. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1127. if (bobjs.empty())
  1128. return nullptr;
  1129. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1130. /*
  1131. if (bobjs.back()->ID == Obj::HERO)
  1132. return bobjs.back();
  1133. else
  1134. return bobjs.front();*/
  1135. }
  1136. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1137. {
  1138. if(mode != EAdvMapMode::NORMAL)
  1139. return;
  1140. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1141. return;
  1142. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1143. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1144. int3 selPos = selection->getSightCenter();
  1145. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1146. {
  1147. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1148. switch(spellBeingCasted->id)
  1149. {
  1150. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1151. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1152. leaveCastingMode(true, mapPos);
  1153. break;
  1154. case SpellID::DIMENSION_DOOR:
  1155. if(!tile || tile->isClear(heroTile))
  1156. leaveCastingMode(true, mapPos);
  1157. break;
  1158. }
  1159. return;
  1160. }
  1161. //check if we can select this object
  1162. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1163. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1164. if (selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1165. {
  1166. assert(!terrain.currentPath); //path can be active only when hero is selected
  1167. if(selection == topBlocking) //selected town clicked
  1168. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1169. else if ( canSelect )
  1170. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1171. return;
  1172. }
  1173. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1174. {
  1175. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1176. if(currentHero == topBlocking) //clicked selected hero
  1177. {
  1178. LOCPLINT->openHeroWindow(currentHero);
  1179. return;
  1180. }
  1181. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1182. {
  1183. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1184. return;
  1185. }
  1186. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1187. {
  1188. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1189. {
  1190. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1191. return;
  1192. }
  1193. else/* if(mp.z == currentHero->pos.z)*/ //remove old path and find a new one if we clicked on the map level on which hero is present
  1194. {
  1195. CGPath &path = LOCPLINT->paths[currentHero];
  1196. terrain.currentPath = &path;
  1197. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(mapPos, path); //try getting path, erase if failed
  1198. updateMoveHero(currentHero);
  1199. if (!gotPath)
  1200. LOCPLINT->eraseCurrentPathOf(currentHero);
  1201. else
  1202. return;
  1203. }
  1204. }
  1205. } //end of hero is selected "case"
  1206. else
  1207. {
  1208. throw std::runtime_error("Nothing is selected...");
  1209. }
  1210. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1211. {
  1212. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1213. }
  1214. }
  1215. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1216. {
  1217. if(mode != EAdvMapMode::NORMAL)
  1218. return;
  1219. if(!LOCPLINT->cb->isVisible(mapPos))
  1220. {
  1221. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1222. statusbar.clear();
  1223. return;
  1224. }
  1225. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1226. if (objAtTile)
  1227. {
  1228. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1229. boost::replace_all(text,"\n"," ");
  1230. statusbar.setText(text);
  1231. }
  1232. else
  1233. {
  1234. std::string hlp;
  1235. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1236. statusbar.setText(hlp);
  1237. }
  1238. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1239. return;
  1240. if(spellBeingCasted)
  1241. {
  1242. switch(spellBeingCasted->id)
  1243. {
  1244. case SpellID::SCUTTLE_BOAT:
  1245. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1246. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1247. else
  1248. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1249. return;
  1250. case SpellID::DIMENSION_DOOR:
  1251. {
  1252. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1253. int3 hpos = selection->getSightCenter();
  1254. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1255. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1256. else
  1257. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1258. return;
  1259. }
  1260. }
  1261. }
  1262. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->getGuardingCreaturePosition(mapPos));
  1263. if(selection->ID == Obj::TOWN)
  1264. {
  1265. if(objAtTile)
  1266. {
  1267. if(objAtTile->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner) != PlayerRelations::ENEMIES)
  1268. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1269. else if(objAtTile->ID == Obj::HERO && objAtTile->tempOwner == LOCPLINT->playerID)
  1270. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1271. else
  1272. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1273. }
  1274. else
  1275. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1276. }
  1277. else if(const CGHeroInstance *h = curHero())
  1278. {
  1279. const CGPathNode *pnode = LOCPLINT->cb->getPathsInfo(h)->getPathInfo(mapPos);
  1280. int turns = pnode->turns;
  1281. vstd::amin(turns, 3);
  1282. bool accessible = pnode->turns < 255;
  1283. if(objAtTile)
  1284. {
  1285. if(objAtTile->ID == Obj::HERO)
  1286. {
  1287. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1288. {
  1289. if(accessible)
  1290. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1291. else
  1292. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1293. }
  1294. else //our or ally hero
  1295. {
  1296. if(selection == objAtTile)
  1297. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1298. else if(accessible)
  1299. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1300. else
  1301. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1302. }
  1303. }
  1304. else if(objAtTile->ID == Obj::TOWN)
  1305. {
  1306. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1307. {
  1308. if(accessible)
  1309. {
  1310. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1311. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1312. if (townObj && !townObj->armedGarrison())
  1313. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1314. else
  1315. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1316. }
  1317. else
  1318. {
  1319. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1320. }
  1321. }
  1322. else //our or ally town
  1323. {
  1324. if(accessible)
  1325. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1326. else
  1327. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1328. }
  1329. }
  1330. else if(objAtTile->ID == Obj::BOAT)
  1331. {
  1332. if(accessible)
  1333. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1334. else
  1335. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1336. }
  1337. else if (objAtTile->ID == Obj::GARRISON || objAtTile->ID == Obj::GARRISON2)
  1338. {
  1339. if (accessible)
  1340. {
  1341. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1342. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1343. if (garrObj && garrObj->stacksCount()
  1344. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1345. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1346. else
  1347. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1348. }
  1349. else
  1350. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1351. }
  1352. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1353. {
  1354. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1355. }
  1356. else
  1357. {
  1358. if(accessible)
  1359. {
  1360. if(pnode->land)
  1361. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1362. else
  1363. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1364. }
  1365. else
  1366. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1367. }
  1368. }
  1369. else //no objs
  1370. {
  1371. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1372. {
  1373. if (guardingCreature)
  1374. {
  1375. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1376. }
  1377. else
  1378. {
  1379. if(pnode->land)
  1380. {
  1381. if(LOCPLINT->cb->getTile(h->getPosition(false))->terType != ETerrainType::WATER)
  1382. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1383. else
  1384. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6); //anchor
  1385. }
  1386. else
  1387. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1388. }
  1389. }
  1390. else
  1391. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1392. }
  1393. }
  1394. if(ourInaccessibleShipyard(objAtTile))
  1395. {
  1396. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1397. }
  1398. }
  1399. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1400. {
  1401. if(mode != EAdvMapMode::NORMAL)
  1402. return;
  1403. if(spellBeingCasted)
  1404. {
  1405. leaveCastingMode();
  1406. return;
  1407. }
  1408. if(!LOCPLINT->cb->isVisible(mapPos))
  1409. {
  1410. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1411. return;
  1412. }
  1413. const CGObjectInstance * obj = getActiveObject(mapPos);
  1414. if(!obj)
  1415. {
  1416. // Bare or undiscovered terrain
  1417. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1418. if (tile)
  1419. {
  1420. std::string hlp;
  1421. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1422. CRClickPopup::createAndPush(hlp);
  1423. }
  1424. return;
  1425. }
  1426. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1427. }
  1428. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1429. {
  1430. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1431. spellBeingCasted = sp;
  1432. deactivate();
  1433. terrain.activate();
  1434. GH.fakeMouseMove();
  1435. }
  1436. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1437. {
  1438. assert(spellBeingCasted);
  1439. SpellID id = spellBeingCasted->id;
  1440. spellBeingCasted = nullptr;
  1441. terrain.deactivate();
  1442. activate();
  1443. if(cast)
  1444. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1445. else
  1446. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1447. }
  1448. const CGHeroInstance * CAdvMapInt::curHero() const
  1449. {
  1450. if(selection && selection->ID == Obj::HERO)
  1451. return static_cast<const CGHeroInstance *>(selection);
  1452. else
  1453. return nullptr;
  1454. }
  1455. const CGTownInstance * CAdvMapInt::curTown() const
  1456. {
  1457. if(selection && selection->ID == Obj::TOWN)
  1458. return static_cast<const CGTownInstance *>(selection);
  1459. else
  1460. return nullptr;
  1461. }
  1462. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1463. {
  1464. const IShipyard *ret = IShipyard::castFrom(obj);
  1465. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1466. return nullptr;
  1467. return ret;
  1468. }
  1469. void CAdvMapInt::aiTurnStarted()
  1470. {
  1471. adjustActiveness(true);
  1472. CCS->musich->playMusicFromSet("enemy-turn", true);
  1473. adventureInt->minimap.setAIRadar(true);
  1474. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1475. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1476. }
  1477. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1478. {
  1479. bool wasActive = isActive();
  1480. if(wasActive)
  1481. deactivate();
  1482. adventureInt->duringAITurn = aiTurnStart;
  1483. if(wasActive)
  1484. activate();
  1485. }
  1486. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale /* = 0.4f */)
  1487. {
  1488. if (mode != newMode)
  1489. {
  1490. mode = newMode;
  1491. switch (mode)
  1492. {
  1493. case EAdvMapMode::NORMAL:
  1494. panelMain->activate();
  1495. panelWorldView->deactivate();
  1496. activeMapPanel = panelMain;
  1497. townList.activate();
  1498. heroList.activate();
  1499. infoBar.activate();
  1500. break;
  1501. case EAdvMapMode::WORLD_VIEW:
  1502. panelMain->deactivate();
  1503. panelWorldView->activate();
  1504. activeMapPanel = panelWorldView;
  1505. townList.deactivate();
  1506. heroList.deactivate();
  1507. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1508. infoBar.deactivate();
  1509. break;
  1510. }
  1511. worldViewScale = newScale;
  1512. redraw();
  1513. }
  1514. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1515. {
  1516. worldViewScale = newScale;
  1517. terrain.redraw();
  1518. minimap.redraw(); // to recalculate radar rect on minimap
  1519. }
  1520. }
  1521. CAdventureOptions::CAdventureOptions():
  1522. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1523. {
  1524. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1525. viewWorld = new CButton(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_x);
  1526. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1527. exit = new CButton(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1528. exit->assignedKeys.insert(SDLK_ESCAPE);
  1529. scenInfo = new CButton(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_i);
  1530. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1531. puzzle = new CButton(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_p);
  1532. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1533. dig = new CButton(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_d);
  1534. if(const CGHeroInstance *h = adventureInt->curHero())
  1535. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1536. else
  1537. dig->block(true);
  1538. }
  1539. void CAdventureOptions::showScenarioInfo()
  1540. {
  1541. auto campState = LOCPLINT->cb->getStartInfo()->campState;
  1542. if(campState)
  1543. {
  1544. GH.pushInt(new CBonusSelection(campState));
  1545. }
  1546. else
  1547. {
  1548. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1549. }
  1550. }