CAdvmapInterface.cpp 55 KB

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