CAdvmapInterface.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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 = std::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, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  362. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  363. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  364. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  365. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  366. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  367. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  368. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  369. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  370. endTurn = makeButton(302, std::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], std::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. minimap.redraw();
  689. }
  690. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  691. {
  692. centerOn(obj->getSightCenter());
  693. }
  694. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  695. {
  696. ui8 Dir = 0;
  697. int k = key.keysym.sym;
  698. const CGHeroInstance *h = curHero(); //selected hero
  699. const CGTownInstance *t = curTown(); //selected town
  700. switch(k)
  701. {
  702. case SDLK_g:
  703. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  704. return;
  705. {
  706. //find first town with tavern
  707. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  708. {
  709. return town->hasBuilt(BuildingID::TAVERN);
  710. });
  711. if(itr != LOCPLINT->towns.end())
  712. LOCPLINT->showThievesGuildWindow(*itr);
  713. else
  714. LOCPLINT->showInfoDialog("No available town with tavern!");
  715. }
  716. return;
  717. case SDLK_i:
  718. if(isActive())
  719. CAdventureOptions::showScenarioInfo();
  720. return;
  721. case SDLK_l:
  722. if(isActive())
  723. LOCPLINT->proposeLoadingGame();
  724. return;
  725. case SDLK_s:
  726. if(isActive())
  727. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  728. return;
  729. case SDLK_d:
  730. {
  731. if(h && isActive() && key.state == SDL_PRESSED)
  732. LOCPLINT->tryDiggging(h);
  733. return;
  734. }
  735. case SDLK_p:
  736. if(isActive())
  737. LOCPLINT->showPuzzleMap();
  738. return;
  739. case SDLK_r:
  740. if(isActive() && LOCPLINT->ctrlPressed())
  741. {
  742. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  743. []{ LOCPLINT->sendCustomEvent(RESTART_GAME); },
  744. []{}, true);
  745. }
  746. return;
  747. case SDLK_SPACE: //space - try to revisit current object with selected hero
  748. {
  749. if(!isActive())
  750. return;
  751. if(h && key.state == SDL_PRESSED)
  752. {
  753. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  754. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  755. //this thread leaves scope and tries to lock pim while holding gs,
  756. //network thread tries to lock gs (appluy cl) while holding pim
  757. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  758. LOCPLINT->cb->moveHero(h,h->pos);
  759. }
  760. }
  761. return;
  762. case SDLK_RETURN:
  763. {
  764. if(!isActive() || !selection || key.state != SDL_PRESSED)
  765. return;
  766. if(h)
  767. LOCPLINT->openHeroWindow(h);
  768. else if(t)
  769. LOCPLINT->openTownWindow(t);
  770. return;
  771. }
  772. case SDLK_ESCAPE:
  773. {
  774. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  775. return;
  776. leaveCastingMode();
  777. return;
  778. }
  779. case SDLK_t:
  780. {
  781. //act on key down if marketplace windows is not already opened
  782. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  783. return;
  784. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  785. {
  786. //check if we have any marketplace
  787. const CGTownInstance *townWithMarket = nullptr;
  788. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  789. {
  790. if(t->hasBuilt(BuildingID::MARKETPLACE))
  791. {
  792. townWithMarket = t;
  793. break;
  794. }
  795. }
  796. if(townWithMarket) //if any town has marketplace, open window
  797. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  798. else //if not - complain
  799. LOCPLINT->showInfoDialog("No available marketplace!");
  800. }
  801. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  802. {
  803. townList.selectNext();
  804. }
  805. return;
  806. }
  807. default:
  808. {
  809. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  810. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  811. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  812. //numpad arrow
  813. if(CGuiHandler::isArrowKey(SDLKey(k)))
  814. k = CGuiHandler::arrowToNum(SDLKey(k));
  815. #ifdef VCMI_SDL1
  816. k -= SDLK_KP0 + 1;
  817. #else
  818. k -= SDLK_KP_1;
  819. #endif // VCMI_SDL1
  820. if(k < 0 || k > 8)
  821. return;
  822. int3 dir = directions[k];
  823. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  824. {
  825. Dir = (dir.x<0 ? LEFT : 0) |
  826. (dir.x>0 ? RIGHT : 0) |
  827. (dir.y<0 ? UP : 0) |
  828. (dir.y>0 ? DOWN : 0) ;
  829. break;
  830. }
  831. if(!h || key.state != SDL_PRESSED)
  832. break;
  833. if(k == 4)
  834. {
  835. centerOn(h);
  836. return;
  837. }
  838. CGPath &path = LOCPLINT->paths[h];
  839. terrain.currentPath = &path;
  840. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  841. {
  842. terrain.currentPath = nullptr;
  843. return;
  844. }
  845. if (path.nodes.size() > 2)
  846. updateMoveHero(h);
  847. else
  848. if(!path.nodes[0].turns)
  849. LOCPLINT->moveHero(h, path);
  850. }
  851. return;
  852. }
  853. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  854. && LOCPLINT->ctrlPressed()
  855. )
  856. scrollingDir |= Dir;
  857. else
  858. scrollingDir &= ~Dir;
  859. }
  860. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  861. {
  862. if(down)
  863. {
  864. CRClickPopup::createAndPush(text);
  865. }
  866. }
  867. int3 CAdvMapInt::verifyPos(int3 ver)
  868. {
  869. if (ver.x<0)
  870. ver.x=0;
  871. if (ver.y<0)
  872. ver.y=0;
  873. if (ver.z<0)
  874. ver.z=0;
  875. if (ver.x>=CGI->mh->sizes.x)
  876. ver.x=CGI->mh->sizes.x-1;
  877. if (ver.y>=CGI->mh->sizes.y)
  878. ver.y=CGI->mh->sizes.y-1;
  879. if (ver.z>=CGI->mh->sizes.z)
  880. ver.z=CGI->mh->sizes.z-1;
  881. return ver;
  882. }
  883. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  884. {
  885. assert(sel);
  886. LOCPLINT->cb->setSelection(sel);
  887. selection = sel;
  888. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  889. {
  890. auto pos = sel->visitablePos();
  891. auto tile = LOCPLINT->cb->getTile(pos);
  892. if(tile)
  893. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  894. }
  895. if(centerView)
  896. centerOn(sel);
  897. terrain.currentPath = nullptr;
  898. if(sel->ID==Obj::TOWN)
  899. {
  900. auto town = dynamic_cast<const CGTownInstance*>(sel);
  901. infoBar.showTownSelection(town);
  902. townList.select(town);
  903. heroList.select(nullptr);
  904. updateSleepWake(nullptr);
  905. updateMoveHero(nullptr);
  906. }
  907. else //hero selected
  908. {
  909. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  910. infoBar.showHeroSelection(hero);
  911. heroList.select(hero);
  912. townList.select(nullptr);
  913. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  914. updateSleepWake(hero);
  915. updateMoveHero(hero);
  916. }
  917. townList.redraw();
  918. heroList.redraw();
  919. }
  920. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  921. {
  922. //adventure map scrolling with mouse
  923. if(!isCtrlKeyDown() && isActive())
  924. {
  925. if(sEvent.x<15)
  926. {
  927. scrollingDir |= LEFT;
  928. }
  929. else
  930. {
  931. scrollingDir &= ~LEFT;
  932. }
  933. if(sEvent.x>screen->w-15)
  934. {
  935. scrollingDir |= RIGHT;
  936. }
  937. else
  938. {
  939. scrollingDir &= ~RIGHT;
  940. }
  941. if(sEvent.y<15)
  942. {
  943. scrollingDir |= UP;
  944. }
  945. else
  946. {
  947. scrollingDir &= ~UP;
  948. }
  949. if(sEvent.y>screen->h-15)
  950. {
  951. scrollingDir |= DOWN;
  952. }
  953. else
  954. {
  955. scrollingDir &= ~DOWN;
  956. }
  957. }
  958. }
  959. bool CAdvMapInt::isActive()
  960. {
  961. return active & ~CIntObject::KEYBOARD;
  962. }
  963. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  964. {
  965. state = WAITING;
  966. }
  967. void CAdvMapInt::setPlayer(PlayerColor Player)
  968. {
  969. player = Player;
  970. graphics->blueToPlayersAdv(bg,player);
  971. kingOverview->setPlayerColor(player);
  972. underground->setPlayerColor(player);
  973. questlog->setPlayerColor(player);
  974. sleepWake->setPlayerColor(player);
  975. moveHero->setPlayerColor(player);
  976. spellbook->setPlayerColor(player);
  977. sysOptions->setPlayerColor(player);
  978. advOptions->setPlayerColor(player);
  979. nextHero->setPlayerColor(player);
  980. endTurn->setPlayerColor(player);
  981. graphics->blueToPlayersAdv(resdatabar.bg,player);
  982. //heroList.updateHList();
  983. //townList.genList();
  984. }
  985. void CAdvMapInt::startTurn()
  986. {
  987. state = INGAME;
  988. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  989. {
  990. adjustActiveness(false);
  991. minimap.setAIRadar(false);
  992. }
  993. }
  994. void CAdvMapInt::endingTurn()
  995. {
  996. if(LOCPLINT->cingconsole->active)
  997. LOCPLINT->cingconsole->deactivate();
  998. LOCPLINT->makingTurn = false;
  999. LOCPLINT->cb->endTurn();
  1000. }
  1001. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1002. {
  1003. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1004. if (bobjs.empty())
  1005. return nullptr;
  1006. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1007. /*
  1008. if (bobjs.back()->ID == Obj::HERO)
  1009. return bobjs.back();
  1010. else
  1011. return bobjs.front();*/
  1012. }
  1013. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1014. {
  1015. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1016. return;
  1017. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1018. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1019. int3 selPos = selection->getSightCenter();
  1020. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1021. {
  1022. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1023. switch(spellBeingCasted->id)
  1024. {
  1025. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1026. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1027. leaveCastingMode(true, mapPos);
  1028. break;
  1029. case SpellID::DIMENSION_DOOR:
  1030. if(!tile || tile->isClear(heroTile))
  1031. leaveCastingMode(true, mapPos);
  1032. break;
  1033. }
  1034. return;
  1035. }
  1036. //check if we can select this object
  1037. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1038. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1039. if (selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1040. {
  1041. assert(!terrain.currentPath); //path can be active only when hero is selected
  1042. if(selection == topBlocking) //selected town clicked
  1043. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1044. else if ( canSelect )
  1045. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1046. return;
  1047. }
  1048. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1049. {
  1050. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mapPos);
  1051. if(currentHero == topBlocking) //clicked selected hero
  1052. {
  1053. LOCPLINT->openHeroWindow(currentHero);
  1054. return;
  1055. }
  1056. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1057. {
  1058. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1059. return;
  1060. }
  1061. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1062. {
  1063. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1064. {
  1065. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1066. return;
  1067. }
  1068. 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
  1069. {
  1070. CGPath &path = LOCPLINT->paths[currentHero];
  1071. terrain.currentPath = &path;
  1072. bool gotPath = LOCPLINT->cb->getPath2(mapPos, path); //try getting path, erase if failed
  1073. updateMoveHero(currentHero);
  1074. if (!gotPath)
  1075. LOCPLINT->eraseCurrentPathOf(currentHero);
  1076. else
  1077. return;
  1078. }
  1079. }
  1080. } //end of hero is selected "case"
  1081. else
  1082. {
  1083. throw std::runtime_error("Nothing is selected...");
  1084. }
  1085. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1086. {
  1087. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1088. }
  1089. }
  1090. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1091. {
  1092. if(!LOCPLINT->cb->isVisible(mapPos))
  1093. {
  1094. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1095. statusbar.clear();
  1096. return;
  1097. }
  1098. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1099. if (objAtTile)
  1100. {
  1101. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1102. boost::replace_all(text,"\n"," ");
  1103. statusbar.setText(text);
  1104. }
  1105. else
  1106. {
  1107. std::string hlp;
  1108. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1109. statusbar.setText(hlp);
  1110. }
  1111. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(mapPos);
  1112. int turns = pnode->turns;
  1113. vstd::amin(turns, 3);
  1114. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1115. return;
  1116. if(spellBeingCasted)
  1117. {
  1118. switch(spellBeingCasted->id)
  1119. {
  1120. case SpellID::SCUTTLE_BOAT:
  1121. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1122. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1123. else
  1124. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1125. return;
  1126. case SpellID::DIMENSION_DOOR:
  1127. {
  1128. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1129. int3 hpos = selection->getSightCenter();
  1130. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1131. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1132. else
  1133. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1134. return;
  1135. }
  1136. }
  1137. }
  1138. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->getGuardingCreaturePosition(mapPos));
  1139. if(selection->ID == Obj::TOWN)
  1140. {
  1141. if(objAtTile)
  1142. {
  1143. if(objAtTile->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner) != PlayerRelations::ENEMIES)
  1144. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1145. else if(objAtTile->ID == Obj::HERO && objAtTile->tempOwner == LOCPLINT->playerID)
  1146. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1147. else
  1148. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1149. }
  1150. else
  1151. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1152. }
  1153. else if(const CGHeroInstance *h = curHero())
  1154. {
  1155. bool accessible = pnode->turns < 255;
  1156. if(objAtTile)
  1157. {
  1158. if(objAtTile->ID == Obj::HERO)
  1159. {
  1160. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1161. {
  1162. if(accessible)
  1163. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1164. else
  1165. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1166. }
  1167. else //our or ally hero
  1168. {
  1169. if(selection == objAtTile)
  1170. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1171. else if(accessible)
  1172. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1173. else
  1174. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1175. }
  1176. }
  1177. else if(objAtTile->ID == Obj::TOWN)
  1178. {
  1179. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1180. {
  1181. if(accessible)
  1182. {
  1183. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1184. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1185. if (townObj && !townObj->armedGarrison())
  1186. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1187. else
  1188. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1189. }
  1190. else
  1191. {
  1192. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1193. }
  1194. }
  1195. else //our or ally town
  1196. {
  1197. if(accessible)
  1198. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1199. else
  1200. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1201. }
  1202. }
  1203. else if(objAtTile->ID == Obj::BOAT)
  1204. {
  1205. if(accessible)
  1206. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1207. else
  1208. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1209. }
  1210. else if (objAtTile->ID == Obj::GARRISON || objAtTile->ID == Obj::GARRISON2)
  1211. {
  1212. if (accessible)
  1213. {
  1214. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1215. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1216. if (garrObj && garrObj->stacksCount()
  1217. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1218. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1219. else
  1220. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1221. }
  1222. else
  1223. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1224. }
  1225. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1226. {
  1227. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1228. }
  1229. else
  1230. {
  1231. if(accessible)
  1232. {
  1233. if(pnode->land)
  1234. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1235. else
  1236. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1237. }
  1238. else
  1239. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1240. }
  1241. }
  1242. else //no objs
  1243. {
  1244. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1245. {
  1246. if (guardingCreature)
  1247. {
  1248. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1249. }
  1250. else
  1251. {
  1252. if(pnode->land)
  1253. {
  1254. if(LOCPLINT->cb->getTile(h->getPosition(false))->terType != ETerrainType::WATER)
  1255. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1256. else
  1257. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6); //anchor
  1258. }
  1259. else
  1260. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1261. }
  1262. }
  1263. else
  1264. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1265. }
  1266. }
  1267. if(ourInaccessibleShipyard(objAtTile))
  1268. {
  1269. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1270. }
  1271. }
  1272. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1273. {
  1274. if(spellBeingCasted)
  1275. {
  1276. leaveCastingMode();
  1277. return;
  1278. }
  1279. if(!LOCPLINT->cb->isVisible(mapPos))
  1280. {
  1281. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1282. return;
  1283. }
  1284. const CGObjectInstance * obj = getActiveObject(mapPos);
  1285. if(!obj)
  1286. {
  1287. // Bare or undiscovered terrain
  1288. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1289. if (tile)
  1290. {
  1291. std::string hlp;
  1292. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1293. CRClickPopup::createAndPush(hlp);
  1294. }
  1295. return;
  1296. }
  1297. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1298. }
  1299. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1300. {
  1301. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1302. spellBeingCasted = sp;
  1303. deactivate();
  1304. terrain.activate();
  1305. GH.fakeMouseMove();
  1306. }
  1307. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1308. {
  1309. assert(spellBeingCasted);
  1310. SpellID id = spellBeingCasted->id;
  1311. spellBeingCasted = nullptr;
  1312. terrain.deactivate();
  1313. activate();
  1314. if(cast)
  1315. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1316. else
  1317. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1318. }
  1319. const CGHeroInstance * CAdvMapInt::curHero() const
  1320. {
  1321. if(selection && selection->ID == Obj::HERO)
  1322. return static_cast<const CGHeroInstance *>(selection);
  1323. else
  1324. return nullptr;
  1325. }
  1326. const CGTownInstance * CAdvMapInt::curTown() const
  1327. {
  1328. if(selection && selection->ID == Obj::TOWN)
  1329. return static_cast<const CGTownInstance *>(selection);
  1330. else
  1331. return nullptr;
  1332. }
  1333. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1334. {
  1335. const IShipyard *ret = IShipyard::castFrom(obj);
  1336. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1337. return nullptr;
  1338. return ret;
  1339. }
  1340. void CAdvMapInt::aiTurnStarted()
  1341. {
  1342. adjustActiveness(true);
  1343. CCS->musich->playMusicFromSet("enemy-turn", true);
  1344. adventureInt->minimap.setAIRadar(true);
  1345. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1346. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1347. }
  1348. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1349. {
  1350. bool wasActive = isActive();
  1351. if(wasActive)
  1352. deactivate();
  1353. adventureInt->duringAITurn = aiTurnStart;
  1354. if(wasActive)
  1355. activate();
  1356. }
  1357. CAdventureOptions::CAdventureOptions():
  1358. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1359. {
  1360. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1361. exit = new CButton(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1362. exit->assignedKeys.insert(SDLK_ESCAPE);
  1363. scenInfo = new CButton(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_i);
  1364. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1365. //viewWorld = new CButton("","",std::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1366. puzzle = new CButton(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_p);
  1367. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1368. dig = new CButton(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_d);
  1369. if(const CGHeroInstance *h = adventureInt->curHero())
  1370. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1371. else
  1372. dig->block(true);
  1373. }
  1374. void CAdventureOptions::showScenarioInfo()
  1375. {
  1376. auto campState = LOCPLINT->cb->getStartInfo()->campState;
  1377. if(campState)
  1378. {
  1379. GH.pushInt(new CBonusSelection(campState));
  1380. }
  1381. else
  1382. {
  1383. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1384. }
  1385. }