CPlayerInterface.cpp 83 KB

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