CAdvmapInterface.cpp 51 KB

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