CAdvmapInterface.cpp 52 KB

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