2
0

CAdvmapInterface.cpp 55 KB

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