CAdvmapInterface.cpp 55 KB

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