2
0

CPlayerInterface.cpp 93 KB

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