CAdvmapInterface.cpp 43 KB

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