CPlayerInterface.cpp 89 KB

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