CAdvmapInterface.cpp 51 KB

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