CPlayerInterface.cpp 81 KB

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