2
0

CPlayerInterface.cpp 84 KB

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