CPlayerInterface.cpp 88 KB

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