CAdvmapInterface.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. /*
  2. * CAdvmapInterface.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 "CAdvmapInterface.h"
  12. #include "CCastleInterface.h"
  13. #include "CHeroWindow.h"
  14. #include "CKingdomInterface.h"
  15. #include "CSpellWindow.h"
  16. #include "GUIClasses.h"
  17. #include "CTradeWindow.h"
  18. #include "../CBitmapHandler.h"
  19. #include "../CGameInfo.h"
  20. #include "../CMessage.h"
  21. #include "../CMusicHandler.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../mainmenu/CMainMenu.h"
  24. #include "../lobby/CBonusSelection.h"
  25. #include "../lobby/CSavingScreen.h"
  26. #include "../lobby/CScenarioInfoScreen.h"
  27. #include "../Graphics.h"
  28. #include "../mapHandler.h"
  29. #include "../gui/CAnimation.h"
  30. #include "../gui/CCursorHandler.h"
  31. #include "../gui/CGuiHandler.h"
  32. #include "../gui/SDL_Extensions.h"
  33. #include "../widgets/MiscWidgets.h"
  34. #include "../windows/InfoWindows.h"
  35. #include "../../CCallback.h"
  36. #include "../../lib/CConfigHandler.h"
  37. #include "../../lib/CGameState.h"
  38. #include "../../lib/CGeneralTextHandler.h"
  39. #include "../../lib/CHeroHandler.h"
  40. #include "../../lib/CSoundBase.h"
  41. #include "../../lib/spells/CSpellHandler.h"
  42. #include "../../lib/CTownHandler.h"
  43. #include "../../lib/JsonNode.h"
  44. #include "../../lib/mapObjects/CGHeroInstance.h"
  45. #include "../../lib/mapping/CMap.h"
  46. #include "../../lib/UnlockGuard.h"
  47. #include "../../lib/VCMI_Lib.h"
  48. #include "../../lib/StartInfo.h"
  49. #ifdef _MSC_VER
  50. #pragma warning (disable : 4355)
  51. #endif
  52. #define ADVOPT (conf.go()->ac)
  53. using namespace CSDL_Ext;
  54. CAdvMapInt *adventureInt;
  55. static void setScrollingCursor(ui8 direction)
  56. {
  57. if(direction & CAdvMapInt::RIGHT)
  58. {
  59. if(direction & CAdvMapInt::UP)
  60. CCS->curh->changeGraphic(ECursor::ADVENTURE, 33);
  61. else if(direction & CAdvMapInt::DOWN)
  62. CCS->curh->changeGraphic(ECursor::ADVENTURE, 35);
  63. else
  64. CCS->curh->changeGraphic(ECursor::ADVENTURE, 34);
  65. }
  66. else if(direction & CAdvMapInt::LEFT)
  67. {
  68. if(direction & CAdvMapInt::UP)
  69. CCS->curh->changeGraphic(ECursor::ADVENTURE, 39);
  70. else if(direction & CAdvMapInt::DOWN)
  71. CCS->curh->changeGraphic(ECursor::ADVENTURE, 37);
  72. else
  73. CCS->curh->changeGraphic(ECursor::ADVENTURE, 38);
  74. }
  75. else if(direction & CAdvMapInt::UP)
  76. CCS->curh->changeGraphic(ECursor::ADVENTURE, 32);
  77. else if(direction & CAdvMapInt::DOWN)
  78. CCS->curh->changeGraphic(ECursor::ADVENTURE, 36);
  79. }
  80. CTerrainRect::CTerrainRect()
  81. : fadeSurface(nullptr),
  82. lastRedrawStatus(EMapAnimRedrawStatus::OK),
  83. fadeAnim(std::make_shared<CFadeAnimation>()),
  84. curHoveredTile(-1,-1,-1),
  85. currentPath(nullptr)
  86. {
  87. tilesw=(ADVOPT.advmapW+31)/32;
  88. tilesh=(ADVOPT.advmapH+31)/32;
  89. pos.x=ADVOPT.advmapX;
  90. pos.y=ADVOPT.advmapY;
  91. pos.w=ADVOPT.advmapW;
  92. pos.h=ADVOPT.advmapH;
  93. moveX = moveY = 0;
  94. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  95. }
  96. CTerrainRect::~CTerrainRect()
  97. {
  98. if(fadeSurface)
  99. SDL_FreeSurface(fadeSurface);
  100. }
  101. void CTerrainRect::deactivate()
  102. {
  103. CIntObject::deactivate();
  104. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  105. }
  106. void CTerrainRect::clickLeft(tribool down, bool previousState)
  107. {
  108. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  109. return;
  110. if(indeterminate(down))
  111. return;
  112. #ifdef VCMI_ANDROID
  113. if(adventureInt->swipeEnabled)
  114. {
  115. if(handleSwipeStateChange(down == true))
  116. {
  117. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  118. }
  119. }
  120. else
  121. {
  122. #endif
  123. if(down == false)
  124. return;
  125. #ifdef VCMI_ANDROID
  126. }
  127. #endif
  128. int3 mp = whichTileIsIt();
  129. if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  130. return;
  131. adventureInt->tileLClicked(mp);
  132. }
  133. void CTerrainRect::clickRight(tribool down, bool previousState)
  134. {
  135. #ifdef VCMI_ANDROID
  136. if(adventureInt->swipeEnabled && isSwiping)
  137. return;
  138. #endif
  139. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  140. return;
  141. int3 mp = whichTileIsIt();
  142. if(CGI->mh->map->isInTheMap(mp) && down)
  143. adventureInt->tileRClicked(mp);
  144. }
  145. void CTerrainRect::clickMiddle(tribool down, bool previousState)
  146. {
  147. handleSwipeStateChange(down == true);
  148. }
  149. void CTerrainRect::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  150. {
  151. handleHover(sEvent);
  152. if(!adventureInt->swipeEnabled)
  153. return;
  154. handleSwipeMove(sEvent);
  155. }
  156. void CTerrainRect::handleSwipeMove(const SDL_MouseMotionEvent & sEvent)
  157. {
  158. #ifdef VCMI_ANDROID
  159. if(sEvent.state == 0) // any "button" is enough on android
  160. #else //!VCMI_ANDROID
  161. if((sEvent.state & SDL_BUTTON_MMASK) == 0) // swipe only works with middle mouse on other platforms
  162. #endif //!VCMI_ANDROID
  163. {
  164. return;
  165. }
  166. if(!isSwiping)
  167. {
  168. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  169. if(abs(sEvent.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  170. abs(sEvent.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  171. {
  172. isSwiping = true;
  173. }
  174. }
  175. if(isSwiping)
  176. {
  177. adventureInt->swipeTargetPosition.x =
  178. swipeInitialMapPos.x + static_cast<si32>(swipeInitialRealPos.x - sEvent.x) / 32;
  179. adventureInt->swipeTargetPosition.y =
  180. swipeInitialMapPos.y + static_cast<si32>(swipeInitialRealPos.y - sEvent.y) / 32;
  181. adventureInt->swipeMovementRequested = true;
  182. }
  183. }
  184. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  185. {
  186. if(btnPressed)
  187. {
  188. swipeInitialRealPos = int3(GH.current->motion.x, GH.current->motion.y, 0);
  189. swipeInitialMapPos = int3(adventureInt->position);
  190. return true;
  191. }
  192. else if(isSwiping) // only accept this touch if it wasn't a swipe
  193. {
  194. isSwiping = false;
  195. return true;
  196. }
  197. return false;
  198. }
  199. void CTerrainRect::handleHover(const SDL_MouseMotionEvent &sEvent)
  200. {
  201. int3 tHovered = whichTileIsIt(sEvent.x, sEvent.y);
  202. int3 pom = adventureInt->verifyPos(tHovered);
  203. if(tHovered != pom) //tile outside the map
  204. {
  205. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  206. return;
  207. }
  208. if (pom != curHoveredTile)
  209. {
  210. curHoveredTile = pom;
  211. adventureInt->tileHovered(pom);
  212. }
  213. }
  214. void CTerrainRect::hover(bool on)
  215. {
  216. if (!on)
  217. {
  218. adventureInt->statusbar.clear();
  219. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  220. }
  221. //Hoverable::hover(on);
  222. }
  223. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  224. {
  225. const static int pns[9][9] = {
  226. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  227. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  228. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  229. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  230. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  231. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  232. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  233. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  234. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  235. }; //table of magic values TODO meaning, change variable name
  236. for (int i=0; i < (int)currentPath->nodes.size()-1; ++i)
  237. {
  238. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  239. if(curPos.z != adventureInt->position.z)
  240. continue;
  241. int pn=-1;//number of picture
  242. if (i==0) //last tile
  243. {
  244. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  245. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  246. if (x<0 || y<0 || x>pos.w || y>pos.h)
  247. continue;
  248. pn=0;
  249. }
  250. else
  251. {
  252. const int3 &prevPos = currentPath->nodes[i-1].coord;
  253. std::vector<CGPathNode> & cv = currentPath->nodes;
  254. /* Vector directions
  255. * 0 1 2
  256. * \ | /
  257. * 3 - 4 - 5
  258. * / | \
  259. * 6 7 8
  260. *For example:
  261. * |
  262. * |__\
  263. * /
  264. * is id1=7, id2=5 (pns[7][5])
  265. */
  266. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  267. if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
  268. {
  269. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  270. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  271. pn=pns[id1][id2];
  272. }
  273. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  274. {
  275. pn = 0;
  276. }
  277. }
  278. if (currentPath->nodes[i].turns)
  279. pn+=25;
  280. if (pn>=0)
  281. {
  282. const auto arrow = graphics->heroMoveArrows->getImage(pn);
  283. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  284. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  285. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  286. continue;
  287. int hvx = (x + arrow->width()) - (pos.x + pos.w),
  288. hvy = (y + arrow->height()) - (pos.y + pos.h);
  289. SDL_Rect prevClip;
  290. SDL_GetClipRect(to, &prevClip);
  291. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  292. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  293. {
  294. if (hvx<0 && hvy<0)
  295. {
  296. arrow->draw(to, x + moveX, y + moveY);
  297. }
  298. else if(hvx<0)
  299. {
  300. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  301. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  302. }
  303. else if (hvy<0)
  304. {
  305. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  306. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  307. }
  308. else
  309. {
  310. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  311. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  312. }
  313. }
  314. else //standard version
  315. {
  316. if (hvx<0 && hvy<0)
  317. {
  318. arrow->draw(to, x, y);
  319. }
  320. else if(hvx<0)
  321. {
  322. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  323. arrow->draw(to, x, y, &srcRect);
  324. }
  325. else if (hvy<0)
  326. {
  327. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  328. arrow->draw(to, x, y, &srcRect);
  329. }
  330. else
  331. {
  332. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  333. arrow->draw(to, x, y, &srcRect);
  334. }
  335. }
  336. SDL_SetClipRect(to, &prevClip);
  337. }
  338. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  339. }
  340. void CTerrainRect::show(SDL_Surface * to)
  341. {
  342. if (adventureInt->mode == EAdvMapMode::NORMAL)
  343. {
  344. MapDrawingInfo info(adventureInt->position, &LOCPLINT->cb->getVisibilityMap(), &pos);
  345. info.otherheroAnim = true;
  346. info.anim = adventureInt->anim;
  347. info.heroAnim = adventureInt->heroAnim;
  348. if (ADVOPT.smoothMove)
  349. info.movement = int3(moveX, moveY, 0);
  350. lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
  351. if (fadeAnim->isFading())
  352. {
  353. Rect r(pos);
  354. fadeAnim->update();
  355. fadeAnim->draw(to, nullptr, &r);
  356. }
  357. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  358. {
  359. showPath(&pos, to);
  360. }
  361. }
  362. }
  363. void CTerrainRect::showAll(SDL_Surface * to)
  364. {
  365. // world view map is static and doesn't need redraw every frame
  366. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  367. {
  368. MapDrawingInfo info(adventureInt->position, &LOCPLINT->cb->getVisibilityMap(), &pos, adventureInt->worldViewIcons);
  369. info.scaled = true;
  370. info.scale = adventureInt->worldViewScale;
  371. adventureInt->worldViewOptions.adjustDrawingInfo(info);
  372. CGI->mh->drawTerrainRectNew(to, &info);
  373. }
  374. }
  375. void CTerrainRect::showAnim(SDL_Surface * to)
  376. {
  377. if (fadeAnim->isFading())
  378. show(to);
  379. else if (lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
  380. show(to); // currently the same; maybe we should pass some flag to map handler so it redraws ONLY tiles that need redraw instead of full
  381. }
  382. int3 CTerrainRect::whichTileIsIt(const int x, const int y)
  383. {
  384. int3 ret;
  385. ret.x = adventureInt->position.x + ((x-CGI->mh->offsetX-pos.x)/32);
  386. ret.y = adventureInt->position.y + ((y-CGI->mh->offsetY-pos.y)/32);
  387. ret.z = adventureInt->position.z;
  388. return ret;
  389. }
  390. int3 CTerrainRect::whichTileIsIt()
  391. {
  392. if(GH.current)
  393. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  394. else
  395. return int3(-1);
  396. }
  397. int3 CTerrainRect::tileCountOnScreen()
  398. {
  399. switch (adventureInt->mode)
  400. {
  401. default:
  402. logGlobal->error("Unknown map mode %d", (int)adventureInt->mode);
  403. return int3();
  404. case EAdvMapMode::NORMAL:
  405. return int3(tilesw, tilesh, 1);
  406. case EAdvMapMode::WORLD_VIEW:
  407. return int3(tilesw / adventureInt->worldViewScale, tilesh / adventureInt->worldViewScale, 1);
  408. }
  409. }
  410. void CTerrainRect::fadeFromCurrentView()
  411. {
  412. if (!ADVOPT.screenFading)
  413. return;
  414. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  415. return;
  416. if (!fadeSurface)
  417. fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
  418. SDL_BlitSurface(screen, &pos, fadeSurface, nullptr);
  419. fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
  420. }
  421. bool CTerrainRect::needsAnimUpdate()
  422. {
  423. return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
  424. }
  425. void CResDataBar::clickRight(tribool down, bool previousState)
  426. {
  427. }
  428. CResDataBar::CResDataBar(const std::string & defname, int x, int y, int offx, int offy, int resdist, int datedist)
  429. {
  430. pos.x += x;
  431. pos.y += y;
  432. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  433. background = std::make_shared<CPicture>(defname, 0, 0);
  434. background->colorize(LOCPLINT->playerID);
  435. pos.w = background->bg->w;
  436. pos.h = background->bg->h;
  437. txtpos.resize(8);
  438. for (int i = 0; i < 8 ; i++)
  439. {
  440. txtpos[i].first = pos.x + offx + resdist*i;
  441. txtpos[i].second = pos.y + offy;
  442. }
  443. txtpos[7].first = txtpos[6].first + datedist;
  444. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  445. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  446. addUsedEvents(RCLICK);
  447. }
  448. CResDataBar::CResDataBar()
  449. {
  450. pos.x += ADVOPT.resdatabarX;
  451. pos.y += ADVOPT.resdatabarY;
  452. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  453. background = std::make_shared<CPicture>(ADVOPT.resdatabarG, 0, 0);
  454. background->colorize(LOCPLINT->playerID);
  455. pos.w = background->bg->w;
  456. pos.h = background->bg->h;
  457. txtpos.resize(8);
  458. for (int i = 0; i < 8 ; i++)
  459. {
  460. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  461. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  462. }
  463. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  464. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  465. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  466. }
  467. CResDataBar::~CResDataBar() = default;
  468. void CResDataBar::draw(SDL_Surface * to)
  469. {
  470. //TODO: all this should be labels, but they require proper text update on change
  471. for (auto i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  472. {
  473. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  474. graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
  475. }
  476. std::vector<std::string> temp;
  477. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  478. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  479. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  480. graphics->fonts[FONT_SMALL]->renderTextLeft(to, processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
  481. }
  482. void CResDataBar::show(SDL_Surface * to)
  483. {
  484. }
  485. void CResDataBar::showAll(SDL_Surface * to)
  486. {
  487. CIntObject::showAll(to);
  488. draw(to);
  489. }
  490. CAdvMapInt::CAdvMapInt():
  491. mode(EAdvMapMode::NORMAL),
  492. worldViewScale(0.0f), //actual init later in changeMode
  493. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  494. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  495. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  496. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  497. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192)), state(NA),
  498. spellBeingCasted(nullptr), position(int3(0, 0, 0)), selection(nullptr),
  499. updateScreen(false), anim(0), animValHitCount(0), heroAnim(0), heroAnimValHitCount(0),
  500. activeMapPanel(nullptr), duringAITurn(false), scrollingDir(0), scrollingState(false),
  501. swipeEnabled(settings["general"]["swipe"].Bool()), swipeMovementRequested(false),
  502. swipeTargetPosition(int3(-1, -1, -1))
  503. {
  504. adventureInt = this;
  505. pos.x = pos.y = 0;
  506. pos.w = screen->w;
  507. pos.h = screen->h;
  508. townList.onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  509. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  510. if (ADVOPT.worldViewGraphic != "")
  511. {
  512. bgWorldView = BitmapHandler::loadBitmap(ADVOPT.worldViewGraphic);
  513. }
  514. else
  515. {
  516. bgWorldView = nullptr;
  517. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  518. }
  519. if (!bgWorldView)
  520. {
  521. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  522. bgWorldView = BitmapHandler::loadBitmap("VWorld.bmp");
  523. }
  524. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  525. worldViewIcons->preload();
  526. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  527. {
  528. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  529. }
  530. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  531. {
  532. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  533. for(auto image : info.additionalDefs)
  534. button->addImage(image);
  535. return button;
  536. };
  537. kingOverview = makeButton(293, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  538. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  539. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  540. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  541. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  542. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  543. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  544. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  545. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  546. endTurn = makeButton(302, std::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  547. int panelSpaceBottom = screen->h - resdatabar.pos.h - 4;
  548. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  549. // TODO correct drawing position
  550. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList.pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  551. panelMain->addChildColorableButton(kingOverview);
  552. panelMain->addChildColorableButton(underground);
  553. panelMain->addChildColorableButton(questlog);
  554. panelMain->addChildColorableButton(sleepWake);
  555. panelMain->addChildColorableButton(moveHero);
  556. panelMain->addChildColorableButton(spellbook);
  557. panelMain->addChildColorableButton(advOptions);
  558. panelMain->addChildColorableButton(sysOptions);
  559. panelMain->addChildColorableButton(nextHero);
  560. panelMain->addChildColorableButton(endTurn);
  561. // TODO move configs to resolutions.json, similarly to previous buttons
  562. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  563. worldViewBackConfig.defName = "IOK6432.DEF";
  564. worldViewBackConfig.x = screen->w - 73;
  565. worldViewBackConfig.y = 343 + 195;
  566. worldViewBackConfig.playerColoured = false;
  567. panelWorldView->addChildToPanel(
  568. makeButton(288, std::bind(&CAdvMapInt::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  569. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  570. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  571. worldViewPuzzleConfig.x = screen->w - 188;
  572. worldViewPuzzleConfig.y = 343 + 195;
  573. worldViewPuzzleConfig.playerColoured = false;
  574. panelWorldView->addChildToPanel( // no help text for this one
  575. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  576. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  577. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  578. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  579. worldViewScale1xConfig.x = screen->w - 191;
  580. worldViewScale1xConfig.y = 23 + 195;
  581. worldViewScale1xConfig.playerColoured = false;
  582. panelWorldView->addChildToPanel( // help text is wrong for this button
  583. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  584. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  585. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  586. worldViewScale2xConfig.x = screen->w - 191 + 63;
  587. worldViewScale2xConfig.y = 23 + 195;
  588. worldViewScale2xConfig.playerColoured = false;
  589. panelWorldView->addChildToPanel( // help text is wrong for this button
  590. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  591. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  592. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  593. worldViewScale4xConfig.x = screen->w - 191 + 126;
  594. worldViewScale4xConfig.y = 23 + 195;
  595. worldViewScale4xConfig.playerColoured = false;
  596. panelWorldView->addChildToPanel( // help text is wrong for this button
  597. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  598. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  599. worldViewUndergroundConfig.defName = "IAM010.DEF";
  600. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  601. worldViewUndergroundConfig.x = screen->w - 115;
  602. worldViewUndergroundConfig.y = 343 + 195;
  603. worldViewUndergroundConfig.playerColoured = true;
  604. worldViewUnderground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  605. panelWorldView->addChildColorableButton(worldViewUnderground);
  606. setPlayer(LOCPLINT->playerID);
  607. int iconColorMultiplier = player.getNum() * 19;
  608. int wvLeft = heroList.pos.x - 2; // TODO correct drawing position
  609. //int wvTop = 195;
  610. for (int i = 0; i < 5; ++i)
  611. {
  612. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  613. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  614. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  615. }
  616. for (int i = 0; i < 7; ++i)
  617. {
  618. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  619. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  620. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  621. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  622. }
  623. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  624. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  625. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  626. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  627. activeMapPanel = panelMain;
  628. changeMode(EAdvMapMode::NORMAL);
  629. underground->block(!CGI->mh->map->twoLevel);
  630. questlog->block(!CGI->mh->map->quests.size());
  631. worldViewUnderground->block(!CGI->mh->map->twoLevel);
  632. addUsedEvents(MOVE);
  633. }
  634. CAdvMapInt::~CAdvMapInt()
  635. {
  636. SDL_FreeSurface(bg);
  637. }
  638. void CAdvMapInt::fshowOverview()
  639. {
  640. GH.pushInt(new CKingdomInterface());
  641. }
  642. void CAdvMapInt::fworldViewBack()
  643. {
  644. changeMode(EAdvMapMode::NORMAL);
  645. CGI->mh->discardWorldViewCache();
  646. auto hero = curHero();
  647. if (hero)
  648. centerOn(hero);
  649. }
  650. void CAdvMapInt::fworldViewScale1x()
  651. {
  652. // TODO set corresponding scale button to "selected" mode
  653. changeMode(EAdvMapMode::WORLD_VIEW, 0.22f);
  654. }
  655. void CAdvMapInt::fworldViewScale2x()
  656. {
  657. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f);
  658. }
  659. void CAdvMapInt::fworldViewScale4x()
  660. {
  661. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f);
  662. }
  663. void CAdvMapInt::fswitchLevel()
  664. {
  665. // with support for future multi-level maps :)
  666. int maxLevels = CGI->mh->map->twoLevel ? 2 : 1;
  667. if (maxLevels < 2)
  668. return;
  669. position.z = (position.z + 1) % maxLevels;
  670. underground->setIndex(position.z, true);
  671. underground->redraw();
  672. worldViewUnderground->setIndex(position.z, true);
  673. worldViewUnderground->redraw();
  674. updateScreen = true;
  675. minimap.setLevel(position.z);
  676. if (mode == EAdvMapMode::WORLD_VIEW)
  677. terrain.redraw();
  678. }
  679. void CAdvMapInt::fshowQuestlog()
  680. {
  681. LOCPLINT->showQuestLog();
  682. }
  683. void CAdvMapInt::fsleepWake()
  684. {
  685. const CGHeroInstance *h = curHero();
  686. if (!h)
  687. return;
  688. bool newSleep = !isHeroSleeping(h);
  689. setHeroSleeping(h, newSleep);
  690. updateSleepWake(h);
  691. if (newSleep)
  692. {
  693. fnextHero();
  694. //moveHero.block(true);
  695. //uncomment to enable original HoMM3 behaviour:
  696. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  697. }
  698. }
  699. void CAdvMapInt::fmoveHero()
  700. {
  701. const CGHeroInstance *h = curHero();
  702. if (!h || !terrain.currentPath || !CGI->mh->canStartHeroMovement())
  703. return;
  704. LOCPLINT->moveHero(h, *terrain.currentPath);
  705. }
  706. void CAdvMapInt::fshowSpellbok()
  707. {
  708. if (!curHero()) //checking necessary values
  709. return;
  710. centerOn(selection);
  711. GH.pushInt(new CSpellWindow(curHero(), LOCPLINT, false));
  712. }
  713. void CAdvMapInt::fadventureOPtions()
  714. {
  715. GH.pushInt(new CAdventureOptions());
  716. }
  717. void CAdvMapInt::fsystemOptions()
  718. {
  719. GH.pushInt(new CSystemOptionsWindow());
  720. }
  721. void CAdvMapInt::fnextHero()
  722. {
  723. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  724. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  725. if (next < 0)
  726. return;
  727. select(LOCPLINT->wanderingHeroes[next], true);
  728. }
  729. void CAdvMapInt::fendTurn()
  730. {
  731. if(!LOCPLINT->makingTurn)
  732. return;
  733. if(settings["adventure"]["heroReminder"].Bool())
  734. {
  735. for(auto hero : LOCPLINT->wanderingHeroes)
  736. {
  737. if(!isHeroSleeping(hero) && hero->movement > 0)
  738. {
  739. // Only show hero reminder if conditions met:
  740. // - There still movement points
  741. // - Hero don't have a path or there not points for first step on path
  742. auto path = LOCPLINT->getAndVerifyPath(hero);
  743. if(!path || path->nodes.size() < 2 || !path->nodes[path->nodes.size()-2].turns)
  744. {
  745. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), nullptr);
  746. return;
  747. }
  748. }
  749. }
  750. }
  751. endingTurn();
  752. }
  753. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  754. {
  755. sleepWake->block(!h);
  756. if (!h)
  757. return;
  758. bool state = isHeroSleeping(h);
  759. sleepWake->setIndex(state ? 1 : 0, true);
  760. sleepWake->assignedKeys.clear();
  761. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  762. }
  763. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  764. {
  765. if(!h)
  766. {
  767. moveHero->block(true);
  768. return;
  769. }
  770. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  771. if(boost::logic::indeterminate(hasPath))
  772. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  773. moveHero->block(!hasPath || (h->movement == 0));
  774. }
  775. void CAdvMapInt::updateSpellbook(const CGHeroInstance *h)
  776. {
  777. spellbook->block(!h);
  778. }
  779. int CAdvMapInt::getNextHeroIndex(int startIndex)
  780. {
  781. if (LOCPLINT->wanderingHeroes.size() == 0)
  782. return -1;
  783. if (startIndex < 0)
  784. startIndex = 0;
  785. int i = startIndex;
  786. do
  787. {
  788. i++;
  789. if (i >= LOCPLINT->wanderingHeroes.size())
  790. i = 0;
  791. }
  792. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  793. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  794. return i;
  795. else
  796. return -1;
  797. }
  798. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  799. {
  800. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  801. int next = getNextHeroIndex(start);
  802. if (next < 0)
  803. {
  804. nextHero->block(true);
  805. return;
  806. }
  807. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  808. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  809. nextHero->block(noActiveHeroes);
  810. }
  811. void CAdvMapInt::activate()
  812. {
  813. CIntObject::activate();
  814. if (!(active & KEYBOARD))
  815. CIntObject::activate(KEYBOARD);
  816. screenBuf = screen;
  817. GH.statusbar = &statusbar;
  818. if(!duringAITurn)
  819. {
  820. activeMapPanel->activate();
  821. if (mode == EAdvMapMode::NORMAL)
  822. {
  823. heroList.activate();
  824. townList.activate();
  825. infoBar.activate();
  826. }
  827. minimap.activate();
  828. terrain.activate();
  829. if(LOCPLINT)
  830. LOCPLINT->cingconsole->activate();
  831. GH.fakeMouseMove(); //to restore the cursor
  832. }
  833. }
  834. void CAdvMapInt::deactivate()
  835. {
  836. CIntObject::deactivate();
  837. if(!duringAITurn)
  838. {
  839. scrollingDir = 0;
  840. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  841. activeMapPanel->deactivate();
  842. if (mode == EAdvMapMode::NORMAL)
  843. {
  844. heroList.deactivate();
  845. townList.deactivate();
  846. infoBar.deactivate();
  847. }
  848. minimap.deactivate();
  849. terrain.deactivate();
  850. if(LOCPLINT)
  851. LOCPLINT->cingconsole->deactivate();
  852. }
  853. }
  854. void CAdvMapInt::showAll(SDL_Surface * to)
  855. {
  856. blitAt(bg,0,0,to);
  857. if(state != INGAME)
  858. return;
  859. switch (mode)
  860. {
  861. case EAdvMapMode::NORMAL:
  862. heroList.showAll(to);
  863. townList.showAll(to);
  864. infoBar.showAll(to);
  865. break;
  866. case EAdvMapMode::WORLD_VIEW:
  867. terrain.showAll(to);
  868. break;
  869. }
  870. activeMapPanel->showAll(to);
  871. updateScreen = true;
  872. minimap.showAll(to);
  873. show(to);
  874. resdatabar.showAll(to);
  875. statusbar.show(to);
  876. LOCPLINT->cingconsole->show(to);
  877. }
  878. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  879. {
  880. if (!hero)
  881. return false;
  882. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  883. }
  884. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  885. {
  886. if (sleep)
  887. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  888. else
  889. LOCPLINT->sleepingHeroes -= hero;
  890. updateNextHero(nullptr);
  891. }
  892. void CAdvMapInt::show(SDL_Surface * to)
  893. {
  894. if(state != INGAME)
  895. return;
  896. ++animValHitCount; //for animations
  897. if(animValHitCount == 8)
  898. {
  899. CGI->mh->updateWater();
  900. animValHitCount = 0;
  901. ++anim;
  902. updateScreen = true;
  903. }
  904. ++heroAnim;
  905. if(swipeEnabled)
  906. {
  907. handleSwipeUpdate();
  908. }
  909. #ifdef VCMI_ANDROID // on android, map-moving mode is exclusive (TODO technically it might work with both enabled; to be checked)
  910. else
  911. #endif // VCMI_ANDROID
  912. {
  913. handleMapScrollingUpdate();
  914. }
  915. for(int i = 0; i < 4; i++)
  916. {
  917. if(settings["session"]["spectate"].Bool())
  918. gems[i]->setFrame(PlayerColor(1).getNum());
  919. else
  920. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  921. }
  922. if(updateScreen)
  923. {
  924. int3 betterPos = LOCPLINT->repairScreenPos(position);
  925. if (betterPos != position)
  926. {
  927. logGlobal->warn("Incorrect position for adventure map!");
  928. position = betterPos;
  929. }
  930. terrain.show(to);
  931. for(int i = 0; i < 4; i++)
  932. gems[i]->showAll(to);
  933. updateScreen=false;
  934. LOCPLINT->cingconsole->show(to);
  935. }
  936. else if (terrain.needsAnimUpdate())
  937. {
  938. terrain.showAnim(to);
  939. for(int i = 0; i < 4; i++)
  940. gems[i]->showAll(to);
  941. }
  942. infoBar.show(to);
  943. statusbar.showAll(to);
  944. }
  945. void CAdvMapInt::handleMapScrollingUpdate()
  946. {
  947. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  948. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  949. if((animValHitCount % (4 / scrollSpeed)) == 0
  950. && ((GH.topInt() == this) || isCtrlKeyDown()))
  951. {
  952. if((scrollingDir & LEFT) && (position.x > -CGI->mh->frameW))
  953. position.x--;
  954. if((scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW))
  955. position.x++;
  956. if((scrollingDir & UP) && (position.y > -CGI->mh->frameH))
  957. position.y--;
  958. if((scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH))
  959. position.y++;
  960. if(scrollingDir)
  961. {
  962. setScrollingCursor(scrollingDir);
  963. scrollingState = true;
  964. updateScreen = true;
  965. minimap.redraw();
  966. if(mode == EAdvMapMode::WORLD_VIEW)
  967. terrain.redraw();
  968. }
  969. else if(scrollingState)
  970. {
  971. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  972. scrollingState = false;
  973. }
  974. }
  975. }
  976. void CAdvMapInt::handleSwipeUpdate()
  977. {
  978. if(swipeMovementRequested)
  979. {
  980. auto fixedPos = LOCPLINT->repairScreenPos(swipeTargetPosition);
  981. position.x = fixedPos.x;
  982. position.y = fixedPos.y;
  983. CCS->curh->changeGraphic(ECursor::DEFAULT, 0);
  984. updateScreen = true;
  985. minimap.redraw();
  986. swipeMovementRequested = false;
  987. }
  988. }
  989. void CAdvMapInt::selectionChanged()
  990. {
  991. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  992. if (selection != to)
  993. select(to);
  994. }
  995. void CAdvMapInt::centerOn(int3 on, bool fade)
  996. {
  997. bool switchedLevels = on.z != position.z;
  998. if (fade)
  999. {
  1000. terrain.fadeFromCurrentView();
  1001. }
  1002. switch (mode)
  1003. {
  1004. default:
  1005. case EAdvMapMode::NORMAL:
  1006. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  1007. on.y -= CGI->mh->frameH;
  1008. break;
  1009. case EAdvMapMode::WORLD_VIEW:
  1010. on.x -= CGI->mh->tilesW / 2 / worldViewScale;
  1011. on.y -= CGI->mh->tilesH / 2 / worldViewScale;
  1012. break;
  1013. }
  1014. on = LOCPLINT->repairScreenPos(on);
  1015. position = on;
  1016. updateScreen=true;
  1017. underground->setIndex(on.z,true); //change underground switch button image
  1018. underground->redraw();
  1019. worldViewUnderground->setIndex(on.z, true);
  1020. worldViewUnderground->redraw();
  1021. if (switchedLevels)
  1022. minimap.setLevel(position.z);
  1023. minimap.redraw();
  1024. if (mode == EAdvMapMode::WORLD_VIEW)
  1025. terrain.redraw();
  1026. }
  1027. void CAdvMapInt::centerOn(const CGObjectInstance * obj, bool fade)
  1028. {
  1029. centerOn(obj->getSightCenter(), fade);
  1030. }
  1031. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1032. {
  1033. if (mode == EAdvMapMode::WORLD_VIEW)
  1034. return;
  1035. ui8 Dir = 0;
  1036. SDL_Keycode k = key.keysym.sym;
  1037. const CGHeroInstance *h = curHero(); //selected hero
  1038. const CGTownInstance *t = curTown(); //selected town
  1039. switch(k)
  1040. {
  1041. case SDLK_g:
  1042. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1043. return;
  1044. {
  1045. //find first town with tavern
  1046. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  1047. {
  1048. return town->hasBuilt(BuildingID::TAVERN);
  1049. });
  1050. if(itr != LOCPLINT->towns.end())
  1051. LOCPLINT->showThievesGuildWindow(*itr);
  1052. else
  1053. LOCPLINT->showInfoDialog("No available town with tavern!");
  1054. }
  1055. return;
  1056. case SDLK_i:
  1057. if(isActive())
  1058. CAdventureOptions::showScenarioInfo();
  1059. return;
  1060. case SDLK_l:
  1061. if(isActive())
  1062. LOCPLINT->proposeLoadingGame();
  1063. return;
  1064. case SDLK_s:
  1065. if(isActive() && key.type == SDL_KEYUP)
  1066. GH.pushInt(new CSavingScreen());
  1067. return;
  1068. case SDLK_d:
  1069. {
  1070. if(h && isActive() && key.state == SDL_PRESSED)
  1071. LOCPLINT->tryDiggging(h);
  1072. return;
  1073. }
  1074. case SDLK_p:
  1075. if(isActive())
  1076. LOCPLINT->showPuzzleMap();
  1077. return;
  1078. case SDLK_v:
  1079. if(isActive())
  1080. LOCPLINT->viewWorldMap();
  1081. return;
  1082. case SDLK_r:
  1083. if(isActive() && LOCPLINT->ctrlPressed())
  1084. {
  1085. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  1086. [](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr);
  1087. }
  1088. return;
  1089. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1090. {
  1091. if(!isActive())
  1092. return;
  1093. if(h && key.state == SDL_PRESSED)
  1094. {
  1095. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  1096. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  1097. //this thread leaves scope and tries to lock pim while holding gs,
  1098. //network thread tries to lock gs (appluy cl) while holding pim
  1099. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  1100. LOCPLINT->cb->moveHero(h,h->pos);
  1101. }
  1102. }
  1103. return;
  1104. case SDLK_RETURN:
  1105. {
  1106. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1107. return;
  1108. if(h)
  1109. LOCPLINT->openHeroWindow(h);
  1110. else if(t)
  1111. LOCPLINT->openTownWindow(t);
  1112. return;
  1113. }
  1114. case SDLK_ESCAPE:
  1115. {
  1116. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1117. return;
  1118. leaveCastingMode();
  1119. return;
  1120. }
  1121. case SDLK_t:
  1122. {
  1123. //act on key down if marketplace windows is not already opened
  1124. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1125. return;
  1126. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  1127. {
  1128. //check if we have any marketplace
  1129. const CGTownInstance *townWithMarket = nullptr;
  1130. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  1131. {
  1132. if(t->hasBuilt(BuildingID::MARKETPLACE))
  1133. {
  1134. townWithMarket = t;
  1135. break;
  1136. }
  1137. }
  1138. if(townWithMarket) //if any town has marketplace, open window
  1139. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  1140. else //if not - complain
  1141. LOCPLINT->showInfoDialog("No available marketplace!");
  1142. }
  1143. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  1144. {
  1145. townList.selectNext();
  1146. }
  1147. return;
  1148. }
  1149. default:
  1150. {
  1151. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1152. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1153. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1154. //numpad arrow
  1155. if(CGuiHandler::isArrowKey(k))
  1156. k = CGuiHandler::arrowToNum(k);
  1157. k -= SDLK_KP_1;
  1158. if(k < 0 || k > 8)
  1159. return;
  1160. if (!CGI->mh->canStartHeroMovement())
  1161. return;
  1162. int3 dir = directions[k];
  1163. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1164. {
  1165. Dir = (dir.x<0 ? LEFT : 0) |
  1166. (dir.x>0 ? RIGHT : 0) |
  1167. (dir.y<0 ? UP : 0) |
  1168. (dir.y>0 ? DOWN : 0) ;
  1169. break;
  1170. }
  1171. if(!h || key.state != SDL_PRESSED)
  1172. break;
  1173. if(k == 4)
  1174. {
  1175. centerOn(h);
  1176. return;
  1177. }
  1178. CGPath &path = LOCPLINT->paths[h];
  1179. terrain.currentPath = &path;
  1180. int3 dst = h->getPosition(false) + dir;
  1181. if(dst != verifyPos(dst) || !LOCPLINT->cb->getPathsInfo(h)->getPath(path, dst))
  1182. {
  1183. terrain.currentPath = nullptr;
  1184. return;
  1185. }
  1186. if (path.nodes.size() > 2)
  1187. updateMoveHero(h);
  1188. else
  1189. if(!path.nodes[0].turns)
  1190. LOCPLINT->moveHero(h, path);
  1191. }
  1192. return;
  1193. }
  1194. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1195. && LOCPLINT->ctrlPressed()
  1196. )
  1197. scrollingDir |= Dir;
  1198. else
  1199. scrollingDir &= ~Dir;
  1200. }
  1201. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1202. {
  1203. if(down)
  1204. {
  1205. CRClickPopup::createAndPush(text);
  1206. }
  1207. }
  1208. int3 CAdvMapInt::verifyPos(int3 ver)
  1209. {
  1210. if (ver.x<0)
  1211. ver.x=0;
  1212. if (ver.y<0)
  1213. ver.y=0;
  1214. if (ver.z<0)
  1215. ver.z=0;
  1216. if (ver.x>=CGI->mh->sizes.x)
  1217. ver.x=CGI->mh->sizes.x-1;
  1218. if (ver.y>=CGI->mh->sizes.y)
  1219. ver.y=CGI->mh->sizes.y-1;
  1220. if (ver.z>=CGI->mh->sizes.z)
  1221. ver.z=CGI->mh->sizes.z-1;
  1222. return ver;
  1223. }
  1224. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  1225. {
  1226. assert(sel);
  1227. LOCPLINT->setSelection(sel);
  1228. selection = sel;
  1229. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1230. {
  1231. auto pos = sel->visitablePos();
  1232. auto tile = LOCPLINT->cb->getTile(pos);
  1233. if(tile)
  1234. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  1235. }
  1236. if(centerView)
  1237. centerOn(sel);
  1238. terrain.currentPath = nullptr;
  1239. if(sel->ID==Obj::TOWN)
  1240. {
  1241. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1242. infoBar.showTownSelection(town);
  1243. townList.select(town);
  1244. heroList.select(nullptr);
  1245. updateSleepWake(nullptr);
  1246. updateMoveHero(nullptr);
  1247. updateSpellbook(nullptr);
  1248. }
  1249. else //hero selected
  1250. {
  1251. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1252. infoBar.showHeroSelection(hero);
  1253. heroList.select(hero);
  1254. townList.select(nullptr);
  1255. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1256. updateSleepWake(hero);
  1257. updateMoveHero(hero);
  1258. updateSpellbook(hero);
  1259. }
  1260. townList.redraw();
  1261. heroList.redraw();
  1262. }
  1263. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1264. {
  1265. #ifdef VCMI_ANDROID
  1266. if(swipeEnabled)
  1267. return;
  1268. #endif
  1269. // adventure map scrolling with mouse
  1270. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1271. if(!isCtrlKeyDown() && isActive() && mode == EAdvMapMode::NORMAL)
  1272. {
  1273. if(sEvent.x<15)
  1274. {
  1275. scrollingDir |= LEFT;
  1276. }
  1277. else
  1278. {
  1279. scrollingDir &= ~LEFT;
  1280. }
  1281. if(sEvent.x>screen->w-15)
  1282. {
  1283. scrollingDir |= RIGHT;
  1284. }
  1285. else
  1286. {
  1287. scrollingDir &= ~RIGHT;
  1288. }
  1289. if(sEvent.y<15)
  1290. {
  1291. scrollingDir |= UP;
  1292. }
  1293. else
  1294. {
  1295. scrollingDir &= ~UP;
  1296. }
  1297. if(sEvent.y>screen->h-15)
  1298. {
  1299. scrollingDir |= DOWN;
  1300. }
  1301. else
  1302. {
  1303. scrollingDir &= ~DOWN;
  1304. }
  1305. }
  1306. }
  1307. bool CAdvMapInt::isActive()
  1308. {
  1309. return active & ~CIntObject::KEYBOARD;
  1310. }
  1311. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1312. {
  1313. state = WAITING;
  1314. }
  1315. void CAdvMapInt::setPlayer(PlayerColor Player)
  1316. {
  1317. player = Player;
  1318. graphics->blueToPlayersAdv(bg,player);
  1319. panelMain->setPlayerColor(player);
  1320. panelWorldView->setPlayerColor(player);
  1321. panelWorldView->recolorIcons(player, player.getNum() * 19);
  1322. resdatabar.background->colorize(player);
  1323. }
  1324. void CAdvMapInt::startTurn()
  1325. {
  1326. state = INGAME;
  1327. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  1328. || settings["session"]["spectate"].Bool())
  1329. {
  1330. adjustActiveness(false);
  1331. minimap.setAIRadar(false);
  1332. }
  1333. }
  1334. void CAdvMapInt::endingTurn()
  1335. {
  1336. if(settings["session"]["spectate"].Bool())
  1337. return;
  1338. if(LOCPLINT->cingconsole->active)
  1339. LOCPLINT->cingconsole->deactivate();
  1340. LOCPLINT->makingTurn = false;
  1341. LOCPLINT->cb->endTurn();
  1342. CCS->soundh->ambientStopAllChannels();
  1343. }
  1344. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1345. {
  1346. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1347. if (bobjs.empty())
  1348. return nullptr;
  1349. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1350. /*
  1351. if (bobjs.back()->ID == Obj::HERO)
  1352. return bobjs.back();
  1353. else
  1354. return bobjs.front();*/
  1355. }
  1356. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1357. {
  1358. if(mode != EAdvMapMode::NORMAL)
  1359. return;
  1360. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1361. return;
  1362. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1363. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1364. int3 selPos = selection->getSightCenter();
  1365. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1366. {
  1367. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1368. switch(spellBeingCasted->id)
  1369. {
  1370. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1371. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1372. leaveCastingMode(true, mapPos);
  1373. break;
  1374. case SpellID::DIMENSION_DOOR:
  1375. if(!tile || tile->isClear(heroTile))
  1376. leaveCastingMode(true, mapPos);
  1377. break;
  1378. }
  1379. return;
  1380. }
  1381. //check if we can select this object
  1382. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1383. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1384. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1385. {
  1386. assert(!terrain.currentPath); //path can be active only when hero is selected
  1387. if(selection == topBlocking) //selected town clicked
  1388. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1389. else if(canSelect)
  1390. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1391. return;
  1392. }
  1393. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1394. {
  1395. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1396. if(currentHero == topBlocking) //clicked selected hero
  1397. {
  1398. LOCPLINT->openHeroWindow(currentHero);
  1399. return;
  1400. }
  1401. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1402. {
  1403. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1404. return;
  1405. }
  1406. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1407. {
  1408. if(terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1409. {
  1410. if(CGI->mh->canStartHeroMovement())
  1411. LOCPLINT->moveHero(currentHero, *terrain.currentPath);
  1412. return;
  1413. }
  1414. else //remove old path and find a new one if we clicked on accessible tile
  1415. {
  1416. CGPath &path = LOCPLINT->paths[currentHero];
  1417. CGPath newpath;
  1418. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(newpath, mapPos); //try getting path, erase if failed
  1419. if(gotPath && newpath.nodes.size())
  1420. path = newpath;
  1421. if(path.nodes.size())
  1422. terrain.currentPath = &path;
  1423. else
  1424. LOCPLINT->eraseCurrentPathOf(currentHero);
  1425. updateMoveHero(currentHero);
  1426. }
  1427. }
  1428. } //end of hero is selected "case"
  1429. else
  1430. {
  1431. throw std::runtime_error("Nothing is selected...");
  1432. }
  1433. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1434. {
  1435. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1436. }
  1437. }
  1438. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1439. {
  1440. if(mode != EAdvMapMode::NORMAL //disable in world view
  1441. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1442. return;
  1443. if(!LOCPLINT->cb->isVisible(mapPos))
  1444. {
  1445. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1446. statusbar.clear();
  1447. return;
  1448. }
  1449. auto objRelations = PlayerRelations::ALLIES;
  1450. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1451. if(objAtTile)
  1452. {
  1453. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1454. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1455. boost::replace_all(text,"\n"," ");
  1456. statusbar.setText(text);
  1457. }
  1458. else
  1459. {
  1460. std::string hlp;
  1461. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1462. statusbar.setText(hlp);
  1463. }
  1464. if(spellBeingCasted)
  1465. {
  1466. switch(spellBeingCasted->id)
  1467. {
  1468. case SpellID::SCUTTLE_BOAT:
  1469. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1470. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1471. else
  1472. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1473. return;
  1474. case SpellID::DIMENSION_DOOR:
  1475. {
  1476. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1477. int3 hpos = selection->getSightCenter();
  1478. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1479. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1480. else
  1481. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1482. return;
  1483. }
  1484. }
  1485. }
  1486. if(selection->ID == Obj::TOWN)
  1487. {
  1488. if(objAtTile)
  1489. {
  1490. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1491. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1492. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1493. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1494. else
  1495. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1496. }
  1497. else
  1498. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1499. }
  1500. else if(const CGHeroInstance * h = curHero())
  1501. {
  1502. int3 mapPosCopy = mapPos;
  1503. const CGPathNode * pnode = LOCPLINT->cb->getPathsInfo(h)->getPathInfo(mapPosCopy);
  1504. assert(pnode);
  1505. int turns = pnode->turns;
  1506. vstd::amin(turns, 3);
  1507. switch(pnode->action)
  1508. {
  1509. case CGPathNode::NORMAL:
  1510. case CGPathNode::TELEPORT_NORMAL:
  1511. if(pnode->layer == EPathfindingLayer::LAND)
  1512. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1513. else
  1514. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1515. break;
  1516. case CGPathNode::VISIT:
  1517. case CGPathNode::BLOCKING_VISIT:
  1518. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1519. if(objAtTile && objAtTile->ID == Obj::HERO)
  1520. {
  1521. if(selection == objAtTile)
  1522. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1523. else
  1524. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1525. }
  1526. else if(pnode->layer == EPathfindingLayer::LAND)
  1527. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1528. else
  1529. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1530. break;
  1531. case CGPathNode::BATTLE:
  1532. case CGPathNode::TELEPORT_BATTLE:
  1533. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1534. break;
  1535. case CGPathNode::EMBARK:
  1536. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1537. break;
  1538. case CGPathNode::DISEMBARK:
  1539. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6);
  1540. break;
  1541. default:
  1542. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1543. {
  1544. if(objAtTile->ID == Obj::TOWN)
  1545. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1546. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1547. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1548. }
  1549. else
  1550. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1551. break;
  1552. }
  1553. }
  1554. if(ourInaccessibleShipyard(objAtTile))
  1555. {
  1556. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1557. }
  1558. }
  1559. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1560. {
  1561. if(mode != EAdvMapMode::NORMAL)
  1562. return;
  1563. if(spellBeingCasted)
  1564. {
  1565. leaveCastingMode();
  1566. return;
  1567. }
  1568. if(!LOCPLINT->cb->isVisible(mapPos))
  1569. {
  1570. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1571. return;
  1572. }
  1573. const CGObjectInstance * obj = getActiveObject(mapPos);
  1574. if(!obj)
  1575. {
  1576. // Bare or undiscovered terrain
  1577. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1578. if (tile)
  1579. {
  1580. std::string hlp;
  1581. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1582. CRClickPopup::createAndPush(hlp);
  1583. }
  1584. return;
  1585. }
  1586. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1587. }
  1588. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1589. {
  1590. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1591. spellBeingCasted = sp;
  1592. deactivate();
  1593. terrain.activate();
  1594. GH.fakeMouseMove();
  1595. }
  1596. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1597. {
  1598. assert(spellBeingCasted);
  1599. SpellID id = spellBeingCasted->id;
  1600. spellBeingCasted = nullptr;
  1601. terrain.deactivate();
  1602. activate();
  1603. if(cast)
  1604. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1605. else
  1606. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1607. }
  1608. const CGHeroInstance * CAdvMapInt::curHero() const
  1609. {
  1610. if(selection && selection->ID == Obj::HERO)
  1611. return static_cast<const CGHeroInstance *>(selection);
  1612. else
  1613. return nullptr;
  1614. }
  1615. const CGTownInstance * CAdvMapInt::curTown() const
  1616. {
  1617. if(selection && selection->ID == Obj::TOWN)
  1618. return static_cast<const CGTownInstance *>(selection);
  1619. else
  1620. return nullptr;
  1621. }
  1622. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1623. {
  1624. const IShipyard *ret = IShipyard::castFrom(obj);
  1625. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1626. return nullptr;
  1627. return ret;
  1628. }
  1629. void CAdvMapInt::aiTurnStarted()
  1630. {
  1631. if(settings["session"]["spectate"].Bool())
  1632. return;
  1633. adjustActiveness(true);
  1634. CCS->musich->playMusicFromSet("enemy-turn", true);
  1635. adventureInt->minimap.setAIRadar(true);
  1636. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1637. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1638. }
  1639. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1640. {
  1641. bool wasActive = isActive();
  1642. if(wasActive)
  1643. deactivate();
  1644. adventureInt->duringAITurn = aiTurnStart;
  1645. if(wasActive)
  1646. activate();
  1647. }
  1648. void CAdvMapInt::quickCombatLock()
  1649. {
  1650. if(!duringAITurn)
  1651. deactivate();
  1652. }
  1653. void CAdvMapInt::quickCombatUnlock()
  1654. {
  1655. if(!duringAITurn)
  1656. activate();
  1657. }
  1658. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale)
  1659. {
  1660. if (mode != newMode)
  1661. {
  1662. mode = newMode;
  1663. switch (mode)
  1664. {
  1665. case EAdvMapMode::NORMAL:
  1666. panelMain->activate();
  1667. panelWorldView->deactivate();
  1668. activeMapPanel = panelMain;
  1669. townList.activate();
  1670. heroList.activate();
  1671. infoBar.activate();
  1672. worldViewOptions.clear();
  1673. break;
  1674. case EAdvMapMode::WORLD_VIEW:
  1675. panelMain->deactivate();
  1676. panelWorldView->activate();
  1677. activeMapPanel = panelWorldView;
  1678. townList.deactivate();
  1679. heroList.deactivate();
  1680. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1681. infoBar.deactivate();
  1682. break;
  1683. }
  1684. worldViewScale = newScale;
  1685. redraw();
  1686. }
  1687. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1688. {
  1689. worldViewScale = newScale;
  1690. redraw();
  1691. }
  1692. }
  1693. CAdventureOptions::CAdventureOptions()
  1694. : CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1695. {
  1696. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1697. viewWorld = std::make_shared<CButton>(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_v);
  1698. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1699. exit = std::make_shared<CButton>(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1700. exit->assignedKeys.insert(SDLK_ESCAPE);
  1701. scenInfo = std::make_shared<CButton>(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_i);
  1702. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1703. puzzle = std::make_shared<CButton>(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_p);
  1704. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1705. dig = std::make_shared<CButton>(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_d);
  1706. if(const CGHeroInstance *h = adventureInt->curHero())
  1707. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1708. else
  1709. dig->block(true);
  1710. }
  1711. void CAdventureOptions::showScenarioInfo()
  1712. {
  1713. if(LOCPLINT->cb->getStartInfo()->campState)
  1714. {
  1715. GH.pushInt(new CBonusSelection());
  1716. }
  1717. else
  1718. {
  1719. GH.pushInt(new CScenarioInfoScreen());
  1720. }
  1721. }
  1722. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1723. {
  1724. clear();
  1725. }
  1726. void CAdvMapInt::WorldViewOptions::clear()
  1727. {
  1728. showAllTerrain = false;
  1729. iconPositions.clear();
  1730. }
  1731. void CAdvMapInt::WorldViewOptions::adjustDrawingInfo(MapDrawingInfo& info)
  1732. {
  1733. info.showAllTerrain = showAllTerrain;
  1734. info.additionalIcons = &iconPositions;
  1735. }