CAdvmapInterface.cpp 41 KB

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