CAdvmapInterface.cpp 41 KB

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