CAdvmapInterface.cpp 52 KB

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