CPlayerInterface.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. #include "../stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "../CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CKingdomInterface.h"
  7. #include "CCursorHandler.h"
  8. #include "CGameInfo.h"
  9. #include "CHeroWindow.h"
  10. #include "CMessage.h"
  11. #include "CPlayerInterface.h"
  12. //#include "SDL_Extensions.h"
  13. #include "SDL_Extensions.h"
  14. //#include "SDL_framerate.h"
  15. #include "SDL_framerate.h"
  16. #include "CConfigHandler.h"
  17. #include "CCreatureAnimation.h"
  18. #include "Graphics.h"
  19. #include "../hch/CArtHandler.h"
  20. #include "../hch/CGeneralTextHandler.h"
  21. #include "../hch/CHeroHandler.h"
  22. #include "../hch/CLodHandler.h"
  23. #include "../hch/CObjectHandler.h"
  24. #include "../lib/Connection.h"
  25. #include "../hch/CSpellHandler.h"
  26. #include "../hch/CTownHandler.h"
  27. #include "../lib/CondSh.h"
  28. #include "../lib/NetPacks.h"
  29. #include "../lib/map.h"
  30. #include "../lib/VCMIDirs.h"
  31. #include "mapHandler.h"
  32. #include "../timeHandler.h"
  33. #include <boost/lexical_cast.hpp>
  34. #include <boost/format.hpp>
  35. #include <boost/algorithm/string.hpp>
  36. #include <boost/algorithm/string/replace.hpp>
  37. #include <boost/assign/std/vector.hpp>
  38. #include <boost/assign/list_of.hpp>
  39. #include <boost/date_time/posix_time/posix_time.hpp>
  40. #include <boost/thread.hpp>
  41. #include <cmath>
  42. #include <queue>
  43. #include <sstream>
  44. #include <boost/filesystem.hpp>
  45. #include "../StartInfo.h"
  46. #ifdef min
  47. #undef min
  48. #endif
  49. #ifdef max
  50. #undef max
  51. #endif
  52. /*
  53. * CPlayerInterface.cpp, part of VCMI engine
  54. *
  55. * Authors: listed in file AUTHORS in main folder
  56. *
  57. * License: GNU General Public License v2.0 or later
  58. * Full text of license available in license.txt file, in main folder
  59. *
  60. */
  61. using namespace boost::assign;
  62. using namespace CSDL_Ext;
  63. void processCommand(const std::string &message, CClient *&client);
  64. extern Point screenLTmax;
  65. extern std::queue<SDL_Event*> events;
  66. extern boost::mutex eventsM;
  67. CPlayerInterface * LOCPLINT;
  68. CBattleInterface * CPlayerInterface::battleInt;
  69. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  70. CondSh<EMoveState> stillMoveHero; //used during hero movement
  71. int CPlayerInterface::howManyPeople = 0;
  72. struct OCM_HLP_CGIN
  73. {
  74. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  75. {
  76. return (*a.first)<(*b.first);
  77. }
  78. } ocmptwo_cgin ;
  79. CPlayerInterface::CPlayerInterface(int Player)
  80. {
  81. howManyPeople++;
  82. GH.defActionsDef = 0;
  83. LOCPLINT = this;
  84. curAction = NULL;
  85. playerID=Player;
  86. human=true;
  87. castleInt = NULL;
  88. battleInt = NULL;
  89. pim = new boost::recursive_mutex;
  90. makingTurn = false;
  91. showingDialog = new CondSh<bool>(false);
  92. sysOpts = GDefaultOptions;
  93. //initializing framerate keeper
  94. mainFPSmng = new FPSmanager;
  95. SDL_initFramerate(mainFPSmng);
  96. SDL_setFramerate(mainFPSmng, 48);
  97. //framerate keeper initialized
  98. cingconsole = new CInGameConsole;
  99. terminate_cond.set(false);
  100. firstCall = 1; //if loading will be overwritten in serialize
  101. autosaveCount = 0;
  102. }
  103. CPlayerInterface::~CPlayerInterface()
  104. {
  105. howManyPeople--;
  106. delete pim;
  107. delete showingDialog;
  108. delete mainFPSmng;
  109. if(adventureInt)
  110. {
  111. if(adventureInt->active & CIntObject::KEYBOARD)
  112. adventureInt->deactivateKeys();
  113. delete adventureInt;
  114. adventureInt = NULL;
  115. }
  116. if(cingconsole->active) //TODO
  117. cingconsole->deactivate();
  118. delete cingconsole;
  119. LOCPLINT = NULL;
  120. }
  121. void CPlayerInterface::init(ICallback * CB)
  122. {
  123. cb = dynamic_cast<CCallback*>(CB);
  124. if(!adventureInt)
  125. adventureInt = new CAdvMapInt();
  126. if(!towns.size() && !wanderingHeroes.size())
  127. {
  128. recreateHeroTownList();
  129. }
  130. }
  131. void CPlayerInterface::yourTurn()
  132. {
  133. {
  134. boost::unique_lock<boost::recursive_mutex> un(*pim);
  135. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  136. LOCPLINT = this;
  137. GH.curInt = this;
  138. adventureInt->selection = NULL;
  139. if(firstCall)
  140. {
  141. if(howManyPeople == 1)
  142. adventureInt->setPlayer(playerID);
  143. autosaveCount = getLastIndex("Autosave_");
  144. if(!GH.listInt.size())
  145. {
  146. GH.pushInt(adventureInt);
  147. adventureInt->activateKeys();
  148. }
  149. if(firstCall > 0) //new game, not loaded
  150. {
  151. int index = getLastIndex("Newgame_Autosave_");
  152. index %= SAVES_COUNT;
  153. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  154. }
  155. firstCall = 0;
  156. }
  157. else
  158. {
  159. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  160. autosaveCount %= 5;
  161. }
  162. if(adventureInt->player != playerID)
  163. adventureInt->setPlayer(playerID);
  164. if(howManyPeople > 1) //hot seat message
  165. {
  166. adventureInt->startHotSeatWait(playerID);
  167. makingTurn = true;
  168. std::string msg = CGI->generaltexth->allTexts[13];
  169. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  170. std::vector<SComponent*> cmp;
  171. cmp.push_back(new SComponent(SComponent::flag, playerID, 0));
  172. showInfoDialog(msg, cmp);
  173. }
  174. else
  175. {
  176. makingTurn = true;
  177. adventureInt->startTurn();
  178. }
  179. }
  180. acceptTurn();
  181. }
  182. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  183. {
  184. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  185. for(int h=0; h<hlp.objects.size(); ++h)
  186. if(hlp.objects[h].first->id==hid)
  187. {
  188. hlp.objects[h].second = r;
  189. return;
  190. }
  191. }
  192. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  193. {
  194. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  195. for(int h=0; h<hlp.objects.size(); ++h)
  196. if(hlp.objects[h].first->id==hid)
  197. {
  198. hlp.objects.erase(hlp.objects.begin()+h);
  199. return;
  200. }
  201. }
  202. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  203. {
  204. if(LOCPLINT != this)
  205. return;
  206. boost::unique_lock<boost::recursive_mutex> un(*pim);
  207. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  208. int3 hp = details.start;
  209. adventureInt->centerOn(ho); //actualizing screen pos
  210. adventureInt->minimap.draw(screen2);
  211. adventureInt->heroList.draw(screen2);
  212. bool directlyAttackingCreature =
  213. CGI->mh->map->isInTheMap(details.attackedFrom)
  214. && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
  215. && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
  216. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  217. {
  218. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  219. {
  220. if(adventureInt->terrain.currentPath)
  221. eraseCurrentPathOf(ho);
  222. return; //teleport - no fancy moving animation
  223. //TODO: smooth disappear / appear effect
  224. }
  225. if (details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED //hero didn't change tile but visit succeeded
  226. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  227. {
  228. eraseCurrentPathOf(ho);
  229. }
  230. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  231. {
  232. //remove one node from the path (the one we went)
  233. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  234. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  235. eraseCurrentPathOf(ho);
  236. }
  237. }
  238. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  239. {
  240. ho->isStanding = true;
  241. stillMoveHero.setn(STOP_MOVE);
  242. GH.totalRedraw();
  243. return;
  244. }
  245. initMovement(details, ho, hp);
  246. //first initializing done
  247. SDL_framerateDelay(mainFPSmng); // after first move
  248. //main moving
  249. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  250. {
  251. movementPxStep(details, i, hp, ho);
  252. adventureInt->updateScreen = true;
  253. adventureInt->show(screen);
  254. CSDL_Ext::update(screen);
  255. SDL_framerateDelay(mainFPSmng); //for animation purposes
  256. } //for(int i=1; i<32; i+=4)
  257. //main moving done
  258. //finishing move
  259. finishMovement(details, hp, ho);
  260. ho->isStanding = true;
  261. //move finished
  262. adventureInt->minimap.draw(screen2);
  263. adventureInt->heroList.updateMove(ho);
  264. //check if user cancelled movement
  265. {
  266. boost::unique_lock<boost::mutex> un(eventsM);
  267. while(events.size())
  268. {
  269. SDL_Event *ev = events.front();
  270. events.pop();
  271. switch(ev->type)
  272. {
  273. case SDL_MOUSEBUTTONDOWN:
  274. stillMoveHero.setn(STOP_MOVE);
  275. break;
  276. case SDL_KEYDOWN:
  277. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  278. stillMoveHero.setn(STOP_MOVE);
  279. break;
  280. }
  281. delete ev;
  282. }
  283. }
  284. if(stillMoveHero.get() == WAITING_MOVE)
  285. stillMoveHero.setn(DURING_MOVE);
  286. // Hero attacked creature directly, set direction to face it.
  287. if (directlyAttackingCreature) {
  288. // Get direction to attacker.
  289. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  290. static const ui8 dirLookup[3][3] = {
  291. { 1, 2, 3 },
  292. { 8, 0, 4 },
  293. { 7, 6, 5 }
  294. };
  295. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  296. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  297. }
  298. }
  299. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  300. {
  301. boost::unique_lock<boost::recursive_mutex> un(*pim);
  302. wanderingHeroes -= hero;
  303. adventureInt->heroList.updateHList(hero);
  304. }
  305. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  306. {
  307. boost::unique_lock<boost::recursive_mutex> un(*pim);
  308. wanderingHeroes.push_back(hero);
  309. adventureInt->heroList.updateHList();
  310. }
  311. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  312. {
  313. if (castleInt)
  314. GH.popIntTotally(castleInt);
  315. castleInt = new CCastleInterface(town);
  316. GH.pushInt(castleInt);
  317. }
  318. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  319. {
  320. if(!specific)
  321. specific = adventureInt->selection;
  322. assert(specific);
  323. switch(specific->ID)
  324. {
  325. case HEROI_TYPE:
  326. {
  327. InfoAboutHero iah;
  328. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  329. assert(gotInfo);
  330. return graphics->drawHeroInfoWin(iah);
  331. }
  332. case TOWNI_TYPE:
  333. case 33: // Garrison
  334. case 219:
  335. {
  336. InfoAboutTown iah;
  337. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  338. assert(gotInfo);
  339. return graphics->drawTownInfoWin(iah);
  340. }
  341. default:
  342. return NULL;
  343. }
  344. }
  345. int3 CPlayerInterface::repairScreenPos(int3 pos)
  346. {
  347. if(pos.x<-CGI->mh->frameW)
  348. pos.x = -CGI->mh->frameW;
  349. if(pos.y<-CGI->mh->frameH)
  350. pos.y = -CGI->mh->frameH;
  351. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  352. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  353. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  354. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  355. return pos;
  356. }
  357. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  358. {
  359. boost::unique_lock<boost::recursive_mutex> un(*pim);
  360. if(which == 4)
  361. {
  362. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  363. ctw->setExpToLevel();
  364. }
  365. else if(which < PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  366. updateInfo(hero);
  367. }
  368. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  369. {
  370. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  371. if(cuw) //university window is open
  372. {
  373. GH.totalRedraw();
  374. }
  375. }
  376. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  377. {
  378. boost::unique_lock<boost::recursive_mutex> un(*pim);
  379. updateInfo(hero);
  380. }
  381. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  382. {
  383. boost::unique_lock<boost::recursive_mutex> un(*pim);
  384. if(makingTurn && hero->tempOwner == playerID)
  385. adventureInt->heroList.redraw();
  386. }
  387. void CPlayerInterface::receivedResource(int type, int val)
  388. {
  389. boost::unique_lock<boost::recursive_mutex> un(*pim);
  390. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  391. mw->resourceChanged(type, val);
  392. GH.totalRedraw();
  393. }
  394. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  395. {
  396. waitWhileDialog();
  397. CGI->soundh->playSound(soundBase::heroNewLevel);
  398. boost::unique_lock<boost::recursive_mutex> un(*pim);
  399. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  400. GH.pushInt(lw);
  401. }
  402. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  403. {
  404. boost::unique_lock<boost::recursive_mutex> un(*pim);
  405. updateInfo(town);
  406. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  407. {
  408. CGI->mh->hideObject(town->garrisonHero);
  409. wanderingHeroes -= town->garrisonHero;
  410. }
  411. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  412. {
  413. CGI->mh->printObject(town->visitingHero);
  414. wanderingHeroes.push_back(town->visitingHero);
  415. }
  416. //adventureInt->heroList.updateHList();
  417. CCastleInterface *c = castleInt;
  418. if(c)
  419. {
  420. c->garr->highlighted = NULL;
  421. c->hslotup.hero = town->garrisonHero;
  422. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  423. c->garr->set2 = town->visitingHero;
  424. c->garr->ourUp = LOCPLINT->playerID==(town->garrisonHero ? town->garrisonHero->tempOwner : town->tempOwner);
  425. c->garr->ourDown = (town->visitingHero ? LOCPLINT->playerID==town->visitingHero->tempOwner : false);
  426. c->garr->recreateSlots();
  427. }
  428. GH.totalRedraw();
  429. }
  430. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  431. {
  432. if(hero->tempOwner != playerID )
  433. return;
  434. boost::unique_lock<boost::recursive_mutex> un(*pim);
  435. openTownWindow(town);
  436. }
  437. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  438. {
  439. boost::unique_lock<boost::recursive_mutex> un(*pim);
  440. updateInfo(obj);
  441. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  442. {
  443. if((*i)->type & IShowActivable::WITH_GARRISON)
  444. {
  445. CGarrisonHolder *cgh = static_cast<CGarrisonHolder*>(*i);
  446. cgh->updateGarrisons();
  447. }
  448. else if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
  449. {
  450. if(obj == cmw->hero)
  451. cmw->garrisonChanged();
  452. }
  453. }
  454. GH.totalRedraw();
  455. }
  456. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  457. {
  458. boost::unique_lock<boost::recursive_mutex> un(*pim);
  459. switch (buildingID)
  460. {
  461. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  462. updateInfo(town);
  463. break;
  464. }
  465. if(!castleInt)
  466. return;
  467. if(castleInt->town!=town)
  468. return;
  469. switch(what)
  470. {
  471. case 1:
  472. CGI->soundh->playSound(soundBase::newBuilding);
  473. castleInt->addBuilding(buildingID);
  474. break;
  475. case 2:
  476. castleInt->removeBuilding(buildingID);
  477. break;
  478. }
  479. }
  480. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  481. {
  482. if(LOCPLINT != this)
  483. { //another local interface should do this
  484. return;
  485. }
  486. while(showingDialog->get())
  487. SDL_Delay(20);
  488. boost::unique_lock<boost::recursive_mutex> un(*pim);
  489. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  490. GH.pushInt(battleInt);
  491. }
  492. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  493. {
  494. }
  495. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  496. {
  497. if(LOCPLINT != this)
  498. { //another local interface should do this
  499. return;
  500. }
  501. for(int b=0; b<healedStacks.size(); ++b)
  502. {
  503. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  504. if(battleInt->creAnims[healed->ID]->getType() == 5)
  505. {
  506. //stack has been resurrected
  507. battleInt->creAnims[healed->ID]->setType(2);
  508. }
  509. }
  510. if (lifeDrain)
  511. {
  512. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  513. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  514. if (attacker)
  515. {
  516. battleInt->displayEffect(50, attacker->position);
  517. }
  518. //print info about life drain
  519. int textOff = 0;
  520. if (attacker->count > 1)
  521. {
  522. textOff += 1;
  523. }
  524. char textBuf[1000];
  525. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  526. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  527. battleInt->console->addText(textBuf);
  528. }
  529. }
  530. void CPlayerInterface::battleNewStackAppeared(int stackID)
  531. {
  532. if(LOCPLINT != this)
  533. { //another local interface should do this
  534. return;
  535. }
  536. //changing necessary things in battle interface
  537. battleInt->newStack(stackID);
  538. }
  539. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  540. {
  541. if(LOCPLINT != this)
  542. { //another local interface should do this
  543. return;
  544. }
  545. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  546. {
  547. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  548. {
  549. if(itBat->first == *it) //remove this obstacle
  550. {
  551. battleInt->idToObstacle.erase(itBat);
  552. break;
  553. }
  554. }
  555. }
  556. //update accessible hexes
  557. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  558. }
  559. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  560. {
  561. if(LOCPLINT != this)
  562. { //another local interface should do this
  563. return;
  564. }
  565. battleInt->stackIsCatapulting(ca);
  566. }
  567. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  568. {
  569. if(LOCPLINT != this)
  570. { //another local interface should do this
  571. return;
  572. }
  573. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  574. {
  575. battleInt->stackRemoved(*it);
  576. }
  577. }
  578. void CPlayerInterface::battleNewRound(int round) //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  579. {
  580. if(LOCPLINT != this)
  581. { //another local interface should do this
  582. return;
  583. }
  584. boost::unique_lock<boost::recursive_mutex> un(*pim);
  585. battleInt->newRound(round);
  586. }
  587. void CPlayerInterface::actionStarted(const BattleAction* action)
  588. {
  589. if(LOCPLINT != this)
  590. { //another local interface should do this
  591. return;
  592. }
  593. boost::unique_lock<boost::recursive_mutex> un(*pim);
  594. curAction = new BattleAction(*action);
  595. battleInt->startAction(action);
  596. }
  597. void CPlayerInterface::actionFinished(const BattleAction* action)
  598. {
  599. if(LOCPLINT != this)
  600. { //another local interface should do this
  601. return;
  602. }
  603. boost::unique_lock<boost::recursive_mutex> un(*pim);
  604. delete curAction;
  605. curAction = NULL;
  606. battleInt->endAction(action);
  607. }
  608. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  609. {
  610. CBattleInterface *b = battleInt;
  611. {
  612. boost::unique_lock<boost::recursive_mutex> un(*pim);
  613. const CStack *stack = cb->battleGetStackByID(stackID);
  614. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  615. {
  616. std::string hlp = CGI->generaltexth->allTexts[33];
  617. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl : stack->getCreature()->nameSing);
  618. battleInt->displayEffect(20,stack->position);
  619. battleInt->console->addText(hlp);
  620. }
  621. b->stackActivated(stackID);
  622. }
  623. //wait till BattleInterface sets its command
  624. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  625. while(!b->givenCommand->data)
  626. b->givenCommand->cond.wait(lock);
  627. //tidy up
  628. BattleAction ret = *(b->givenCommand->data);
  629. delete b->givenCommand->data;
  630. b->givenCommand->data = NULL;
  631. //return command
  632. return ret;
  633. }
  634. void CPlayerInterface::battleEnd(BattleResult *br)
  635. {
  636. if(LOCPLINT != this)
  637. { //another local interface should do this
  638. return;
  639. }
  640. boost::unique_lock<boost::recursive_mutex> un(*pim);
  641. battleInt->battleFinished(*br);
  642. }
  643. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  644. {
  645. if(LOCPLINT != this)
  646. { //another local interface should do this
  647. return;
  648. }
  649. boost::unique_lock<boost::recursive_mutex> un(*pim);
  650. battleInt->stackMoved(ID, dest, end, distance);
  651. }
  652. void CPlayerInterface::battleSpellCast(BattleSpellCast *sc)
  653. {
  654. if(LOCPLINT != this)
  655. { //another local interface should do this
  656. return;
  657. }
  658. boost::unique_lock<boost::recursive_mutex> un(*pim);
  659. battleInt->spellCast(sc);
  660. }
  661. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  662. {
  663. if(LOCPLINT != this)
  664. { //another local interface should do this
  665. return;
  666. }
  667. boost::unique_lock<boost::recursive_mutex> un(*pim);
  668. battleInt->battleStacksEffectsSet(sse);
  669. }
  670. void CPlayerInterface::battleStacksAttacked(std::vector<BattleStackAttacked> & bsa)
  671. {
  672. if(LOCPLINT != this)
  673. { //another local interface should do this
  674. return;
  675. }
  676. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  677. boost::unique_lock<boost::recursive_mutex> un(*pim);
  678. tlog5 << "done!\n";
  679. std::vector<SStackAttackedInfo> arg;
  680. for(std::vector<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  681. {
  682. if(i->isEffect() && i->effect != 12) //and not armageddon
  683. {
  684. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  685. if (stack != NULL)
  686. battleInt->displayEffect(i->effect, stack->position);
  687. }
  688. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  689. arg.push_back(to_put);
  690. }
  691. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  692. {
  693. battleInt->displayEffect(bsa.begin()->effect, -1);
  694. }
  695. battleInt->stacksAreAttacked(arg);
  696. }
  697. void CPlayerInterface::battleAttack(BattleAttack *ba)
  698. {
  699. if(LOCPLINT != this)
  700. { //another local interface should do this
  701. return;
  702. }
  703. tlog5 << "CPlayerInterface::battleAttack - locking...";
  704. boost::unique_lock<boost::recursive_mutex> un(*pim);
  705. tlog5 << "done!\n";
  706. assert(curAction);
  707. if(ba->lucky()) //lucky hit
  708. {
  709. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  710. std::string hlp = CGI->generaltexth->allTexts[45];
  711. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  712. battleInt->console->addText(hlp);
  713. battleInt->displayEffect(18,stack->position);
  714. }
  715. //TODO: bad luck?
  716. if(ba->shot())
  717. {
  718. for(std::vector<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  719. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked), i->stackAttacked);
  720. }
  721. else
  722. {//WARNING: does not support multiple attacked creatures
  723. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  724. int shift = 0;
  725. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  726. {
  727. int distp = BattleInfo::getDistance(curAction->destinationTile + 1, attacker->position);
  728. int distm = BattleInfo::getDistance(curAction->destinationTile - 1, attacker->position);
  729. if( distp < distm )
  730. shift = 1;
  731. else
  732. shift = -1;
  733. }
  734. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, ba->bsa.begin()->stackAttacked );
  735. }
  736. }
  737. void CPlayerInterface::showComp(SComponent comp)
  738. {
  739. boost::unique_lock<boost::recursive_mutex> un(*pim);
  740. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  741. adventureInt->infoBar.showComp(&comp,4000);
  742. }
  743. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  744. {
  745. std::vector<SComponent*> intComps;
  746. for(int i=0;i<components.size();i++)
  747. intComps.push_back(new SComponent(*components[i]));
  748. showInfoDialog(text,intComps,soundID);
  749. }
  750. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID, bool delComps)
  751. {
  752. waitWhileDialog();
  753. boost::unique_lock<boost::recursive_mutex> un(*pim);
  754. stopMovement();
  755. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  756. temp->setDelComps(delComps);
  757. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  758. {
  759. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  760. showingDialog->set(true);
  761. GH.pushInt(temp);
  762. }
  763. else
  764. {
  765. dialogs.push_back(temp);
  766. }
  767. }
  768. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  769. {
  770. boost::unique_lock<boost::recursive_mutex> un(*pim);
  771. stopMovement();
  772. LOCPLINT->showingDialog->setn(true);
  773. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  774. }
  775. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  776. {
  777. waitWhileDialog();
  778. boost::unique_lock<boost::recursive_mutex> un(*pim);
  779. stopMovement();
  780. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  781. if(!selection && cancel) //simple yes/no dialog
  782. {
  783. std::vector<SComponent*> intComps;
  784. for(int i=0;i<components.size();i++)
  785. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  786. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  787. }
  788. else if(selection)
  789. {
  790. std::vector<CSelectableComponent*> intComps;
  791. for(int i=0;i<components.size();i++)
  792. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  793. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  794. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  795. if(cancel)
  796. {
  797. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  798. }
  799. int charperline = 35;
  800. if (pom.size() > 1)
  801. charperline = 50;
  802. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  803. GH.pushInt(temp);
  804. intComps[0]->clickLeft(true, false);
  805. }
  806. }
  807. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  808. {
  809. boost::unique_lock<boost::recursive_mutex> un(*pim);
  810. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  811. adventureInt->minimap.showTile(*i);
  812. if(pos.size())
  813. GH.totalRedraw();
  814. }
  815. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  816. {
  817. boost::unique_lock<boost::recursive_mutex> un(*pim);
  818. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  819. adventureInt->minimap.hideTile(*i);
  820. if(pos.size())
  821. GH.totalRedraw();
  822. }
  823. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  824. {
  825. boost::unique_lock<boost::recursive_mutex> un(*pim);
  826. adventureInt->heroWindow->setHero(hero);
  827. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  828. GH.pushInt(adventureInt->heroWindow);
  829. }
  830. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  831. {
  832. boost::unique_lock<boost::recursive_mutex> un(*pim);
  833. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  834. {
  835. adventureInt->heroWindow->deactivate();
  836. adventureInt->heroWindow->setHero(hero);
  837. adventureInt->heroWindow->activate();
  838. }
  839. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  840. {
  841. cew->deactivate();
  842. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  843. {
  844. if(cew->heroInst[g]->id == hero->id)
  845. {
  846. cew->heroInst[g] = hero;
  847. cew->artifs[g]->updateState = true;
  848. cew->artifs[g]->setHero(hero);
  849. cew->artifs[g]->updateState = false;
  850. }
  851. }
  852. cew->prepareBackground();
  853. cew->activate();
  854. }
  855. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  856. {
  857. if(caw->arts)
  858. {
  859. caw->deactivate();
  860. caw->arts->updateState = true;
  861. caw->arts->setHero(hero);
  862. caw->arts->updateState = false;
  863. caw->activate();
  864. }
  865. }
  866. updateInfo(hero);
  867. }
  868. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  869. {
  870. boost::unique_lock<boost::recursive_mutex> un(*pim);
  871. if(castleInt && town->ID == TOWNI_TYPE)
  872. {
  873. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  874. if(fs)
  875. fs->draw(castleInt,false);
  876. }
  877. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  878. {
  879. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  880. if(crw)
  881. crw->initCres();
  882. }
  883. }
  884. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  885. {
  886. if(bonus.type == Bonus::NONE) return;
  887. boost::unique_lock<boost::recursive_mutex> un(*pim);
  888. updateInfo(hero);
  889. if (bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING && !gain)
  890. {
  891. //recalculate paths because hero has lost bonus influencing pathfinding
  892. cb->recalculatePaths();
  893. eraseCurrentPathOf(hero, false);
  894. }
  895. }
  896. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  897. {
  898. h & playerID;
  899. h & sysOpts;
  900. h & spellbookSettings;
  901. }
  902. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  903. {
  904. serializeTempl(h,version);
  905. }
  906. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  907. {
  908. serializeTempl(h,version);
  909. sysOpts.apply();
  910. firstCall = -1;
  911. }
  912. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  913. {
  914. if (!h)
  915. return false; //can't find hero
  916. pim->unlock();
  917. bool result = false;
  918. {
  919. path.convert(0);
  920. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  921. stillMoveHero.data = CONTINUE_MOVE;
  922. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  923. enum TerrainTile::EterrainType newTerrain;
  924. int sh = -1;
  925. const TerrainTile * curTile = cb->getTileInfo(CGHeroInstance::convertPosition(h->pos, false));
  926. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  927. {
  928. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  929. if(path.nodes[i-1].turns)
  930. {
  931. stillMoveHero.data = STOP_MOVE;
  932. break;
  933. }
  934. // Start a new sound for the hero movement or let the existing one carry on.
  935. #if 0
  936. // TODO
  937. if (hero is flying && sh == -1)
  938. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  939. #endif
  940. {
  941. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  942. if (newTerrain != currentTerrain) {
  943. CGI->soundh->stopSound(sh);
  944. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  945. currentTerrain = newTerrain;
  946. }
  947. }
  948. stillMoveHero.data = WAITING_MOVE;
  949. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  950. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  951. cb->moveHero(h,endpos);
  952. eventsM.unlock();
  953. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  954. stillMoveHero.cond.wait(un);
  955. eventsM.lock();
  956. if (guarded) // Abort movement if a guard was fought.
  957. break;
  958. }
  959. CGI->soundh->stopSound(sh);
  960. cb->recalculatePaths();
  961. }
  962. pim->lock();
  963. return result;
  964. }
  965. bool CPlayerInterface::shiftPressed() const
  966. {
  967. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  968. }
  969. bool CPlayerInterface::altPressed() const
  970. {
  971. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  972. }
  973. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  974. {
  975. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  976. return;
  977. {
  978. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  979. while(showingDialog->data)
  980. showingDialog->cond.wait(un);
  981. }
  982. boost::unique_lock<boost::recursive_mutex> un(*pim);
  983. while(dialogs.size())
  984. {
  985. pim->unlock();
  986. SDL_Delay(20);
  987. pim->lock();
  988. }
  989. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  990. cgw->quit->callback += onEnd;
  991. GH.pushInt(cgw);
  992. }
  993. /**
  994. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  995. * into a combinational one on an artifact screen. Does not require the combination of
  996. * artifacts to be legal.
  997. * @param artifactID ID of a constituent artifact.
  998. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  999. * is false.
  1000. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1001. */
  1002. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1003. {
  1004. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1005. std::string text = artifact.Description();
  1006. text += "\n\n";
  1007. std::vector<SComponent*> scs;
  1008. if (assemble) {
  1009. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1010. // You possess all of the components to...
  1011. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1012. // Picture of assembled artifact at bottom.
  1013. SComponent* sc = new SComponent;
  1014. sc->type = SComponent::artifact;
  1015. sc->subtype = assembledArtifact.id;
  1016. sc->description = assembledArtifact.Description();
  1017. sc->subtitle = assembledArtifact.Name();
  1018. scs.push_back(sc);
  1019. } else {
  1020. // Do you wish to disassemble this artifact?
  1021. text += CGI->generaltexth->allTexts[733];
  1022. }
  1023. showYesNoDialog(text, scs, onYes, onNo, true);
  1024. }
  1025. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1026. {
  1027. if(stillMoveHero.get() == DURING_MOVE)
  1028. stillMoveHero.setn(CONTINUE_MOVE);
  1029. }
  1030. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1031. {
  1032. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1033. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1034. }
  1035. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1036. {
  1037. //redraw minimap if owner changed
  1038. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1039. if(sop->what == ObjProperty::OWNER)
  1040. {
  1041. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1042. std::set<int3> pos = obj->getBlockedPos();
  1043. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1044. {
  1045. if(cb->isVisible(*it))
  1046. adventureInt->minimap.showTile(*it);
  1047. }
  1048. if(obj->ID == TOWNI_TYPE)
  1049. if(obj->tempOwner == playerID)
  1050. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1051. else
  1052. towns -= obj;
  1053. assert(cb->getTownsInfo().size() == towns.size());
  1054. }
  1055. }
  1056. void CPlayerInterface::recreateHeroTownList()
  1057. {
  1058. wanderingHeroes.clear();
  1059. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1060. for(size_t i = 0; i < heroes.size(); i++)
  1061. if(!heroes[i]->inTownGarrison)
  1062. wanderingHeroes.push_back(heroes[i]);
  1063. towns.clear();
  1064. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1065. for(size_t i = 0; i < townInfo.size(); i++)
  1066. towns.push_back(townInfo[i]);
  1067. }
  1068. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1069. {
  1070. if(pos < 0 || pos >= wanderingHeroes.size())
  1071. return NULL;
  1072. return wanderingHeroes[pos];
  1073. }
  1074. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1075. {
  1076. waitWhileDialog();
  1077. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1078. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1079. GH.pushInt(cr);
  1080. }
  1081. void CPlayerInterface::waitWhileDialog()
  1082. {
  1083. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1084. while(showingDialog->data)
  1085. showingDialog->cond.wait(un);
  1086. }
  1087. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1088. {
  1089. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1090. int state = obj->state();
  1091. std::vector<si32> cost;
  1092. obj->getBoatCost(cost);
  1093. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1094. GH.pushInt(csw);
  1095. }
  1096. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1097. {
  1098. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1099. CGI->mh->printObject(obj);
  1100. //we might have built a boat in shipyard in opened town screen
  1101. if(obj->ID == 8
  1102. && LOCPLINT->castleInt
  1103. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1104. {
  1105. CGI->soundh->playSound(soundBase::newBuilding);
  1106. LOCPLINT->castleInt->recreateBuildings();
  1107. }
  1108. }
  1109. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1110. {
  1111. waitWhileDialog();
  1112. adventureInt->centerOn (pos);
  1113. if(focusTime)
  1114. {
  1115. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1116. if(activeAdv)
  1117. adventureInt->deactivate();
  1118. SDL_Delay(focusTime);
  1119. if(activeAdv)
  1120. adventureInt->activate();
  1121. }
  1122. }
  1123. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1124. {
  1125. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1126. {
  1127. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1128. heroKilled(h);
  1129. }
  1130. }
  1131. bool CPlayerInterface::ctrlPressed() const
  1132. {
  1133. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1134. }
  1135. void CPlayerInterface::update()
  1136. {
  1137. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1138. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1139. if(terminate_cond.get())
  1140. return;
  1141. //if there are any waiting dialogs, show them
  1142. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1143. {
  1144. showingDialog->set(true);
  1145. GH.pushInt(dialogs.front());
  1146. dialogs.pop_front();
  1147. }
  1148. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1149. if(!adventureInt->selection && GH.topInt() == adventureInt)
  1150. return;
  1151. GH.updateTime();
  1152. GH.handleEvents();
  1153. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1154. GH.totalRedraw();
  1155. else
  1156. GH.simpleRedraw();
  1157. CGI->curh->draw1();
  1158. CSDL_Ext::update(screen);
  1159. CGI->curh->draw2();
  1160. screenLTmax = Point(conf.cc.resx - screen->w, conf.cc.resy - screen->h);
  1161. pim->unlock();
  1162. SDL_framerateDelay(mainFPSmng);
  1163. }
  1164. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1165. {
  1166. using namespace boost::filesystem;
  1167. using namespace boost::algorithm;
  1168. std::map<std::time_t, int> dates; //save number => datestamp
  1169. directory_iterator enddir;
  1170. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1171. {
  1172. if(is_regular(dir->status()))
  1173. {
  1174. std::string name = dir->path().leaf();
  1175. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1176. {
  1177. char nr = name[namePrefix.size()];
  1178. if(std::isdigit(nr))
  1179. {
  1180. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1181. }
  1182. }
  1183. }
  1184. }
  1185. if(dates.size())
  1186. return (--dates.end())->second; //return latest file number
  1187. return 0;
  1188. }
  1189. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1190. {
  1191. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1192. {
  1193. //ho->moveDir = 1;
  1194. ho->isStanding = false;
  1195. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1196. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1197. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1198. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1199. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1200. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1201. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1202. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1203. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1204. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1205. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1206. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1207. 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);
  1208. 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);
  1209. 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);
  1210. 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);
  1211. 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);
  1212. 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);
  1213. }
  1214. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1215. {
  1216. //ho->moveDir = 2;
  1217. ho->isStanding = false;
  1218. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1219. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1220. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1221. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1222. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1223. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1224. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1225. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1226. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1227. 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);
  1228. 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);
  1229. 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);
  1230. }
  1231. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1232. {
  1233. //ho->moveDir = 3;
  1234. ho->isStanding = false;
  1235. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1236. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1237. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1238. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1239. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1240. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1241. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1242. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1243. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1244. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1245. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1246. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1247. 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);
  1248. 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);
  1249. 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);
  1250. 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);
  1251. 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);
  1252. 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);
  1253. }
  1254. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1255. {
  1256. //ho->moveDir = 4;
  1257. ho->isStanding = false;
  1258. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1259. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1260. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1261. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1262. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1263. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1264. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1265. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1266. 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);
  1267. 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);
  1268. }
  1269. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1270. {
  1271. //ho->moveDir = 5;
  1272. ho->isStanding = false;
  1273. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1274. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1275. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1276. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1277. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1278. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1279. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1280. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1281. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1282. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1283. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1284. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1285. 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);
  1286. 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);
  1287. 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);
  1288. 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);
  1289. 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);
  1290. 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);
  1291. }
  1292. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1293. {
  1294. //ho->moveDir = 6;
  1295. ho->isStanding = false;
  1296. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1297. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1298. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1299. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1300. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1301. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1302. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1303. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1304. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1305. 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);
  1306. 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);
  1307. 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);
  1308. }
  1309. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1310. {
  1311. //ho->moveDir = 7;
  1312. ho->isStanding = false;
  1313. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1314. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1315. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1316. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1317. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1318. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1319. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1320. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1321. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1322. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1323. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1324. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1325. 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);
  1326. 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);
  1327. 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);
  1328. 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);
  1329. 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);
  1330. 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);
  1331. }
  1332. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1333. {
  1334. //ho->moveDir = 8;
  1335. ho->isStanding = false;
  1336. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1337. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1338. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1339. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1340. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1341. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1342. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1343. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1344. 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);
  1345. 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);
  1346. }
  1347. }
  1348. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1349. {
  1350. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1351. {
  1352. //setting advmap shift
  1353. adventureInt->terrain.moveX = i-32;
  1354. adventureInt->terrain.moveY = i-32;
  1355. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1356. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1357. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1358. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1359. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1360. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1361. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1362. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1363. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1364. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1365. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1366. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1367. }
  1368. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1369. {
  1370. //setting advmap shift
  1371. adventureInt->terrain.moveY = i-32;
  1372. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1373. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1374. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1375. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1376. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1377. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1378. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1379. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1380. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1381. }
  1382. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1383. {
  1384. //setting advmap shift
  1385. adventureInt->terrain.moveX = -i+32;
  1386. adventureInt->terrain.moveY = i-32;
  1387. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1388. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1389. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1390. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1391. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1392. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1393. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1394. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1395. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1396. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1397. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1398. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1399. }
  1400. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1401. {
  1402. //setting advmap shift
  1403. adventureInt->terrain.moveX = -i+32;
  1404. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1405. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1406. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1407. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1408. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1409. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1410. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1411. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1412. }
  1413. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1414. {
  1415. //setting advmap shift
  1416. adventureInt->terrain.moveX = -i+32;
  1417. adventureInt->terrain.moveY = -i+32;
  1418. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1419. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1420. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1421. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1422. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1423. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1424. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1425. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1426. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1427. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1428. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1429. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1430. }
  1431. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1432. {
  1433. //setting advmap shift
  1434. adventureInt->terrain.moveY = -i+32;
  1435. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1436. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1437. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1438. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1439. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1440. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1441. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1442. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1443. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1444. }
  1445. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1446. {
  1447. //setting advmap shift
  1448. adventureInt->terrain.moveX = i-32;
  1449. adventureInt->terrain.moveY = -i+32;
  1450. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1451. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1452. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1453. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1454. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1455. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1456. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1457. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1458. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1459. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1460. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1461. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1462. }
  1463. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1464. {
  1465. //setting advmap shift
  1466. adventureInt->terrain.moveX = i-32;
  1467. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1468. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1469. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1470. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1471. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1472. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1473. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1474. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1475. }
  1476. }
  1477. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1478. {
  1479. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1480. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1481. {
  1482. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1483. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1484. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1485. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1486. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1487. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1488. }
  1489. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1490. {
  1491. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1492. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1493. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1494. }
  1495. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1496. {
  1497. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1498. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1499. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1500. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1501. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1502. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1503. }
  1504. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1505. {
  1506. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1507. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1508. }
  1509. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1510. {
  1511. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1512. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1513. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1514. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1515. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1516. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1517. }
  1518. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1519. {
  1520. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1521. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1522. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1523. }
  1524. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1525. {
  1526. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1527. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1528. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1529. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1530. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1531. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1532. }
  1533. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1534. {
  1535. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1536. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1537. }
  1538. //restoring good rects
  1539. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1540. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1541. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1542. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1543. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1544. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1545. //restoring good order of objects
  1546. 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);
  1547. 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);
  1548. 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);
  1549. 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);
  1550. 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);
  1551. 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);
  1552. }
  1553. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1554. {
  1555. if(LOCPLINT != this)
  1556. return;
  1557. if(player == playerID)
  1558. {
  1559. if(!victory)
  1560. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1561. // else
  1562. // showInfoDialog("Placeholder message: you won!");
  1563. makingTurn = true;
  1564. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1565. makingTurn = false;
  1566. howManyPeople--;
  1567. if(!howManyPeople) //all human players eliminated
  1568. {
  1569. if(cb->getStartInfo()->mode != StartInfo::CAMPAIGN)
  1570. requestReturningToMainMenu();
  1571. else
  1572. requestStoppingClient();
  1573. }
  1574. }
  1575. else
  1576. {
  1577. if(!victory && cb->getPlayerStatus(playerID) == PlayerState::INGAME) //enemy has lost
  1578. {
  1579. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1580. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1581. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1582. }
  1583. }
  1584. }
  1585. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1586. {
  1587. }
  1588. void CPlayerInterface::showPuzzleMap()
  1589. {
  1590. waitWhileDialog();
  1591. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1592. //TODO: interface should not know the real position of Grail...
  1593. float ratio = 0;
  1594. int3 grailPos = cb->getGrailPos(ratio);
  1595. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1596. }
  1597. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1598. {
  1599. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1600. {
  1601. cb->recalculatePaths();
  1602. eraseCurrentPathOf(caster, false);
  1603. }
  1604. }
  1605. void SystemOptions::setMusicVolume( int newVolume )
  1606. {
  1607. musicVolume = newVolume;
  1608. CGI->musich->setVolume(newVolume);
  1609. settingsChanged();
  1610. }
  1611. void SystemOptions::setSoundVolume( int newVolume )
  1612. {
  1613. soundVolume = newVolume;
  1614. CGI->soundh->setVolume(newVolume);
  1615. settingsChanged();
  1616. }
  1617. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1618. {
  1619. heroMoveSpeed = newSpeed;
  1620. settingsChanged();
  1621. }
  1622. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1623. {
  1624. mapScrollingSpeed = newSpeed;
  1625. settingsChanged();
  1626. }
  1627. void SystemOptions::settingsChanged()
  1628. {
  1629. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1630. if(settings.sfile)
  1631. settings << *this;
  1632. else
  1633. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1634. }
  1635. void SystemOptions::apply()
  1636. {
  1637. if(CGI->musich->getVolume() != musicVolume)
  1638. CGI->musich->setVolume(musicVolume);
  1639. if(CGI->soundh->getVolume() != soundVolume)
  1640. CGI->soundh->setVolume(soundVolume);
  1641. settingsChanged();
  1642. }
  1643. SystemOptions::SystemOptions()
  1644. {
  1645. heroMoveSpeed = 2;
  1646. mapScrollingSpeed = 2;
  1647. musicVolume = 88;
  1648. soundVolume = 88;
  1649. printCellBorders = true;
  1650. printStackRange = true;
  1651. animSpeed = 2;
  1652. printMouseShadow = true;
  1653. showQueue = true;
  1654. playerName = "Player";
  1655. }
  1656. void SystemOptions::setPlayerName(const std::string &newPlayerName)
  1657. {
  1658. playerName = newPlayerName;
  1659. settingsChanged();
  1660. }
  1661. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1662. {
  1663. if(checkForExistanceOfPath)
  1664. {
  1665. assert(vstd::contains(paths, ho));
  1666. }
  1667. else if (!vstd::contains(paths, ho))
  1668. {
  1669. return;
  1670. }
  1671. assert(ho == adventureInt->selection);
  1672. paths.erase(ho);
  1673. adventureInt->terrain.currentPath = NULL;
  1674. }
  1675. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1676. {
  1677. if(vstd::contains(paths,h)) //hero has assigned path
  1678. {
  1679. CGPath &path = paths[h];
  1680. if(!path.nodes.size())
  1681. {
  1682. tlog3 << "Warning: empty path found...\n";
  1683. paths.erase(h);
  1684. }
  1685. else
  1686. {
  1687. assert(h->getPosition(false) == path.startPos());
  1688. //update the hero path in case of something has changed on map
  1689. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1690. return &path;
  1691. else
  1692. paths.erase(h);
  1693. }
  1694. }
  1695. return NULL;
  1696. }
  1697. void CPlayerInterface::acceptTurn()
  1698. {
  1699. waitWhileDialog();
  1700. if(howManyPeople > 1)
  1701. adventureInt->startTurn();
  1702. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1703. /* TODO: This isn't quite right. First day in game should play
  1704. * NEWDAY. And we don't play NEWMONTH. */
  1705. int day = cb->getDate(1);
  1706. if (day != 1)
  1707. CGI->soundh->playSound(soundBase::newDay);
  1708. else
  1709. CGI->soundh->playSound(soundBase::newWeek);
  1710. adventureInt->infoBar.newDay(day);
  1711. //select first hero if available.
  1712. //TODO: check if hero is slept
  1713. if(wanderingHeroes.size())
  1714. adventureInt->select(wanderingHeroes.front());
  1715. else
  1716. adventureInt->select(towns.front());
  1717. adventureInt->showAll(screen);
  1718. }
  1719. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1720. {
  1721. std::string hlp;
  1722. if(h->movement < h->maxMovePoints(true))
  1723. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1724. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1725. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1726. else
  1727. {
  1728. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1729. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1730. if(hlp.length() || t->blockingObjects.size() > 1)
  1731. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1732. else
  1733. cb->dig(h);
  1734. }
  1735. }
  1736. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1737. {
  1738. adventureInt->infoBar.updateSelection(specific);
  1739. // if (adventureInt->selection == specific)
  1740. // adventureInt->infoBar.showAll(screen);
  1741. }
  1742. void CPlayerInterface::battleNewRoundFirst( int round )
  1743. {
  1744. if(LOCPLINT != this)
  1745. { //another local interface should do this
  1746. return;
  1747. }
  1748. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1749. battleInt->newRoundFirst(round);
  1750. }
  1751. void CPlayerInterface::stopMovement()
  1752. {
  1753. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1754. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1755. }
  1756. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1757. {
  1758. if(market->o->ID == 2) //Altar
  1759. {
  1760. EMarketMode mode = market->availableModes().front();
  1761. if(market->allowsTrade(ARTIFACT_EXP) && visitor->getAlignment() != EVIL)
  1762. GH.pushInt(new CAltarWindow(market, visitor, ARTIFACT_EXP));
  1763. else if(market->allowsTrade(CREATURE_EXP) && visitor->getAlignment() != GOOD)
  1764. GH.pushInt(new CAltarWindow(market, visitor, CREATURE_EXP));
  1765. }
  1766. else
  1767. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1768. }
  1769. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1770. {
  1771. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1772. GH.pushInt(cuw);
  1773. }
  1774. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  1775. {
  1776. CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
  1777. GH.pushInt(chfw);
  1778. }
  1779. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1780. {
  1781. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1782. cmw->artifactsChanged(false);
  1783. }
  1784. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1785. {
  1786. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1787. GH.pushInt(tv);
  1788. }
  1789. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1790. {
  1791. if(obj->state())
  1792. {
  1793. MetaString txt;
  1794. obj->getProblemText(txt);
  1795. showInfoDialog(txt.toString());
  1796. }
  1797. else
  1798. showShipyardDialog(obj);
  1799. }
  1800. void CPlayerInterface::requestReturningToMainMenu()
  1801. {
  1802. sendCustomEvent(2);
  1803. }
  1804. void CPlayerInterface::requestStoppingClient()
  1805. {
  1806. sendCustomEvent(3);
  1807. }
  1808. void CPlayerInterface::sendCustomEvent( int code )
  1809. {
  1810. SDL_Event event;
  1811. event.type = SDL_USEREVENT;
  1812. event.user.code = code;
  1813. SDL_PushEvent(&event);
  1814. }
  1815. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  1816. {
  1817. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  1818. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  1819. }