CPlayerInterface.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. #include "StdInc.h"
  2. #include "CAdvmapInterface.h"
  3. #include "battle/CBattleInterface.h"
  4. #include "battle/CBattleInterfaceClasses.h"
  5. #include "../CCallback.h"
  6. #include "CCastleInterface.h"
  7. #include "gui/CCursorHandler.h"
  8. #include "CKingdomInterface.h"
  9. #include "CGameInfo.h"
  10. #include "CHeroWindow.h"
  11. #include "CCreatureWindow.h"
  12. #include "CQuestLog.h"
  13. #include "CMessage.h"
  14. #include "CPlayerInterface.h"
  15. #include "gui/SDL_Extensions.h"
  16. #include "../lib/CConfigHandler.h"
  17. #include "battle/CCreatureAnimation.h"
  18. #include "Graphics.h"
  19. #include "../lib/CArtHandler.h"
  20. #include "../lib/CGeneralTextHandler.h"
  21. #include "../lib/CHeroHandler.h"
  22. #include "../lib/Connection.h"
  23. #include "../lib/CSpellHandler.h"
  24. #include "../lib/CTownHandler.h"
  25. #include "../lib/BattleState.h"
  26. #include "../lib/JsonNode.h"
  27. #include "CMusicHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/NetPacks.h"
  30. #include "../lib/mapping/CMap.h"
  31. #include "../lib/VCMIDirs.h"
  32. #include "mapHandler.h"
  33. #include "../lib/CStopWatch.h"
  34. #include "../lib/StartInfo.h"
  35. #include "../lib/CGameState.h"
  36. #include "../lib/GameConstants.h"
  37. #include "gui/CGuiHandler.h"
  38. #include "../lib/UnlockGuard.h"
  39. #ifdef min
  40. #undef min
  41. #endif
  42. #ifdef max
  43. #undef max
  44. #endif
  45. /*
  46. * CPlayerInterface.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. // The macro below is used to mark functions that are called by client when game state changes.
  55. // They all assume that CPlayerInterface::pim mutex is locked.
  56. #define EVENT_HANDLER_CALLED_BY_CLIENT
  57. // The macro marks functions that are run on a new thread by client.
  58. // They do not own any mutexes intiially.
  59. #define THREAD_CREATED_BY_CLIENT
  60. #define RETURN_IF_QUICK_COMBAT \
  61. if(isAutoFightOn && !battleInt) \
  62. return;
  63. #define BATTLE_EVENT_POSSIBLE_RETURN\
  64. if(LOCPLINT != this) \
  65. return; \
  66. RETURN_IF_QUICK_COMBAT
  67. using namespace boost::assign;
  68. using namespace CSDL_Ext;
  69. void processCommand(const std::string &message, CClient *&client);
  70. extern std::queue<SDL_Event> events;
  71. extern boost::mutex eventsM;
  72. boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
  73. CondSh<bool> CPlayerInterface::terminate_cond;
  74. CPlayerInterface * LOCPLINT;
  75. CBattleInterface * CPlayerInterface::battleInt;
  76. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  77. CondSh<EMoveState> stillMoveHero; //used during hero movement
  78. int CPlayerInterface::howManyPeople = 0;
  79. static bool objectBlitOrderSorter(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b)
  80. {
  81. return CMapHandler::compareObjectBlitOrder(a.first, b.first);
  82. }
  83. CPlayerInterface::CPlayerInterface(PlayerColor Player)
  84. {
  85. logGlobal->traceStream() << "\tHuman player interface for player " << Player << " being constructed";
  86. observerInDuelMode = false;
  87. howManyPeople++;
  88. GH.defActionsDef = 0;
  89. LOCPLINT = this;
  90. curAction = nullptr;
  91. playerID=Player;
  92. human=true;
  93. castleInt = nullptr;
  94. battleInt = nullptr;
  95. //pim = new boost::recursive_mutex;
  96. makingTurn = false;
  97. showingDialog = new CondSh<bool>(false);
  98. cingconsole = new CInGameConsole;
  99. terminate_cond.set(false);
  100. firstCall = 1; //if loading will be overwritten in serialize
  101. autosaveCount = 0;
  102. isAutoFightOn = false;
  103. }
  104. CPlayerInterface::~CPlayerInterface()
  105. {
  106. logGlobal->traceStream() << "\tHuman player interface for player " << playerID << " being destructed";
  107. //howManyPeople--;
  108. //delete pim;
  109. //vstd::clear_pointer(pim);
  110. delete showingDialog;
  111. delete cingconsole;
  112. if(LOCPLINT == this)
  113. LOCPLINT = nullptr;
  114. }
  115. void CPlayerInterface::init(shared_ptr<CCallback> CB)
  116. {
  117. cb = CB;
  118. if(observerInDuelMode)
  119. return;
  120. if(!towns.size() && !wanderingHeroes.size())
  121. initializeHeroTownList();
  122. if(!adventureInt)
  123. adventureInt = new CAdvMapInt();
  124. }
  125. void CPlayerInterface::yourTurn()
  126. {
  127. EVENT_HANDLER_CALLED_BY_CLIENT;
  128. {
  129. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  130. LOCPLINT = this;
  131. GH.curInt = this;
  132. adventureInt->selection = nullptr;
  133. if(firstCall)
  134. {
  135. if(howManyPeople == 1)
  136. adventureInt->setPlayer(playerID);
  137. autosaveCount = getLastIndex("Autosave_");
  138. if(firstCall > 0) //new game, not loaded
  139. {
  140. int index = getLastIndex("Newgame_Autosave_");
  141. index %= SAVES_COUNT;
  142. cb->save("Saves/Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  143. }
  144. firstCall = 0;
  145. }
  146. else
  147. {
  148. LOCPLINT->cb->save("Saves/Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  149. autosaveCount %= 5;
  150. }
  151. if(adventureInt->player != playerID)
  152. adventureInt->setPlayer(playerID);
  153. if(howManyPeople > 1) //hot seat message
  154. {
  155. adventureInt->startHotSeatWait(playerID);
  156. makingTurn = true;
  157. std::string msg = CGI->generaltexth->allTexts[13];
  158. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  159. std::vector<CComponent*> cmp;
  160. cmp.push_back(new CComponent(CComponent::flag, playerID.getNum(), 0));
  161. showInfoDialog(msg, cmp);
  162. }
  163. else
  164. {
  165. makingTurn = true;
  166. adventureInt->startTurn();
  167. }
  168. }
  169. acceptTurn();
  170. }
  171. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const ObjectInstanceID & hid)
  172. {
  173. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  174. for(auto & elem : hlp.objects)
  175. if(elem.first->id == hid)
  176. {
  177. elem.second = r;
  178. return;
  179. }
  180. }
  181. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const ObjectInstanceID & hid)
  182. {
  183. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  184. for(int h=0; h<hlp.objects.size(); ++h)
  185. if(hlp.objects[h].first->id == hid)
  186. {
  187. hlp.objects.erase(hlp.objects.begin()+h);
  188. return;
  189. }
  190. }
  191. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  192. {
  193. EVENT_HANDLER_CALLED_BY_CLIENT;
  194. waitWhileDialog();
  195. if(LOCPLINT != this)
  196. return;
  197. const CGHeroInstance * hero = cb->getHero(details.id); //object representing this hero
  198. int3 hp = details.start;
  199. if(!hero)
  200. {
  201. //AI hero left the visible area (we can't obtain info)
  202. //TODO very evil workaround -> retrieve pointer to hero so we could animate it
  203. // TODO -> we should not need full CGHeroInstance structure to display animation or it should not be handled by playerint (but by the client itself)
  204. const TerrainTile2 &tile = CGI->mh->ttiles[hp.x-1][hp.y][hp.z];
  205. for(auto & elem : tile.objects)
  206. if(elem.first->id == details.id)
  207. hero = dynamic_cast<const CGHeroInstance *>(elem.first);
  208. if(!hero) //still nothing...
  209. return;
  210. }
  211. adventureInt->centerOn(hero); //actualizing screen pos
  212. adventureInt->minimap.redraw();
  213. adventureInt->heroList.redraw();
  214. bool directlyAttackingCreature =
  215. details.attackedFrom
  216. && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
  217. && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
  218. if(makingTurn && hero->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  219. {
  220. //We may need to change music - select new track, music handler will change it if needed
  221. CCS->musich->playMusicFromSet("terrain", LOCPLINT->cb->getTile(hero->visitablePos())->terType, true);
  222. if(details.result == TryMoveHero::TELEPORTATION)
  223. {
  224. if(adventureInt->terrain.currentPath)
  225. {
  226. assert(adventureInt->terrain.currentPath->nodes.size() >= 2);
  227. std::vector<CGPathNode>::const_iterator nodesIt = adventureInt->terrain.currentPath->nodes.end() - 1;
  228. if((nodesIt)->coord == CGHeroInstance::convertPosition(details.start, false)
  229. && (nodesIt-1)->coord == CGHeroInstance::convertPosition(details.end, false))
  230. {
  231. //path was between entrance and exit of teleport -> OK, erase node as usual
  232. removeLastNodeFromPath(hero);
  233. }
  234. else
  235. {
  236. //teleport was not along current path, it'll now be invalid (hero is somewhere else)
  237. eraseCurrentPathOf(hero);
  238. }
  239. }
  240. adventureInt->heroList.update(hero);
  241. return; //teleport - no fancy moving animation
  242. //TODO: smooth disappear / appear effect
  243. }
  244. if (hero->pos != details.end //hero didn't change tile but visit succeeded
  245. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  246. {
  247. eraseCurrentPathOf(hero, false);
  248. }
  249. else if(adventureInt->terrain.currentPath && hero->pos == details.end) //&& hero is moving
  250. {
  251. if(details.start != details.end) //so we don't touch path when revisiting with spacebar
  252. removeLastNodeFromPath(hero);
  253. }
  254. }
  255. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  256. {
  257. hero->isStanding = true;
  258. stillMoveHero.setn(STOP_MOVE);
  259. GH.totalRedraw();
  260. adventureInt->heroList.update(hero);
  261. return;
  262. }
  263. initMovement(details, hero, hp);
  264. //first initializing done
  265. GH.mainFPSmng->framerateDelay(); // after first move
  266. ui32 speed = settings["adventure"]["heroSpeed"].Float();
  267. //main moving
  268. for(int i=1; i<32; i+=2*speed)
  269. {
  270. movementPxStep(details, i, hp, hero);
  271. adventureInt->updateScreen = true;
  272. adventureInt->show(screen);
  273. CSDL_Ext::update(screen);
  274. GH.mainFPSmng->framerateDelay(); //for animation purposes
  275. } //for(int i=1; i<32; i+=4)
  276. //main moving done
  277. //finishing move
  278. finishMovement(details, hp, hero);
  279. hero->isStanding = true;
  280. //move finished
  281. adventureInt->minimap.redraw();
  282. adventureInt->heroList.update(hero);
  283. //check if user cancelled movement
  284. {
  285. boost::unique_lock<boost::mutex> un(eventsM);
  286. while(!events.empty())
  287. {
  288. SDL_Event ev = events.front();
  289. events.pop();
  290. switch(ev.type)
  291. {
  292. case SDL_MOUSEBUTTONDOWN:
  293. stillMoveHero.setn(STOP_MOVE);
  294. break;
  295. case SDL_KEYDOWN:
  296. if(ev.key.keysym.sym < SDLK_F1 || ev.key.keysym.sym > SDLK_F15)
  297. stillMoveHero.setn(STOP_MOVE);
  298. break;
  299. }
  300. }
  301. }
  302. if(stillMoveHero.get() == WAITING_MOVE)
  303. stillMoveHero.setn(DURING_MOVE);
  304. // Hero attacked creature directly, set direction to face it.
  305. if (directlyAttackingCreature) {
  306. // Get direction to attacker.
  307. int3 posOffset = *details.attackedFrom - details.end + int3(2, 1, 0);
  308. static const ui8 dirLookup[3][3] = {
  309. { 1, 2, 3 },
  310. { 8, 0, 4 },
  311. { 7, 6, 5 }
  312. };
  313. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  314. const_cast<CGHeroInstance *>(hero)->moveDir = dirLookup[posOffset.y][posOffset.x];
  315. }
  316. }
  317. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  318. {
  319. EVENT_HANDLER_CALLED_BY_CLIENT;
  320. LOG_TRACE_PARAMS(logGlobal, "Hero %s killed handler for player %s", hero->name % playerID);
  321. const CArmedInstance *newSelection = nullptr;
  322. if (makingTurn)
  323. {
  324. //find new object for selection: either hero
  325. int next = adventureInt->getNextHeroIndex(vstd::find_pos(wanderingHeroes, hero));
  326. if (next >= 0)
  327. newSelection = wanderingHeroes[next];
  328. //or town
  329. if (!newSelection || newSelection == hero)
  330. {
  331. if (towns.empty())
  332. newSelection = nullptr;
  333. else
  334. newSelection = towns.front();
  335. }
  336. }
  337. wanderingHeroes -= hero;
  338. if(vstd::contains(paths, hero))
  339. paths.erase(hero);
  340. adventureInt->heroList.update(hero);
  341. if (makingTurn && newSelection)
  342. adventureInt->select(newSelection, true);
  343. else if(adventureInt->selection == hero)
  344. adventureInt->selection = nullptr;
  345. }
  346. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  347. {
  348. EVENT_HANDLER_CALLED_BY_CLIENT;
  349. wanderingHeroes.push_back(hero);
  350. adventureInt->heroList.update(hero);
  351. }
  352. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  353. {
  354. if (castleInt)
  355. castleInt->close();
  356. castleInt = new CCastleInterface(town);
  357. GH.pushInt(castleInt);
  358. }
  359. int3 CPlayerInterface::repairScreenPos(int3 pos)
  360. {
  361. if(pos.x<-CGI->mh->frameW)
  362. pos.x = -CGI->mh->frameW;
  363. if(pos.y<-CGI->mh->frameH)
  364. pos.y = -CGI->mh->frameH;
  365. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  366. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  367. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  368. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  369. return pos;
  370. }
  371. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  372. {
  373. EVENT_HANDLER_CALLED_BY_CLIENT;
  374. if(which == 4)
  375. {
  376. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  377. ctw->setExpToLevel();
  378. }
  379. else if(which < GameConstants::PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  380. updateInfo(hero);
  381. }
  382. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  383. {
  384. EVENT_HANDLER_CALLED_BY_CLIENT;
  385. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  386. if(cuw) //university window is open
  387. {
  388. GH.totalRedraw();
  389. }
  390. }
  391. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  392. {
  393. EVENT_HANDLER_CALLED_BY_CLIENT;
  394. updateInfo(hero);
  395. if(makingTurn && hero->tempOwner == playerID)
  396. adventureInt->heroList.update(hero);
  397. }
  398. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  399. {
  400. EVENT_HANDLER_CALLED_BY_CLIENT;
  401. if(makingTurn && hero->tempOwner == playerID)
  402. adventureInt->heroList.update(hero);
  403. }
  404. void CPlayerInterface::receivedResource(int type, int val)
  405. {
  406. EVENT_HANDLER_CALLED_BY_CLIENT;
  407. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  408. mw->resourceChanged(type, val);
  409. GH.totalRedraw();
  410. }
  411. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill>& skills, QueryID queryID)
  412. {
  413. EVENT_HANDLER_CALLED_BY_CLIENT;
  414. waitWhileDialog();
  415. CCS->soundh->playSound(soundBase::heroNewLevel);
  416. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,
  417. [=](ui32 selection){ cb->selectionMade(selection, queryID); });
  418. GH.pushInt(lw);
  419. }
  420. void CPlayerInterface::commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID)
  421. {
  422. EVENT_HANDLER_CALLED_BY_CLIENT;
  423. waitWhileDialog();
  424. CCS->soundh->playSound(soundBase::heroNewLevel);
  425. GH.pushInt(new CStackWindow(commander, skills, [=](ui32 selection)
  426. {
  427. cb->selectionMade(selection, queryID);
  428. }));
  429. }
  430. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  431. {
  432. EVENT_HANDLER_CALLED_BY_CLIENT;
  433. updateInfo(town);
  434. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  435. {
  436. CGI->mh->hideObject(town->garrisonHero);
  437. if (town->garrisonHero->tempOwner == playerID) // our hero
  438. wanderingHeroes -= town->garrisonHero;
  439. }
  440. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  441. {
  442. CGI->mh->printObject(town->visitingHero);
  443. if (town->visitingHero->tempOwner == playerID) // our hero
  444. wanderingHeroes.push_back(town->visitingHero);
  445. }
  446. adventureInt->heroList.update();
  447. adventureInt->updateNextHero(nullptr);
  448. if(CCastleInterface *c = castleInt)
  449. {
  450. c->garr->selectSlot(nullptr);
  451. c->garr->setArmy(town->getUpperArmy(), 0);
  452. c->garr->setArmy(town->visitingHero, 1);
  453. c->garr->recreateSlots();
  454. c->heroes->update();
  455. }
  456. for(IShowActivatable *isa : GH.listInt)
  457. {
  458. CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
  459. if (ki)
  460. {
  461. ki->townChanged(town);
  462. ki->updateGarrisons();
  463. }
  464. }
  465. GH.totalRedraw();
  466. }
  467. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  468. {
  469. EVENT_HANDLER_CALLED_BY_CLIENT;
  470. if(hero->tempOwner != playerID )
  471. return;
  472. waitWhileDialog();
  473. openTownWindow(town);
  474. }
  475. void CPlayerInterface::garrisonsChanged(std::vector<const CGObjectInstance *> objs)
  476. {
  477. boost::unique_lock<boost::recursive_mutex> un(*pim);
  478. for(auto object : objs)
  479. updateInfo(object);
  480. for(auto & elem : GH.listInt)
  481. {
  482. CGarrisonHolder *cgh = dynamic_cast<CGarrisonHolder*>(elem);
  483. if (cgh)
  484. cgh->updateGarrisons();
  485. if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(elem))
  486. {
  487. if(vstd::contains(objs, cmw->hero))
  488. cmw->garrisonChanged();
  489. }
  490. }
  491. GH.totalRedraw();
  492. }
  493. void CPlayerInterface::garrisonChanged( const CGObjectInstance * obj)
  494. {
  495. garrisonsChanged(std::vector<const CGObjectInstance *>(1, obj));
  496. }
  497. void CPlayerInterface::buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) //what: 1 - built, 2 - demolished
  498. {
  499. EVENT_HANDLER_CALLED_BY_CLIENT;
  500. switch (buildingID)
  501. {
  502. case BuildingID::FORT: case BuildingID::CITADEL: case BuildingID::CASTLE:
  503. case BuildingID::VILLAGE_HALL: case BuildingID::TOWN_HALL: case BuildingID::CITY_HALL: case BuildingID::CAPITOL:
  504. case BuildingID::RESOURCE_SILO:
  505. updateInfo(town);
  506. break;
  507. }
  508. if(!castleInt)
  509. return;
  510. if(castleInt->town!=town)
  511. return;
  512. switch(what)
  513. {
  514. case 1:
  515. CCS->soundh->playSound(soundBase::newBuilding);
  516. castleInt->addBuilding(buildingID);
  517. break;
  518. case 2:
  519. castleInt->removeBuilding(buildingID);
  520. break;
  521. }
  522. adventureInt->townList.update(town);
  523. castleInt->townlist->update(town);
  524. }
  525. void CPlayerInterface::battleStartBefore(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2)
  526. {
  527. //Don't wait for dialogs when we are non-active hot-seat player
  528. if(LOCPLINT == this)
  529. waitForAllDialogs();
  530. }
  531. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  532. {
  533. EVENT_HANDLER_CALLED_BY_CLIENT;
  534. if(settings["adventure"]["quickCombat"].Bool())
  535. {
  536. autofightingAI = CDynLibHandler::getNewBattleAI(settings["server"]["neutralAI"].String());
  537. autofightingAI->init(cb);
  538. autofightingAI->battleStart(army1, army2, int3(0,0,0), hero1, hero2, side);
  539. isAutoFightOn = true;
  540. cb->registerBattleInterface(autofightingAI);
  541. }
  542. //Don't wait for dialogs when we are non-active hot-seat player
  543. if(LOCPLINT == this)
  544. waitForAllDialogs();
  545. BATTLE_EVENT_POSSIBLE_RETURN;
  546. }
  547. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  548. {
  549. EVENT_HANDLER_CALLED_BY_CLIENT;
  550. BATTLE_EVENT_POSSIBLE_RETURN;
  551. for(auto & healedStack : healedStacks)
  552. {
  553. const CStack * healed = cb->battleGetStackByID(healedStack.first);
  554. if(battleInt->creAnims[healed->ID]->isDead())
  555. {
  556. //stack has been resurrected
  557. battleInt->creAnims[healed->ID]->setType(CCreatureAnim::HOLDING);
  558. }
  559. }
  560. if (lifeDrain)
  561. {
  562. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  563. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  564. int textOff = 0;
  565. if (attacker)
  566. {
  567. battleInt->displayEffect(52, attacker->position); //TODO: transparency
  568. if (attacker->count > 1)
  569. {
  570. textOff += 1;
  571. }
  572. CCS->soundh->playSound(soundBase::DRAINLIF);
  573. //print info about life drain
  574. char textBuf[1000];
  575. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  576. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  577. battleInt->console->addText(textBuf);
  578. }
  579. }
  580. if (tentHeal)
  581. {
  582. std::string text = CGI->generaltexth->allTexts[414];
  583. boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(lifeDrainFrom, false)->getCreature()->nameSing);
  584. boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(healedStacks[0].first, false)->getCreature()->nameSing);
  585. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(healedStacks[0].second));
  586. battleInt->console->addText(text);
  587. }
  588. }
  589. void CPlayerInterface::battleNewStackAppeared(const CStack * stack)
  590. {
  591. EVENT_HANDLER_CALLED_BY_CLIENT;
  592. BATTLE_EVENT_POSSIBLE_RETURN;
  593. battleInt->newStack(stack);
  594. }
  595. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  596. {
  597. EVENT_HANDLER_CALLED_BY_CLIENT;
  598. BATTLE_EVENT_POSSIBLE_RETURN;
  599. // for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  600. // {
  601. // for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  602. // {
  603. // if(itBat->first == *it) //remove this obstacle
  604. // {
  605. // battleInt->idToObstacle.erase(itBat);
  606. // break;
  607. // }
  608. // }
  609. // }
  610. //update accessible hexes
  611. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  612. }
  613. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  614. {
  615. EVENT_HANDLER_CALLED_BY_CLIENT;
  616. BATTLE_EVENT_POSSIBLE_RETURN;
  617. battleInt->stackIsCatapulting(ca);
  618. }
  619. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  620. {
  621. EVENT_HANDLER_CALLED_BY_CLIENT;
  622. BATTLE_EVENT_POSSIBLE_RETURN;
  623. for(auto & elem : bsr.stackIDs) //for each removed stack
  624. {
  625. battleInt->stackRemoved(elem);
  626. }
  627. }
  628. void CPlayerInterface::battleNewRound(int round) //called at the beginning of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  629. {
  630. EVENT_HANDLER_CALLED_BY_CLIENT;
  631. BATTLE_EVENT_POSSIBLE_RETURN;
  632. battleInt->newRound(round);
  633. }
  634. void CPlayerInterface::actionStarted(const BattleAction &action)
  635. {
  636. EVENT_HANDLER_CALLED_BY_CLIENT;
  637. BATTLE_EVENT_POSSIBLE_RETURN;
  638. curAction = new BattleAction(action);
  639. battleInt->startAction(curAction);
  640. }
  641. void CPlayerInterface::actionFinished(const BattleAction &action)
  642. {
  643. EVENT_HANDLER_CALLED_BY_CLIENT;
  644. BATTLE_EVENT_POSSIBLE_RETURN;
  645. battleInt->endAction(curAction);
  646. delete curAction;
  647. curAction = nullptr;
  648. }
  649. BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
  650. {
  651. THREAD_CREATED_BY_CLIENT;
  652. logGlobal->traceStream() << "Awaiting command for " << stack->nodeName();
  653. if(autofightingAI)
  654. {
  655. if(isAutoFightOn)
  656. {
  657. auto ret = autofightingAI->activeStack(stack);
  658. if(isAutoFightOn)
  659. {
  660. return ret;
  661. }
  662. }
  663. cb->unregisterBattleInterface(autofightingAI);
  664. autofightingAI.reset();
  665. }
  666. CBattleInterface *b = battleInt;
  667. if (b->givenCommand->get())
  668. {
  669. logGlobal->errorStream() << "Command buffer must be clean! (we don't want to use old command)";
  670. vstd::clear_pointer(b->givenCommand->data);
  671. }
  672. {
  673. boost::unique_lock<boost::recursive_mutex> un(*pim);
  674. b->stackActivated(stack);
  675. //Regeneration & mana drain go there
  676. }
  677. //wait till BattleInterface sets its command
  678. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  679. while(!b->givenCommand->data)
  680. {
  681. b->givenCommand->cond.wait(lock);
  682. if(!battleInt) //batle ended while we were waiting for movement (eg. because of spell)
  683. throw boost::thread_interrupted(); //will shut the thread peacefully
  684. }
  685. //tidy up
  686. BattleAction ret = *(b->givenCommand->data);
  687. delete b->givenCommand->data;
  688. b->givenCommand->data = nullptr;
  689. //return command
  690. logGlobal->traceStream() << "Giving command for " << stack->nodeName();
  691. return ret;
  692. }
  693. void CPlayerInterface::battleEnd(const BattleResult *br)
  694. {
  695. EVENT_HANDLER_CALLED_BY_CLIENT;
  696. if(isAutoFightOn)
  697. {
  698. isAutoFightOn = false;
  699. cb->unregisterBattleInterface(autofightingAI);
  700. autofightingAI.reset();
  701. if(!battleInt)
  702. {
  703. SDL_Rect temp_rect = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  704. auto resWindow = new CBattleResultWindow(*br, temp_rect, *this);
  705. GH.pushInt(resWindow);
  706. // #1490 - during AI turn when quick combat is on, we need to display the message and wait for user to close it.
  707. // Otherwise NewTurn causes freeze.
  708. waitWhileDialog();
  709. return;
  710. }
  711. }
  712. BATTLE_EVENT_POSSIBLE_RETURN;
  713. battleInt->battleFinished(*br);
  714. }
  715. void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  716. {
  717. EVENT_HANDLER_CALLED_BY_CLIENT;
  718. BATTLE_EVENT_POSSIBLE_RETURN;
  719. battleInt->stackMoved(stack, dest, distance);
  720. }
  721. void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
  722. {
  723. EVENT_HANDLER_CALLED_BY_CLIENT;
  724. BATTLE_EVENT_POSSIBLE_RETURN;
  725. battleInt->spellCast(sc);
  726. }
  727. void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
  728. {
  729. EVENT_HANDLER_CALLED_BY_CLIENT;
  730. BATTLE_EVENT_POSSIBLE_RETURN;
  731. battleInt->battleStacksEffectsSet(sse);
  732. }
  733. void CPlayerInterface::battleTriggerEffect (const BattleTriggerEffect & bte)
  734. {
  735. EVENT_HANDLER_CALLED_BY_CLIENT;
  736. //TODO why is this different (no return on LOPLINT != this) ?
  737. RETURN_IF_QUICK_COMBAT;
  738. battleInt->battleTriggerEffect(bte);
  739. }
  740. void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  741. {
  742. EVENT_HANDLER_CALLED_BY_CLIENT;
  743. BATTLE_EVENT_POSSIBLE_RETURN;
  744. std::vector<StackAttackedInfo> arg;
  745. for(auto & elem : bsa)
  746. {
  747. const CStack *defender = cb->battleGetStackByID(elem.stackAttacked, false);
  748. const CStack *attacker = cb->battleGetStackByID(elem.attackerID, false);
  749. if(elem.isEffect() && elem.effect != 12) //and not armageddon
  750. {
  751. if (defender && !elem.isSecondary())
  752. battleInt->displayEffect(elem.effect, defender->position);
  753. }
  754. //FIXME: why action is deleted during enchanter cast?
  755. bool remoteAttack = false;
  756. if (LOCPLINT->curAction)
  757. remoteAttack |= LOCPLINT->curAction->actionType != Battle::WALK_AND_ATTACK;
  758. StackAttackedInfo to_put = {defender, elem.damageAmount, elem.killedAmount, attacker, remoteAttack, elem.killed(), elem.willRebirth(), elem.cloneKilled()};
  759. arg.push_back(to_put);
  760. }
  761. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  762. {
  763. battleInt->displayEffect(bsa.begin()->effect, -1);
  764. }
  765. battleInt->stacksAreAttacked(arg);
  766. }
  767. void CPlayerInterface::battleAttack(const BattleAttack *ba)
  768. {
  769. EVENT_HANDLER_CALLED_BY_CLIENT;
  770. BATTLE_EVENT_POSSIBLE_RETURN;
  771. assert(curAction);
  772. if(ba->lucky()) //lucky hit
  773. {
  774. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  775. std::string hlp = CGI->generaltexth->allTexts[45];
  776. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  777. battleInt->console->addText(hlp);
  778. battleInt->displayEffect(18, stack->position);
  779. CCS->soundh->playSound(soundBase::GOODLUCK);
  780. }
  781. if(ba->unlucky()) //unlucky hit
  782. {
  783. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  784. std::string hlp = CGI->generaltexth->allTexts[44];
  785. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  786. battleInt->console->addText(hlp);
  787. battleInt->displayEffect(48, stack->position);
  788. CCS->soundh->playSound(soundBase::BADLUCK);
  789. }
  790. if (ba->deathBlow())
  791. {
  792. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  793. std::string hlp = CGI->generaltexth->allTexts[(stack->count != 1) ? 366 : 365];
  794. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  795. battleInt->console->addText(hlp);
  796. for (auto & elem : ba->bsa)
  797. {
  798. const CStack * attacked = cb->battleGetStackByID(elem.stackAttacked);
  799. battleInt->displayEffect(73, attacked->position);
  800. }
  801. CCS->soundh->playSound(soundBase::deathBlow);
  802. }
  803. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  804. if(ba->shot())
  805. {
  806. for(auto & elem : ba->bsa)
  807. {
  808. if (!elem.isSecondary()) //display projectile only for primary target
  809. {
  810. const CStack * attacked = cb->battleGetStackByID(elem.stackAttacked);
  811. battleInt->stackAttacking(attacker, attacked->position, attacked, true);
  812. }
  813. }
  814. }
  815. else
  816. {
  817. int shift = 0;
  818. if(ba->counter() && BattleHex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  819. {
  820. int distp = BattleHex::getDistance(curAction->destinationTile + 1, attacker->position);
  821. int distm = BattleHex::getDistance(curAction->destinationTile - 1, attacker->position);
  822. if( distp < distm )
  823. shift = 1;
  824. else
  825. shift = -1;
  826. }
  827. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  828. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  829. }
  830. }
  831. void CPlayerInterface::battleObstaclePlaced(const CObstacleInstance &obstacle)
  832. {
  833. EVENT_HANDLER_CALLED_BY_CLIENT;
  834. BATTLE_EVENT_POSSIBLE_RETURN;
  835. battleInt->obstaclePlaced(obstacle);
  836. }
  837. void CPlayerInterface::yourTacticPhase(int distance)
  838. {
  839. THREAD_CREATED_BY_CLIENT;
  840. while(battleInt && battleInt->tacticsMode)
  841. boost::this_thread::sleep(boost::posix_time::millisec(1));
  842. }
  843. void CPlayerInterface::showComp(const Component &comp, std::string message)
  844. {
  845. EVENT_HANDLER_CALLED_BY_CLIENT;
  846. waitWhileDialog(); //Fix for mantis #98
  847. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  848. adventureInt->infoBar.showComponent(comp, message);
  849. }
  850. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  851. {
  852. EVENT_HANDLER_CALLED_BY_CLIENT;
  853. if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
  854. {
  855. return;
  856. }
  857. std::vector<CComponent*> intComps;
  858. for(auto & component : components)
  859. intComps.push_back(new CComponent(*component));
  860. showInfoDialog(text,intComps,soundID);
  861. }
  862. void CPlayerInterface::showInfoDialog(const std::string &text, CComponent * component)
  863. {
  864. std::vector<CComponent*> intComps;
  865. intComps.push_back(component);
  866. showInfoDialog(text, intComps, soundBase::sound_todo, true);
  867. }
  868. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
  869. {
  870. LOG_TRACE_PARAMS(logGlobal, "player=%s, text=%s, is LOCPLINT=%d", playerID % text % (this==LOCPLINT));
  871. waitWhileDialog();
  872. if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
  873. {
  874. return;
  875. }
  876. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  877. temp->setDelComps(delComps);
  878. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  879. {
  880. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  881. showingDialog->set(true);
  882. stopMovement(); // interrupt movement to show dialog
  883. GH.pushInt(temp);
  884. }
  885. else
  886. {
  887. dialogs.push_back(temp);
  888. }
  889. }
  890. void CPlayerInterface::showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text)
  891. {
  892. EVENT_HANDLER_CALLED_BY_CLIENT;
  893. std::vector<Component*> comps;
  894. for(auto & elem : components)
  895. {
  896. comps.push_back(&elem);
  897. }
  898. std::string str;
  899. text.toString(str);
  900. showInfoDialog(str,comps, 0);
  901. waitWhileDialog();
  902. }
  903. void CPlayerInterface::showYesNoDialog(const std::string &text, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps, const std::vector<CComponent*> & components)
  904. {
  905. boost::unique_lock<boost::recursive_mutex> un(*pim);
  906. stopMovement();
  907. LOCPLINT->showingDialog->setn(true);
  908. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  909. }
  910. void CPlayerInterface::showOkDialog(std::vector<Component> & components, const MetaString & text, const boost::function<void()> & onOk)
  911. {
  912. boost::unique_lock<boost::recursive_mutex> un(*pim);
  913. std::vector<Component*> comps;
  914. for(auto & elem : components)
  915. {
  916. comps.push_back(&elem);
  917. }
  918. std::string str;
  919. text.toString(str);
  920. stopMovement();
  921. showingDialog->setn(true);
  922. std::vector<CComponent*> intComps;
  923. for(auto & component : comps)
  924. intComps.push_back(new CComponent(*component));
  925. CInfoWindow::showOkDialog(str, &intComps, onOk, true, playerID);
  926. }
  927. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, QueryID askID, int soundID, bool selection, bool cancel )
  928. {
  929. EVENT_HANDLER_CALLED_BY_CLIENT;
  930. waitWhileDialog();
  931. stopMovement();
  932. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  933. if(!selection && cancel) //simple yes/no dialog
  934. {
  935. std::vector<CComponent*> intComps;
  936. for(auto & component : components)
  937. intComps.push_back(new CComponent(component)); //will be deleted by close in window
  938. showYesNoDialog(text, [=]{ cb->selectionMade(1, askID); }, [=]{ cb->selectionMade(0, askID); }, true, intComps);
  939. }
  940. else if(selection)
  941. {
  942. std::vector<CSelectableComponent*> intComps;
  943. for(auto & component : components)
  944. intComps.push_back(new CSelectableComponent(component)); //will be deleted by CSelWindow::close
  945. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  946. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  947. if(cancel)
  948. {
  949. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  950. }
  951. int charperline = 35;
  952. if (pom.size() > 1)
  953. charperline = 50;
  954. auto temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  955. GH.pushInt(temp);
  956. intComps[0]->clickLeft(true, false);
  957. }
  958. }
  959. void CPlayerInterface::tileRevealed(const std::unordered_set<int3, ShashInt3> &pos)
  960. {
  961. EVENT_HANDLER_CALLED_BY_CLIENT;
  962. //FIXME: wait for dialog? Magi hut/eye would benefit from this but may break other areas
  963. for(auto & po : pos)
  964. adventureInt->minimap.showTile(po);
  965. if(!pos.empty())
  966. GH.totalRedraw();
  967. }
  968. void CPlayerInterface::tileHidden(const std::unordered_set<int3, ShashInt3> &pos)
  969. {
  970. EVENT_HANDLER_CALLED_BY_CLIENT;
  971. for(auto & po : pos)
  972. adventureInt->minimap.hideTile(po);
  973. if(!pos.empty())
  974. GH.totalRedraw();
  975. }
  976. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  977. {
  978. boost::unique_lock<boost::recursive_mutex> un(*pim);
  979. GH.pushInt(new CHeroWindow(hero));
  980. }
  981. /*
  982. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  983. {
  984. boost::unique_lock<boost::recursive_mutex> un(*pim);
  985. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  986. {
  987. adventureInt->heroWindow->deactivate();
  988. adventureInt->heroWindow->setHero(hero);
  989. adventureInt->heroWindow->activate();
  990. }
  991. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  992. {
  993. cew->deactivate();
  994. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  995. {
  996. if(cew->heroInst[g]->id == hero->id)
  997. {
  998. cew->heroInst[g] = hero;
  999. cew->artifs[g]->updateState = true;
  1000. cew->artifs[g]->setHero(hero);
  1001. cew->artifs[g]->updateState = false;
  1002. }
  1003. }
  1004. cew->prepareBackground();
  1005. cew->activate();
  1006. }
  1007. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  1008. {
  1009. if(caw->arts)
  1010. {
  1011. caw->deactivate();
  1012. caw->arts->updateState = true;
  1013. caw->arts->setHero(hero);
  1014. caw->arts->updateState = false;
  1015. caw->activate();
  1016. }
  1017. }
  1018. updateInfo(hero);
  1019. }*/
  1020. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  1021. {
  1022. EVENT_HANDLER_CALLED_BY_CLIENT;
  1023. if(const CGTownInstance * townObj = dynamic_cast<const CGTownInstance*>(town))
  1024. {
  1025. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  1026. if(fs)
  1027. fs->creaturesChanged();
  1028. for(IShowActivatable *isa : GH.listInt)
  1029. {
  1030. CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
  1031. if (ki && townObj)
  1032. ki->townChanged(townObj);
  1033. }
  1034. }
  1035. else if(GH.listInt.size() && (town->ID == Obj::CREATURE_GENERATOR1
  1036. || town->ID == Obj::CREATURE_GENERATOR4 || town->ID == Obj::WAR_MACHINE_FACTORY))
  1037. {
  1038. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  1039. if(crw && crw->dwelling == town)
  1040. crw->availableCreaturesChanged();
  1041. }
  1042. }
  1043. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  1044. {
  1045. EVENT_HANDLER_CALLED_BY_CLIENT;
  1046. if(bonus.type == Bonus::NONE)
  1047. return;
  1048. updateInfo(hero);
  1049. if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
  1050. {
  1051. //recalculate paths because hero has lost bonus influencing pathfinding
  1052. eraseCurrentPathOf(hero, false);
  1053. }
  1054. }
  1055. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1056. {
  1057. h & observerInDuelMode;
  1058. h & wanderingHeroes & towns & sleepingHeroes;
  1059. std::map<const CGHeroInstance *, int3> pathsMap; //hero -> dest
  1060. if(h.saving)
  1061. {
  1062. for(auto &p : paths)
  1063. {
  1064. if(p.second.nodes.size())
  1065. pathsMap[p.first] = p.second.endPos();
  1066. else
  1067. logGlobal->errorStream() << p.first->name << " has assigned an empty path! Ignoring it...";
  1068. }
  1069. h & pathsMap;
  1070. }
  1071. else
  1072. {
  1073. h & pathsMap;
  1074. CPathsInfo pathsInfo(cb->getMapSize());
  1075. for(auto &p : pathsMap)
  1076. {
  1077. cb->calculatePaths(p.first, pathsInfo);
  1078. CGPath path;
  1079. pathsInfo.getPath(p.second, path);
  1080. paths[p.first] = path;
  1081. logGlobal->traceStream() << boost::format("Restored path for hero %s leading to %s with %d nodes")
  1082. % p.first->nodeName() % p.second % path.nodes.size();
  1083. }
  1084. }
  1085. h & spellbookSettings;
  1086. }
  1087. void CPlayerInterface::saveGame( COSer<CSaveFile> &h, const int version )
  1088. {
  1089. EVENT_HANDLER_CALLED_BY_CLIENT;
  1090. serializeTempl(h,version);
  1091. }
  1092. void CPlayerInterface::loadGame( CISer<CLoadFile> &h, const int version )
  1093. {
  1094. EVENT_HANDLER_CALLED_BY_CLIENT;
  1095. serializeTempl(h,version);
  1096. firstCall = -1;
  1097. }
  1098. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  1099. {
  1100. logGlobal->traceStream() << __FUNCTION__;
  1101. if(!LOCPLINT->makingTurn)
  1102. return false;
  1103. if (!h)
  1104. return false; //can't find hero
  1105. //It shouldn't be possible to move hero with open dialog (or dialog waiting in bg)
  1106. if(showingDialog->get() || !dialogs.empty())
  1107. return false;
  1108. if (adventureInt && adventureInt->isHeroSleeping(h))
  1109. {
  1110. adventureInt->sleepWake.clickLeft(true, false);
  1111. adventureInt->sleepWake.clickLeft(false, true);
  1112. //could've just called
  1113. //adventureInt->fsleepWake();
  1114. //but no authentic button click/sound ;-)
  1115. }
  1116. int i = 1;
  1117. bool result = false; //TODO why not set to true anywhere?
  1118. {
  1119. //evil...
  1120. logGlobal->traceStream() << "before [un]locks in " << __FUNCTION__;
  1121. auto unlockEvents = vstd::makeUnlockGuard(eventsM);
  1122. auto unlockGs = vstd::makeUnlockSharedGuard(cb->getGsMutex()); //GS mutex is above PIM because CClient::run thread first locks PIM and then GS -> so this way we avoid deadlocks
  1123. auto unlockPim = vstd::makeUnlockGuard(*pim);
  1124. logGlobal->traceStream() << "after [un]locks in " << __FUNCTION__;
  1125. //TODO the above combination works... but it should all be atomic (unlock all three or none)
  1126. {
  1127. path.convert(0);
  1128. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1129. stillMoveHero.data = CONTINUE_MOVE;
  1130. ETerrainType currentTerrain = ETerrainType::BORDER; // not init yet
  1131. ETerrainType newTerrain;
  1132. int sh = -1;
  1133. const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
  1134. for(i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  1135. {
  1136. //changing z coordinate means we're moving through subterranean gate -> it's done automatically upon the visit, so we don't have to request that move here
  1137. if(path.nodes[i-1].coord.z != path.nodes[i].coord.z)
  1138. continue;
  1139. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  1140. if(path.nodes[i-1].turns)
  1141. {
  1142. stillMoveHero.data = STOP_MOVE;
  1143. break;
  1144. }
  1145. // Start a new sound for the hero movement or let the existing one carry on.
  1146. #if 0
  1147. // TODO
  1148. if (hero is flying && sh == -1)
  1149. sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
  1150. #endif
  1151. {
  1152. newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->terType;
  1153. if (newTerrain != currentTerrain)
  1154. {
  1155. CCS->soundh->stopSound(sh);
  1156. sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
  1157. currentTerrain = newTerrain;
  1158. }
  1159. }
  1160. stillMoveHero.data = WAITING_MOVE;
  1161. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1162. bool guarded = CGI->mh->map->isInTheMap(cb->getGuardingCreaturePosition(endpos - int3(1, 0, 0)));
  1163. logGlobal->traceStream() << "Requesting hero movement to " << endpos;
  1164. cb->moveHero(h,endpos);
  1165. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1166. stillMoveHero.cond.wait(un);
  1167. logGlobal->traceStream() << "Resuming " << __FUNCTION__;
  1168. if (guarded || showingDialog->get() == true) // Abort movement if a guard was fought or there is a dialog to display (Mantis #1136)
  1169. break;
  1170. }
  1171. CCS->soundh->stopSound(sh);
  1172. }
  1173. //Update cursor so icon can change if needed when it reappears; doesn;'t apply if a dialog box pops up at the end of the movement
  1174. if(!showingDialog->get())
  1175. GH.fakeMouseMove();
  1176. //RAII unlocks
  1177. }
  1178. if (adventureInt)
  1179. {
  1180. // (i == 0) means hero went through all the path
  1181. adventureInt->updateMoveHero(h, (i != 0));
  1182. adventureInt->updateNextHero(h);
  1183. }
  1184. return result;
  1185. }
  1186. bool CPlayerInterface::shiftPressed() const
  1187. {
  1188. return SDL_GetKeyState(nullptr)[SDLK_LSHIFT] || SDL_GetKeyState(nullptr)[SDLK_RSHIFT];
  1189. }
  1190. bool CPlayerInterface::altPressed() const
  1191. {
  1192. return SDL_GetKeyState(nullptr)[SDLK_LALT] || SDL_GetKeyState(nullptr)[SDLK_RALT];
  1193. }
  1194. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID)
  1195. {
  1196. EVENT_HANDLER_CALLED_BY_CLIENT;
  1197. auto onEnd = [=]{ cb->selectionMade(0, queryID); };
  1198. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  1199. {
  1200. onEnd();
  1201. return;
  1202. }
  1203. waitForAllDialogs();
  1204. auto cgw = new CGarrisonWindow(up,down,removableUnits);
  1205. cgw->quit->callback += onEnd;
  1206. GH.pushInt(cgw);
  1207. }
  1208. /**
  1209. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  1210. * into a combinational one on an artifact screen. Does not require the combination of
  1211. * artifacts to be legal.
  1212. * @param artifactID ID of a constituent artifact.
  1213. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1214. * is false.
  1215. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1216. */
  1217. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1218. {
  1219. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1220. std::string text = artifact.Description();
  1221. text += "\n\n";
  1222. std::vector<CComponent*> scs;
  1223. if (assemble) {
  1224. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1225. // You possess all of the components to...
  1226. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1227. // Picture of assembled artifact at bottom.
  1228. auto sc = new CComponent(CComponent::artifact, assembledArtifact.id, 0);
  1229. //sc->description = assembledArtifact.Description();
  1230. //sc->subtitle = assembledArtifact.Name();
  1231. scs.push_back(sc);
  1232. } else {
  1233. // Do you wish to disassemble this artifact?
  1234. text += CGI->generaltexth->allTexts[733];
  1235. }
  1236. showYesNoDialog(text, onYes, onNo, true, scs);
  1237. }
  1238. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1239. {
  1240. EVENT_HANDLER_CALLED_BY_CLIENT;
  1241. if(pa->packType == typeList.getTypeID<MoveHero>() && stillMoveHero.get() == DURING_MOVE)
  1242. stillMoveHero.setn(CONTINUE_MOVE);
  1243. }
  1244. void CPlayerInterface::heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query)
  1245. {
  1246. EVENT_HANDLER_CALLED_BY_CLIENT;
  1247. GH.pushInt(new CExchangeWindow(hero1, hero2, query));
  1248. }
  1249. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1250. {
  1251. EVENT_HANDLER_CALLED_BY_CLIENT;
  1252. //redraw minimap if owner changed
  1253. if(sop->what == ObjProperty::OWNER)
  1254. {
  1255. const CGObjectInstance * obj = cb->getObj(sop->id);
  1256. std::set<int3> pos = obj->getBlockedPos();
  1257. for(auto & po : pos)
  1258. {
  1259. if(cb->isVisible(po))
  1260. adventureInt->minimap.showTile(po);
  1261. }
  1262. if(obj->ID == Obj::TOWN)
  1263. {
  1264. if(obj->tempOwner == playerID)
  1265. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1266. else
  1267. towns -= obj;
  1268. adventureInt->townList.update();
  1269. }
  1270. assert(cb->getTownsInfo().size() == towns.size());
  1271. }
  1272. }
  1273. void CPlayerInterface::initializeHeroTownList()
  1274. {
  1275. std::vector<const CGHeroInstance*> allHeroes = cb->getHeroesInfo();
  1276. /*
  1277. std::vector <const CGHeroInstance *> newWanderingHeroes;
  1278. //applying current heroes order to new heroes info
  1279. int j;
  1280. for (int i = 0; i < wanderingHeroes.size(); i++)
  1281. if ((j = vstd::find_pos(allHeroes, wanderingHeroes[i])) >= 0)
  1282. if (!allHeroes[j]->inTownGarrison)
  1283. {
  1284. newWanderingHeroes += allHeroes[j];
  1285. allHeroes -= allHeroes[j];
  1286. }
  1287. //all the rest of new heroes go the end of the list
  1288. wanderingHeroes.clear();
  1289. wanderingHeroes = newWanderingHeroes;
  1290. newWanderingHeroes.clear();*/
  1291. for (auto & allHeroe : allHeroes)
  1292. if (!allHeroe->inTownGarrison)
  1293. wanderingHeroes += allHeroe;
  1294. std::vector<const CGTownInstance*> allTowns = cb->getTownsInfo();
  1295. /*
  1296. std::vector<const CGTownInstance*> newTowns;
  1297. for (int i = 0; i < towns.size(); i++)
  1298. if ((j = vstd::find_pos(allTowns, towns[i])) >= 0)
  1299. {
  1300. newTowns += allTowns[j];
  1301. allTowns -= allTowns[j];
  1302. }
  1303. towns.clear();
  1304. towns = newTowns;
  1305. newTowns.clear();*/
  1306. for(auto & allTown : allTowns)
  1307. towns.push_back(allTown);
  1308. if (adventureInt)
  1309. adventureInt->updateNextHero(nullptr);
  1310. }
  1311. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1312. {
  1313. EVENT_HANDLER_CALLED_BY_CLIENT;
  1314. waitWhileDialog();
  1315. auto recruitCb = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(dwelling, id, count, -1); };
  1316. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, recruitCb);
  1317. GH.pushInt(cr);
  1318. }
  1319. void CPlayerInterface::waitWhileDialog(bool unlockPim /*= true*/)
  1320. {
  1321. if(GH.amIGuiThread())
  1322. {
  1323. logGlobal->warnStream() << "Cannot wait for dialogs in gui thread (deadlock risk)!";
  1324. return;
  1325. }
  1326. auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim);
  1327. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1328. while(showingDialog->data)
  1329. showingDialog->cond.wait(un);
  1330. }
  1331. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1332. {
  1333. EVENT_HANDLER_CALLED_BY_CLIENT;
  1334. auto state = obj->shipyardStatus();
  1335. std::vector<si32> cost;
  1336. obj->getBoatCost(cost);
  1337. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), [=]{ cb->buildBoat(obj); });
  1338. GH.pushInt(csw);
  1339. }
  1340. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1341. {
  1342. EVENT_HANDLER_CALLED_BY_CLIENT;
  1343. //we might have built a boat in shipyard in opened town screen
  1344. if(obj->ID == Obj::BOAT
  1345. && LOCPLINT->castleInt
  1346. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1347. {
  1348. CCS->soundh->playSound(soundBase::newBuilding);
  1349. LOCPLINT->castleInt->addBuilding(BuildingID::SHIP);
  1350. }
  1351. }
  1352. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1353. {
  1354. EVENT_HANDLER_CALLED_BY_CLIENT;
  1355. waitWhileDialog();
  1356. adventureInt->centerOn (pos);
  1357. if(focusTime)
  1358. {
  1359. GH.totalRedraw();
  1360. CSDL_Ext::update(screen);
  1361. SDL_Delay(focusTime);
  1362. }
  1363. }
  1364. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1365. {
  1366. EVENT_HANDLER_CALLED_BY_CLIENT;
  1367. if(obj->ID == Obj::HERO && obj->tempOwner == playerID)
  1368. {
  1369. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1370. heroKilled(h);
  1371. }
  1372. }
  1373. bool CPlayerInterface::ctrlPressed() const
  1374. {
  1375. return SDL_GetKeyState(nullptr)[SDLK_LCTRL] || SDL_GetKeyState(nullptr)[SDLK_RCTRL];
  1376. }
  1377. void CPlayerInterface::update()
  1378. {
  1379. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  1380. // When ending the game, the pim mutex might be hold bo other thread,
  1381. // that will notify us about the ending game by setting terminate_cond flag.
  1382. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  1383. while(!terminate_cond.get() && !(acquiredTheLockOnPim = pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  1384. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1385. if(!acquiredTheLockOnPim)
  1386. {
  1387. // We broke the while loop above and not because of mutex, so we must be terminating.
  1388. assert(terminate_cond.get());
  1389. return;
  1390. }
  1391. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  1392. boost::unique_lock<boost::recursive_mutex> un(*pim, boost::adopt_lock);
  1393. // While mutexes were locked away we may be have stopped being the active interface
  1394. if(LOCPLINT != this)
  1395. return;
  1396. // Make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
  1397. boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
  1398. //if there are any waiting dialogs, show them
  1399. if((howManyPeople <= 1 || makingTurn) && !dialogs.empty() && !showingDialog->get())
  1400. {
  1401. showingDialog->set(true);
  1402. GH.pushInt(dialogs.front());
  1403. dialogs.pop_front();
  1404. }
  1405. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1406. if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
  1407. {
  1408. return;
  1409. }
  1410. // Handles mouse and key input
  1411. GH.updateTime();
  1412. GH.handleEvents();
  1413. if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1414. GH.totalRedraw();
  1415. else
  1416. GH.simpleRedraw();
  1417. if (settings["general"]["showfps"].Bool())
  1418. GH.drawFPSCounter();
  1419. // draw the mouse cursor and update the screen
  1420. CCS->curh->drawWithScreenRestore();
  1421. CSDL_Ext::update(screen);
  1422. CCS->curh->drawRestored();
  1423. }
  1424. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1425. {
  1426. using namespace boost::filesystem;
  1427. using namespace boost::algorithm;
  1428. path gamesDir = VCMIDirs::get().userSavePath();
  1429. std::map<std::time_t, int> dates; //save number => datestamp
  1430. directory_iterator enddir;
  1431. if(!exists(gamesDir))
  1432. create_directory(gamesDir);
  1433. for (directory_iterator dir(gamesDir); dir!=enddir; dir++)
  1434. {
  1435. if(is_regular(dir->status()))
  1436. {
  1437. std::string name = dir->path().leaf().string();
  1438. if(starts_with(name, namePrefix) && ends_with(name, ".vcgm1"))
  1439. {
  1440. char nr = name[namePrefix.size()];
  1441. if(std::isdigit(nr))
  1442. {
  1443. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1444. }
  1445. }
  1446. }
  1447. }
  1448. if(!dates.empty())
  1449. return (--dates.end())->second; //return latest file number
  1450. return 0;
  1451. }
  1452. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1453. {
  1454. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1455. {
  1456. //ho->moveDir = 1;
  1457. ho->isStanding = false;
  1458. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1459. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1460. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1461. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1462. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1463. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1464. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1465. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1466. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1467. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1468. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1469. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1470. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1471. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1472. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1473. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1474. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1475. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1476. }
  1477. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1478. {
  1479. //ho->moveDir = 2;
  1480. ho->isStanding = false;
  1481. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1482. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1483. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1484. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1485. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1486. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1487. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1488. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1489. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1490. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1491. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1492. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1493. }
  1494. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1495. {
  1496. //ho->moveDir = 3;
  1497. ho->isStanding = false;
  1498. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1499. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1500. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1501. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1502. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1503. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1504. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1505. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1506. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1507. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1508. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1509. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1510. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1511. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1512. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1513. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.end(), objectBlitOrderSorter);
  1514. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1515. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1516. }
  1517. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1518. {
  1519. //ho->moveDir = 4;
  1520. ho->isStanding = false;
  1521. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1522. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1523. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1524. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1525. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1526. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1527. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1528. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1529. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1530. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1531. }
  1532. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1533. {
  1534. //ho->moveDir = 5;
  1535. ho->isStanding = false;
  1536. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1537. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1538. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1539. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1540. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1541. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1542. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1543. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1544. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1545. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1546. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1547. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1548. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1549. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1550. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1551. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1552. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1553. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1554. }
  1555. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1556. {
  1557. //ho->moveDir = 6;
  1558. ho->isStanding = false;
  1559. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1560. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1561. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1562. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1563. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1564. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1565. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1566. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1567. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1568. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1569. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1570. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1571. }
  1572. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1573. {
  1574. //ho->moveDir = 7;
  1575. ho->isStanding = false;
  1576. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1577. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1578. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1579. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1580. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1581. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1582. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1583. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1584. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1585. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1586. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1587. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1588. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1589. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1590. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1591. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1592. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1593. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), objectBlitOrderSorter);
  1594. }
  1595. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1596. {
  1597. //ho->moveDir = 8;
  1598. ho->isStanding = false;
  1599. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1600. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1601. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1602. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1603. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1604. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1605. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1606. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1607. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), objectBlitOrderSorter);
  1608. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), objectBlitOrderSorter);
  1609. }
  1610. }
  1611. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1612. {
  1613. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1614. {
  1615. //setting advmap shift
  1616. adventureInt->terrain.moveX = i-32;
  1617. adventureInt->terrain.moveY = i-32;
  1618. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1619. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1620. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1621. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1622. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1623. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1624. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1625. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1626. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1627. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1628. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1629. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1630. }
  1631. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1632. {
  1633. //setting advmap shift
  1634. adventureInt->terrain.moveY = i-32;
  1635. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1636. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1637. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1638. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1639. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1640. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1641. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1642. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1643. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1644. }
  1645. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1646. {
  1647. //setting advmap shift
  1648. adventureInt->terrain.moveX = -i+32;
  1649. adventureInt->terrain.moveY = i-32;
  1650. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1651. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1652. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1653. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1654. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1655. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1656. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1657. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1658. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1659. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1660. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1661. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1662. }
  1663. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1664. {
  1665. //setting advmap shift
  1666. adventureInt->terrain.moveX = -i+32;
  1667. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1668. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1669. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1670. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1671. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1672. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1673. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1674. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1675. }
  1676. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1677. {
  1678. //setting advmap shift
  1679. adventureInt->terrain.moveX = -i+32;
  1680. adventureInt->terrain.moveY = -i+32;
  1681. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1682. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1683. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1684. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1685. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1686. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1687. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1688. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1689. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1690. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1691. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1692. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1693. }
  1694. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1695. {
  1696. //setting advmap shift
  1697. adventureInt->terrain.moveY = -i+32;
  1698. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1699. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1700. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1701. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1702. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1703. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1704. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1705. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1706. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1707. }
  1708. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1709. {
  1710. //setting advmap shift
  1711. adventureInt->terrain.moveX = i-32;
  1712. adventureInt->terrain.moveY = -i+32;
  1713. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1714. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1715. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1716. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1717. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1718. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1719. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1720. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1721. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1722. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1723. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1724. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1725. }
  1726. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1727. {
  1728. //setting advmap shift
  1729. adventureInt->terrain.moveX = i-32;
  1730. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1731. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1732. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1733. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1734. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1735. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1736. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1737. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1738. }
  1739. }
  1740. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1741. {
  1742. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1743. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1744. {
  1745. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1746. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1747. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1748. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1749. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1750. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1751. }
  1752. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1753. {
  1754. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1755. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1756. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1757. }
  1758. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1759. {
  1760. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1761. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1762. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1763. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1764. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1765. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1766. }
  1767. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1768. {
  1769. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1770. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1771. }
  1772. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1773. {
  1774. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1775. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1776. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1777. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1778. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1779. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1780. }
  1781. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1782. {
  1783. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1784. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1785. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1786. }
  1787. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1788. {
  1789. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1790. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1791. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1792. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1793. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1794. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1795. }
  1796. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1797. {
  1798. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1799. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1800. }
  1801. //restoring good rects
  1802. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1803. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1804. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1805. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1806. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1807. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1808. //restoring good order of objects
  1809. std::stable_sort(CGI->mh->ttiles[details.end.x-2][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-2][details.end.y-1][details.end.z].objects.end(), objectBlitOrderSorter);
  1810. std::stable_sort(CGI->mh->ttiles[details.end.x-1][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-1][details.end.y-1][details.end.z].objects.end(), objectBlitOrderSorter);
  1811. std::stable_sort(CGI->mh->ttiles[details.end.x][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x][details.end.y-1][details.end.z].objects.end(), objectBlitOrderSorter);
  1812. std::stable_sort(CGI->mh->ttiles[details.end.x-2][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-2][details.end.y][details.end.z].objects.end(), objectBlitOrderSorter);
  1813. std::stable_sort(CGI->mh->ttiles[details.end.x-1][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-1][details.end.y][details.end.z].objects.end(), objectBlitOrderSorter);
  1814. std::stable_sort(CGI->mh->ttiles[details.end.x][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x][details.end.y][details.end.z].objects.end(), objectBlitOrderSorter);
  1815. }
  1816. void CPlayerInterface::gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult )
  1817. {
  1818. EVENT_HANDLER_CALLED_BY_CLIENT;
  1819. if(player == playerID)
  1820. {
  1821. if(victoryLossCheckResult.loss())
  1822. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1823. if(LOCPLINT == this)
  1824. {
  1825. GH.curInt = this; //waiting for dialogs requires this to get events
  1826. waitForAllDialogs(); //wait till all dialogs are displayed and closed
  1827. }
  1828. --howManyPeople;
  1829. if(howManyPeople == 0) //all human players eliminated
  1830. {
  1831. if(adventureInt)
  1832. {
  1833. terminate_cond.setn(true);
  1834. adventureInt->deactivate();
  1835. if(GH.topInt() == adventureInt)
  1836. GH.popInt(adventureInt);
  1837. delete adventureInt;
  1838. adventureInt = nullptr;
  1839. }
  1840. }
  1841. if(cb->getStartInfo()->mode == StartInfo::CAMPAIGN)
  1842. {
  1843. // if you lose the campaign go back to the main menu
  1844. // campaign wins are handled in proposeNextMission
  1845. if(victoryLossCheckResult.loss()) requestReturningToMainMenu();
  1846. }
  1847. else
  1848. {
  1849. if(howManyPeople == 0) //all human players eliminated
  1850. {
  1851. requestReturningToMainMenu();
  1852. }
  1853. else if(victoryLossCheckResult.victory() && LOCPLINT == this) // end game if current human player has won
  1854. {
  1855. requestReturningToMainMenu();
  1856. }
  1857. }
  1858. if(GH.curInt == this) GH.curInt = nullptr;
  1859. }
  1860. else
  1861. {
  1862. if(victoryLossCheckResult.loss() && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME) //enemy has lost
  1863. {
  1864. std::string str = victoryLossCheckResult.messageToSelf;
  1865. boost::algorithm::replace_first(str, "%s", CGI->generaltexth->capColors[player.getNum()]);
  1866. showInfoDialog(str, std::vector<CComponent*>(1, new CComponent(CComponent::flag, player.getNum(), 0)));
  1867. }
  1868. }
  1869. }
  1870. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1871. {
  1872. EVENT_HANDLER_CALLED_BY_CLIENT;
  1873. }
  1874. void CPlayerInterface::showPuzzleMap()
  1875. {
  1876. EVENT_HANDLER_CALLED_BY_CLIENT;
  1877. waitWhileDialog();
  1878. //TODO: interface should not know the real position of Grail...
  1879. double ratio = 0;
  1880. int3 grailPos = cb->getGrailPos(ratio);
  1881. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1882. }
  1883. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1884. {
  1885. EVENT_HANDLER_CALLED_BY_CLIENT;
  1886. if (spellID == SpellID::FLY || spellID == SpellID::WATER_WALK)
  1887. {
  1888. eraseCurrentPathOf(caster, false);
  1889. }
  1890. const CSpell * spell = CGI->spellh->objects[spellID];
  1891. auto castSoundPath = spell->getCastSound();
  1892. if (!castSoundPath.empty())
  1893. CCS->soundh->playSound(castSoundPath);
  1894. }
  1895. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1896. {
  1897. if(checkForExistanceOfPath)
  1898. {
  1899. assert(vstd::contains(paths, ho));
  1900. }
  1901. else if (!vstd::contains(paths, ho))
  1902. {
  1903. return;
  1904. }
  1905. assert(ho == adventureInt->selection);
  1906. paths.erase(ho);
  1907. adventureInt->terrain.currentPath = nullptr;
  1908. adventureInt->updateMoveHero(ho, false);
  1909. }
  1910. void CPlayerInterface::removeLastNodeFromPath(const CGHeroInstance *ho)
  1911. {
  1912. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1913. if(adventureInt->terrain.currentPath->nodes.size() < 2) //if it was the last one, remove entire path and path with only one tile is not a real path
  1914. eraseCurrentPathOf(ho);
  1915. }
  1916. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1917. {
  1918. if(vstd::contains(paths,h)) //hero has assigned path
  1919. {
  1920. CGPath &path = paths[h];
  1921. if(!path.nodes.size())
  1922. {
  1923. logGlobal->warnStream() << "Warning: empty path found...";
  1924. paths.erase(h);
  1925. }
  1926. else
  1927. {
  1928. assert(h->getPosition(false) == path.startPos());
  1929. //update the hero path in case of something has changed on map
  1930. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1931. return &path;
  1932. else
  1933. paths.erase(h);
  1934. }
  1935. }
  1936. return nullptr;
  1937. }
  1938. void CPlayerInterface::acceptTurn()
  1939. {
  1940. if(settings["session"]["autoSkip"].Bool())
  1941. {
  1942. while(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1943. iw->close();
  1944. }
  1945. waitWhileDialog();
  1946. if(howManyPeople > 1)
  1947. adventureInt->startTurn();
  1948. adventureInt->heroList.update();
  1949. adventureInt->townList.update();
  1950. const CGHeroInstance * heroToSelect = nullptr;
  1951. // find first non-sleeping hero
  1952. for (auto hero : wanderingHeroes)
  1953. {
  1954. if (boost::range::find(sleepingHeroes, hero) == sleepingHeroes.end())
  1955. {
  1956. heroToSelect = hero;
  1957. break;
  1958. }
  1959. }
  1960. //select first hero if available.
  1961. if(heroToSelect != nullptr)
  1962. {
  1963. adventureInt->select(heroToSelect);
  1964. }
  1965. else
  1966. adventureInt->select(towns.front());
  1967. //show new day animation and sound on infobar
  1968. adventureInt->infoBar.showDate();
  1969. adventureInt->updateNextHero(nullptr);
  1970. adventureInt->showAll(screen);
  1971. if(settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
  1972. {
  1973. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1974. iw->close();
  1975. adventureInt->endTurn.callback();
  1976. }
  1977. // warn player if he has no town
  1978. if(cb->howManyTowns() == 0)
  1979. {
  1980. auto playerColor = *cb->getPlayerID();
  1981. std::vector<Component> components;
  1982. components.push_back(Component(Component::FLAG, playerColor.getNum(), 0, 0));
  1983. MetaString text;
  1984. auto daysWithoutCastle = *cb->getPlayer(playerColor)->daysWithoutCastle;
  1985. if (daysWithoutCastle < 6)
  1986. {
  1987. text.addTxt(MetaString::ARRAY_TXT,128); //%s, you only have %d days left to capture a town or you will be banished from this land.
  1988. text.addReplacement(MetaString::COLOR, playerColor.getNum());
  1989. text.addReplacement(7 - daysWithoutCastle);
  1990. }
  1991. else if(daysWithoutCastle == 6)
  1992. {
  1993. text.addTxt(MetaString::ARRAY_TXT,129); //%s, this is your last day to capture a town or you will be banished from this land.
  1994. text.addReplacement(MetaString::COLOR, playerColor.getNum());
  1995. }
  1996. showInfoDialogAndWait(components, text);
  1997. }
  1998. }
  1999. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  2000. {
  2001. std::string hlp;
  2002. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  2003. int msgToShow = -1;
  2004. CGHeroInstance::ECanDig isDiggingPossible = h->diggingStatus();
  2005. if(hlp.length())
  2006. isDiggingPossible = CGHeroInstance::TILE_OCCUPIED; //TODO integrate with canDig
  2007. switch(isDiggingPossible)
  2008. {
  2009. case CGHeroInstance::CAN_DIG:
  2010. break;
  2011. case CGHeroInstance::LACK_OF_MOVEMENT:
  2012. msgToShow = 56; //"Digging for artifacts requires a whole day, try again tomorrow."
  2013. break;
  2014. case CGHeroInstance::TILE_OCCUPIED:
  2015. msgToShow = 97; //Try searching on clear ground.
  2016. break;
  2017. case CGHeroInstance::WRONG_TERRAIN:
  2018. msgToShow = 60; ////Try looking on land!
  2019. break;
  2020. default:
  2021. assert(0);
  2022. }
  2023. if(msgToShow < 0)
  2024. cb->dig(h);
  2025. else
  2026. showInfoDialog(CGI->generaltexth->allTexts[msgToShow]);
  2027. }
  2028. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  2029. {
  2030. adventureInt->infoBar.showSelection();
  2031. }
  2032. void CPlayerInterface::battleNewRoundFirst( int round )
  2033. {
  2034. EVENT_HANDLER_CALLED_BY_CLIENT;
  2035. BATTLE_EVENT_POSSIBLE_RETURN;
  2036. battleInt->newRoundFirst(round);
  2037. }
  2038. void CPlayerInterface::stopMovement()
  2039. {
  2040. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  2041. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  2042. }
  2043. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  2044. {
  2045. EVENT_HANDLER_CALLED_BY_CLIENT;
  2046. if(market->o->ID == Obj::ALTAR_OF_SACRIFICE)
  2047. {
  2048. //EEMarketMode mode = market->availableModes().front();
  2049. if(market->allowsTrade(EMarketMode::ARTIFACT_EXP) && visitor->getAlignment() != EAlignment::EVIL)
  2050. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::ARTIFACT_EXP));
  2051. else if(market->allowsTrade(EMarketMode::CREATURE_EXP) && visitor->getAlignment() != EAlignment::GOOD)
  2052. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::CREATURE_EXP));
  2053. }
  2054. else
  2055. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  2056. }
  2057. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  2058. {
  2059. EVENT_HANDLER_CALLED_BY_CLIENT;
  2060. auto cuw = new CUniversityWindow(visitor, market);
  2061. GH.pushInt(cuw);
  2062. }
  2063. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  2064. {
  2065. EVENT_HANDLER_CALLED_BY_CLIENT;
  2066. auto chfw = new CHillFortWindow(visitor, object);
  2067. GH.pushInt(chfw);
  2068. }
  2069. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= nullptr*/)
  2070. {
  2071. EVENT_HANDLER_CALLED_BY_CLIENT;
  2072. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  2073. cmw->artifactsChanged(false);
  2074. }
  2075. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  2076. {
  2077. EVENT_HANDLER_CALLED_BY_CLIENT;
  2078. auto tv = new CTavernWindow(townOrTavern);
  2079. GH.pushInt(tv);
  2080. }
  2081. void CPlayerInterface::showThievesGuildWindow (const CGObjectInstance * obj)
  2082. {
  2083. EVENT_HANDLER_CALLED_BY_CLIENT;
  2084. auto tgw = new CThievesGuildWindow(obj);
  2085. GH.pushInt(tgw);
  2086. }
  2087. void CPlayerInterface::showQuestLog()
  2088. {
  2089. EVENT_HANDLER_CALLED_BY_CLIENT;
  2090. CQuestLog * ql = new CQuestLog (LOCPLINT->cb->getMyQuests());
  2091. GH.pushInt (ql);
  2092. }
  2093. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  2094. {
  2095. if(obj->shipyardStatus() != IBoatGenerator::GOOD)
  2096. {
  2097. MetaString txt;
  2098. obj->getProblemText(txt);
  2099. showInfoDialog(txt.toString());
  2100. }
  2101. else
  2102. showShipyardDialog(obj);
  2103. }
  2104. void CPlayerInterface::requestReturningToMainMenu()
  2105. {
  2106. sendCustomEvent(RETURN_TO_MAIN_MENU);
  2107. cb->unregisterAllInterfaces();
  2108. }
  2109. void CPlayerInterface::requestStoppingClient()
  2110. {
  2111. sendCustomEvent(STOP_CLIENT);
  2112. }
  2113. void CPlayerInterface::sendCustomEvent( int code )
  2114. {
  2115. CGuiHandler::pushSDLEvent(SDL_USEREVENT, code);
  2116. }
  2117. void CPlayerInterface::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
  2118. {
  2119. EVENT_HANDLER_CALLED_BY_CLIENT;
  2120. garrisonChanged(location.army);
  2121. }
  2122. void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
  2123. {
  2124. EVENT_HANDLER_CALLED_BY_CLIENT;
  2125. garrisonChanged(location.army);
  2126. }
  2127. void CPlayerInterface::stacksErased(const StackLocation &location)
  2128. {
  2129. EVENT_HANDLER_CALLED_BY_CLIENT;
  2130. garrisonChanged(location.army);
  2131. }
  2132. void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  2133. {
  2134. EVENT_HANDLER_CALLED_BY_CLIENT;
  2135. std::vector<const CGObjectInstance *> objects;
  2136. objects.push_back(loc1.army);
  2137. if(loc2.army != loc1.army)
  2138. objects.push_back(loc2.army);
  2139. garrisonsChanged(objects);
  2140. }
  2141. void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  2142. {
  2143. EVENT_HANDLER_CALLED_BY_CLIENT;
  2144. garrisonChanged(location.army);
  2145. }
  2146. void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  2147. {
  2148. EVENT_HANDLER_CALLED_BY_CLIENT;
  2149. std::vector<const CGObjectInstance *> objects;
  2150. objects.push_back(src.army);
  2151. if(src.army != dst.army)
  2152. objects.push_back(dst.army);
  2153. garrisonsChanged(objects);
  2154. }
  2155. void CPlayerInterface::artifactPut(const ArtifactLocation &al)
  2156. {
  2157. EVENT_HANDLER_CALLED_BY_CLIENT;
  2158. }
  2159. void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
  2160. {
  2161. EVENT_HANDLER_CALLED_BY_CLIENT;
  2162. adventureInt->infoBar.showSelection();
  2163. for(IShowActivatable *isa : GH.listInt)
  2164. {
  2165. auto artWin = dynamic_cast<CArtifactHolder*>(isa);
  2166. if(artWin)
  2167. artWin->artifactRemoved(al);
  2168. }
  2169. }
  2170. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  2171. {
  2172. EVENT_HANDLER_CALLED_BY_CLIENT;
  2173. adventureInt->infoBar.showSelection();
  2174. for(IShowActivatable *isa : GH.listInt)
  2175. {
  2176. auto artWin = dynamic_cast<CArtifactHolder*>(isa);
  2177. if(artWin)
  2178. artWin->artifactMoved(src, dst);
  2179. }
  2180. }
  2181. void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
  2182. {
  2183. EVENT_HANDLER_CALLED_BY_CLIENT;
  2184. adventureInt->infoBar.showSelection();
  2185. for(IShowActivatable *isa : GH.listInt)
  2186. {
  2187. auto artWin = dynamic_cast<CArtifactHolder*>(isa);
  2188. if(artWin)
  2189. artWin->artifactAssembled(al);
  2190. }
  2191. }
  2192. void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
  2193. {
  2194. EVENT_HANDLER_CALLED_BY_CLIENT;
  2195. adventureInt->infoBar.showSelection();
  2196. for(IShowActivatable *isa : GH.listInt)
  2197. {
  2198. auto artWin = dynamic_cast<CArtifactHolder*>(isa);
  2199. if(artWin)
  2200. artWin->artifactDisassembled(al);
  2201. }
  2202. }
  2203. void CPlayerInterface::playerStartsTurn(PlayerColor player)
  2204. {
  2205. EVENT_HANDLER_CALLED_BY_CLIENT;
  2206. adventureInt->infoBar.showSelection();
  2207. if (!vstd::contains (GH.listInt, adventureInt))
  2208. {
  2209. GH.popInts (GH.listInt.size()); //after map load - remove everything else
  2210. GH.pushInt (adventureInt);
  2211. }
  2212. else
  2213. {
  2214. while (GH.listInt.front() != adventureInt && !dynamic_cast<CInfoWindow*>(GH.listInt.front())) //don't remove dialogs that expect query answer
  2215. GH.popInts(1);
  2216. }
  2217. if(howManyPeople == 1)
  2218. {
  2219. GH.curInt = this;
  2220. adventureInt->startTurn();
  2221. }
  2222. if(player != playerID && this == LOCPLINT)
  2223. {
  2224. waitWhileDialog();
  2225. adventureInt->aiTurnStarted();
  2226. }
  2227. }
  2228. void CPlayerInterface::waitForAllDialogs(bool unlockPim /*= true*/)
  2229. {
  2230. while(!dialogs.empty())
  2231. {
  2232. auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim);
  2233. SDL_Delay(5);
  2234. }
  2235. waitWhileDialog(unlockPim);
  2236. }
  2237. void CPlayerInterface::proposeLoadingGame()
  2238. {
  2239. showYesNoDialog(CGI->generaltexth->allTexts[68], [this] { sendCustomEvent(RETURN_TO_MENU_LOAD); }, 0, false);
  2240. }
  2241. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  2242. {
  2243. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  2244. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  2245. }