CPlayerInterface.cpp 88 KB

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