CAdvMapInt.cpp 41 KB

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