CAdventureMapInterface.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /*
  2. * CAdvMapInt.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CAdventureMapInterface.h"
  12. #include "CAdvMapPanel.h"
  13. #include "CAdventureOptions.h"
  14. #include "CInGameConsole.h"
  15. #include "CMinimap.h"
  16. #include "CResDataBar.h"
  17. #include "CList.h"
  18. #include "CInfoBar.h"
  19. #include "MapAudioPlayer.h"
  20. #include "../mapView/mapHandler.h"
  21. #include "../mapView/MapView.h"
  22. #include "../windows/CKingdomInterface.h"
  23. #include "../windows/CSpellWindow.h"
  24. #include "../windows/CTradeWindow.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../CGameInfo.h"
  28. #include "../CMusicHandler.h"
  29. #include "../CPlayerInterface.h"
  30. #include "../lobby/CSavingScreen.h"
  31. #include "../render/CAnimation.h"
  32. #include "../gui/CursorHandler.h"
  33. #include "../render/IImage.h"
  34. #include "../gui/CGuiHandler.h"
  35. #include "../widgets/TextControls.h"
  36. #include "../widgets/Buttons.h"
  37. #include "../windows/settings/SettingsMainWindow.h"
  38. #include "../CMT.h"
  39. #include "../../CCallback.h"
  40. #include "../../lib/CConfigHandler.h"
  41. #include "../../lib/CGeneralTextHandler.h"
  42. #include "../../lib/spells/CSpellHandler.h"
  43. #include "../../lib/mapObjects/CGHeroInstance.h"
  44. #include "../../lib/mapObjects/CGTownInstance.h"
  45. #include "../../lib/CPathfinder.h"
  46. #include "../../lib/mapping/CMap.h"
  47. #include "../../lib/UnlockGuard.h"
  48. #include "../../lib/TerrainHandler.h"
  49. #include <SDL_keycode.h>
  50. #define ADVOPT (conf.go()->ac)
  51. std::shared_ptr<CAdventureMapInterface> adventureInt;
  52. void CAdventureMapInterface::setScrollingCursor(ui8 direction) const
  53. {
  54. if(direction & CAdventureMapInterface::RIGHT)
  55. {
  56. if(direction & CAdventureMapInterface::UP)
  57. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  58. else if(direction & CAdventureMapInterface::DOWN)
  59. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  60. else
  61. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  62. }
  63. else if(direction & CAdventureMapInterface::LEFT)
  64. {
  65. if(direction & CAdventureMapInterface::UP)
  66. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  67. else if(direction & CAdventureMapInterface::DOWN)
  68. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  69. else
  70. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  71. }
  72. else if(direction & CAdventureMapInterface::UP)
  73. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  74. else if(direction & CAdventureMapInterface::DOWN)
  75. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  76. }
  77. CAdventureMapInterface::CAdventureMapInterface():
  78. mode(EAdvMapMode::NORMAL),
  79. minimap(new CMinimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH))),
  80. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  81. heroList(new CHeroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD)),
  82. townList(new CTownList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD)),
  83. infoBar(new CInfoBar(Point(ADVOPT.infoboxX, ADVOPT.infoboxY))),
  84. resdatabar(new CResDataBar),
  85. mapAudio(new MapAudioPlayer()),
  86. terrain(new MapView(Point(ADVOPT.advmapX, ADVOPT.advmapY), Point(ADVOPT.advmapW, ADVOPT.advmapH))),
  87. state(EGameStates::NA),
  88. spellBeingCasted(nullptr),
  89. selection(nullptr),
  90. activeMapPanel(nullptr),
  91. duringAITurn(false),
  92. scrollingDir(0),
  93. scrollingState(false)
  94. {
  95. pos.x = pos.y = 0;
  96. pos.w = GH.screenDimensions().x;
  97. pos.h = GH.screenDimensions().y;
  98. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  99. townList->onSelect = std::bind(&CAdventureMapInterface::selectionChanged,this);
  100. bg = IImage::createFromFile(ADVOPT.mainGraphic);
  101. if(!ADVOPT.worldViewGraphic.empty())
  102. {
  103. bgWorldView = IImage::createFromFile(ADVOPT.worldViewGraphic);
  104. }
  105. else
  106. {
  107. bgWorldView = nullptr;
  108. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  109. }
  110. if (!bgWorldView)
  111. {
  112. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  113. bgWorldView = IImage::createFromFile("VWorld.bmp");
  114. }
  115. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  116. worldViewIcons->preload();
  117. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  118. {
  119. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  120. }
  121. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  122. {
  123. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  124. for(auto image : info.additionalDefs)
  125. button->addImage(image);
  126. return button;
  127. };
  128. kingOverview = makeButton(293, std::bind(&CAdventureMapInterface::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  129. underground = makeButton(294, std::bind(&CAdventureMapInterface::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  130. questlog = makeButton(295, std::bind(&CAdventureMapInterface::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  131. sleepWake = makeButton(296, std::bind(&CAdventureMapInterface::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  132. moveHero = makeButton(297, std::bind(&CAdventureMapInterface::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  133. spellbook = makeButton(298, std::bind(&CAdventureMapInterface::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  134. advOptions = makeButton(299, std::bind(&CAdventureMapInterface::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  135. sysOptions = makeButton(300, std::bind(&CAdventureMapInterface::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  136. nextHero = makeButton(301, std::bind(&CAdventureMapInterface::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  137. endTurn = makeButton(302, std::bind(&CAdventureMapInterface::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  138. int panelSpaceBottom = GH.screenDimensions().y - resdatabar->pos.h - 4;
  139. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  140. // TODO correct drawing position
  141. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList->pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  142. panelMain->addChildColorableButton(kingOverview);
  143. panelMain->addChildColorableButton(underground);
  144. panelMain->addChildColorableButton(questlog);
  145. panelMain->addChildColorableButton(sleepWake);
  146. panelMain->addChildColorableButton(moveHero);
  147. panelMain->addChildColorableButton(spellbook);
  148. panelMain->addChildColorableButton(advOptions);
  149. panelMain->addChildColorableButton(sysOptions);
  150. panelMain->addChildColorableButton(nextHero);
  151. panelMain->addChildColorableButton(endTurn);
  152. // TODO move configs to resolutions.json, similarly to previous buttons
  153. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  154. worldViewBackConfig.defName = "IOK6432.DEF";
  155. worldViewBackConfig.x = GH.screenDimensions().x - 73;
  156. worldViewBackConfig.y = 343 + 195;
  157. worldViewBackConfig.playerColoured = false;
  158. panelWorldView->addChildToPanel(
  159. makeButton(288, std::bind(&CAdventureMapInterface::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  160. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  161. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  162. worldViewPuzzleConfig.x = GH.screenDimensions().x - 188;
  163. worldViewPuzzleConfig.y = 343 + 195;
  164. worldViewPuzzleConfig.playerColoured = false;
  165. panelWorldView->addChildToPanel( // no help text for this one
  166. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  167. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  168. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  169. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  170. worldViewScale1xConfig.x = GH.screenDimensions().x - 191;
  171. worldViewScale1xConfig.y = 23 + 195;
  172. worldViewScale1xConfig.playerColoured = false;
  173. panelWorldView->addChildToPanel( // help text is wrong for this button
  174. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  175. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  176. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  177. worldViewScale2xConfig.x = GH.screenDimensions().x- 191 + 63;
  178. worldViewScale2xConfig.y = 23 + 195;
  179. worldViewScale2xConfig.playerColoured = false;
  180. panelWorldView->addChildToPanel( // help text is wrong for this button
  181. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  182. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  183. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  184. worldViewScale4xConfig.x = GH.screenDimensions().x- 191 + 126;
  185. worldViewScale4xConfig.y = 23 + 195;
  186. worldViewScale4xConfig.playerColoured = false;
  187. panelWorldView->addChildToPanel( // help text is wrong for this button
  188. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  189. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  190. worldViewUndergroundConfig.defName = "IAM010.DEF";
  191. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  192. worldViewUndergroundConfig.x = GH.screenDimensions().x - 115;
  193. worldViewUndergroundConfig.y = 343 + 195;
  194. worldViewUndergroundConfig.playerColoured = true;
  195. worldViewUnderground = makeButton(294, std::bind(&CAdventureMapInterface::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  196. panelWorldView->addChildColorableButton(worldViewUnderground);
  197. onCurrentPlayerChanged(LOCPLINT->playerID);
  198. int iconColorMultiplier = player.getNum() * 19;
  199. int wvLeft = heroList->pos.x - 2; // TODO correct drawing position
  200. //int wvTop = 195;
  201. for (int i = 0; i < 5; ++i)
  202. {
  203. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  204. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  205. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  206. }
  207. for (int i = 0; i < 7; ++i)
  208. {
  209. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  210. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  211. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  212. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  213. }
  214. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  215. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  216. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  217. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  218. activeMapPanel = panelMain;
  219. exitWorldView();
  220. underground->block(!CGI->mh->getMap()->twoLevel);
  221. questlog->block(!CGI->mh->getMap()->quests.size());
  222. worldViewUnderground->block(!CGI->mh->getMap()->twoLevel);
  223. addUsedEvents(MOVE);
  224. }
  225. void CAdventureMapInterface::fshowOverview()
  226. {
  227. GH.pushIntT<CKingdomInterface>();
  228. }
  229. void CAdventureMapInterface::fworldViewBack()
  230. {
  231. exitWorldView();
  232. auto hero = getCurrentHero();
  233. if (hero)
  234. centerOnObject(hero);
  235. }
  236. void CAdventureMapInterface::fworldViewScale1x()
  237. {
  238. // TODO set corresponding scale button to "selected" mode
  239. openWorldView(7);
  240. }
  241. void CAdventureMapInterface::fworldViewScale2x()
  242. {
  243. openWorldView(11);
  244. }
  245. void CAdventureMapInterface::fworldViewScale4x()
  246. {
  247. openWorldView(16);
  248. }
  249. void CAdventureMapInterface::fswitchLevel()
  250. {
  251. // with support for future multi-level maps :)
  252. int maxLevels = CGI->mh->getMap()->levels();
  253. if (maxLevels < 2)
  254. return;
  255. terrain->onMapLevelSwitched();
  256. }
  257. void CAdventureMapInterface::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  258. {
  259. underground->setIndex(mapLevel, true);
  260. underground->redraw();
  261. worldViewUnderground->setIndex(mapLevel, true);
  262. worldViewUnderground->redraw();
  263. minimap->onMapViewMoved(visibleArea, mapLevel);
  264. }
  265. void CAdventureMapInterface::onAudioResumed()
  266. {
  267. mapAudio->onAudioResumed();
  268. }
  269. void CAdventureMapInterface::onAudioPaused()
  270. {
  271. mapAudio->onAudioPaused();
  272. }
  273. void CAdventureMapInterface::fshowQuestlog()
  274. {
  275. LOCPLINT->showQuestLog();
  276. }
  277. void CAdventureMapInterface::fsleepWake()
  278. {
  279. const CGHeroInstance *h = getCurrentHero();
  280. if (!h)
  281. return;
  282. bool newSleep = !isHeroSleeping(h);
  283. setHeroSleeping(h, newSleep);
  284. updateSleepWake(h);
  285. if (newSleep)
  286. {
  287. fnextHero();
  288. //moveHero.block(true);
  289. //uncomment to enable original HoMM3 behaviour:
  290. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  291. }
  292. // redraw to update the image of sleep/wake button
  293. panelMain->redraw();
  294. }
  295. void CAdventureMapInterface::fmoveHero()
  296. {
  297. const CGHeroInstance *h = getCurrentHero();
  298. if (!h || !LOCPLINT->paths.hasPath(h) || CGI->mh->hasOngoingAnimations())
  299. return;
  300. LOCPLINT->moveHero(h, LOCPLINT->paths.getPath(h));
  301. }
  302. void CAdventureMapInterface::fshowSpellbok()
  303. {
  304. if (!getCurrentHero()) //checking necessary values
  305. return;
  306. centerOnObject(selection);
  307. GH.pushIntT<CSpellWindow>(getCurrentHero(), LOCPLINT, false);
  308. }
  309. void CAdventureMapInterface::fadventureOPtions()
  310. {
  311. GH.pushIntT<CAdventureOptions>();
  312. }
  313. void CAdventureMapInterface::fsystemOptions()
  314. {
  315. GH.pushIntT<SettingsMainWindow>();
  316. }
  317. void CAdventureMapInterface::fnextHero()
  318. {
  319. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  320. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  321. if (next < 0)
  322. return;
  323. setSelection(LOCPLINT->wanderingHeroes[next], true);
  324. }
  325. void CAdventureMapInterface::fendTurn()
  326. {
  327. if(!LOCPLINT->makingTurn)
  328. return;
  329. if(settings["adventure"]["heroReminder"].Bool())
  330. {
  331. for(auto hero : LOCPLINT->wanderingHeroes)
  332. {
  333. if(!isHeroSleeping(hero) && hero->movement > 0)
  334. {
  335. // Only show hero reminder if conditions met:
  336. // - There still movement points
  337. // - Hero don't have a path or there not points for first step on path
  338. LOCPLINT->paths.verifyPath(hero);
  339. if(!LOCPLINT->paths.hasPath(hero))
  340. {
  341. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  342. return;
  343. }
  344. auto path = LOCPLINT->paths.getPath(hero);
  345. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  346. {
  347. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  348. return;
  349. }
  350. }
  351. }
  352. }
  353. endingTurn();
  354. }
  355. void CAdventureMapInterface::updateSleepWake(const CGHeroInstance *h)
  356. {
  357. sleepWake->block(!h);
  358. if (!h)
  359. return;
  360. bool state = isHeroSleeping(h);
  361. sleepWake->setIndex(state ? 1 : 0, true);
  362. sleepWake->assignedKeys.clear();
  363. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  364. }
  365. void CAdventureMapInterface::updateSpellbook(const CGHeroInstance *h)
  366. {
  367. spellbook->block(!h);
  368. }
  369. int CAdventureMapInterface::getNextHeroIndex(int startIndex)
  370. {
  371. if (LOCPLINT->wanderingHeroes.size() == 0)
  372. return -1;
  373. if (startIndex < 0)
  374. startIndex = 0;
  375. int i = startIndex;
  376. do
  377. {
  378. i++;
  379. if (i >= LOCPLINT->wanderingHeroes.size())
  380. i = 0;
  381. }
  382. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  383. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  384. return i;
  385. else
  386. return -1;
  387. }
  388. void CAdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
  389. {
  390. heroList->update(h);
  391. if (h == getCurrentHero())
  392. adventureInt->infoBar->showSelection();
  393. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  394. int next = getNextHeroIndex(start);
  395. if (next < 0)
  396. {
  397. nextHero->block(true);
  398. return;
  399. }
  400. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  401. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  402. nextHero->block(noActiveHeroes);
  403. if(!h)
  404. {
  405. moveHero->block(true);
  406. return;
  407. }
  408. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  409. bool hasPath = LOCPLINT->paths.hasPath(h);
  410. moveHero->block(!(bool)hasPath || (h->movement == 0));
  411. }
  412. void CAdventureMapInterface::onTownChanged(const CGTownInstance * town)
  413. {
  414. townList->update(town);
  415. adventureInt->infoBar->showSelection();
  416. }
  417. void CAdventureMapInterface::showInfoBoxMessage(const std::vector<Component> & components, std::string message, int timer)
  418. {
  419. infoBar->pushComponents(components, message, timer);
  420. }
  421. void CAdventureMapInterface::activate()
  422. {
  423. CIntObject::activate();
  424. if (!(active & KEYBOARD))
  425. CIntObject::activate(KEYBOARD);
  426. screenBuf = screen;
  427. GH.statusbar = statusbar;
  428. if(LOCPLINT)
  429. {
  430. LOCPLINT->cingconsole->activate();
  431. LOCPLINT->cingconsole->pos = this->pos;
  432. }
  433. if(!duringAITurn)
  434. {
  435. activeMapPanel->activate();
  436. if (mode == EAdvMapMode::NORMAL)
  437. {
  438. heroList->activate();
  439. townList->activate();
  440. infoBar->activate();
  441. }
  442. minimap->activate();
  443. terrain->activate();
  444. statusbar->activate();
  445. GH.fakeMouseMove(); //to restore the cursor
  446. }
  447. }
  448. void CAdventureMapInterface::deactivate()
  449. {
  450. CIntObject::deactivate();
  451. if(!duringAITurn)
  452. {
  453. scrollingDir = 0;
  454. CCS->curh->set(Cursor::Map::POINTER);
  455. activeMapPanel->deactivate();
  456. if (mode == EAdvMapMode::NORMAL)
  457. {
  458. heroList->deactivate();
  459. townList->deactivate();
  460. infoBar->deactivate();
  461. }
  462. minimap->deactivate();
  463. terrain->deactivate();
  464. statusbar->deactivate();
  465. }
  466. }
  467. void CAdventureMapInterface::showAll(SDL_Surface * to)
  468. {
  469. bg->draw(to, 0, 0);
  470. if(state != EGameStates::INGAME)
  471. return;
  472. switch (mode)
  473. {
  474. case EAdvMapMode::NORMAL:
  475. heroList->showAll(to);
  476. townList->showAll(to);
  477. infoBar->showAll(to);
  478. break;
  479. case EAdvMapMode::WORLD_VIEW:
  480. break;
  481. }
  482. activeMapPanel->showAll(to);
  483. minimap->showAll(to);
  484. terrain->showAll(to);
  485. show(to);
  486. resdatabar->showAll(to);
  487. statusbar->show(to);
  488. LOCPLINT->cingconsole->show(to);
  489. }
  490. bool CAdventureMapInterface::isHeroSleeping(const CGHeroInstance *hero)
  491. {
  492. if (!hero)
  493. return false;
  494. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  495. }
  496. void CAdventureMapInterface::onHeroWokeUp(const CGHeroInstance * hero)
  497. {
  498. if (!isHeroSleeping(hero))
  499. return;
  500. sleepWake->clickLeft(true, false);
  501. sleepWake->clickLeft(false, true);
  502. //could've just called
  503. //adventureInt->fsleepWake();
  504. //but no authentic button click/sound ;-)
  505. }
  506. void CAdventureMapInterface::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  507. {
  508. if (sleep)
  509. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  510. else
  511. LOCPLINT->sleepingHeroes -= hero;
  512. onHeroChanged(nullptr);
  513. }
  514. void CAdventureMapInterface::show(SDL_Surface * to)
  515. {
  516. if(state != EGameStates::INGAME)
  517. return;
  518. handleMapScrollingUpdate();
  519. for(int i = 0; i < 4; i++)
  520. {
  521. if(settings["session"]["spectate"].Bool())
  522. gems[i]->setFrame(PlayerColor(1).getNum());
  523. else
  524. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  525. }
  526. minimap->show(to);
  527. terrain->show(to);
  528. for(int i = 0; i < 4; i++)
  529. gems[i]->showAll(to);
  530. LOCPLINT->cingconsole->show(to);
  531. infoBar->show(to);
  532. statusbar->showAll(to);
  533. }
  534. void CAdventureMapInterface::handleMapScrollingUpdate()
  535. {
  536. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  537. double scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  538. int32_t scrollDistance = static_cast<int32_t>(scrollSpeedPixels * timePassed / 1000);
  539. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  540. if(scrollingDir & LEFT)
  541. terrain->onMapScrolled(Point(-scrollDistance, 0));
  542. if(scrollingDir & RIGHT)
  543. terrain->onMapScrolled(Point(+scrollDistance, 0));
  544. if(scrollingDir & UP)
  545. terrain->onMapScrolled(Point(0, -scrollDistance));
  546. if(scrollingDir & DOWN)
  547. terrain->onMapScrolled(Point(0, +scrollDistance));
  548. if(scrollingDir)
  549. {
  550. setScrollingCursor(scrollingDir);
  551. scrollingState = true;
  552. }
  553. else if(scrollingState)
  554. {
  555. CCS->curh->set(Cursor::Map::POINTER);
  556. scrollingState = false;
  557. }
  558. }
  559. void CAdventureMapInterface::selectionChanged()
  560. {
  561. const CGTownInstance *to = LOCPLINT->towns[townList->getSelectedIndex()];
  562. if (selection != to)
  563. setSelection(to);
  564. }
  565. void CAdventureMapInterface::centerOnTile(int3 on)
  566. {
  567. terrain->onCenteredTile(on);
  568. }
  569. void CAdventureMapInterface::centerOnObject(const CGObjectInstance * obj)
  570. {
  571. terrain->onCenteredObject(obj);
  572. }
  573. void CAdventureMapInterface::keyReleased(const SDL_Keycode &key)
  574. {
  575. if (mode != EAdvMapMode::NORMAL)
  576. return;
  577. switch (key)
  578. {
  579. case SDLK_s:
  580. if(isActive())
  581. GH.pushIntT<CSavingScreen>();
  582. return;
  583. default:
  584. {
  585. auto direction = keyToMoveDirection(key);
  586. if (!direction)
  587. return;
  588. ui8 Dir = (direction->x<0 ? LEFT : 0) |
  589. (direction->x>0 ? RIGHT : 0) |
  590. (direction->y<0 ? UP : 0) |
  591. (direction->y>0 ? DOWN : 0) ;
  592. scrollingDir &= ~Dir;
  593. }
  594. }
  595. }
  596. void CAdventureMapInterface::keyPressed(const SDL_Keycode & key)
  597. {
  598. if (mode != EAdvMapMode::NORMAL)
  599. return;
  600. const CGHeroInstance *h = getCurrentHero(); //selected hero
  601. const CGTownInstance *t = getCurrentTown(); //selected town
  602. switch(key)
  603. {
  604. case SDLK_g:
  605. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  606. return;
  607. {
  608. //find first town with tavern
  609. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  610. {
  611. return town->hasBuilt(BuildingID::TAVERN);
  612. });
  613. if(itr != LOCPLINT->towns.end())
  614. LOCPLINT->showThievesGuildWindow(*itr);
  615. else
  616. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  617. }
  618. return;
  619. case SDLK_i:
  620. if(isActive())
  621. CAdventureOptions::showScenarioInfo();
  622. return;
  623. case SDLK_l:
  624. if(isActive())
  625. LOCPLINT->proposeLoadingGame();
  626. return;
  627. case SDLK_d:
  628. {
  629. if(h && isActive() && LOCPLINT->makingTurn)
  630. LOCPLINT->tryDiggging(h);
  631. return;
  632. }
  633. case SDLK_p:
  634. if(isActive())
  635. LOCPLINT->showPuzzleMap();
  636. return;
  637. case SDLK_v:
  638. if(isActive())
  639. LOCPLINT->viewWorldMap();
  640. return;
  641. case SDLK_r:
  642. if(isActive() && GH.isKeyboardCtrlDown())
  643. {
  644. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  645. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  646. }
  647. return;
  648. case SDLK_SPACE: //space - try to revisit current object with selected hero
  649. {
  650. if(!isActive())
  651. return;
  652. if(h)
  653. {
  654. LOCPLINT->cb->moveHero(h,h->pos);
  655. }
  656. }
  657. return;
  658. case SDLK_RETURN:
  659. {
  660. if(!isActive() || !selection)
  661. return;
  662. if(h)
  663. LOCPLINT->openHeroWindow(h);
  664. else if(t)
  665. LOCPLINT->openTownWindow(t);
  666. return;
  667. }
  668. case SDLK_ESCAPE:
  669. {
  670. //FIXME: this case is never executed since AdvMapInt is disabled while in spellcasting mode
  671. if(!isActive() || GH.topInt().get() != this || !spellBeingCasted)
  672. return;
  673. leaveCastingMode();
  674. return;
  675. }
  676. case SDLK_t:
  677. {
  678. //act on key down if marketplace windows is not already opened
  679. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  680. return;
  681. if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace
  682. {
  683. //check if we have any marketplace
  684. const CGTownInstance *townWithMarket = nullptr;
  685. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  686. {
  687. if(t->hasBuilt(BuildingID::MARKETPLACE))
  688. {
  689. townWithMarket = t;
  690. break;
  691. }
  692. }
  693. if(townWithMarket) //if any town has marketplace, open window
  694. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  695. else //if not - complain
  696. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  697. }
  698. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  699. {
  700. townList->selectNext();
  701. }
  702. return;
  703. }
  704. case SDLK_LALT:
  705. case SDLK_RALT:
  706. {
  707. //fake mouse use to trigger onTileHovered()
  708. GH.fakeMouseMove();
  709. return;
  710. }
  711. default:
  712. {
  713. auto direction = keyToMoveDirection(key);
  714. if (!direction)
  715. return;
  716. ui8 Dir = (direction->x<0 ? LEFT : 0) |
  717. (direction->x>0 ? RIGHT : 0) |
  718. (direction->y<0 ? UP : 0) |
  719. (direction->y>0 ? DOWN : 0) ;
  720. //ctrl makes arrow move screen, not hero
  721. if(GH.isKeyboardCtrlDown())
  722. {
  723. scrollingDir |= Dir;
  724. return;
  725. }
  726. if(!h || !isActive())
  727. return;
  728. if (CGI->mh->hasOngoingAnimations())
  729. return;
  730. if(*direction == Point(0,0))
  731. {
  732. centerOnObject(h);
  733. return;
  734. }
  735. int3 dst = h->visitablePos() + int3(direction->x, direction->y, 0);
  736. if (!CGI->mh->isInMap((dst)))
  737. return;
  738. if ( !LOCPLINT->paths.setPath(h, dst))
  739. return;
  740. const CGPath & path = LOCPLINT->paths.getPath(h);
  741. if (path.nodes.size() > 2)
  742. onHeroChanged(h);
  743. else
  744. if(!path.nodes[0].turns)
  745. LOCPLINT->moveHero(h, path);
  746. }
  747. return;
  748. }
  749. }
  750. std::optional<Point> CAdventureMapInterface::keyToMoveDirection(const SDL_Keycode & key)
  751. {
  752. switch (key) {
  753. case SDLK_DOWN: return Point( 0, +1);
  754. case SDLK_LEFT: return Point(-1, 0);
  755. case SDLK_RIGHT: return Point(+1, 0);
  756. case SDLK_UP: return Point( 0, -1);
  757. case SDLK_KP_1: return Point(-1, +1);
  758. case SDLK_KP_2: return Point( 0, +1);
  759. case SDLK_KP_3: return Point(+1, +1);
  760. case SDLK_KP_4: return Point(-1, 0);
  761. case SDLK_KP_5: return Point( 0, 0);
  762. case SDLK_KP_6: return Point(+1, 0);
  763. case SDLK_KP_7: return Point(-1, -1);
  764. case SDLK_KP_8: return Point( 0, -1);
  765. case SDLK_KP_9: return Point(+1, -1);
  766. }
  767. return std::nullopt;
  768. }
  769. void CAdventureMapInterface::setSelection(const CArmedInstance *sel, bool centerView)
  770. {
  771. assert(sel);
  772. if(selection != sel)
  773. infoBar->popAll();
  774. selection = sel;
  775. mapAudio->onSelectionChanged(sel);
  776. if(centerView)
  777. centerOnObject(sel);
  778. if(sel->ID==Obj::TOWN)
  779. {
  780. auto town = dynamic_cast<const CGTownInstance*>(sel);
  781. infoBar->showTownSelection(town);
  782. townList->select(town);
  783. heroList->select(nullptr);
  784. updateSleepWake(nullptr);
  785. onHeroChanged(nullptr);
  786. updateSpellbook(nullptr);
  787. }
  788. else //hero selected
  789. {
  790. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  791. infoBar->showHeroSelection(hero);
  792. heroList->select(hero);
  793. townList->select(nullptr);
  794. LOCPLINT->paths.verifyPath(hero);
  795. updateSleepWake(hero);
  796. onHeroChanged(hero);
  797. updateSpellbook(hero);
  798. }
  799. townList->redraw();
  800. heroList->redraw();
  801. }
  802. void CAdventureMapInterface::mouseMoved( const Point & cursorPosition )
  803. {
  804. // adventure map scrolling with mouse
  805. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  806. if(!GH.isKeyboardCtrlDown() && isActive() && mode == EAdvMapMode::NORMAL)
  807. {
  808. if(cursorPosition.x<15)
  809. {
  810. scrollingDir |= LEFT;
  811. }
  812. else
  813. {
  814. scrollingDir &= ~LEFT;
  815. }
  816. if(cursorPosition.x > GH.screenDimensions().x - 15)
  817. {
  818. scrollingDir |= RIGHT;
  819. }
  820. else
  821. {
  822. scrollingDir &= ~RIGHT;
  823. }
  824. if(cursorPosition.y<15)
  825. {
  826. scrollingDir |= UP;
  827. }
  828. else
  829. {
  830. scrollingDir &= ~UP;
  831. }
  832. if(cursorPosition.y > GH.screenDimensions().y - 15)
  833. {
  834. scrollingDir |= DOWN;
  835. }
  836. else
  837. {
  838. scrollingDir &= ~DOWN;
  839. }
  840. }
  841. }
  842. bool CAdventureMapInterface::isActive()
  843. {
  844. return active & ~CIntObject::KEYBOARD;
  845. }
  846. void CAdventureMapInterface::startHotSeatWait(PlayerColor Player)
  847. {
  848. state = EGameStates::WAITING;
  849. }
  850. void CAdventureMapInterface::onMapTilesChanged(boost::optional<std::unordered_set<int3>> positions)
  851. {
  852. if (positions)
  853. minimap->updateTiles(*positions);
  854. else
  855. minimap->update();
  856. }
  857. void CAdventureMapInterface::onCurrentPlayerChanged(PlayerColor Player)
  858. {
  859. selection = nullptr;
  860. if (Player == player)
  861. return;
  862. player = Player;
  863. bg->playerColored(player);
  864. panelMain->setPlayerColor(player);
  865. panelWorldView->setPlayerColor(player);
  866. panelWorldView->recolorIcons(player, player.getNum() * 19);
  867. resdatabar->colorize(player);
  868. }
  869. void CAdventureMapInterface::startTurn()
  870. {
  871. state = EGameStates::INGAME;
  872. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  873. || settings["session"]["spectate"].Bool())
  874. {
  875. adjustActiveness(false);
  876. minimap->setAIRadar(false);
  877. infoBar->showSelection();
  878. }
  879. }
  880. void CAdventureMapInterface::initializeNewTurn()
  881. {
  882. heroList->update();
  883. townList->update();
  884. const CGHeroInstance * heroToSelect = nullptr;
  885. // find first non-sleeping hero
  886. for (auto hero : LOCPLINT->wanderingHeroes)
  887. {
  888. if (boost::range::find(LOCPLINT->sleepingHeroes, hero) == LOCPLINT->sleepingHeroes.end())
  889. {
  890. heroToSelect = hero;
  891. break;
  892. }
  893. }
  894. bool centerView = !settings["session"]["autoSkip"].Bool();
  895. //select first hero if available.
  896. if (heroToSelect != nullptr)
  897. {
  898. setSelection(heroToSelect, centerView);
  899. }
  900. else if (LOCPLINT->towns.size())
  901. setSelection(LOCPLINT->towns.front(), centerView);
  902. else
  903. setSelection(LOCPLINT->wanderingHeroes.front());
  904. //show new day animation and sound on infobar
  905. infoBar->showDate();
  906. onHeroChanged(nullptr);
  907. showAll(screen);
  908. mapAudio->onPlayerTurnStarted();
  909. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  910. {
  911. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  912. iw->close();
  913. endingTurn();
  914. }
  915. }
  916. void CAdventureMapInterface::endingTurn()
  917. {
  918. if(settings["session"]["spectate"].Bool())
  919. return;
  920. LOCPLINT->makingTurn = false;
  921. LOCPLINT->cb->endTurn();
  922. mapAudio->onPlayerTurnEnded();
  923. }
  924. const CGObjectInstance* CAdventureMapInterface::getActiveObject(const int3 &mapPos)
  925. {
  926. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  927. if (bobjs.empty())
  928. return nullptr;
  929. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  930. /*
  931. if (bobjs.back()->ID == Obj::HERO)
  932. return bobjs.back();
  933. else
  934. return bobjs.front();*/
  935. }
  936. void CAdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  937. {
  938. if(mode != EAdvMapMode::NORMAL)
  939. return;
  940. //FIXME: this line breaks H3 behavior for Dimension Door
  941. if(!LOCPLINT->cb->isVisible(mapPos))
  942. return;
  943. if(!LOCPLINT->makingTurn)
  944. return;
  945. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  946. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  947. int3 selPos = selection->getSightCenter();
  948. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  949. {
  950. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  951. switch(spellBeingCasted->id)
  952. {
  953. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  954. if(topBlocking && topBlocking->ID == Obj::BOAT)
  955. leaveCastingMode(true, mapPos);
  956. break;
  957. case SpellID::DIMENSION_DOOR:
  958. if(!tile || tile->isClear(heroTile))
  959. leaveCastingMode(true, mapPos);
  960. break;
  961. }
  962. return;
  963. }
  964. //check if we can select this object
  965. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  966. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  967. bool isHero = false;
  968. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  969. {
  970. if(selection == topBlocking) //selected town clicked
  971. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  972. else if(canSelect)
  973. setSelection(static_cast<const CArmedInstance*>(topBlocking), false);
  974. }
  975. else if(const CGHeroInstance * currentHero = getCurrentHero()) //hero is selected
  976. {
  977. isHero = true;
  978. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  979. if(currentHero == topBlocking) //clicked selected hero
  980. {
  981. LOCPLINT->openHeroWindow(currentHero);
  982. return;
  983. }
  984. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  985. {
  986. setSelection(static_cast<const CArmedInstance*>(topBlocking), false);
  987. return;
  988. }
  989. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  990. {
  991. if(LOCPLINT->paths.hasPath(currentHero) &&
  992. LOCPLINT->paths.getPath(currentHero).endPos() == mapPos)//we'll be moving
  993. {
  994. if(!CGI->mh->hasOngoingAnimations())
  995. LOCPLINT->moveHero(currentHero, LOCPLINT->paths.getPath(currentHero));
  996. return;
  997. }
  998. else //remove old path and find a new one if we clicked on accessible tile
  999. {
  1000. LOCPLINT->paths.setPath(currentHero, mapPos);
  1001. onHeroChanged(currentHero);
  1002. }
  1003. }
  1004. } //end of hero is selected "case"
  1005. else
  1006. {
  1007. throw std::runtime_error("Nothing is selected...");
  1008. }
  1009. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  1010. if(isHero && shipyard != nullptr)
  1011. {
  1012. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1013. }
  1014. }
  1015. void CAdventureMapInterface::onTileHovered(const int3 &mapPos)
  1016. {
  1017. if(mode != EAdvMapMode::NORMAL //disable in world view
  1018. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1019. return;
  1020. if(!LOCPLINT->cb->isVisible(mapPos))
  1021. {
  1022. CCS->curh->set(Cursor::Map::POINTER);
  1023. statusbar->clear();
  1024. return;
  1025. }
  1026. auto objRelations = PlayerRelations::ALLIES;
  1027. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1028. if(objAtTile)
  1029. {
  1030. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1031. std::string text = getCurrentHero() ? objAtTile->getHoverText(getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1032. boost::replace_all(text,"\n"," ");
  1033. statusbar->write(text);
  1034. }
  1035. else
  1036. {
  1037. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  1038. statusbar->write(hlp);
  1039. }
  1040. if(spellBeingCasted)
  1041. {
  1042. switch(spellBeingCasted->id)
  1043. {
  1044. case SpellID::SCUTTLE_BOAT:
  1045. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1046. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  1047. else
  1048. CCS->curh->set(Cursor::Map::POINTER);
  1049. return;
  1050. case SpellID::DIMENSION_DOOR:
  1051. {
  1052. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1053. int3 hpos = selection->getSightCenter();
  1054. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1055. CCS->curh->set(Cursor::Map::TELEPORT);
  1056. else
  1057. CCS->curh->set(Cursor::Map::POINTER);
  1058. return;
  1059. }
  1060. }
  1061. }
  1062. if(selection->ID == Obj::TOWN)
  1063. {
  1064. if(objAtTile)
  1065. {
  1066. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1067. CCS->curh->set(Cursor::Map::TOWN);
  1068. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1069. CCS->curh->set(Cursor::Map::HERO);
  1070. else
  1071. CCS->curh->set(Cursor::Map::POINTER);
  1072. }
  1073. else
  1074. CCS->curh->set(Cursor::Map::POINTER);
  1075. }
  1076. else if(const CGHeroInstance * hero = getCurrentHero())
  1077. {
  1078. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  1079. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  1080. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  1081. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  1082. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  1083. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  1084. std::array<Cursor::Map, 4> cursorSailVisit = { Cursor::Map::T1_SAIL_VISIT, Cursor::Map::T2_SAIL_VISIT, Cursor::Map::T3_SAIL_VISIT, Cursor::Map::T4_SAIL_VISIT, };
  1085. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  1086. assert(pathNode);
  1087. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  1088. {
  1089. showMoveDetailsInStatusbar(*hero, *pathNode);
  1090. }
  1091. int turns = pathNode->turns;
  1092. vstd::amin(turns, 3);
  1093. switch(pathNode->action)
  1094. {
  1095. case CGPathNode::NORMAL:
  1096. case CGPathNode::TELEPORT_NORMAL:
  1097. if(pathNode->layer == EPathfindingLayer::LAND)
  1098. CCS->curh->set(cursorMove[turns]);
  1099. else
  1100. CCS->curh->set(cursorSailVisit[turns]);
  1101. break;
  1102. case CGPathNode::VISIT:
  1103. case CGPathNode::BLOCKING_VISIT:
  1104. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1105. if(objAtTile && objAtTile->ID == Obj::HERO)
  1106. {
  1107. if(selection == objAtTile)
  1108. CCS->curh->set(Cursor::Map::HERO);
  1109. else
  1110. CCS->curh->set(cursorExchange[turns]);
  1111. }
  1112. else if(pathNode->layer == EPathfindingLayer::LAND)
  1113. CCS->curh->set(cursorVisit[turns]);
  1114. else
  1115. CCS->curh->set(cursorSailVisit[turns]);
  1116. break;
  1117. case CGPathNode::BATTLE:
  1118. case CGPathNode::TELEPORT_BATTLE:
  1119. CCS->curh->set(cursorAttack[turns]);
  1120. break;
  1121. case CGPathNode::EMBARK:
  1122. CCS->curh->set(cursorSail[turns]);
  1123. break;
  1124. case CGPathNode::DISEMBARK:
  1125. CCS->curh->set(cursorDisembark[turns]);
  1126. break;
  1127. default:
  1128. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1129. {
  1130. if(objAtTile->ID == Obj::TOWN)
  1131. CCS->curh->set(Cursor::Map::TOWN);
  1132. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1133. CCS->curh->set(Cursor::Map::HERO);
  1134. else
  1135. CCS->curh->set(Cursor::Map::POINTER);
  1136. }
  1137. else
  1138. CCS->curh->set(Cursor::Map::POINTER);
  1139. break;
  1140. }
  1141. }
  1142. if(ourInaccessibleShipyard(objAtTile))
  1143. {
  1144. CCS->curh->set(Cursor::Map::T1_SAIL);
  1145. }
  1146. }
  1147. void CAdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  1148. {
  1149. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  1150. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  1151. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  1152. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  1153. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  1154. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  1155. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  1156. statusbar->write(result);
  1157. }
  1158. void CAdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  1159. {
  1160. if(mode != EAdvMapMode::NORMAL)
  1161. return;
  1162. if(spellBeingCasted)
  1163. {
  1164. leaveCastingMode();
  1165. return;
  1166. }
  1167. if(!LOCPLINT->cb->isVisible(mapPos))
  1168. {
  1169. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1170. return;
  1171. }
  1172. const CGObjectInstance * obj = getActiveObject(mapPos);
  1173. if(!obj)
  1174. {
  1175. // Bare or undiscovered terrain
  1176. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1177. if(tile)
  1178. {
  1179. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  1180. CRClickPopup::createAndPush(hlp);
  1181. }
  1182. return;
  1183. }
  1184. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  1185. }
  1186. void CAdventureMapInterface::enterCastingMode(const CSpell * sp)
  1187. {
  1188. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1189. spellBeingCasted = sp;
  1190. deactivate();
  1191. terrain->activate();
  1192. GH.fakeMouseMove();
  1193. }
  1194. void CAdventureMapInterface::leaveCastingMode(bool cast, int3 dest)
  1195. {
  1196. assert(spellBeingCasted);
  1197. SpellID id = spellBeingCasted->id;
  1198. spellBeingCasted = nullptr;
  1199. terrain->deactivate();
  1200. activate();
  1201. if(cast)
  1202. LOCPLINT->cb->castSpell(getCurrentHero(), id, dest);
  1203. else
  1204. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1205. }
  1206. const CGHeroInstance * CAdventureMapInterface::getCurrentHero() const
  1207. {
  1208. if(selection && selection->ID == Obj::HERO)
  1209. return dynamic_cast<const CGHeroInstance *>(selection);
  1210. else
  1211. return nullptr;
  1212. }
  1213. const CGTownInstance * CAdventureMapInterface::getCurrentTown() const
  1214. {
  1215. if(selection && selection->ID == Obj::TOWN)
  1216. return dynamic_cast<const CGTownInstance *>(selection);
  1217. else
  1218. return nullptr;
  1219. }
  1220. const CArmedInstance * CAdventureMapInterface::getCurrentArmy() const
  1221. {
  1222. if (selection)
  1223. return dynamic_cast<const CArmedInstance *>(selection);
  1224. else
  1225. return nullptr;
  1226. }
  1227. Rect CAdventureMapInterface::terrainAreaPixels() const
  1228. {
  1229. return terrain->pos;
  1230. }
  1231. const IShipyard * CAdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1232. {
  1233. const IShipyard *ret = IShipyard::castFrom(obj);
  1234. if(!ret ||
  1235. obj->tempOwner != player ||
  1236. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1237. return nullptr;
  1238. return ret;
  1239. }
  1240. void CAdventureMapInterface::aiTurnStarted()
  1241. {
  1242. if(settings["session"]["spectate"].Bool())
  1243. return;
  1244. adjustActiveness(true);
  1245. mapAudio->onEnemyTurnStarted();
  1246. adventureInt->minimap->setAIRadar(true);
  1247. adventureInt->infoBar->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1248. adventureInt->minimap->showAll(screen);//force refresh on inactive object
  1249. adventureInt->infoBar->showAll(screen);//force refresh on inactive object
  1250. }
  1251. void CAdventureMapInterface::adjustActiveness(bool aiTurnStart)
  1252. {
  1253. bool wasActive = isActive();
  1254. if(wasActive)
  1255. deactivate();
  1256. adventureInt->duringAITurn = aiTurnStart;
  1257. if(wasActive)
  1258. activate();
  1259. }
  1260. void CAdventureMapInterface::exitWorldView()
  1261. {
  1262. mode = EAdvMapMode::NORMAL;
  1263. panelMain->activate();
  1264. panelWorldView->deactivate();
  1265. activeMapPanel = panelMain;
  1266. townList->activate();
  1267. heroList->activate();
  1268. infoBar->activate();
  1269. redraw();
  1270. terrain->onViewMapActivated();
  1271. }
  1272. void CAdventureMapInterface::openWorldView(int tileSize)
  1273. {
  1274. mode = EAdvMapMode::WORLD_VIEW;
  1275. panelMain->deactivate();
  1276. panelWorldView->activate();
  1277. activeMapPanel = panelWorldView;
  1278. townList->deactivate();
  1279. heroList->deactivate();
  1280. infoBar->showSelection(); // to prevent new day animation interfering world view mode
  1281. infoBar->deactivate();
  1282. redraw();
  1283. terrain->onViewWorldActivated(tileSize);
  1284. }
  1285. void CAdventureMapInterface::openWorldView()
  1286. {
  1287. openWorldView(11);
  1288. }
  1289. void CAdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  1290. {
  1291. openWorldView(11);
  1292. terrain->onViewSpellActivated(11, objectPositions, showTerrain);
  1293. }