CAdvmapInterface.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  330. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  331. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192) )
  332. {
  333. duringAITurn = false;
  334. state = NA;
  335. spellBeingCasted = nullptr;
  336. pos.x = pos.y = 0;
  337. pos.w = screen->w;
  338. pos.h = screen->h;
  339. position = int3(0,0,0);
  340. selection = nullptr;
  341. townList.onSelect = boost::bind(&CAdvMapInt::selectionChanged,this);
  342. adventureInt=this;
  343. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  344. scrollingDir = 0;
  345. updateScreen = false;
  346. anim=0;
  347. animValHitCount=0; //animation frame
  348. heroAnim=0;
  349. heroAnimValHitCount=0; // hero animation frame
  350. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  351. {
  352. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  353. }
  354. auto makeButton = [&] (int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> CButton *
  355. {
  356. auto button = new CButton(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  357. for (auto image : info.additionalDefs)
  358. button->addImage(image);
  359. return button;
  360. };
  361. kingOverview = makeButton(293, boost::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  362. underground = makeButton(294, boost::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  363. questlog = makeButton(295, boost::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  364. sleepWake = makeButton(296, boost::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  365. moveHero = makeButton(297, boost::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  366. spellbook = makeButton(298, boost::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  367. advOptions = makeButton(299, boost::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  368. sysOptions = makeButton(300, boost::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  369. nextHero = makeButton(301, boost::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  370. endTurn = makeButton(302, boost::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  371. setPlayer(LOCPLINT->playerID);
  372. underground->block(!CGI->mh->map->twoLevel);
  373. addUsedEvents(MOVE);
  374. }
  375. CAdvMapInt::~CAdvMapInt()
  376. {
  377. SDL_FreeSurface(bg);
  378. for(int i=0; i<gems.size(); i++)
  379. delete gems[i];
  380. }
  381. void CAdvMapInt::fshowOverview()
  382. {
  383. GH.pushInt(new CKingdomInterface);
  384. }
  385. void CAdvMapInt::fswitchLevel()
  386. {
  387. if(!CGI->mh->map->twoLevel)
  388. return;
  389. if (position.z)
  390. {
  391. position.z--;
  392. underground->setIndex(0,true);
  393. underground->showAll(screenBuf);
  394. }
  395. else
  396. {
  397. underground->setIndex(1,true);
  398. position.z++;
  399. underground->showAll(screenBuf);
  400. }
  401. updateScreen = true;
  402. minimap.setLevel(position.z);
  403. }
  404. void CAdvMapInt::fshowQuestlog()
  405. {
  406. LOCPLINT->showQuestLog();
  407. }
  408. void CAdvMapInt::fsleepWake()
  409. {
  410. const CGHeroInstance *h = curHero();
  411. if (!h)
  412. return;
  413. bool newSleep = !isHeroSleeping(h);
  414. setHeroSleeping(h, newSleep);
  415. updateSleepWake(h);
  416. if (newSleep)
  417. {
  418. fnextHero();
  419. //moveHero.block(true);
  420. //uncomment to enable original HoMM3 behaviour:
  421. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  422. }
  423. }
  424. void CAdvMapInt::fmoveHero()
  425. {
  426. const CGHeroInstance *h = curHero();
  427. if (!h || !terrain.currentPath)
  428. return;
  429. LOCPLINT->moveHero(h, *terrain.currentPath);
  430. }
  431. void CAdvMapInt::fshowSpellbok()
  432. {
  433. if (!curHero()) //checking necessary values
  434. return;
  435. centerOn(selection);
  436. auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  437. GH.pushInt(spellWindow);
  438. }
  439. void CAdvMapInt::fadventureOPtions()
  440. {
  441. GH.pushInt(new CAdventureOptions);
  442. }
  443. void CAdvMapInt::fsystemOptions()
  444. {
  445. GH.pushInt(new CSystemOptionsWindow());
  446. }
  447. void CAdvMapInt::fnextHero()
  448. {
  449. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  450. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  451. if (next < 0)
  452. return;
  453. select(LOCPLINT->wanderingHeroes[next], true);
  454. }
  455. void CAdvMapInt::fendTurn()
  456. {
  457. if(!LOCPLINT->makingTurn)
  458. return;
  459. if ( settings["adventure"]["heroReminder"].Bool())
  460. {
  461. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  462. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  463. {
  464. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], boost::bind(&CAdvMapInt::endingTurn, this), 0, false);
  465. return;
  466. }
  467. }
  468. endingTurn();
  469. }
  470. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  471. {
  472. sleepWake->block(!h);
  473. if (!h)
  474. return;
  475. bool state = isHeroSleeping(h);
  476. sleepWake->setIndex(state ? 1 : 0, true);
  477. sleepWake->assignedKeys.clear();
  478. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  479. }
  480. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  481. {
  482. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  483. if (hasPath == boost::indeterminate)
  484. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  485. if (!h)
  486. {
  487. moveHero->block(true);
  488. return;
  489. }
  490. moveHero->block(!hasPath || (h->movement == 0));
  491. }
  492. int CAdvMapInt::getNextHeroIndex(int startIndex)
  493. {
  494. if (LOCPLINT->wanderingHeroes.size() == 0)
  495. return -1;
  496. if (startIndex < 0)
  497. startIndex = 0;
  498. int i = startIndex;
  499. do
  500. {
  501. i++;
  502. if (i >= LOCPLINT->wanderingHeroes.size())
  503. i = 0;
  504. }
  505. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  506. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  507. return i;
  508. else
  509. return -1;
  510. }
  511. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  512. {
  513. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  514. int next = getNextHeroIndex(start);
  515. if (next < 0)
  516. {
  517. nextHero->block(true);
  518. return;
  519. }
  520. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  521. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  522. nextHero->block(noActiveHeroes);
  523. }
  524. void CAdvMapInt::activate()
  525. {
  526. CIntObject::activate();
  527. if (!(active & KEYBOARD))
  528. CIntObject::activate(KEYBOARD);
  529. screenBuf = screen;
  530. GH.statusbar = &statusbar;
  531. if(!duringAITurn)
  532. {
  533. kingOverview->activate();
  534. underground->activate();
  535. questlog->activate();
  536. sleepWake->activate();
  537. moveHero->activate();
  538. spellbook->activate();
  539. sysOptions->activate();
  540. advOptions->activate();
  541. nextHero->activate();
  542. endTurn->activate();
  543. minimap.activate();
  544. heroList.activate();
  545. townList.activate();
  546. terrain.activate();
  547. infoBar.activate();
  548. LOCPLINT->cingconsole->activate();
  549. GH.fakeMouseMove(); //to restore the cursor
  550. }
  551. }
  552. void CAdvMapInt::deactivate()
  553. {
  554. CIntObject::deactivate();
  555. if(!duringAITurn)
  556. {
  557. scrollingDir = 0;
  558. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  559. kingOverview->deactivate();
  560. underground->deactivate();
  561. questlog->deactivate();
  562. sleepWake->deactivate();
  563. moveHero->deactivate();
  564. spellbook->deactivate();
  565. advOptions->deactivate();
  566. sysOptions->deactivate();
  567. nextHero->deactivate();
  568. endTurn->deactivate();
  569. minimap.deactivate();
  570. heroList.deactivate();
  571. townList.deactivate();
  572. terrain.deactivate();
  573. infoBar.deactivate();
  574. if(LOCPLINT)
  575. LOCPLINT->cingconsole->deactivate();
  576. }
  577. }
  578. void CAdvMapInt::showAll(SDL_Surface * to)
  579. {
  580. blitAt(bg,0,0,to);
  581. if(state != INGAME)
  582. return;
  583. kingOverview->showAll(to);
  584. underground->showAll(to);
  585. questlog->showAll(to);
  586. sleepWake->showAll(to);
  587. moveHero->showAll(to);
  588. spellbook->showAll(to);
  589. advOptions->showAll(to);
  590. sysOptions->showAll(to);
  591. nextHero->showAll(to);
  592. endTurn->showAll(to);
  593. minimap.showAll(to);
  594. heroList.showAll(to);
  595. townList.showAll(to);
  596. updateScreen = true;
  597. show(to);
  598. resdatabar.draw(to);
  599. statusbar.show(to);
  600. infoBar.showAll(to);
  601. LOCPLINT->cingconsole->showAll(to);
  602. }
  603. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  604. {
  605. if (!hero)
  606. return false;
  607. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  608. }
  609. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  610. {
  611. if (sleep)
  612. LOCPLINT->sleepingHeroes += hero;
  613. else
  614. LOCPLINT->sleepingHeroes -= hero;
  615. updateNextHero(nullptr);
  616. }
  617. void CAdvMapInt::show(SDL_Surface * to)
  618. {
  619. if(state != INGAME)
  620. return;
  621. ++animValHitCount; //for animations
  622. if(animValHitCount == 8)
  623. {
  624. CGI->mh->updateWater();
  625. animValHitCount = 0;
  626. ++anim;
  627. updateScreen = true;
  628. }
  629. ++heroAnim;
  630. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  631. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  632. if((animValHitCount % (4/scrollSpeed)) == 0
  633. && (
  634. (GH.topInt() == this)
  635. || isCtrlKeyDown()
  636. )
  637. )
  638. {
  639. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  640. position.x--;
  641. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  642. position.x++;
  643. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  644. position.y--;
  645. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  646. position.y++;
  647. if(scrollingDir)
  648. {
  649. updateScreen = true;
  650. minimap.redraw();
  651. }
  652. }
  653. if(updateScreen)
  654. {
  655. int3 betterPos = LOCPLINT->repairScreenPos(position);
  656. if (betterPos != position)
  657. {
  658. logGlobal->warnStream() << "Incorrect position for adventure map!";
  659. position = betterPos;
  660. }
  661. terrain.show(to);
  662. for(int i=0;i<4;i++)
  663. blitAt(gems[i]->ourImages[LOCPLINT->playerID.getNum()].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  664. updateScreen=false;
  665. LOCPLINT->cingconsole->showAll(to);
  666. }
  667. infoBar.show(to);
  668. statusbar.showAll(to);
  669. }
  670. void CAdvMapInt::selectionChanged()
  671. {
  672. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  673. if (selection != to)
  674. select(to);
  675. }
  676. void CAdvMapInt::centerOn(int3 on)
  677. {
  678. bool switchedLevels = on.z != position.z;
  679. on.x -= CGI->mh->frameW;
  680. on.y -= CGI->mh->frameH;
  681. on = LOCPLINT->repairScreenPos(on);
  682. position = on;
  683. updateScreen=true;
  684. underground->setIndex(on.z,true); //change underground switch button image
  685. underground->redraw();
  686. if (switchedLevels)
  687. minimap.setLevel(position.z);
  688. }
  689. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  690. {
  691. centerOn(obj->getSightCenter());
  692. }
  693. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  694. {
  695. ui8 Dir = 0;
  696. int k = key.keysym.sym;
  697. const CGHeroInstance *h = curHero(); //selected hero
  698. const CGTownInstance *t = curTown(); //selected town
  699. switch(k)
  700. {
  701. case SDLK_g:
  702. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  703. return;
  704. {
  705. //find first town with tavern
  706. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  707. {
  708. return town->hasBuilt(BuildingID::TAVERN);
  709. });
  710. if(itr != LOCPLINT->towns.end())
  711. LOCPLINT->showThievesGuildWindow(*itr);
  712. else
  713. LOCPLINT->showInfoDialog("No available town with tavern!");
  714. }
  715. return;
  716. case SDLK_i:
  717. if(isActive())
  718. CAdventureOptions::showScenarioInfo();
  719. return;
  720. case SDLK_l:
  721. if(isActive())
  722. LOCPLINT->proposeLoadingGame();
  723. return;
  724. case SDLK_s:
  725. if(isActive())
  726. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  727. return;
  728. case SDLK_d:
  729. {
  730. if(h && isActive() && key.state == SDL_PRESSED)
  731. LOCPLINT->tryDiggging(h);
  732. return;
  733. }
  734. case SDLK_p:
  735. if(isActive())
  736. LOCPLINT->showPuzzleMap();
  737. return;
  738. case SDLK_r:
  739. if(isActive() && LOCPLINT->ctrlPressed())
  740. {
  741. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  742. []{ LOCPLINT->sendCustomEvent(RESTART_GAME); },
  743. []{}, true);
  744. }
  745. return;
  746. case SDLK_SPACE: //space - try to revisit current object with selected hero
  747. {
  748. if(!isActive())
  749. return;
  750. if(h && key.state == SDL_PRESSED)
  751. {
  752. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  753. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  754. //this thread leaves scope and tries to lock pim while holding gs,
  755. //network thread tries to lock gs (appluy cl) while holding pim
  756. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  757. LOCPLINT->cb->moveHero(h,h->pos);
  758. }
  759. }
  760. return;
  761. case SDLK_RETURN:
  762. {
  763. if(!isActive() || !selection || key.state != SDL_PRESSED)
  764. return;
  765. if(h)
  766. LOCPLINT->openHeroWindow(h);
  767. else if(t)
  768. LOCPLINT->openTownWindow(t);
  769. return;
  770. }
  771. case SDLK_ESCAPE:
  772. {
  773. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  774. return;
  775. leaveCastingMode();
  776. return;
  777. }
  778. case SDLK_t:
  779. {
  780. //act on key down if marketplace windows is not already opened
  781. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  782. return;
  783. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  784. {
  785. //check if we have any marketplace
  786. const CGTownInstance *townWithMarket = nullptr;
  787. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  788. {
  789. if(t->hasBuilt(BuildingID::MARKETPLACE))
  790. {
  791. townWithMarket = t;
  792. break;
  793. }
  794. }
  795. if(townWithMarket) //if any town has marketplace, open window
  796. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  797. else //if not - complain
  798. LOCPLINT->showInfoDialog("No available marketplace!");
  799. }
  800. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  801. {
  802. townList.selectNext();
  803. }
  804. return;
  805. }
  806. default:
  807. {
  808. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  809. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  810. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  811. //numpad arrow
  812. if(CGuiHandler::isArrowKey(SDLKey(k)))
  813. k = CGuiHandler::arrowToNum(SDLKey(k));
  814. #ifdef VCMI_SDL1
  815. k -= SDLK_KP0 + 1;
  816. #else
  817. k -= SDLK_KP_1;
  818. #endif // VCMI_SDL1
  819. if(k < 0 || k > 8)
  820. return;
  821. int3 dir = directions[k];
  822. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  823. {
  824. Dir = (dir.x<0 ? LEFT : 0) |
  825. (dir.x>0 ? RIGHT : 0) |
  826. (dir.y<0 ? UP : 0) |
  827. (dir.y>0 ? DOWN : 0) ;
  828. break;
  829. }
  830. if(!h || key.state != SDL_PRESSED)
  831. break;
  832. if(k == 4)
  833. {
  834. centerOn(h);
  835. return;
  836. }
  837. CGPath &path = LOCPLINT->paths[h];
  838. terrain.currentPath = &path;
  839. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  840. {
  841. terrain.currentPath = nullptr;
  842. return;
  843. }
  844. if (path.nodes.size() > 2)
  845. updateMoveHero(h);
  846. else
  847. if(!path.nodes[0].turns)
  848. LOCPLINT->moveHero(h, path);
  849. }
  850. return;
  851. }
  852. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  853. && LOCPLINT->ctrlPressed()
  854. )
  855. scrollingDir |= Dir;
  856. else
  857. scrollingDir &= ~Dir;
  858. }
  859. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  860. {
  861. if(down)
  862. {
  863. CRClickPopup::createAndPush(text);
  864. }
  865. }
  866. int3 CAdvMapInt::verifyPos(int3 ver)
  867. {
  868. if (ver.x<0)
  869. ver.x=0;
  870. if (ver.y<0)
  871. ver.y=0;
  872. if (ver.z<0)
  873. ver.z=0;
  874. if (ver.x>=CGI->mh->sizes.x)
  875. ver.x=CGI->mh->sizes.x-1;
  876. if (ver.y>=CGI->mh->sizes.y)
  877. ver.y=CGI->mh->sizes.y-1;
  878. if (ver.z>=CGI->mh->sizes.z)
  879. ver.z=CGI->mh->sizes.z-1;
  880. return ver;
  881. }
  882. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  883. {
  884. assert(sel);
  885. LOCPLINT->cb->setSelection(sel);
  886. selection = sel;
  887. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  888. {
  889. auto pos = sel->visitablePos();
  890. auto tile = LOCPLINT->cb->getTile(pos);
  891. if(tile)
  892. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  893. }
  894. if(centerView)
  895. centerOn(sel);
  896. terrain.currentPath = nullptr;
  897. if(sel->ID==Obj::TOWN)
  898. {
  899. auto town = dynamic_cast<const CGTownInstance*>(sel);
  900. infoBar.showTownSelection(town);
  901. townList.select(town);
  902. heroList.select(nullptr);
  903. updateSleepWake(nullptr);
  904. updateMoveHero(nullptr);
  905. }
  906. else //hero selected
  907. {
  908. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  909. infoBar.showHeroSelection(hero);
  910. heroList.select(hero);
  911. townList.select(nullptr);
  912. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  913. updateSleepWake(hero);
  914. updateMoveHero(hero);
  915. }
  916. townList.redraw();
  917. heroList.redraw();
  918. }
  919. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  920. {
  921. //adventure map scrolling with mouse
  922. if(!isCtrlKeyDown() && isActive())
  923. {
  924. if(sEvent.x<15)
  925. {
  926. scrollingDir |= LEFT;
  927. }
  928. else
  929. {
  930. scrollingDir &= ~LEFT;
  931. }
  932. if(sEvent.x>screen->w-15)
  933. {
  934. scrollingDir |= RIGHT;
  935. }
  936. else
  937. {
  938. scrollingDir &= ~RIGHT;
  939. }
  940. if(sEvent.y<15)
  941. {
  942. scrollingDir |= UP;
  943. }
  944. else
  945. {
  946. scrollingDir &= ~UP;
  947. }
  948. if(sEvent.y>screen->h-15)
  949. {
  950. scrollingDir |= DOWN;
  951. }
  952. else
  953. {
  954. scrollingDir &= ~DOWN;
  955. }
  956. }
  957. }
  958. bool CAdvMapInt::isActive()
  959. {
  960. return active & ~CIntObject::KEYBOARD;
  961. }
  962. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  963. {
  964. state = WAITING;
  965. }
  966. void CAdvMapInt::setPlayer(PlayerColor Player)
  967. {
  968. player = Player;
  969. graphics->blueToPlayersAdv(bg,player);
  970. kingOverview->setPlayerColor(player);
  971. underground->setPlayerColor(player);
  972. questlog->setPlayerColor(player);
  973. sleepWake->setPlayerColor(player);
  974. moveHero->setPlayerColor(player);
  975. spellbook->setPlayerColor(player);
  976. sysOptions->setPlayerColor(player);
  977. advOptions->setPlayerColor(player);
  978. nextHero->setPlayerColor(player);
  979. endTurn->setPlayerColor(player);
  980. graphics->blueToPlayersAdv(resdatabar.bg,player);
  981. //heroList.updateHList();
  982. //townList.genList();
  983. }
  984. void CAdvMapInt::startTurn()
  985. {
  986. state = INGAME;
  987. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  988. {
  989. adjustActiveness(false);
  990. minimap.setAIRadar(false);
  991. }
  992. }
  993. void CAdvMapInt::endingTurn()
  994. {
  995. if(LOCPLINT->cingconsole->active)
  996. LOCPLINT->cingconsole->deactivate();
  997. LOCPLINT->makingTurn = false;
  998. LOCPLINT->cb->endTurn();
  999. }
  1000. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1001. {
  1002. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1003. if (bobjs.empty())
  1004. return nullptr;
  1005. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1006. /*
  1007. if (bobjs.back()->ID == Obj::HERO)
  1008. return bobjs.back();
  1009. else
  1010. return bobjs.front();*/
  1011. }
  1012. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1013. {
  1014. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1015. return;
  1016. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1017. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1018. int3 selPos = selection->getSightCenter();
  1019. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1020. {
  1021. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1022. switch(spellBeingCasted->id)
  1023. {
  1024. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1025. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1026. leaveCastingMode(true, mapPos);
  1027. break;
  1028. case SpellID::DIMENSION_DOOR:
  1029. if(!tile || tile->isClear(heroTile))
  1030. leaveCastingMode(true, mapPos);
  1031. break;
  1032. }
  1033. return;
  1034. }
  1035. //check if we can select this object
  1036. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1037. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1038. if (selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1039. {
  1040. assert(!terrain.currentPath); //path can be active only when hero is selected
  1041. if(selection == topBlocking) //selected town clicked
  1042. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1043. else if ( canSelect )
  1044. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1045. return;
  1046. }
  1047. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1048. {
  1049. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mapPos);
  1050. if(currentHero == topBlocking) //clicked selected hero
  1051. {
  1052. LOCPLINT->openHeroWindow(currentHero);
  1053. return;
  1054. }
  1055. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1056. {
  1057. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1058. return;
  1059. }
  1060. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1061. {
  1062. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1063. {
  1064. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1065. return;
  1066. }
  1067. 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
  1068. {
  1069. CGPath &path = LOCPLINT->paths[currentHero];
  1070. terrain.currentPath = &path;
  1071. bool gotPath = LOCPLINT->cb->getPath2(mapPos, path); //try getting path, erase if failed
  1072. updateMoveHero(currentHero);
  1073. if (!gotPath)
  1074. LOCPLINT->eraseCurrentPathOf(currentHero);
  1075. else
  1076. return;
  1077. }
  1078. }
  1079. } //end of hero is selected "case"
  1080. else
  1081. {
  1082. throw std::runtime_error("Nothing is selected...");
  1083. }
  1084. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1085. {
  1086. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1087. }
  1088. }
  1089. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1090. {
  1091. if(!LOCPLINT->cb->isVisible(mapPos))
  1092. {
  1093. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1094. statusbar.clear();
  1095. return;
  1096. }
  1097. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1098. if (objAtTile)
  1099. {
  1100. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1101. boost::replace_all(text,"\n"," ");
  1102. statusbar.setText(text);
  1103. }
  1104. else
  1105. {
  1106. std::string hlp;
  1107. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1108. statusbar.setText(hlp);
  1109. }
  1110. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(mapPos);
  1111. int turns = pnode->turns;
  1112. vstd::amin(turns, 3);
  1113. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1114. return;
  1115. if(spellBeingCasted)
  1116. {
  1117. switch(spellBeingCasted->id)
  1118. {
  1119. case SpellID::SCUTTLE_BOAT:
  1120. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1121. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1122. else
  1123. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1124. return;
  1125. case SpellID::DIMENSION_DOOR:
  1126. {
  1127. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1128. int3 hpos = selection->getSightCenter();
  1129. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1130. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1131. else
  1132. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1133. return;
  1134. }
  1135. }
  1136. }
  1137. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->getGuardingCreaturePosition(mapPos));
  1138. if(selection->ID == Obj::TOWN)
  1139. {
  1140. if(objAtTile)
  1141. {
  1142. if(objAtTile->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner) != PlayerRelations::ENEMIES)
  1143. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1144. else if(objAtTile->ID == Obj::HERO && objAtTile->tempOwner == LOCPLINT->playerID)
  1145. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1146. else
  1147. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1148. }
  1149. else
  1150. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1151. }
  1152. else if(const CGHeroInstance *h = curHero())
  1153. {
  1154. bool accessible = pnode->turns < 255;
  1155. if(objAtTile)
  1156. {
  1157. if(objAtTile->ID == Obj::HERO)
  1158. {
  1159. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1160. {
  1161. if(accessible)
  1162. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1163. else
  1164. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1165. }
  1166. else //our or ally hero
  1167. {
  1168. if(selection == objAtTile)
  1169. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1170. else if(accessible)
  1171. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1172. else
  1173. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1174. }
  1175. }
  1176. else if(objAtTile->ID == Obj::TOWN)
  1177. {
  1178. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1179. {
  1180. if(accessible)
  1181. {
  1182. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1183. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1184. if (townObj && !townObj->armedGarrison())
  1185. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1186. else
  1187. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1188. }
  1189. else
  1190. {
  1191. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1192. }
  1193. }
  1194. else //our or ally town
  1195. {
  1196. if(accessible)
  1197. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1198. else
  1199. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1200. }
  1201. }
  1202. else if(objAtTile->ID == Obj::BOAT)
  1203. {
  1204. if(accessible)
  1205. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1206. else
  1207. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1208. }
  1209. else if (objAtTile->ID == Obj::GARRISON || objAtTile->ID == Obj::GARRISON2)
  1210. {
  1211. if (accessible)
  1212. {
  1213. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1214. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1215. if (garrObj && garrObj->stacksCount()
  1216. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1217. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1218. else
  1219. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1220. }
  1221. else
  1222. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1223. }
  1224. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1225. {
  1226. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1227. }
  1228. else
  1229. {
  1230. if(accessible)
  1231. {
  1232. if(pnode->land)
  1233. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1234. else
  1235. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1236. }
  1237. else
  1238. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1239. }
  1240. }
  1241. else //no objs
  1242. {
  1243. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1244. {
  1245. if (guardingCreature)
  1246. {
  1247. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1248. }
  1249. else
  1250. {
  1251. if(pnode->land)
  1252. {
  1253. if(LOCPLINT->cb->getTile(h->getPosition(false))->terType != ETerrainType::WATER)
  1254. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1255. else
  1256. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6); //anchor
  1257. }
  1258. else
  1259. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1260. }
  1261. }
  1262. else
  1263. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1264. }
  1265. }
  1266. if(ourInaccessibleShipyard(objAtTile))
  1267. {
  1268. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1269. }
  1270. }
  1271. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1272. {
  1273. if(spellBeingCasted)
  1274. {
  1275. leaveCastingMode();
  1276. return;
  1277. }
  1278. if(!LOCPLINT->cb->isVisible(mapPos))
  1279. {
  1280. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1281. return;
  1282. }
  1283. const CGObjectInstance * obj = getActiveObject(mapPos);
  1284. if(!obj)
  1285. {
  1286. // Bare or undiscovered terrain
  1287. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1288. if (tile)
  1289. {
  1290. std::string hlp;
  1291. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1292. CRClickPopup::createAndPush(hlp);
  1293. }
  1294. return;
  1295. }
  1296. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1297. }
  1298. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1299. {
  1300. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1301. spellBeingCasted = sp;
  1302. deactivate();
  1303. terrain.activate();
  1304. GH.fakeMouseMove();
  1305. }
  1306. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1307. {
  1308. assert(spellBeingCasted);
  1309. SpellID id = spellBeingCasted->id;
  1310. spellBeingCasted = nullptr;
  1311. terrain.deactivate();
  1312. activate();
  1313. if(cast)
  1314. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1315. else
  1316. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1317. }
  1318. const CGHeroInstance * CAdvMapInt::curHero() const
  1319. {
  1320. if(selection && selection->ID == Obj::HERO)
  1321. return static_cast<const CGHeroInstance *>(selection);
  1322. else
  1323. return nullptr;
  1324. }
  1325. const CGTownInstance * CAdvMapInt::curTown() const
  1326. {
  1327. if(selection && selection->ID == Obj::TOWN)
  1328. return static_cast<const CGTownInstance *>(selection);
  1329. else
  1330. return nullptr;
  1331. }
  1332. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1333. {
  1334. const IShipyard *ret = IShipyard::castFrom(obj);
  1335. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1336. return nullptr;
  1337. return ret;
  1338. }
  1339. void CAdvMapInt::aiTurnStarted()
  1340. {
  1341. adjustActiveness(true);
  1342. CCS->musich->playMusicFromSet("enemy-turn", true);
  1343. adventureInt->minimap.setAIRadar(true);
  1344. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1345. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1346. }
  1347. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1348. {
  1349. bool wasActive = isActive();
  1350. if(wasActive)
  1351. deactivate();
  1352. adventureInt->duringAITurn = aiTurnStart;
  1353. if(wasActive)
  1354. activate();
  1355. }
  1356. CAdventureOptions::CAdventureOptions():
  1357. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1358. {
  1359. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1360. exit = new CButton(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), boost::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1361. exit->assignedKeys.insert(SDLK_ESCAPE);
  1362. scenInfo = new CButton(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_i);
  1363. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1364. //viewWorld = new CButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1365. puzzle = new CButton(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_p);
  1366. puzzle->addCallback(boost::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1367. dig = new CButton(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_d);
  1368. if(const CGHeroInstance *h = adventureInt->curHero())
  1369. dig->addCallback(boost::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1370. else
  1371. dig->block(true);
  1372. }
  1373. void CAdventureOptions::showScenarioInfo()
  1374. {
  1375. auto campState = LOCPLINT->cb->getStartInfo()->campState;
  1376. if(campState)
  1377. {
  1378. GH.pushInt(new CBonusSelection(campState));
  1379. }
  1380. else
  1381. {
  1382. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1383. }
  1384. }