CAdvmapInterface.cpp 54 KB

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