CPlayerInterface.cpp 88 KB

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