CAdvmapInterface.cpp 57 KB

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