CAdvmapInterface.cpp 53 KB

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