CPlayerInterface.cpp 88 KB

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