CAdvmapInterface.cpp 55 KB

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