CPlayerInterface.cpp 85 KB

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