2
0

CPlayerInterface.cpp 85 KB

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