CPlayerInterface.cpp 81 KB

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