CAdvmapInterface.cpp 43 KB

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