CAdvmapInterface.cpp 43 KB

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