CPlayerInterface.cpp 91 KB

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