CAdvMapInt.cpp 45 KB

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