CPlayerInterface.cpp 89 KB

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