CAdventureMapInterface.cpp 42 KB

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