CAdvmapInterface.cpp 53 KB

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