CAdvmapInterface.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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. worldViewUnderground->block(!CGI->mh->map->twoLevel);
  495. }
  496. CAdvMapInt::~CAdvMapInt()
  497. {
  498. SDL_FreeSurface(bg);
  499. for(int i=0; i<gems.size(); i++)
  500. delete gems[i];
  501. delete worldViewIconsDef;
  502. }
  503. void CAdvMapInt::fshowOverview()
  504. {
  505. GH.pushInt(new CKingdomInterface);
  506. }
  507. void CAdvMapInt::fworldViewBack()
  508. {
  509. changeMode(EAdvMapMode::NORMAL);
  510. CGI->mh->discardWorldViewCache();
  511. auto hero = curHero();
  512. if (hero)
  513. centerOn(hero);
  514. }
  515. void CAdvMapInt::fworldViewScale1x()
  516. {
  517. // TODO set corresponding scale button to "selected" mode
  518. changeMode(EAdvMapMode::WORLD_VIEW, 0.22f);
  519. }
  520. void CAdvMapInt::fworldViewScale2x()
  521. {
  522. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f);
  523. }
  524. void CAdvMapInt::fworldViewScale4x()
  525. {
  526. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f);
  527. }
  528. void CAdvMapInt::fswitchLevel()
  529. {
  530. // with support for future multi-level maps :)
  531. int maxLevels = CGI->mh->map->twoLevel ? 2 : 1;
  532. if (maxLevels < 2)
  533. return;
  534. position.z = (position.z + 1) % maxLevels;
  535. underground->setIndex(position.z, true);
  536. underground->redraw();
  537. worldViewUnderground->setIndex(position.z, true);
  538. worldViewUnderground->redraw();
  539. updateScreen = true;
  540. minimap.setLevel(position.z);
  541. if (mode == EAdvMapMode::WORLD_VIEW)
  542. terrain.redraw();
  543. }
  544. void CAdvMapInt::fshowQuestlog()
  545. {
  546. LOCPLINT->showQuestLog();
  547. }
  548. void CAdvMapInt::fsleepWake()
  549. {
  550. const CGHeroInstance *h = curHero();
  551. if (!h)
  552. return;
  553. bool newSleep = !isHeroSleeping(h);
  554. setHeroSleeping(h, newSleep);
  555. updateSleepWake(h);
  556. if (newSleep)
  557. {
  558. fnextHero();
  559. //moveHero.block(true);
  560. //uncomment to enable original HoMM3 behaviour:
  561. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  562. }
  563. }
  564. void CAdvMapInt::fmoveHero()
  565. {
  566. const CGHeroInstance *h = curHero();
  567. if (!h || !terrain.currentPath)
  568. return;
  569. LOCPLINT->moveHero(h, *terrain.currentPath);
  570. }
  571. void CAdvMapInt::fshowSpellbok()
  572. {
  573. if (!curHero()) //checking necessary values
  574. return;
  575. centerOn(selection);
  576. auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  577. GH.pushInt(spellWindow);
  578. }
  579. void CAdvMapInt::fadventureOPtions()
  580. {
  581. GH.pushInt(new CAdventureOptions);
  582. }
  583. void CAdvMapInt::fsystemOptions()
  584. {
  585. GH.pushInt(new CSystemOptionsWindow());
  586. }
  587. void CAdvMapInt::fnextHero()
  588. {
  589. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  590. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  591. if (next < 0)
  592. return;
  593. select(LOCPLINT->wanderingHeroes[next], true);
  594. }
  595. void CAdvMapInt::fendTurn()
  596. {
  597. if(!LOCPLINT->makingTurn)
  598. return;
  599. if ( settings["adventure"]["heroReminder"].Bool())
  600. {
  601. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  602. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  603. {
  604. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), 0, false);
  605. return;
  606. }
  607. }
  608. endingTurn();
  609. }
  610. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  611. {
  612. sleepWake->block(!h);
  613. if (!h)
  614. return;
  615. bool state = isHeroSleeping(h);
  616. sleepWake->setIndex(state ? 1 : 0, true);
  617. sleepWake->assignedKeys.clear();
  618. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  619. }
  620. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  621. {
  622. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  623. if (hasPath == boost::indeterminate)
  624. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  625. if (!h)
  626. {
  627. moveHero->block(true);
  628. return;
  629. }
  630. moveHero->block(!hasPath || (h->movement == 0));
  631. }
  632. int CAdvMapInt::getNextHeroIndex(int startIndex)
  633. {
  634. if (LOCPLINT->wanderingHeroes.size() == 0)
  635. return -1;
  636. if (startIndex < 0)
  637. startIndex = 0;
  638. int i = startIndex;
  639. do
  640. {
  641. i++;
  642. if (i >= LOCPLINT->wanderingHeroes.size())
  643. i = 0;
  644. }
  645. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  646. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  647. return i;
  648. else
  649. return -1;
  650. }
  651. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  652. {
  653. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  654. int next = getNextHeroIndex(start);
  655. if (next < 0)
  656. {
  657. nextHero->block(true);
  658. return;
  659. }
  660. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  661. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  662. nextHero->block(noActiveHeroes);
  663. }
  664. void CAdvMapInt::activate()
  665. {
  666. CIntObject::activate();
  667. if (!(active & KEYBOARD))
  668. CIntObject::activate(KEYBOARD);
  669. screenBuf = screen;
  670. GH.statusbar = &statusbar;
  671. if(!duringAITurn)
  672. {
  673. activeMapPanel->activate();
  674. if (mode == EAdvMapMode::NORMAL)
  675. {
  676. heroList.activate();
  677. townList.activate();
  678. infoBar.activate();
  679. }
  680. minimap.activate();
  681. terrain.activate();
  682. LOCPLINT->cingconsole->activate();
  683. GH.fakeMouseMove(); //to restore the cursor
  684. }
  685. }
  686. void CAdvMapInt::deactivate()
  687. {
  688. CIntObject::deactivate();
  689. if(!duringAITurn)
  690. {
  691. scrollingDir = 0;
  692. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  693. activeMapPanel->deactivate();
  694. if (mode == EAdvMapMode::NORMAL)
  695. {
  696. heroList.deactivate();
  697. townList.deactivate();
  698. infoBar.deactivate();
  699. }
  700. minimap.deactivate();
  701. terrain.deactivate();
  702. if(LOCPLINT)
  703. LOCPLINT->cingconsole->deactivate();
  704. }
  705. }
  706. void CAdvMapInt::showAll(SDL_Surface * to)
  707. {
  708. blitAt(bg,0,0,to);
  709. if(state != INGAME)
  710. return;
  711. switch (mode)
  712. {
  713. case EAdvMapMode::NORMAL:
  714. heroList.showAll(to);
  715. townList.showAll(to);
  716. infoBar.showAll(to);
  717. break;
  718. case EAdvMapMode::WORLD_VIEW:
  719. terrain.showAll(to);
  720. break;
  721. }
  722. activeMapPanel->showAll(to);
  723. updateScreen = true;
  724. minimap.showAll(to);
  725. show(to);
  726. resdatabar.draw(to);
  727. statusbar.show(to);
  728. LOCPLINT->cingconsole->showAll(to);
  729. }
  730. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  731. {
  732. if (!hero)
  733. return false;
  734. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  735. }
  736. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  737. {
  738. if (sleep)
  739. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  740. else
  741. LOCPLINT->sleepingHeroes -= hero;
  742. updateNextHero(nullptr);
  743. }
  744. void CAdvMapInt::show(SDL_Surface * to)
  745. {
  746. if(state != INGAME)
  747. return;
  748. ++animValHitCount; //for animations
  749. if(animValHitCount == 8)
  750. {
  751. CGI->mh->updateWater();
  752. animValHitCount = 0;
  753. ++anim;
  754. updateScreen = true;
  755. }
  756. ++heroAnim;
  757. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  758. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  759. if((animValHitCount % (4/scrollSpeed)) == 0
  760. && (
  761. (GH.topInt() == this)
  762. || isCtrlKeyDown()
  763. )
  764. )
  765. {
  766. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  767. position.x--;
  768. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  769. position.x++;
  770. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  771. position.y--;
  772. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  773. position.y++;
  774. if(scrollingDir)
  775. {
  776. updateScreen = true;
  777. minimap.redraw();
  778. if (mode == EAdvMapMode::WORLD_VIEW)
  779. terrain.redraw();
  780. }
  781. }
  782. if(updateScreen)
  783. {
  784. int3 betterPos = LOCPLINT->repairScreenPos(position);
  785. if (betterPos != position)
  786. {
  787. logGlobal->warnStream() << "Incorrect position for adventure map!";
  788. position = betterPos;
  789. }
  790. terrain.show(to);
  791. for(int i=0;i<4;i++)
  792. blitAt(gems[i]->ourImages[LOCPLINT->playerID.getNum()].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  793. updateScreen=false;
  794. LOCPLINT->cingconsole->showAll(to);
  795. }
  796. infoBar.show(to);
  797. statusbar.showAll(to);
  798. }
  799. void CAdvMapInt::selectionChanged()
  800. {
  801. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  802. if (selection != to)
  803. select(to);
  804. }
  805. void CAdvMapInt::centerOn(int3 on)
  806. {
  807. bool switchedLevels = on.z != position.z;
  808. switch (mode)
  809. {
  810. default:
  811. case EAdvMapMode::NORMAL:
  812. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  813. on.y -= CGI->mh->frameH;
  814. break;
  815. case EAdvMapMode::WORLD_VIEW:
  816. on.x -= CGI->mh->tilesW / 2 / worldViewScale;
  817. on.y -= CGI->mh->tilesH / 2 / worldViewScale;
  818. break;
  819. }
  820. on = LOCPLINT->repairScreenPos(on);
  821. position = on;
  822. updateScreen=true;
  823. underground->setIndex(on.z,true); //change underground switch button image
  824. underground->redraw();
  825. worldViewUnderground->setIndex(on.z, true);
  826. worldViewUnderground->redraw();
  827. if (switchedLevels)
  828. minimap.setLevel(position.z);
  829. minimap.redraw();
  830. if (mode == EAdvMapMode::WORLD_VIEW)
  831. terrain.redraw();
  832. }
  833. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  834. {
  835. centerOn(obj->getSightCenter());
  836. }
  837. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  838. {
  839. if (mode == EAdvMapMode::WORLD_VIEW)
  840. return;
  841. ui8 Dir = 0;
  842. int k = key.keysym.sym;
  843. const CGHeroInstance *h = curHero(); //selected hero
  844. const CGTownInstance *t = curTown(); //selected town
  845. switch(k)
  846. {
  847. case SDLK_g:
  848. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  849. return;
  850. {
  851. //find first town with tavern
  852. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  853. {
  854. return town->hasBuilt(BuildingID::TAVERN);
  855. });
  856. if(itr != LOCPLINT->towns.end())
  857. LOCPLINT->showThievesGuildWindow(*itr);
  858. else
  859. LOCPLINT->showInfoDialog("No available town with tavern!");
  860. }
  861. return;
  862. case SDLK_i:
  863. if(isActive())
  864. CAdventureOptions::showScenarioInfo();
  865. return;
  866. case SDLK_l:
  867. if(isActive())
  868. LOCPLINT->proposeLoadingGame();
  869. return;
  870. case SDLK_s:
  871. if(isActive())
  872. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  873. return;
  874. case SDLK_d:
  875. {
  876. if(h && isActive() && key.state == SDL_PRESSED)
  877. LOCPLINT->tryDiggging(h);
  878. return;
  879. }
  880. case SDLK_p:
  881. if(isActive())
  882. LOCPLINT->showPuzzleMap();
  883. return;
  884. case SDLK_v:
  885. if(isActive())
  886. LOCPLINT->viewWorldMap();
  887. return;
  888. case SDLK_r:
  889. if(isActive() && LOCPLINT->ctrlPressed())
  890. {
  891. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  892. []{ LOCPLINT->sendCustomEvent(RESTART_GAME); },
  893. []{}, true);
  894. }
  895. return;
  896. case SDLK_SPACE: //space - try to revisit current object with selected hero
  897. {
  898. if(!isActive())
  899. return;
  900. if(h && key.state == SDL_PRESSED)
  901. {
  902. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  903. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  904. //this thread leaves scope and tries to lock pim while holding gs,
  905. //network thread tries to lock gs (appluy cl) while holding pim
  906. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  907. LOCPLINT->cb->moveHero(h,h->pos);
  908. }
  909. }
  910. return;
  911. case SDLK_RETURN:
  912. {
  913. if(!isActive() || !selection || key.state != SDL_PRESSED)
  914. return;
  915. if(h)
  916. LOCPLINT->openHeroWindow(h);
  917. else if(t)
  918. LOCPLINT->openTownWindow(t);
  919. return;
  920. }
  921. case SDLK_ESCAPE:
  922. {
  923. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  924. return;
  925. leaveCastingMode();
  926. return;
  927. }
  928. case SDLK_t:
  929. {
  930. //act on key down if marketplace windows is not already opened
  931. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  932. return;
  933. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  934. {
  935. //check if we have any marketplace
  936. const CGTownInstance *townWithMarket = nullptr;
  937. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  938. {
  939. if(t->hasBuilt(BuildingID::MARKETPLACE))
  940. {
  941. townWithMarket = t;
  942. break;
  943. }
  944. }
  945. if(townWithMarket) //if any town has marketplace, open window
  946. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  947. else //if not - complain
  948. LOCPLINT->showInfoDialog("No available marketplace!");
  949. }
  950. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  951. {
  952. townList.selectNext();
  953. }
  954. return;
  955. }
  956. default:
  957. {
  958. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  959. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  960. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  961. //numpad arrow
  962. if(CGuiHandler::isArrowKey(SDLKey(k)))
  963. k = CGuiHandler::arrowToNum(SDLKey(k));
  964. #ifdef VCMI_SDL1
  965. k -= SDLK_KP0 + 1;
  966. #else
  967. k -= SDLK_KP_1;
  968. #endif // VCMI_SDL1
  969. if(k < 0 || k > 8)
  970. return;
  971. int3 dir = directions[k];
  972. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  973. {
  974. Dir = (dir.x<0 ? LEFT : 0) |
  975. (dir.x>0 ? RIGHT : 0) |
  976. (dir.y<0 ? UP : 0) |
  977. (dir.y>0 ? DOWN : 0) ;
  978. break;
  979. }
  980. if(!h || key.state != SDL_PRESSED)
  981. break;
  982. if(k == 4)
  983. {
  984. centerOn(h);
  985. return;
  986. }
  987. CGPath &path = LOCPLINT->paths[h];
  988. terrain.currentPath = &path;
  989. if(!LOCPLINT->cb->getPathsInfo(h)->getPath(h->getPosition(false) + dir, path))
  990. {
  991. terrain.currentPath = nullptr;
  992. return;
  993. }
  994. if (path.nodes.size() > 2)
  995. updateMoveHero(h);
  996. else
  997. if(!path.nodes[0].turns)
  998. LOCPLINT->moveHero(h, path);
  999. }
  1000. return;
  1001. }
  1002. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1003. && LOCPLINT->ctrlPressed()
  1004. )
  1005. scrollingDir |= Dir;
  1006. else
  1007. scrollingDir &= ~Dir;
  1008. }
  1009. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1010. {
  1011. if(down)
  1012. {
  1013. CRClickPopup::createAndPush(text);
  1014. }
  1015. }
  1016. int3 CAdvMapInt::verifyPos(int3 ver)
  1017. {
  1018. if (ver.x<0)
  1019. ver.x=0;
  1020. if (ver.y<0)
  1021. ver.y=0;
  1022. if (ver.z<0)
  1023. ver.z=0;
  1024. if (ver.x>=CGI->mh->sizes.x)
  1025. ver.x=CGI->mh->sizes.x-1;
  1026. if (ver.y>=CGI->mh->sizes.y)
  1027. ver.y=CGI->mh->sizes.y-1;
  1028. if (ver.z>=CGI->mh->sizes.z)
  1029. ver.z=CGI->mh->sizes.z-1;
  1030. return ver;
  1031. }
  1032. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  1033. {
  1034. assert(sel);
  1035. LOCPLINT->setSelection(sel);
  1036. selection = sel;
  1037. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1038. {
  1039. auto pos = sel->visitablePos();
  1040. auto tile = LOCPLINT->cb->getTile(pos);
  1041. if(tile)
  1042. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  1043. }
  1044. if(centerView)
  1045. centerOn(sel);
  1046. terrain.currentPath = nullptr;
  1047. if(sel->ID==Obj::TOWN)
  1048. {
  1049. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1050. infoBar.showTownSelection(town);
  1051. townList.select(town);
  1052. heroList.select(nullptr);
  1053. updateSleepWake(nullptr);
  1054. updateMoveHero(nullptr);
  1055. }
  1056. else //hero selected
  1057. {
  1058. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1059. infoBar.showHeroSelection(hero);
  1060. heroList.select(hero);
  1061. townList.select(nullptr);
  1062. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1063. updateSleepWake(hero);
  1064. updateMoveHero(hero);
  1065. }
  1066. townList.redraw();
  1067. heroList.redraw();
  1068. }
  1069. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1070. {
  1071. // adventure map scrolling with mouse
  1072. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1073. if(!isCtrlKeyDown() && isActive() && mode == EAdvMapMode::NORMAL)
  1074. {
  1075. if(sEvent.x<15)
  1076. {
  1077. scrollingDir |= LEFT;
  1078. }
  1079. else
  1080. {
  1081. scrollingDir &= ~LEFT;
  1082. }
  1083. if(sEvent.x>screen->w-15)
  1084. {
  1085. scrollingDir |= RIGHT;
  1086. }
  1087. else
  1088. {
  1089. scrollingDir &= ~RIGHT;
  1090. }
  1091. if(sEvent.y<15)
  1092. {
  1093. scrollingDir |= UP;
  1094. }
  1095. else
  1096. {
  1097. scrollingDir &= ~UP;
  1098. }
  1099. if(sEvent.y>screen->h-15)
  1100. {
  1101. scrollingDir |= DOWN;
  1102. }
  1103. else
  1104. {
  1105. scrollingDir &= ~DOWN;
  1106. }
  1107. }
  1108. }
  1109. bool CAdvMapInt::isActive()
  1110. {
  1111. return active & ~CIntObject::KEYBOARD;
  1112. }
  1113. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1114. {
  1115. state = WAITING;
  1116. }
  1117. void CAdvMapInt::setPlayer(PlayerColor Player)
  1118. {
  1119. player = Player;
  1120. graphics->blueToPlayersAdv(bg,player);
  1121. panelMain->setPlayerColor(player);
  1122. panelWorldView->setPlayerColor(player);
  1123. panelWorldView->recolorIcons(player, worldViewIconsDef, player.getNum() * 19);
  1124. graphics->blueToPlayersAdv(resdatabar.bg,player);
  1125. //heroList.updateHList();
  1126. //townList.genList();
  1127. }
  1128. void CAdvMapInt::startTurn()
  1129. {
  1130. state = INGAME;
  1131. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  1132. {
  1133. adjustActiveness(false);
  1134. minimap.setAIRadar(false);
  1135. }
  1136. }
  1137. void CAdvMapInt::endingTurn()
  1138. {
  1139. if(LOCPLINT->cingconsole->active)
  1140. LOCPLINT->cingconsole->deactivate();
  1141. LOCPLINT->makingTurn = false;
  1142. LOCPLINT->cb->endTurn();
  1143. }
  1144. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1145. {
  1146. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1147. if (bobjs.empty())
  1148. return nullptr;
  1149. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1150. /*
  1151. if (bobjs.back()->ID == Obj::HERO)
  1152. return bobjs.back();
  1153. else
  1154. return bobjs.front();*/
  1155. }
  1156. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1157. {
  1158. if(mode != EAdvMapMode::NORMAL)
  1159. return;
  1160. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1161. return;
  1162. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1163. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1164. int3 selPos = selection->getSightCenter();
  1165. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1166. {
  1167. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1168. switch(spellBeingCasted->id)
  1169. {
  1170. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1171. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1172. leaveCastingMode(true, mapPos);
  1173. break;
  1174. case SpellID::DIMENSION_DOOR:
  1175. if(!tile || tile->isClear(heroTile))
  1176. leaveCastingMode(true, mapPos);
  1177. break;
  1178. }
  1179. return;
  1180. }
  1181. //check if we can select this object
  1182. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1183. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1184. if (selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1185. {
  1186. assert(!terrain.currentPath); //path can be active only when hero is selected
  1187. if(selection == topBlocking) //selected town clicked
  1188. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1189. else if ( canSelect )
  1190. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1191. return;
  1192. }
  1193. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1194. {
  1195. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1196. if(currentHero == topBlocking) //clicked selected hero
  1197. {
  1198. LOCPLINT->openHeroWindow(currentHero);
  1199. return;
  1200. }
  1201. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1202. {
  1203. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1204. return;
  1205. }
  1206. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1207. {
  1208. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1209. {
  1210. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1211. return;
  1212. }
  1213. 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
  1214. {
  1215. CGPath &path = LOCPLINT->paths[currentHero];
  1216. terrain.currentPath = &path;
  1217. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(mapPos, path); //try getting path, erase if failed
  1218. updateMoveHero(currentHero);
  1219. if (!gotPath)
  1220. LOCPLINT->eraseCurrentPathOf(currentHero);
  1221. else
  1222. return;
  1223. }
  1224. }
  1225. } //end of hero is selected "case"
  1226. else
  1227. {
  1228. throw std::runtime_error("Nothing is selected...");
  1229. }
  1230. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1231. {
  1232. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1233. }
  1234. }
  1235. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1236. {
  1237. if(mode != EAdvMapMode::NORMAL)
  1238. return;
  1239. if(!LOCPLINT->cb->isVisible(mapPos))
  1240. {
  1241. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1242. statusbar.clear();
  1243. return;
  1244. }
  1245. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1246. if (objAtTile)
  1247. {
  1248. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1249. boost::replace_all(text,"\n"," ");
  1250. statusbar.setText(text);
  1251. }
  1252. else
  1253. {
  1254. std::string hlp;
  1255. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1256. statusbar.setText(hlp);
  1257. }
  1258. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1259. return;
  1260. if(spellBeingCasted)
  1261. {
  1262. switch(spellBeingCasted->id)
  1263. {
  1264. case SpellID::SCUTTLE_BOAT:
  1265. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1266. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1267. else
  1268. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1269. return;
  1270. case SpellID::DIMENSION_DOOR:
  1271. {
  1272. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1273. int3 hpos = selection->getSightCenter();
  1274. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1275. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1276. else
  1277. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1278. return;
  1279. }
  1280. }
  1281. }
  1282. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->getGuardingCreaturePosition(mapPos));
  1283. if(selection->ID == Obj::TOWN)
  1284. {
  1285. if(objAtTile)
  1286. {
  1287. if(objAtTile->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner) != PlayerRelations::ENEMIES)
  1288. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1289. else if(objAtTile->ID == Obj::HERO && objAtTile->tempOwner == LOCPLINT->playerID)
  1290. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1291. else
  1292. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1293. }
  1294. else
  1295. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1296. }
  1297. else if(const CGHeroInstance *h = curHero())
  1298. {
  1299. const CGPathNode *pnode = LOCPLINT->cb->getPathsInfo(h)->getPathInfo(mapPos);
  1300. int turns = pnode->turns;
  1301. vstd::amin(turns, 3);
  1302. bool accessible = pnode->turns < 255;
  1303. if(objAtTile)
  1304. {
  1305. if(objAtTile->ID == Obj::HERO)
  1306. {
  1307. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1308. {
  1309. if(accessible)
  1310. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1311. else
  1312. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1313. }
  1314. else //our or ally hero
  1315. {
  1316. if(selection == objAtTile)
  1317. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1318. else if(accessible)
  1319. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1320. else
  1321. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1322. }
  1323. }
  1324. else if(objAtTile->ID == Obj::TOWN)
  1325. {
  1326. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1327. {
  1328. if(accessible)
  1329. {
  1330. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1331. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1332. if (townObj && !townObj->armedGarrison())
  1333. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1334. else
  1335. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1336. }
  1337. else
  1338. {
  1339. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1340. }
  1341. }
  1342. else //our or ally town
  1343. {
  1344. if(accessible)
  1345. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1346. else
  1347. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1348. }
  1349. }
  1350. else if(objAtTile->ID == Obj::BOAT)
  1351. {
  1352. if(accessible)
  1353. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1354. else
  1355. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1356. }
  1357. else if (objAtTile->ID == Obj::GARRISON || objAtTile->ID == Obj::GARRISON2)
  1358. {
  1359. if (accessible)
  1360. {
  1361. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1362. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1363. if (garrObj && garrObj->stacksCount()
  1364. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1365. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1366. else
  1367. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1368. }
  1369. else
  1370. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1371. }
  1372. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1373. {
  1374. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1375. }
  1376. else
  1377. {
  1378. if(accessible)
  1379. {
  1380. if(pnode->land)
  1381. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1382. else
  1383. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1384. }
  1385. else
  1386. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1387. }
  1388. }
  1389. else //no objs
  1390. {
  1391. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1392. {
  1393. if (guardingCreature)
  1394. {
  1395. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1396. }
  1397. else
  1398. {
  1399. if(pnode->land)
  1400. {
  1401. if(LOCPLINT->cb->getTile(h->getPosition(false))->terType != ETerrainType::WATER)
  1402. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1403. else
  1404. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6); //anchor
  1405. }
  1406. else
  1407. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1408. }
  1409. }
  1410. else
  1411. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1412. }
  1413. }
  1414. if(ourInaccessibleShipyard(objAtTile))
  1415. {
  1416. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1417. }
  1418. }
  1419. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1420. {
  1421. if(mode != EAdvMapMode::NORMAL)
  1422. return;
  1423. if(spellBeingCasted)
  1424. {
  1425. leaveCastingMode();
  1426. return;
  1427. }
  1428. if(!LOCPLINT->cb->isVisible(mapPos))
  1429. {
  1430. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1431. return;
  1432. }
  1433. const CGObjectInstance * obj = getActiveObject(mapPos);
  1434. if(!obj)
  1435. {
  1436. // Bare or undiscovered terrain
  1437. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1438. if (tile)
  1439. {
  1440. std::string hlp;
  1441. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1442. CRClickPopup::createAndPush(hlp);
  1443. }
  1444. return;
  1445. }
  1446. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1447. }
  1448. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1449. {
  1450. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1451. spellBeingCasted = sp;
  1452. deactivate();
  1453. terrain.activate();
  1454. GH.fakeMouseMove();
  1455. }
  1456. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1457. {
  1458. assert(spellBeingCasted);
  1459. SpellID id = spellBeingCasted->id;
  1460. spellBeingCasted = nullptr;
  1461. terrain.deactivate();
  1462. activate();
  1463. if(cast)
  1464. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1465. else
  1466. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1467. }
  1468. const CGHeroInstance * CAdvMapInt::curHero() const
  1469. {
  1470. if(selection && selection->ID == Obj::HERO)
  1471. return static_cast<const CGHeroInstance *>(selection);
  1472. else
  1473. return nullptr;
  1474. }
  1475. const CGTownInstance * CAdvMapInt::curTown() const
  1476. {
  1477. if(selection && selection->ID == Obj::TOWN)
  1478. return static_cast<const CGTownInstance *>(selection);
  1479. else
  1480. return nullptr;
  1481. }
  1482. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1483. {
  1484. const IShipyard *ret = IShipyard::castFrom(obj);
  1485. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1486. return nullptr;
  1487. return ret;
  1488. }
  1489. void CAdvMapInt::aiTurnStarted()
  1490. {
  1491. adjustActiveness(true);
  1492. CCS->musich->playMusicFromSet("enemy-turn", true);
  1493. adventureInt->minimap.setAIRadar(true);
  1494. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1495. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1496. }
  1497. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1498. {
  1499. bool wasActive = isActive();
  1500. if(wasActive)
  1501. deactivate();
  1502. adventureInt->duringAITurn = aiTurnStart;
  1503. if(wasActive)
  1504. activate();
  1505. }
  1506. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale /* = 0.4f */)
  1507. {
  1508. if (mode != newMode)
  1509. {
  1510. mode = newMode;
  1511. switch (mode)
  1512. {
  1513. case EAdvMapMode::NORMAL:
  1514. panelMain->activate();
  1515. panelWorldView->deactivate();
  1516. activeMapPanel = panelMain;
  1517. townList.activate();
  1518. heroList.activate();
  1519. infoBar.activate();
  1520. break;
  1521. case EAdvMapMode::WORLD_VIEW:
  1522. panelMain->deactivate();
  1523. panelWorldView->activate();
  1524. activeMapPanel = panelWorldView;
  1525. townList.deactivate();
  1526. heroList.deactivate();
  1527. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1528. infoBar.deactivate();
  1529. break;
  1530. }
  1531. worldViewScale = newScale;
  1532. redraw();
  1533. }
  1534. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1535. {
  1536. worldViewScale = newScale;
  1537. redraw();
  1538. }
  1539. }
  1540. CAdventureOptions::CAdventureOptions():
  1541. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1542. {
  1543. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1544. viewWorld = new CButton(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_v);
  1545. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1546. exit = new CButton(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1547. exit->assignedKeys.insert(SDLK_ESCAPE);
  1548. scenInfo = new CButton(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_i);
  1549. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1550. puzzle = new CButton(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_p);
  1551. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1552. dig = new CButton(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&]{ close(); }, SDLK_d);
  1553. if(const CGHeroInstance *h = adventureInt->curHero())
  1554. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1555. else
  1556. dig->block(true);
  1557. }
  1558. void CAdventureOptions::showScenarioInfo()
  1559. {
  1560. auto campState = LOCPLINT->cb->getStartInfo()->campState;
  1561. if(campState)
  1562. {
  1563. GH.pushInt(new CBonusSelection(campState));
  1564. }
  1565. else
  1566. {
  1567. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1568. }
  1569. }