CAdvmapInterface.cpp 51 KB

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