CPlayerInterface.cpp 88 KB

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