CPlayerInterface.cpp 88 KB

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