CAdvmapInterface.cpp 51 KB

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