CAdvMapInt.cpp 41 KB

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