CAdvmapInterface.cpp 45 KB

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