CAdvMapInt.cpp 43 KB

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