2
0

CPlayerInterface.cpp 91 KB

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