CAdvmapInterface.cpp 52 KB

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