CPlayerInterface.cpp 96 KB

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