CPlayerInterface.cpp 94 KB

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