CAdvmapInterface.cpp 51 KB

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