CPlayerInterface.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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 std::queue<SDL_Event*> events;
  65. extern boost::mutex eventsM;
  66. CPlayerInterface * LOCPLINT;
  67. CBattleInterface * CPlayerInterface::battleInt;
  68. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  69. CondSh<EMoveState> stillMoveHero; //used during hero movement
  70. int CPlayerInterface::howManyPeople = 0;
  71. struct OCM_HLP_CGIN
  72. {
  73. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  74. {
  75. return (*a.first)<(*b.first);
  76. }
  77. } ocmptwo_cgin ;
  78. CPlayerInterface::CPlayerInterface(int Player, int serial)
  79. {
  80. howManyPeople++;
  81. GH.defActionsDef = 0;
  82. LOCPLINT = this;
  83. curAction = NULL;
  84. playerID=Player;
  85. serialID=serial;
  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(howManyPeople > 1) //hot seat message
  163. {
  164. adventureInt->startHotSeatWait(playerID);
  165. makingTurn = true;
  166. std::string msg = CGI->generaltexth->allTexts[13];
  167. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos[serialID].name);
  168. std::vector<SComponent*> cmp;
  169. cmp.push_back(new SComponent(SComponent::flag, playerID, 0));
  170. showInfoDialog(msg, cmp);
  171. }
  172. else
  173. {
  174. makingTurn = true;
  175. adventureInt->startTurn();
  176. }
  177. }
  178. acceptTurn();
  179. }
  180. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  181. {
  182. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  183. for(int h=0; h<hlp.objects.size(); ++h)
  184. if(hlp.objects[h].first->id==hid)
  185. {
  186. hlp.objects[h].second = r;
  187. return;
  188. }
  189. }
  190. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  191. {
  192. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  193. for(int h=0; h<hlp.objects.size(); ++h)
  194. if(hlp.objects[h].first->id==hid)
  195. {
  196. hlp.objects.erase(hlp.objects.begin()+h);
  197. return;
  198. }
  199. }
  200. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  201. {
  202. if(LOCPLINT != this)
  203. return;
  204. boost::unique_lock<boost::recursive_mutex> un(*pim);
  205. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  206. int3 hp = details.start;
  207. adventureInt->centerOn(ho); //actualizing screen pos
  208. adventureInt->minimap.draw(screen2);
  209. adventureInt->heroList.draw(screen2);
  210. bool directlyAttackingCreature =
  211. CGI->mh->map->isInTheMap(details.attackedFrom)
  212. && adventureInt->terrain.currentPath->nodes.size() == 3;
  213. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  214. {
  215. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  216. {
  217. if(adventureInt->terrain.currentPath)
  218. eraseCurrentPathOf(ho);
  219. return; //teleport - no fancy moving animation
  220. //TODO: smooth disappear / appear effect
  221. }
  222. if (details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED //hero didn't change tile but visit succeeded
  223. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  224. {
  225. eraseCurrentPathOf(ho);
  226. }
  227. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  228. {
  229. //remove one node from the path (the one we went)
  230. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  231. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  232. eraseCurrentPathOf(ho);
  233. }
  234. }
  235. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  236. {
  237. ho->isStanding = true;
  238. stillMoveHero.setn(STOP_MOVE);
  239. GH.totalRedraw();
  240. return;
  241. }
  242. initMovement(details, ho, hp);
  243. //first initializing done
  244. SDL_framerateDelay(mainFPSmng); // after first move
  245. //main moving
  246. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  247. {
  248. movementPxStep(details, i, hp, ho);
  249. adventureInt->updateScreen = true;
  250. adventureInt->show(screen);
  251. CSDL_Ext::update(screen);
  252. SDL_framerateDelay(mainFPSmng); //for animation purposes
  253. } //for(int i=1; i<32; i+=4)
  254. //main moving done
  255. //finishing move
  256. finishMovement(details, hp, ho);
  257. ho->isStanding = true;
  258. //move finished
  259. adventureInt->minimap.draw(screen2);
  260. adventureInt->heroList.updateMove(ho);
  261. //check if user cancelled movement
  262. {
  263. boost::unique_lock<boost::mutex> un(eventsM);
  264. while(events.size())
  265. {
  266. SDL_Event *ev = events.front();
  267. events.pop();
  268. switch(ev->type)
  269. {
  270. case SDL_MOUSEBUTTONDOWN:
  271. stillMoveHero.setn(STOP_MOVE);
  272. break;
  273. case SDL_KEYDOWN:
  274. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  275. stillMoveHero.setn(STOP_MOVE);
  276. break;
  277. }
  278. delete ev;
  279. }
  280. }
  281. if(stillMoveHero.get() == WAITING_MOVE)
  282. stillMoveHero.setn(DURING_MOVE);
  283. // Hero attacked creature directly, set direction to face it.
  284. if (directlyAttackingCreature) {
  285. // Get direction to attacker.
  286. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  287. const ui8 dirLookup[3][3] = {
  288. 1, 2, 3,
  289. 8, 0, 4,
  290. 7, 6, 5
  291. };
  292. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  293. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  294. }
  295. }
  296. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  297. {
  298. boost::unique_lock<boost::recursive_mutex> un(*pim);
  299. wanderingHeroes -= hero;
  300. adventureInt->heroList.updateHList(hero);
  301. }
  302. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  303. {
  304. boost::unique_lock<boost::recursive_mutex> un(*pim);
  305. wanderingHeroes.push_back(hero);
  306. adventureInt->heroList.updateHList();
  307. }
  308. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  309. {
  310. castleInt = new CCastleInterface(town);
  311. GH.pushInt(castleInt);
  312. }
  313. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  314. {
  315. if(!specific)
  316. specific = adventureInt->selection;
  317. assert(specific);
  318. switch(specific->ID)
  319. {
  320. case HEROI_TYPE:
  321. {
  322. InfoAboutHero iah;
  323. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  324. assert(gotInfo);
  325. return graphics->drawHeroInfoWin(iah);
  326. }
  327. case TOWNI_TYPE:
  328. case 33: // Garrison
  329. case 219:
  330. {
  331. InfoAboutTown iah;
  332. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  333. assert(gotInfo);
  334. return graphics->drawTownInfoWin(iah);
  335. }
  336. default:
  337. return NULL;
  338. }
  339. }
  340. int3 CPlayerInterface::repairScreenPos(int3 pos)
  341. {
  342. if(pos.x<-CGI->mh->frameW)
  343. pos.x = -CGI->mh->frameW;
  344. if(pos.y<-CGI->mh->frameH)
  345. pos.y = -CGI->mh->frameH;
  346. if(pos.x>CGI->mh->map->width - adventureInt->terrain.tilesw + CGI->mh->frameW)
  347. pos.x = CGI->mh->map->width - adventureInt->terrain.tilesw + CGI->mh->frameW;
  348. if(pos.y>CGI->mh->map->height - adventureInt->terrain.tilesh + CGI->mh->frameH)
  349. pos.y = CGI->mh->map->height - adventureInt->terrain.tilesh + CGI->mh->frameH;
  350. return pos;
  351. }
  352. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  353. {
  354. boost::unique_lock<boost::recursive_mutex> un(*pim);
  355. if(which == 4)
  356. {
  357. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  358. ctw->setExpToLevel();
  359. }
  360. else if(which < PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  361. updateInfo(hero);
  362. }
  363. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  364. {
  365. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  366. if(cuw) //university window is open
  367. {
  368. GH.totalRedraw();
  369. }
  370. }
  371. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  372. {
  373. boost::unique_lock<boost::recursive_mutex> un(*pim);
  374. updateInfo(hero);
  375. }
  376. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  377. {
  378. boost::unique_lock<boost::recursive_mutex> un(*pim);
  379. if(makingTurn && hero->tempOwner == playerID)
  380. adventureInt->heroList.redraw();
  381. }
  382. void CPlayerInterface::receivedResource(int type, int val)
  383. {
  384. boost::unique_lock<boost::recursive_mutex> un(*pim);
  385. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  386. mw->resourceChanged(type, val);
  387. GH.totalRedraw();
  388. }
  389. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  390. {
  391. waitWhileDialog();
  392. CGI->soundh->playSound(soundBase::heroNewLevel);
  393. boost::unique_lock<boost::recursive_mutex> un(*pim);
  394. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  395. GH.pushInt(lw);
  396. }
  397. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  398. {
  399. boost::unique_lock<boost::recursive_mutex> un(*pim);
  400. updateInfo(town);
  401. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  402. {
  403. CGI->mh->hideObject(town->garrisonHero);
  404. wanderingHeroes -= town->garrisonHero;
  405. }
  406. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  407. {
  408. CGI->mh->printObject(town->visitingHero);
  409. wanderingHeroes.push_back(town->visitingHero);
  410. }
  411. //adventureInt->heroList.updateHList();
  412. CCastleInterface *c = castleInt;
  413. if(c)
  414. {
  415. c->garr->highlighted = NULL;
  416. c->hslotup.hero = town->garrisonHero;
  417. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  418. c->garr->set2 = town->visitingHero;
  419. c->garr->recreateSlots();
  420. }
  421. GH.totalRedraw();
  422. }
  423. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  424. {
  425. if(hero->tempOwner != town->tempOwner)
  426. return;
  427. boost::unique_lock<boost::recursive_mutex> un(*pim);
  428. openTownWindow(town);
  429. }
  430. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  431. {
  432. boost::unique_lock<boost::recursive_mutex> un(*pim);
  433. updateInfo(obj);
  434. bool wasGarrison = false;
  435. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  436. {
  437. if((*i)->type & IShowActivable::WITH_GARRISON)
  438. {
  439. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  440. wwg->garr->recreateSlots();
  441. wasGarrison = true;
  442. }
  443. else if(CKingdomInterface *cki = dynamic_cast<CKingdomInterface*>(*i))
  444. {//a cheat for Kingdom Overview window (it has CWindowWithGarrison-childrens which are not present in ListInt)
  445. //need to create "Garrison Holder" class thingy
  446. cki->updateAllGarrisons();
  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->type->namePl : stack->type->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->type->namePl.c_str() : stack->type->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. if( BattleInfo::mutualPosition(curAction->destinationTile + 1, attacker->position) >= 0 )
  728. shift = 1;
  729. else
  730. shift = -1;
  731. }
  732. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, ba->bsa.begin()->stackAttacked );
  733. }
  734. }
  735. void CPlayerInterface::showComp(SComponent comp)
  736. {
  737. boost::unique_lock<boost::recursive_mutex> un(*pim);
  738. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  739. adventureInt->infoBar.showComp(&comp,4000);
  740. }
  741. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  742. {
  743. std::vector<SComponent*> intComps;
  744. for(int i=0;i<components.size();i++)
  745. intComps.push_back(new SComponent(*components[i]));
  746. showInfoDialog(text,intComps,soundID);
  747. }
  748. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  749. {
  750. waitWhileDialog();
  751. boost::unique_lock<boost::recursive_mutex> un(*pim);
  752. stopMovement();
  753. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  754. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  755. {
  756. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  757. showingDialog->set(true);
  758. GH.pushInt(temp);
  759. }
  760. else
  761. {
  762. dialogs.push_back(temp);
  763. }
  764. }
  765. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  766. {
  767. boost::unique_lock<boost::recursive_mutex> un(*pim);
  768. stopMovement();
  769. LOCPLINT->showingDialog->setn(true);
  770. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  771. }
  772. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  773. {
  774. waitWhileDialog();
  775. boost::unique_lock<boost::recursive_mutex> un(*pim);
  776. stopMovement();
  777. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  778. if(!selection && cancel) //simple yes/no dialog
  779. {
  780. std::vector<SComponent*> intComps;
  781. for(int i=0;i<components.size();i++)
  782. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  783. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  784. }
  785. else if(selection)
  786. {
  787. std::vector<CSelectableComponent*> intComps;
  788. for(int i=0;i<components.size();i++)
  789. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  790. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  791. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  792. if(cancel)
  793. {
  794. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  795. }
  796. int charperline = 35;
  797. if (pom.size() > 1)
  798. charperline = 50;
  799. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  800. GH.pushInt(temp);
  801. intComps[0]->clickLeft(true, false);
  802. }
  803. }
  804. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  805. {
  806. boost::unique_lock<boost::recursive_mutex> un(*pim);
  807. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  808. adventureInt->minimap.showTile(*i);
  809. if(pos.size())
  810. GH.totalRedraw();
  811. }
  812. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  813. {
  814. boost::unique_lock<boost::recursive_mutex> un(*pim);
  815. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  816. adventureInt->minimap.hideTile(*i);
  817. if(pos.size())
  818. GH.totalRedraw();
  819. }
  820. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  821. {
  822. boost::unique_lock<boost::recursive_mutex> un(*pim);
  823. adventureInt->heroWindow->setHero(hero);
  824. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  825. GH.pushInt(adventureInt->heroWindow);
  826. }
  827. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  828. {
  829. boost::unique_lock<boost::recursive_mutex> un(*pim);
  830. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  831. {
  832. adventureInt->heroWindow->deactivate();
  833. adventureInt->heroWindow->setHero(hero);
  834. adventureInt->heroWindow->activate();
  835. return;
  836. }
  837. if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  838. {
  839. cew->deactivate();
  840. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  841. {
  842. if(cew->heroInst[g]->id == hero->id)
  843. {
  844. cew->heroInst[g] = hero;
  845. cew->artifs[g]->updateState = true;
  846. cew->artifs[g]->setHero(hero);
  847. cew->artifs[g]->updateState = false;
  848. }
  849. }
  850. cew->prepareBackground();
  851. cew->activate();
  852. }
  853. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  854. {
  855. caw->deactivate();
  856. caw->arts->updateState = true;
  857. caw->arts->setHero(hero);
  858. caw->arts->updateState = false;
  859. caw->activate();
  860. }
  861. }
  862. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  863. {
  864. boost::unique_lock<boost::recursive_mutex> un(*pim);
  865. if(castleInt && town->ID == TOWNI_TYPE)
  866. {
  867. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  868. if(fs)
  869. fs->draw(castleInt,false);
  870. }
  871. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  872. {
  873. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  874. if(crw)
  875. crw->initCres();
  876. }
  877. }
  878. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  879. {
  880. if(bonus.type == Bonus::NONE) return;
  881. boost::unique_lock<boost::recursive_mutex> un(*pim);
  882. updateInfo(hero);
  883. if (bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING && !gain)
  884. {
  885. //recalculate paths because hero has lost bonus influencing pathfinding
  886. cb->recalculatePaths();
  887. eraseCurrentPathOf(hero, false);
  888. }
  889. }
  890. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  891. {
  892. h & playerID & serialID;
  893. h & sysOpts;
  894. }
  895. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  896. {
  897. serializeTempl(h,version);
  898. }
  899. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  900. {
  901. serializeTempl(h,version);
  902. sysOpts.apply();
  903. firstCall = -1;
  904. }
  905. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  906. {
  907. if (!h)
  908. return false; //can't find hero
  909. pim->unlock();
  910. bool result = false;
  911. {
  912. path.convert(0);
  913. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  914. stillMoveHero.data = CONTINUE_MOVE;
  915. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  916. enum TerrainTile::EterrainType newTerrain;
  917. int sh = -1;
  918. const TerrainTile * curTile = cb->getTileInfo(CGHeroInstance::convertPosition(h->pos, false));
  919. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  920. {
  921. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  922. if(path.nodes[i-1].turns)
  923. {
  924. stillMoveHero.data = STOP_MOVE;
  925. break;
  926. }
  927. // Start a new sound for the hero movement or let the existing one carry on.
  928. #if 0
  929. // TODO
  930. if (hero is flying && sh == -1)
  931. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  932. #endif
  933. {
  934. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  935. if (newTerrain != currentTerrain) {
  936. CGI->soundh->stopSound(sh);
  937. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  938. currentTerrain = newTerrain;
  939. }
  940. }
  941. stillMoveHero.data = WAITING_MOVE;
  942. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  943. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  944. cb->moveHero(h,endpos);
  945. curTile = cb->getTileInfo(endpos);
  946. eventsM.unlock();
  947. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  948. stillMoveHero.cond.wait(un);
  949. eventsM.lock();
  950. if (guarded) // Abort movement if a guard was fought.
  951. break;
  952. }
  953. CGI->soundh->stopSound(sh);
  954. cb->recalculatePaths();
  955. }
  956. pim->lock();
  957. return result;
  958. }
  959. bool CPlayerInterface::shiftPressed() const
  960. {
  961. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  962. }
  963. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  964. {
  965. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  966. return;
  967. {
  968. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  969. while(showingDialog->data)
  970. showingDialog->cond.wait(un);
  971. }
  972. boost::unique_lock<boost::recursive_mutex> un(*pim);
  973. while(dialogs.size())
  974. {
  975. pim->unlock();
  976. SDL_Delay(20);
  977. pim->lock();
  978. }
  979. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  980. cgw->quit->callback += onEnd;
  981. GH.pushInt(cgw);
  982. }
  983. /**
  984. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  985. * into a combinational one on an artifact screen. Does not require the combination of
  986. * artifacts to be legal.
  987. * @param artifactID ID of a constituent artifact.
  988. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  989. * is false.
  990. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  991. */
  992. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  993. {
  994. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  995. std::string text = artifact.Description();
  996. text += "\n\n";
  997. std::vector<SComponent*> scs;
  998. if (assemble) {
  999. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1000. // You possess all of the components to...
  1001. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1002. // Picture of assembled artifact at bottom.
  1003. SComponent* sc = new SComponent;
  1004. sc->type = SComponent::artifact;
  1005. sc->subtype = assembledArtifact.id;
  1006. sc->description = assembledArtifact.Description();
  1007. sc->subtitle = assembledArtifact.Name();
  1008. scs.push_back(sc);
  1009. } else {
  1010. // Do you wish to disassemble this artifact?
  1011. text += CGI->generaltexth->allTexts[733];
  1012. }
  1013. showYesNoDialog(text, scs, onYes, onNo, true);
  1014. }
  1015. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1016. {
  1017. if(stillMoveHero.get() == DURING_MOVE)
  1018. stillMoveHero.setn(CONTINUE_MOVE);
  1019. }
  1020. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1021. {
  1022. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1023. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1024. }
  1025. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1026. {
  1027. //redraw minimap if owner changed
  1028. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1029. if(sop->what == ObjProperty::OWNER)
  1030. {
  1031. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1032. std::set<int3> pos = obj->getBlockedPos();
  1033. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1034. {
  1035. if(cb->isVisible(*it))
  1036. adventureInt->minimap.showTile(*it);
  1037. }
  1038. if(obj->ID == TOWNI_TYPE)
  1039. if(obj->tempOwner == playerID)
  1040. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1041. else
  1042. towns -= obj;
  1043. assert(cb->getTownsInfo().size() == towns.size());
  1044. }
  1045. }
  1046. void CPlayerInterface::recreateHeroTownList()
  1047. {
  1048. wanderingHeroes.clear();
  1049. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1050. for(size_t i = 0; i < heroes.size(); i++)
  1051. if(!heroes[i]->inTownGarrison)
  1052. wanderingHeroes.push_back(heroes[i]);
  1053. towns.clear();
  1054. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1055. for(size_t i = 0; i < townInfo.size(); i++)
  1056. towns.push_back(townInfo[i]);
  1057. }
  1058. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1059. {
  1060. if(pos < 0 || pos >= wanderingHeroes.size())
  1061. return NULL;
  1062. return wanderingHeroes[pos];
  1063. }
  1064. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1065. {
  1066. waitWhileDialog();
  1067. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1068. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1069. GH.pushInt(cr);
  1070. }
  1071. void CPlayerInterface::waitWhileDialog()
  1072. {
  1073. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1074. while(showingDialog->data)
  1075. showingDialog->cond.wait(un);
  1076. }
  1077. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1078. {
  1079. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1080. int state = obj->state();
  1081. std::vector<si32> cost;
  1082. obj->getBoatCost(cost);
  1083. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1084. GH.pushInt(csw);
  1085. }
  1086. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1087. {
  1088. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1089. CGI->mh->printObject(obj);
  1090. //we might have built a boat in shipyard in opened town screen
  1091. if(obj->ID == 8
  1092. && LOCPLINT->castleInt
  1093. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1094. {
  1095. CGI->soundh->playSound(soundBase::newBuilding);
  1096. LOCPLINT->castleInt->recreateBuildings();
  1097. }
  1098. }
  1099. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1100. {
  1101. adventureInt->centerOn (pos);
  1102. if(focusTime)
  1103. {
  1104. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1105. if(activeAdv)
  1106. adventureInt->deactivate();
  1107. SDL_Delay(focusTime);
  1108. if(activeAdv)
  1109. adventureInt->activate();
  1110. }
  1111. }
  1112. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1113. {
  1114. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1115. {
  1116. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1117. heroKilled(h);
  1118. }
  1119. }
  1120. bool CPlayerInterface::ctrlPressed() const
  1121. {
  1122. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1123. }
  1124. void CPlayerInterface::update()
  1125. {
  1126. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1127. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1128. if(terminate_cond.get())
  1129. return;
  1130. //if there are any waiting dialogs, show them
  1131. if(dialogs.size() && !showingDialog->get())
  1132. {
  1133. showingDialog->set(true);
  1134. GH.pushInt(dialogs.front());
  1135. dialogs.pop_front();
  1136. }
  1137. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1138. if(!adventureInt->selection && GH.topInt() == adventureInt)
  1139. return;
  1140. GH.updateTime();
  1141. GH.handleEvents();
  1142. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1143. GH.totalRedraw();
  1144. else
  1145. GH.simpleRedraw();
  1146. CGI->curh->draw1();
  1147. CSDL_Ext::update(screen);
  1148. CGI->curh->draw2();
  1149. pim->unlock();
  1150. SDL_framerateDelay(mainFPSmng);
  1151. }
  1152. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1153. {
  1154. using namespace boost::filesystem;
  1155. using namespace boost::algorithm;
  1156. std::map<std::time_t, int> dates; //save number => datestamp
  1157. directory_iterator enddir;
  1158. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1159. {
  1160. if(is_regular(dir->status()))
  1161. {
  1162. std::string name = dir->path().leaf();
  1163. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1164. {
  1165. char nr = name[namePrefix.size()];
  1166. if(std::isdigit(nr))
  1167. {
  1168. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. if(dates.size())
  1174. return (--dates.end())->second; //return latest file number
  1175. return 0;
  1176. }
  1177. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1178. {
  1179. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1180. {
  1181. //ho->moveDir = 1;
  1182. ho->isStanding = false;
  1183. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1184. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1185. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1186. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1187. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1188. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1189. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1190. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1191. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1192. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1193. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1194. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1195. 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);
  1196. 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);
  1197. 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);
  1198. 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);
  1199. 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);
  1200. 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);
  1201. }
  1202. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1203. {
  1204. //ho->moveDir = 2;
  1205. ho->isStanding = false;
  1206. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1207. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1208. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1209. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1210. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1211. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1212. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1213. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1214. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1215. 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);
  1216. 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);
  1217. 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);
  1218. }
  1219. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1220. {
  1221. //ho->moveDir = 3;
  1222. ho->isStanding = false;
  1223. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1224. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1225. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1226. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1227. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1228. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1229. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1230. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1231. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1232. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1233. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1234. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1235. 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);
  1236. 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);
  1237. 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);
  1238. 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);
  1239. 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);
  1240. 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);
  1241. }
  1242. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1243. {
  1244. //ho->moveDir = 4;
  1245. ho->isStanding = false;
  1246. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1247. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1248. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1249. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1250. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1251. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1252. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1253. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1254. 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);
  1255. 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);
  1256. }
  1257. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1258. {
  1259. //ho->moveDir = 5;
  1260. ho->isStanding = false;
  1261. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1262. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1263. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1264. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1265. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1266. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1267. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1268. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1269. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1270. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1271. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1272. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1273. 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);
  1274. 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);
  1275. 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);
  1276. 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);
  1277. 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);
  1278. 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);
  1279. }
  1280. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1281. {
  1282. //ho->moveDir = 6;
  1283. ho->isStanding = false;
  1284. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1285. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1286. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1287. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1288. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1289. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1290. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1291. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1292. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1293. 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);
  1294. 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);
  1295. 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);
  1296. }
  1297. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1298. {
  1299. //ho->moveDir = 7;
  1300. ho->isStanding = false;
  1301. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1302. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1303. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1304. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1305. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1306. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1307. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1308. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1309. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1310. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1311. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1312. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1313. 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);
  1314. 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);
  1315. 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);
  1316. 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);
  1317. 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);
  1318. 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);
  1319. }
  1320. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1321. {
  1322. //ho->moveDir = 8;
  1323. ho->isStanding = false;
  1324. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1325. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1326. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1327. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1328. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1329. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1330. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1331. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1332. 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);
  1333. 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);
  1334. }
  1335. }
  1336. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1337. {
  1338. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1339. {
  1340. //setting advmap shift
  1341. adventureInt->terrain.moveX = i-32;
  1342. adventureInt->terrain.moveY = i-32;
  1343. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1344. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1345. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1346. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1347. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1348. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1349. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1350. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1351. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1352. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1353. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1354. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1355. }
  1356. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1357. {
  1358. //setting advmap shift
  1359. adventureInt->terrain.moveY = i-32;
  1360. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1361. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1362. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1363. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1364. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1365. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1366. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1367. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1368. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1369. }
  1370. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1371. {
  1372. //setting advmap shift
  1373. adventureInt->terrain.moveX = -i+32;
  1374. adventureInt->terrain.moveY = i-32;
  1375. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1376. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1377. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1378. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1379. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1380. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1381. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1382. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1383. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1384. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1385. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1386. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1387. }
  1388. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1389. {
  1390. //setting advmap shift
  1391. adventureInt->terrain.moveX = -i+32;
  1392. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1393. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1394. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1395. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1396. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1397. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1398. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1399. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1400. }
  1401. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1402. {
  1403. //setting advmap shift
  1404. adventureInt->terrain.moveX = -i+32;
  1405. adventureInt->terrain.moveY = -i+32;
  1406. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1407. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1408. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1409. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1410. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1411. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1412. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1413. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1414. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1415. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1416. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1417. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1418. }
  1419. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1420. {
  1421. //setting advmap shift
  1422. adventureInt->terrain.moveY = -i+32;
  1423. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1424. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1425. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1426. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1427. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1428. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1429. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1430. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1431. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1432. }
  1433. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1434. {
  1435. //setting advmap shift
  1436. adventureInt->terrain.moveX = i-32;
  1437. adventureInt->terrain.moveY = -i+32;
  1438. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1439. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1440. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1441. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1442. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1443. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1444. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1445. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1446. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1447. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1448. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1449. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1450. }
  1451. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1452. {
  1453. //setting advmap shift
  1454. adventureInt->terrain.moveX = i-32;
  1455. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1456. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1457. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1458. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1459. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1460. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1461. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1462. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1463. }
  1464. }
  1465. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1466. {
  1467. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1468. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1469. {
  1470. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1471. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1472. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1473. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1474. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1475. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1476. }
  1477. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1478. {
  1479. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1480. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1481. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1482. }
  1483. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1484. {
  1485. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1486. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1487. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1488. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1489. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1490. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1491. }
  1492. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1493. {
  1494. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1495. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1496. }
  1497. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1498. {
  1499. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1500. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1501. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1502. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1503. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1504. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1505. }
  1506. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1507. {
  1508. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1509. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1510. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1511. }
  1512. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1513. {
  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. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1518. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1519. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1520. }
  1521. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1522. {
  1523. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1524. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1525. }
  1526. //restoring good rects
  1527. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1528. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1529. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1530. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1531. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1532. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1533. //restoring good order of objects
  1534. 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);
  1535. 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);
  1536. 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);
  1537. 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);
  1538. 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);
  1539. 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);
  1540. }
  1541. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1542. {
  1543. if(LOCPLINT != this)
  1544. return;
  1545. if(player == playerID)
  1546. {
  1547. if(!victory)
  1548. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1549. // else
  1550. // showInfoDialog("Placeholder message: you won!");
  1551. makingTurn = true;
  1552. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1553. makingTurn = false;
  1554. //return to main menu
  1555. SDL_Event event;
  1556. event.type = SDL_USEREVENT;
  1557. event.user.code = 2;
  1558. SDL_PushEvent(&event);
  1559. }
  1560. else
  1561. {
  1562. if(!victory) //enemy has lost
  1563. {
  1564. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1565. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1566. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1567. }
  1568. }
  1569. }
  1570. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1571. {
  1572. }
  1573. void CPlayerInterface::showPuzzleMap()
  1574. {
  1575. waitWhileDialog();
  1576. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1577. //TODO: interface should not know the real position of Grail...
  1578. float ratio = 0;
  1579. int3 grailPos = cb->getGrailPos(ratio);
  1580. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1581. }
  1582. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1583. {
  1584. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1585. {
  1586. cb->recalculatePaths();
  1587. eraseCurrentPathOf(caster, false);
  1588. }
  1589. }
  1590. void SystemOptions::setMusicVolume( int newVolume )
  1591. {
  1592. musicVolume = newVolume;
  1593. CGI->musich->setVolume(newVolume);
  1594. settingsChanged();
  1595. }
  1596. void SystemOptions::setSoundVolume( int newVolume )
  1597. {
  1598. soundVolume = newVolume;
  1599. CGI->soundh->setVolume(newVolume);
  1600. settingsChanged();
  1601. }
  1602. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1603. {
  1604. heroMoveSpeed = newSpeed;
  1605. settingsChanged();
  1606. }
  1607. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1608. {
  1609. mapScrollingSpeed = newSpeed;
  1610. settingsChanged();
  1611. }
  1612. void SystemOptions::settingsChanged()
  1613. {
  1614. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1615. if(settings.sfile)
  1616. settings << *this;
  1617. else
  1618. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1619. }
  1620. void SystemOptions::apply()
  1621. {
  1622. if(CGI->musich->getVolume() != musicVolume)
  1623. CGI->musich->setVolume(musicVolume);
  1624. if(CGI->soundh->getVolume() != soundVolume)
  1625. CGI->soundh->setVolume(soundVolume);
  1626. settingsChanged();
  1627. }
  1628. SystemOptions::SystemOptions()
  1629. {
  1630. heroMoveSpeed = 2;
  1631. mapScrollingSpeed = 2;
  1632. musicVolume = 88;
  1633. soundVolume = 88;
  1634. printCellBorders = true;
  1635. printStackRange = true;
  1636. animSpeed = 2;
  1637. printMouseShadow = true;
  1638. showQueue = true;
  1639. }
  1640. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1641. {
  1642. if(checkForExistanceOfPath)
  1643. {
  1644. assert(vstd::contains(paths, ho));
  1645. }
  1646. else if (!vstd::contains(paths, ho))
  1647. {
  1648. return;
  1649. }
  1650. assert(ho == adventureInt->selection);
  1651. paths.erase(ho);
  1652. adventureInt->terrain.currentPath = NULL;
  1653. }
  1654. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1655. {
  1656. if(vstd::contains(paths,h)) //hero has assigned path
  1657. {
  1658. CGPath &path = paths[h];
  1659. if(!path.nodes.size())
  1660. {
  1661. tlog3 << "Warning: empty path found...\n";
  1662. paths.erase(h);
  1663. }
  1664. else
  1665. {
  1666. assert(h->getPosition(false) == path.startPos());
  1667. //update the hero path in case of something has changed on map
  1668. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1669. return &path;
  1670. else
  1671. paths.erase(h);
  1672. }
  1673. }
  1674. return NULL;
  1675. }
  1676. void CPlayerInterface::acceptTurn()
  1677. {
  1678. waitWhileDialog();
  1679. if(howManyPeople > 1)
  1680. adventureInt->startTurn();
  1681. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1682. /* TODO: This isn't quite right. First day in game should play
  1683. * NEWDAY. And we don't play NEWMONTH. */
  1684. int day = cb->getDate(1);
  1685. if (day != 1)
  1686. CGI->soundh->playSound(soundBase::newDay);
  1687. else
  1688. CGI->soundh->playSound(soundBase::newWeek);
  1689. adventureInt->infoBar.newDay(day);
  1690. //select first hero if available.
  1691. //TODO: check if hero is slept
  1692. if(wanderingHeroes.size())
  1693. adventureInt->select(wanderingHeroes.front());
  1694. else
  1695. adventureInt->select(towns.front());
  1696. adventureInt->showAll(screen);
  1697. }
  1698. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1699. {
  1700. std::string hlp;
  1701. if(h->movement < h->maxMovePoints(true))
  1702. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1703. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1704. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1705. else
  1706. {
  1707. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1708. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1709. if(hlp.length() || t->blockingObjects.size() > 1)
  1710. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1711. else
  1712. cb->dig(h);
  1713. }
  1714. }
  1715. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1716. {
  1717. adventureInt->infoBar.updateSelection(specific);
  1718. // if (adventureInt->selection == specific)
  1719. // adventureInt->infoBar.showAll(screen);
  1720. }
  1721. void CPlayerInterface::battleNewRoundFirst( int round )
  1722. {
  1723. if(LOCPLINT != this)
  1724. { //another local interface should do this
  1725. return;
  1726. }
  1727. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1728. battleInt->newRoundFirst(round);
  1729. }
  1730. void CPlayerInterface::stopMovement()
  1731. {
  1732. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1733. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1734. }
  1735. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1736. {
  1737. if(market->o->ID == 2) //Altar
  1738. {
  1739. EMarketMode mode = market->availableModes().front();
  1740. if(market->allowsTrade(ARTIFACT_EXP) && visitor->getAlignment() != EVIL)
  1741. GH.pushInt(new CAltarWindow(market, visitor, ARTIFACT_EXP));
  1742. else if(market->allowsTrade(CREATURE_EXP) && visitor->getAlignment() != GOOD)
  1743. GH.pushInt(new CAltarWindow(market, visitor, CREATURE_EXP));
  1744. }
  1745. else
  1746. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1747. }
  1748. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1749. {
  1750. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1751. GH.pushInt(cuw);
  1752. }
  1753. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1754. {
  1755. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1756. cmw->artifactsChanged(false);
  1757. }
  1758. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1759. {
  1760. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1761. GH.pushInt(tv);
  1762. }
  1763. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1764. {
  1765. if(obj->state())
  1766. {
  1767. MetaString txt;
  1768. obj->getProblemText(txt);
  1769. showInfoDialog(txt.toString());
  1770. }
  1771. else
  1772. showShipyardDialog(obj);
  1773. }