CAdvMapInt.cpp 42 KB

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