2
0

CAdventureMapInterface.cpp 40 KB

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