CPlayerInterface.cpp 96 KB

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