CPlayerInterface.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  355. return;
  356. boost::unique_lock<boost::recursive_mutex> un(*pim);
  357. updateInfo(hero);
  358. }
  359. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  360. {
  361. boost::unique_lock<boost::recursive_mutex> un(*pim);
  362. updateInfo(hero);
  363. }
  364. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  365. {
  366. boost::unique_lock<boost::recursive_mutex> un(*pim);
  367. if(makingTurn && hero->tempOwner == playerID)
  368. adventureInt->heroList.redraw();
  369. }
  370. void CPlayerInterface::receivedResource(int type, int val)
  371. {
  372. boost::unique_lock<boost::recursive_mutex> un(*pim);
  373. GH.totalRedraw();
  374. }
  375. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  376. {
  377. waitWhileDialog();
  378. CGI->soundh->playSound(soundBase::heroNewLevel);
  379. boost::unique_lock<boost::recursive_mutex> un(*pim);
  380. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  381. GH.pushInt(lw);
  382. }
  383. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  384. {
  385. boost::unique_lock<boost::recursive_mutex> un(*pim);
  386. updateInfo(town);
  387. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  388. {
  389. CGI->mh->hideObject(town->garrisonHero);
  390. wanderingHeroes -= town->garrisonHero;
  391. }
  392. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  393. {
  394. CGI->mh->printObject(town->visitingHero);
  395. wanderingHeroes.push_back(town->visitingHero);
  396. }
  397. //adventureInt->heroList.updateHList();
  398. CCastleInterface *c = castleInt;
  399. if(c)
  400. {
  401. c->garr->highlighted = NULL;
  402. c->hslotup.hero = town->garrisonHero;
  403. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  404. c->garr->set2 = town->visitingHero;
  405. c->garr->recreateSlots();
  406. }
  407. GH.totalRedraw();
  408. }
  409. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  410. {
  411. if(hero->tempOwner != town->tempOwner)
  412. return;
  413. boost::unique_lock<boost::recursive_mutex> un(*pim);
  414. openTownWindow(town);
  415. }
  416. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  417. {
  418. boost::unique_lock<boost::recursive_mutex> un(*pim);
  419. updateInfo(obj);
  420. bool wasGarrison = false;
  421. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  422. {
  423. if((*i)->type & IShowActivable::WITH_GARRISON)
  424. {
  425. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  426. wwg->garr->recreateSlots();
  427. wasGarrison = true;
  428. }
  429. else if(CKingdomInterface *cki = dynamic_cast<CKingdomInterface*>(*i))
  430. {//a cheat for Kingdom Overview window (it has CWindowWithGarrison-childrens which are not present in ListInt)
  431. //need to create "Garrison Holder" class thingy
  432. cki->updateAllGarrisons();
  433. }
  434. else if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(*i))
  435. {
  436. if(obj == cmw->hero)
  437. cmw->garrisonChanged();
  438. }
  439. }
  440. GH.totalRedraw();
  441. }
  442. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  443. {
  444. boost::unique_lock<boost::recursive_mutex> un(*pim);
  445. switch (buildingID)
  446. {
  447. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  448. updateInfo(town);
  449. break;
  450. }
  451. if(!castleInt)
  452. return;
  453. if(castleInt->town!=town)
  454. return;
  455. switch(what)
  456. {
  457. case 1:
  458. CGI->soundh->playSound(soundBase::newBuilding);
  459. castleInt->addBuilding(buildingID);
  460. break;
  461. case 2:
  462. castleInt->removeBuilding(buildingID);
  463. break;
  464. }
  465. }
  466. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  467. {
  468. if(LOCPLINT != this)
  469. { //another local interface should do this
  470. return;
  471. }
  472. while(showingDialog->get())
  473. SDL_Delay(20);
  474. boost::unique_lock<boost::recursive_mutex> un(*pim);
  475. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  476. GH.pushInt(battleInt);
  477. }
  478. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  479. {
  480. }
  481. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  482. {
  483. if(LOCPLINT != this)
  484. { //another local interface should do this
  485. return;
  486. }
  487. for(int b=0; b<healedStacks.size(); ++b)
  488. {
  489. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  490. if(battleInt->creAnims[healed->ID]->getType() == 5)
  491. {
  492. //stack has been resurrected
  493. battleInt->creAnims[healed->ID]->setType(2);
  494. }
  495. }
  496. if (lifeDrain)
  497. {
  498. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  499. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  500. if (attacker)
  501. {
  502. battleInt->displayEffect(50, attacker->position);
  503. }
  504. //print info about life drain
  505. int textOff = 0;
  506. if (attacker->count > 1)
  507. {
  508. textOff += 1;
  509. }
  510. char textBuf[1000];
  511. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  512. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  513. battleInt->console->addText(textBuf);
  514. }
  515. }
  516. void CPlayerInterface::battleNewStackAppeared(int stackID)
  517. {
  518. if(LOCPLINT != this)
  519. { //another local interface should do this
  520. return;
  521. }
  522. //changing necessary things in battle interface
  523. battleInt->newStack(stackID);
  524. }
  525. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  526. {
  527. if(LOCPLINT != this)
  528. { //another local interface should do this
  529. return;
  530. }
  531. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  532. {
  533. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  534. {
  535. if(itBat->first == *it) //remove this obstacle
  536. {
  537. battleInt->idToObstacle.erase(itBat);
  538. break;
  539. }
  540. }
  541. }
  542. //update accessible hexes
  543. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  544. }
  545. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  546. {
  547. if(LOCPLINT != this)
  548. { //another local interface should do this
  549. return;
  550. }
  551. battleInt->stackIsCatapulting(ca);
  552. }
  553. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  554. {
  555. if(LOCPLINT != this)
  556. { //another local interface should do this
  557. return;
  558. }
  559. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  560. {
  561. battleInt->stackRemoved(*it);
  562. }
  563. }
  564. 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
  565. {
  566. if(LOCPLINT != this)
  567. { //another local interface should do this
  568. return;
  569. }
  570. boost::unique_lock<boost::recursive_mutex> un(*pim);
  571. battleInt->newRound(round);
  572. }
  573. void CPlayerInterface::actionStarted(const BattleAction* action)
  574. {
  575. if(LOCPLINT != this)
  576. { //another local interface should do this
  577. return;
  578. }
  579. boost::unique_lock<boost::recursive_mutex> un(*pim);
  580. curAction = new BattleAction(*action);
  581. battleInt->startAction(action);
  582. }
  583. void CPlayerInterface::actionFinished(const BattleAction* action)
  584. {
  585. if(LOCPLINT != this)
  586. { //another local interface should do this
  587. return;
  588. }
  589. boost::unique_lock<boost::recursive_mutex> un(*pim);
  590. delete curAction;
  591. curAction = NULL;
  592. battleInt->endAction(action);
  593. }
  594. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  595. {
  596. CBattleInterface *b = battleInt;
  597. {
  598. boost::unique_lock<boost::recursive_mutex> un(*pim);
  599. const CStack *stack = cb->battleGetStackByID(stackID);
  600. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  601. {
  602. std::string hlp = CGI->generaltexth->allTexts[33];
  603. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->type->namePl : stack->type->nameSing);
  604. battleInt->displayEffect(20,stack->position);
  605. battleInt->console->addText(hlp);
  606. }
  607. b->stackActivated(stackID);
  608. }
  609. //wait till BattleInterface sets its command
  610. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  611. while(!b->givenCommand->data)
  612. b->givenCommand->cond.wait(lock);
  613. //tidy up
  614. BattleAction ret = *(b->givenCommand->data);
  615. delete b->givenCommand->data;
  616. b->givenCommand->data = NULL;
  617. //return command
  618. return ret;
  619. }
  620. void CPlayerInterface::battleEnd(BattleResult *br)
  621. {
  622. if(LOCPLINT != this)
  623. { //another local interface should do this
  624. return;
  625. }
  626. boost::unique_lock<boost::recursive_mutex> un(*pim);
  627. battleInt->battleFinished(*br);
  628. }
  629. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  630. {
  631. if(LOCPLINT != this)
  632. { //another local interface should do this
  633. return;
  634. }
  635. boost::unique_lock<boost::recursive_mutex> un(*pim);
  636. battleInt->stackMoved(ID, dest, end, distance);
  637. }
  638. void CPlayerInterface::battleSpellCast(BattleSpellCast *sc)
  639. {
  640. if(LOCPLINT != this)
  641. { //another local interface should do this
  642. return;
  643. }
  644. boost::unique_lock<boost::recursive_mutex> un(*pim);
  645. battleInt->spellCast(sc);
  646. }
  647. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  648. {
  649. if(LOCPLINT != this)
  650. { //another local interface should do this
  651. return;
  652. }
  653. boost::unique_lock<boost::recursive_mutex> un(*pim);
  654. battleInt->battleStacksEffectsSet(sse);
  655. }
  656. void CPlayerInterface::battleStacksAttacked(std::vector<BattleStackAttacked> & bsa)
  657. {
  658. if(LOCPLINT != this)
  659. { //another local interface should do this
  660. return;
  661. }
  662. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  663. boost::unique_lock<boost::recursive_mutex> un(*pim);
  664. tlog5 << "done!\n";
  665. std::vector<SStackAttackedInfo> arg;
  666. for(std::vector<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  667. {
  668. if(i->isEffect() && i->effect != 12) //and not armageddon
  669. {
  670. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  671. if (stack != NULL)
  672. battleInt->displayEffect(i->effect, stack->position);
  673. }
  674. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  675. arg.push_back(to_put);
  676. }
  677. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  678. {
  679. battleInt->displayEffect(bsa.begin()->effect, -1);
  680. }
  681. battleInt->stacksAreAttacked(arg);
  682. }
  683. void CPlayerInterface::battleAttack(BattleAttack *ba)
  684. {
  685. if(LOCPLINT != this)
  686. { //another local interface should do this
  687. return;
  688. }
  689. tlog5 << "CPlayerInterface::battleAttack - locking...";
  690. boost::unique_lock<boost::recursive_mutex> un(*pim);
  691. tlog5 << "done!\n";
  692. assert(curAction);
  693. if(ba->lucky()) //lucky hit
  694. {
  695. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  696. std::string hlp = CGI->generaltexth->allTexts[45];
  697. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->type->namePl.c_str() : stack->type->nameSing.c_str());
  698. battleInt->console->addText(hlp);
  699. battleInt->displayEffect(18,stack->position);
  700. }
  701. //TODO: bad luck?
  702. if(ba->shot())
  703. {
  704. for(std::vector<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  705. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked), i->stackAttacked);
  706. }
  707. else
  708. {//WARNING: does not support multiple attacked creatures
  709. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  710. int shift = 0;
  711. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  712. {
  713. if( BattleInfo::mutualPosition(curAction->destinationTile + 1, attacker->position) >= 0 )
  714. shift = 1;
  715. else
  716. shift = -1;
  717. }
  718. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, ba->bsa.begin()->stackAttacked );
  719. }
  720. }
  721. void CPlayerInterface::showComp(SComponent comp)
  722. {
  723. boost::unique_lock<boost::recursive_mutex> un(*pim);
  724. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  725. adventureInt->infoBar.showComp(&comp,4000);
  726. }
  727. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  728. {
  729. std::vector<SComponent*> intComps;
  730. for(int i=0;i<components.size();i++)
  731. intComps.push_back(new SComponent(*components[i]));
  732. showInfoDialog(text,intComps,soundID);
  733. }
  734. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  735. {
  736. waitWhileDialog();
  737. boost::unique_lock<boost::recursive_mutex> un(*pim);
  738. stopMovement();
  739. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  740. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  741. {
  742. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  743. showingDialog->set(true);
  744. GH.pushInt(temp);
  745. }
  746. else
  747. {
  748. dialogs.push_back(temp);
  749. }
  750. }
  751. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  752. {
  753. boost::unique_lock<boost::recursive_mutex> un(*pim);
  754. stopMovement();
  755. LOCPLINT->showingDialog->setn(true);
  756. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  757. }
  758. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  759. {
  760. waitWhileDialog();
  761. boost::unique_lock<boost::recursive_mutex> un(*pim);
  762. stopMovement();
  763. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  764. if(!selection && cancel) //simple yes/no dialog
  765. {
  766. std::vector<SComponent*> intComps;
  767. for(int i=0;i<components.size();i++)
  768. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  769. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  770. }
  771. else if(selection)
  772. {
  773. std::vector<CSelectableComponent*> intComps;
  774. for(int i=0;i<components.size();i++)
  775. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  776. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  777. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  778. if(cancel)
  779. {
  780. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  781. }
  782. int charperline = 35;
  783. if (pom.size() > 1)
  784. charperline = 50;
  785. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  786. GH.pushInt(temp);
  787. intComps[0]->clickLeft(true, false);
  788. }
  789. }
  790. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  791. {
  792. boost::unique_lock<boost::recursive_mutex> un(*pim);
  793. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  794. adventureInt->minimap.showTile(*i);
  795. if(pos.size())
  796. GH.totalRedraw();
  797. }
  798. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  799. {
  800. boost::unique_lock<boost::recursive_mutex> un(*pim);
  801. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  802. adventureInt->minimap.hideTile(*i);
  803. if(pos.size())
  804. GH.totalRedraw();
  805. }
  806. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  807. {
  808. boost::unique_lock<boost::recursive_mutex> un(*pim);
  809. adventureInt->heroWindow->setHero(hero);
  810. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  811. GH.pushInt(adventureInt->heroWindow);
  812. }
  813. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  814. {
  815. boost::unique_lock<boost::recursive_mutex> un(*pim);
  816. if(adventureInt->heroWindow->curHero) //hero window is opened
  817. {
  818. adventureInt->heroWindow->deactivate();
  819. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  820. adventureInt->heroWindow->activate();
  821. return;
  822. }
  823. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  824. if(cew) //exchange window is open
  825. {
  826. cew->deactivate();
  827. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  828. {
  829. if(cew->heroInst[g] == hero)
  830. {
  831. cew->artifs[g]->updateState = true;
  832. cew->artifs[g]->setHero(hero);
  833. cew->artifs[g]->updateState = false;
  834. }
  835. }
  836. cew->prepareBackground();
  837. cew->activate();
  838. }
  839. }
  840. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  841. {
  842. boost::unique_lock<boost::recursive_mutex> un(*pim);
  843. if(castleInt && town->ID == TOWNI_TYPE)
  844. {
  845. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  846. if(fs)
  847. fs->draw(castleInt,false);
  848. }
  849. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  850. {
  851. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  852. if(crw)
  853. crw->initCres();
  854. }
  855. }
  856. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  857. {
  858. if(bonus.type == Bonus::NONE) return;
  859. boost::unique_lock<boost::recursive_mutex> un(*pim);
  860. updateInfo(hero);
  861. if (bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING && !gain)
  862. {
  863. //recalculate paths because hero has lost bonus influencing pathfinding
  864. cb->recalculatePaths();
  865. eraseCurrentPathOf(hero, false);
  866. }
  867. }
  868. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  869. {
  870. h & playerID & serialID;
  871. h & sysOpts;
  872. }
  873. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  874. {
  875. serializeTempl(h,version);
  876. }
  877. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  878. {
  879. serializeTempl(h,version);
  880. sysOpts.apply();
  881. firstCall = -1;
  882. }
  883. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  884. {
  885. if (!h)
  886. return false; //can't find hero
  887. pim->unlock();
  888. bool result = false;
  889. {
  890. path.convert(0);
  891. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  892. stillMoveHero.data = CONTINUE_MOVE;
  893. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  894. enum TerrainTile::EterrainType newTerrain;
  895. int sh = -1;
  896. const TerrainTile * curTile = cb->getTileInfo(CGHeroInstance::convertPosition(h->pos, false));
  897. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  898. {
  899. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  900. if(path.nodes[i-1].turns)
  901. {
  902. stillMoveHero.data = STOP_MOVE;
  903. break;
  904. }
  905. // Start a new sound for the hero movement or let the existing one carry on.
  906. #if 0
  907. // TODO
  908. if (hero is flying && sh == -1)
  909. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  910. #endif
  911. {
  912. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  913. if (newTerrain != currentTerrain) {
  914. CGI->soundh->stopSound(sh);
  915. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  916. currentTerrain = newTerrain;
  917. }
  918. }
  919. stillMoveHero.data = WAITING_MOVE;
  920. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  921. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  922. cb->moveHero(h,endpos);
  923. curTile = cb->getTileInfo(endpos);
  924. eventsM.unlock();
  925. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  926. stillMoveHero.cond.wait(un);
  927. eventsM.lock();
  928. if (guarded) // Abort movement if a guard was fought.
  929. break;
  930. }
  931. CGI->soundh->stopSound(sh);
  932. cb->recalculatePaths();
  933. }
  934. pim->lock();
  935. return result;
  936. }
  937. bool CPlayerInterface::shiftPressed() const
  938. {
  939. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  940. }
  941. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  942. {
  943. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  944. return;
  945. {
  946. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  947. while(showingDialog->data)
  948. showingDialog->cond.wait(un);
  949. }
  950. boost::unique_lock<boost::recursive_mutex> un(*pim);
  951. while(dialogs.size())
  952. {
  953. pim->unlock();
  954. SDL_Delay(20);
  955. pim->lock();
  956. }
  957. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  958. cgw->quit->callback += onEnd;
  959. GH.pushInt(cgw);
  960. }
  961. /**
  962. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  963. * into a combinational one on an artifact screen. Does not require the combination of
  964. * artifacts to be legal.
  965. * @param artifactID ID of a constituent artifact.
  966. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  967. * is false.
  968. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  969. */
  970. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  971. {
  972. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  973. std::string text = artifact.Description();
  974. text += "\n\n";
  975. std::vector<SComponent*> scs;
  976. if (assemble) {
  977. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  978. // You possess all of the components to...
  979. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  980. // Picture of assembled artifact at bottom.
  981. SComponent* sc = new SComponent;
  982. sc->type = SComponent::artifact;
  983. sc->subtype = assembledArtifact.id;
  984. sc->description = assembledArtifact.Description();
  985. sc->subtitle = assembledArtifact.Name();
  986. scs.push_back(sc);
  987. } else {
  988. // Do you wish to disassemble this artifact?
  989. text += CGI->generaltexth->allTexts[733];
  990. }
  991. showYesNoDialog(text, scs, onYes, onNo, true);
  992. }
  993. void CPlayerInterface::requestRealized( PackageApplied *pa )
  994. {
  995. if(stillMoveHero.get() == DURING_MOVE)
  996. stillMoveHero.setn(CONTINUE_MOVE);
  997. }
  998. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  999. {
  1000. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1001. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1002. }
  1003. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1004. {
  1005. //redraw minimap if owner changed
  1006. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1007. if(sop->what == ObjProperty::OWNER)
  1008. {
  1009. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1010. std::set<int3> pos = obj->getBlockedPos();
  1011. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1012. {
  1013. if(cb->isVisible(*it))
  1014. adventureInt->minimap.showTile(*it);
  1015. }
  1016. if(obj->ID == TOWNI_TYPE)
  1017. if(obj->tempOwner == playerID)
  1018. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1019. else
  1020. towns -= obj;
  1021. assert(cb->getTownsInfo().size() == towns.size());
  1022. }
  1023. }
  1024. void CPlayerInterface::recreateHeroTownList()
  1025. {
  1026. wanderingHeroes.clear();
  1027. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1028. for(size_t i = 0; i < heroes.size(); i++)
  1029. if(!heroes[i]->inTownGarrison)
  1030. wanderingHeroes.push_back(heroes[i]);
  1031. towns.clear();
  1032. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1033. for(size_t i = 0; i < townInfo.size(); i++)
  1034. towns.push_back(townInfo[i]);
  1035. }
  1036. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1037. {
  1038. if(pos < 0 || pos >= wanderingHeroes.size())
  1039. return NULL;
  1040. return wanderingHeroes[pos];
  1041. }
  1042. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1043. {
  1044. waitWhileDialog();
  1045. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1046. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1047. GH.pushInt(cr);
  1048. }
  1049. void CPlayerInterface::waitWhileDialog()
  1050. {
  1051. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1052. while(showingDialog->data)
  1053. showingDialog->cond.wait(un);
  1054. }
  1055. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1056. {
  1057. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1058. int state = obj->state();
  1059. std::vector<si32> cost;
  1060. obj->getBoatCost(cost);
  1061. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1062. GH.pushInt(csw);
  1063. }
  1064. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1065. {
  1066. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1067. CGI->mh->printObject(obj);
  1068. //we might have built a boat in shipyard in opened town screen
  1069. if(obj->ID == 8
  1070. && LOCPLINT->castleInt
  1071. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1072. {
  1073. CGI->soundh->playSound(soundBase::newBuilding);
  1074. LOCPLINT->castleInt->recreateBuildings();
  1075. }
  1076. }
  1077. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1078. {
  1079. adventureInt->centerOn (pos);
  1080. if(focusTime)
  1081. {
  1082. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1083. if(activeAdv)
  1084. adventureInt->deactivate();
  1085. SDL_Delay(focusTime);
  1086. if(activeAdv)
  1087. adventureInt->activate();
  1088. }
  1089. }
  1090. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1091. {
  1092. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1093. {
  1094. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1095. heroKilled(h);
  1096. }
  1097. }
  1098. bool CPlayerInterface::ctrlPressed() const
  1099. {
  1100. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1101. }
  1102. void CPlayerInterface::update()
  1103. {
  1104. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1105. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1106. if(terminate_cond.get())
  1107. return;
  1108. //if there are any waiting dialogs, show them
  1109. if(dialogs.size() && !showingDialog->get())
  1110. {
  1111. showingDialog->set(true);
  1112. GH.pushInt(dialogs.front());
  1113. dialogs.pop_front();
  1114. }
  1115. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1116. if(!adventureInt->selection && GH.topInt() == adventureInt)
  1117. return;
  1118. GH.updateTime();
  1119. GH.handleEvents();
  1120. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1121. GH.totalRedraw();
  1122. else
  1123. GH.simpleRedraw();
  1124. CGI->curh->draw1();
  1125. CSDL_Ext::update(screen);
  1126. CGI->curh->draw2();
  1127. pim->unlock();
  1128. SDL_framerateDelay(mainFPSmng);
  1129. }
  1130. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1131. {
  1132. using namespace boost::filesystem;
  1133. using namespace boost::algorithm;
  1134. std::map<std::time_t, int> dates; //save number => datestamp
  1135. directory_iterator enddir;
  1136. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1137. {
  1138. if(is_regular(dir->status()))
  1139. {
  1140. std::string name = dir->path().leaf();
  1141. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1142. {
  1143. char nr = name[namePrefix.size()];
  1144. if(std::isdigit(nr))
  1145. {
  1146. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. if(dates.size())
  1152. return (--dates.end())->second; //return latest file number
  1153. return 0;
  1154. }
  1155. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1156. {
  1157. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1158. {
  1159. //ho->moveDir = 1;
  1160. ho->isStanding = false;
  1161. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1162. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1163. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1164. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1165. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1166. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1167. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1168. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1169. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1170. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1171. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1172. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1173. 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);
  1174. 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);
  1175. 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);
  1176. 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);
  1177. 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);
  1178. 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);
  1179. }
  1180. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1181. {
  1182. //ho->moveDir = 2;
  1183. ho->isStanding = false;
  1184. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1185. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1186. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1187. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1188. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1189. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1190. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1191. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1192. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1193. 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);
  1194. 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);
  1195. 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);
  1196. }
  1197. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1198. {
  1199. //ho->moveDir = 3;
  1200. ho->isStanding = false;
  1201. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1202. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1203. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1204. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1205. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1206. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1207. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1208. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1209. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1210. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1211. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1212. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1213. 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);
  1214. 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);
  1215. 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);
  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+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1218. 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);
  1219. }
  1220. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1221. {
  1222. //ho->moveDir = 4;
  1223. ho->isStanding = false;
  1224. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1225. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1226. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1227. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1228. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1229. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1230. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1231. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1232. 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);
  1233. 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);
  1234. }
  1235. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1236. {
  1237. //ho->moveDir = 5;
  1238. ho->isStanding = false;
  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, 31), ho->id);
  1244. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1245. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1246. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1247. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1248. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1249. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1250. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  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. 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);
  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][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1256. 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);
  1257. }
  1258. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1259. {
  1260. //ho->moveDir = 6;
  1261. ho->isStanding = false;
  1262. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1263. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1264. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1265. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1266. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1267. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1268. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1269. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1270. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1271. 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);
  1272. 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);
  1273. 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);
  1274. }
  1275. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1276. {
  1277. //ho->moveDir = 7;
  1278. ho->isStanding = false;
  1279. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1280. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1281. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1282. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1283. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1284. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1285. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1286. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1287. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1288. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1289. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1290. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1291. 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);
  1292. 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);
  1293. 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);
  1294. 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);
  1295. 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);
  1296. 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);
  1297. }
  1298. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1299. {
  1300. //ho->moveDir = 8;
  1301. ho->isStanding = false;
  1302. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1303. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1304. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1305. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1306. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1307. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1308. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1309. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1310. 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);
  1311. 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);
  1312. }
  1313. }
  1314. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1315. {
  1316. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1317. {
  1318. //setting advmap shift
  1319. adventureInt->terrain.moveX = i-32;
  1320. adventureInt->terrain.moveY = i-32;
  1321. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1322. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1323. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1324. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1325. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1326. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1327. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1328. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1329. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1330. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1331. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1332. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1333. }
  1334. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1335. {
  1336. //setting advmap shift
  1337. adventureInt->terrain.moveY = i-32;
  1338. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1339. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1340. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1341. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1342. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1343. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1344. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1345. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1346. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1347. }
  1348. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1349. {
  1350. //setting advmap shift
  1351. adventureInt->terrain.moveX = -i+32;
  1352. adventureInt->terrain.moveY = i-32;
  1353. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1354. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1355. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1356. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1357. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1358. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1359. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1360. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1361. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1362. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1363. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1364. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1365. }
  1366. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1367. {
  1368. //setting advmap shift
  1369. adventureInt->terrain.moveX = -i+32;
  1370. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1371. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1372. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1373. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1374. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1375. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1376. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1377. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1378. }
  1379. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1380. {
  1381. //setting advmap shift
  1382. adventureInt->terrain.moveX = -i+32;
  1383. adventureInt->terrain.moveY = -i+32;
  1384. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1385. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1386. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1387. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1388. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1389. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1390. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1391. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1392. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1393. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1394. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1395. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1396. }
  1397. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1398. {
  1399. //setting advmap shift
  1400. adventureInt->terrain.moveY = -i+32;
  1401. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1402. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1403. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1404. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1405. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1406. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1407. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1408. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1409. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1410. }
  1411. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1412. {
  1413. //setting advmap shift
  1414. adventureInt->terrain.moveX = i-32;
  1415. adventureInt->terrain.moveY = -i+32;
  1416. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1417. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1418. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1419. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1420. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1421. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1422. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1423. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1424. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1425. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1426. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1427. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1428. }
  1429. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1430. {
  1431. //setting advmap shift
  1432. adventureInt->terrain.moveX = i-32;
  1433. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1434. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1435. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1436. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1437. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1438. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1439. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1440. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1441. }
  1442. }
  1443. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1444. {
  1445. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1446. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1447. {
  1448. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1449. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1450. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1451. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1452. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1453. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1454. }
  1455. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1456. {
  1457. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1458. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1459. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1460. }
  1461. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1462. {
  1463. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1464. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1465. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1466. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1467. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1468. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1469. }
  1470. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1471. {
  1472. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1473. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1474. }
  1475. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1476. {
  1477. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1478. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1479. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1480. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1481. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1482. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1483. }
  1484. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1485. {
  1486. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1487. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1488. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1489. }
  1490. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1491. {
  1492. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1493. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1494. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1495. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1496. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1497. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1498. }
  1499. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1500. {
  1501. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1502. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1503. }
  1504. //restoring good rects
  1505. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1506. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1507. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1508. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1509. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1510. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1511. //restoring good order of objects
  1512. 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);
  1513. 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);
  1514. 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);
  1515. 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);
  1516. 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);
  1517. 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);
  1518. }
  1519. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1520. {
  1521. if(LOCPLINT != this)
  1522. return;
  1523. if(player == playerID)
  1524. {
  1525. if(!victory)
  1526. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1527. // else
  1528. // showInfoDialog("Placeholder message: you won!");
  1529. makingTurn = true;
  1530. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1531. makingTurn = false;
  1532. //return to main menu
  1533. SDL_Event event;
  1534. event.type = SDL_USEREVENT;
  1535. event.user.code = 2;
  1536. SDL_PushEvent(&event);
  1537. }
  1538. else
  1539. {
  1540. if(!victory) //enemy has lost
  1541. {
  1542. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1543. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1544. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1545. }
  1546. }
  1547. }
  1548. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1549. {
  1550. }
  1551. void CPlayerInterface::showPuzzleMap()
  1552. {
  1553. waitWhileDialog();
  1554. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1555. //TODO: interface should not know the real position of Grail...
  1556. float ratio = 0;
  1557. int3 grailPos = cb->getGrailPos(ratio);
  1558. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1559. }
  1560. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1561. {
  1562. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1563. {
  1564. cb->recalculatePaths();
  1565. eraseCurrentPathOf(caster, false);
  1566. }
  1567. }
  1568. void SystemOptions::setMusicVolume( int newVolume )
  1569. {
  1570. musicVolume = newVolume;
  1571. CGI->musich->setVolume(newVolume);
  1572. settingsChanged();
  1573. }
  1574. void SystemOptions::setSoundVolume( int newVolume )
  1575. {
  1576. soundVolume = newVolume;
  1577. CGI->soundh->setVolume(newVolume);
  1578. settingsChanged();
  1579. }
  1580. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1581. {
  1582. heroMoveSpeed = newSpeed;
  1583. settingsChanged();
  1584. }
  1585. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1586. {
  1587. mapScrollingSpeed = newSpeed;
  1588. settingsChanged();
  1589. }
  1590. void SystemOptions::settingsChanged()
  1591. {
  1592. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1593. if(settings.sfile)
  1594. settings << *this;
  1595. else
  1596. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1597. }
  1598. void SystemOptions::apply()
  1599. {
  1600. if(CGI->musich->getVolume() != musicVolume)
  1601. CGI->musich->setVolume(musicVolume);
  1602. if(CGI->soundh->getVolume() != soundVolume)
  1603. CGI->soundh->setVolume(soundVolume);
  1604. settingsChanged();
  1605. }
  1606. SystemOptions::SystemOptions()
  1607. {
  1608. heroMoveSpeed = 2;
  1609. mapScrollingSpeed = 2;
  1610. musicVolume = 88;
  1611. soundVolume = 88;
  1612. printCellBorders = true;
  1613. printStackRange = true;
  1614. animSpeed = 2;
  1615. printMouseShadow = true;
  1616. showQueue = true;
  1617. }
  1618. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1619. {
  1620. if(checkForExistanceOfPath)
  1621. {
  1622. assert(vstd::contains(paths, ho));
  1623. }
  1624. else if (!vstd::contains(paths, ho))
  1625. {
  1626. return;
  1627. }
  1628. assert(ho == adventureInt->selection);
  1629. paths.erase(ho);
  1630. adventureInt->terrain.currentPath = NULL;
  1631. }
  1632. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1633. {
  1634. if(vstd::contains(paths,h)) //hero has assigned path
  1635. {
  1636. CGPath &path = paths[h];
  1637. if(!path.nodes.size())
  1638. {
  1639. tlog3 << "Warning: empty path found...\n";
  1640. paths.erase(h);
  1641. }
  1642. else
  1643. {
  1644. assert(h->getPosition(false) == path.startPos());
  1645. //update the hero path in case of something has changed on map
  1646. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1647. return &path;
  1648. else
  1649. paths.erase(h);
  1650. }
  1651. }
  1652. return NULL;
  1653. }
  1654. void CPlayerInterface::acceptTurn()
  1655. {
  1656. waitWhileDialog();
  1657. if(howManyPeople > 1)
  1658. adventureInt->startTurn();
  1659. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1660. /* TODO: This isn't quite right. First day in game should play
  1661. * NEWDAY. And we don't play NEWMONTH. */
  1662. int day = cb->getDate(1);
  1663. if (day != 1)
  1664. CGI->soundh->playSound(soundBase::newDay);
  1665. else
  1666. CGI->soundh->playSound(soundBase::newWeek);
  1667. adventureInt->infoBar.newDay(day);
  1668. //select first hero if available.
  1669. //TODO: check if hero is slept
  1670. if(wanderingHeroes.size())
  1671. adventureInt->select(wanderingHeroes.front());
  1672. else
  1673. adventureInt->select(towns.front());
  1674. adventureInt->showAll(screen);
  1675. }
  1676. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1677. {
  1678. std::string hlp;
  1679. if(h->movement < h->maxMovePoints(true))
  1680. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1681. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1682. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1683. else
  1684. {
  1685. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1686. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1687. if(hlp.length() || t->blockingObjects.size() > 1)
  1688. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1689. else
  1690. cb->dig(h);
  1691. }
  1692. }
  1693. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1694. {
  1695. adventureInt->infoBar.updateSelection(specific);
  1696. // if (adventureInt->selection == specific)
  1697. // adventureInt->infoBar.showAll(screen);
  1698. }
  1699. void CPlayerInterface::battleNewRoundFirst( int round )
  1700. {
  1701. if(LOCPLINT != this)
  1702. { //another local interface should do this
  1703. return;
  1704. }
  1705. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1706. battleInt->newRoundFirst(round);
  1707. }
  1708. void CPlayerInterface::stopMovement()
  1709. {
  1710. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1711. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1712. }
  1713. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1714. {
  1715. CMarketplaceWindow *cmw = new CMarketplaceWindow(market, visitor, market->availableModes().front());
  1716. GH.pushInt(cmw);
  1717. }
  1718. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1719. {
  1720. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1721. cmw->artifactsChanged(false);
  1722. }
  1723. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1724. {
  1725. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1726. GH.pushInt(tv);
  1727. }