CAdventureMapInterface.cpp 42 KB

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