CPlayerInterface.cpp 90 KB

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