CPlayerInterface.cpp 83 KB

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