CAdvMapInt.cpp 41 KB

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