CPlayerInterface.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. }
  804. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  805. {
  806. boost::unique_lock<boost::recursive_mutex> un(*pim);
  807. adventureInt->heroWindow->setHero(hero);
  808. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  809. GH.pushInt(adventureInt->heroWindow);
  810. }
  811. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  812. {
  813. boost::unique_lock<boost::recursive_mutex> un(*pim);
  814. if(adventureInt->heroWindow->curHero) //hero window is opened
  815. {
  816. adventureInt->heroWindow->deactivate();
  817. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  818. adventureInt->heroWindow->activate();
  819. return;
  820. }
  821. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  822. if(cew) //exchange window is open
  823. {
  824. cew->deactivate();
  825. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  826. {
  827. if(cew->heroInst[g] == hero)
  828. {
  829. cew->artifs[g]->updateState = true;
  830. cew->artifs[g]->setHero(hero);
  831. cew->artifs[g]->updateState = false;
  832. }
  833. }
  834. cew->prepareBackground();
  835. cew->activate();
  836. }
  837. }
  838. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  839. {
  840. boost::unique_lock<boost::recursive_mutex> un(*pim);
  841. if(castleInt && town->ID == TOWNI_TYPE)
  842. {
  843. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  844. if(fs)
  845. fs->draw(castleInt,false);
  846. }
  847. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  848. {
  849. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  850. if(crw)
  851. crw->initCres();
  852. }
  853. }
  854. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  855. {
  856. if(bonus.type == Bonus::NONE) return;
  857. boost::unique_lock<boost::recursive_mutex> un(*pim);
  858. updateInfo(hero);
  859. if (bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING && !gain)
  860. {
  861. //recalculate paths because hero has lost bonus influencing pathfinding
  862. cb->recalculatePaths();
  863. eraseCurrentPathOf(hero, false);
  864. }
  865. }
  866. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  867. {
  868. h & playerID & serialID;
  869. h & sysOpts;
  870. }
  871. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  872. {
  873. serializeTempl(h,version);
  874. }
  875. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  876. {
  877. serializeTempl(h,version);
  878. sysOpts.apply();
  879. firstCall = -1;
  880. }
  881. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  882. {
  883. if (!h)
  884. return false; //can't find hero
  885. pim->unlock();
  886. bool result = false;
  887. {
  888. path.convert(0);
  889. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  890. stillMoveHero.data = CONTINUE_MOVE;
  891. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  892. enum TerrainTile::EterrainType newTerrain;
  893. int sh = -1;
  894. const TerrainTile * curTile = cb->getTileInfo(CGHeroInstance::convertPosition(h->pos, false));
  895. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  896. {
  897. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  898. if(path.nodes[i-1].turns)
  899. {
  900. stillMoveHero.data = STOP_MOVE;
  901. break;
  902. }
  903. // Start a new sound for the hero movement or let the existing one carry on.
  904. #if 0
  905. // TODO
  906. if (hero is flying && sh == -1)
  907. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  908. #endif
  909. {
  910. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  911. if (newTerrain != currentTerrain) {
  912. CGI->soundh->stopSound(sh);
  913. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  914. currentTerrain = newTerrain;
  915. }
  916. }
  917. stillMoveHero.data = WAITING_MOVE;
  918. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  919. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  920. cb->moveHero(h,endpos);
  921. curTile = cb->getTileInfo(endpos);
  922. eventsM.unlock();
  923. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  924. stillMoveHero.cond.wait(un);
  925. eventsM.lock();
  926. if (guarded) // Abort movement if a guard was fought.
  927. break;
  928. }
  929. CGI->soundh->stopSound(sh);
  930. cb->recalculatePaths();
  931. }
  932. pim->lock();
  933. return result;
  934. }
  935. bool CPlayerInterface::shiftPressed() const
  936. {
  937. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  938. }
  939. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  940. {
  941. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  942. return;
  943. {
  944. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  945. while(showingDialog->data)
  946. showingDialog->cond.wait(un);
  947. }
  948. boost::unique_lock<boost::recursive_mutex> un(*pim);
  949. while(dialogs.size())
  950. {
  951. pim->unlock();
  952. SDL_Delay(20);
  953. pim->lock();
  954. }
  955. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  956. cgw->quit->callback += onEnd;
  957. GH.pushInt(cgw);
  958. }
  959. /**
  960. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  961. * into a combinational one on an artifact screen. Does not require the combination of
  962. * artifacts to be legal.
  963. * @param artifactID ID of a constituent artifact.
  964. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  965. * is false.
  966. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  967. */
  968. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  969. {
  970. const CArtifact &artifact = CGI->arth->artifacts[artifactID];
  971. std::string text = artifact.Description();
  972. text += "\n\n";
  973. std::vector<SComponent*> scs;
  974. if (assemble) {
  975. const CArtifact &assembledArtifact = CGI->arth->artifacts[assembleTo];
  976. // You possess all of the components to...
  977. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  978. // Picture of assembled artifact at bottom.
  979. SComponent* sc = new SComponent;
  980. sc->type = SComponent::artifact;
  981. sc->subtype = assembledArtifact.id;
  982. sc->description = assembledArtifact.Description();
  983. sc->subtitle = assembledArtifact.Name();
  984. scs.push_back(sc);
  985. } else {
  986. // Do you wish to disassemble this artifact?
  987. text += CGI->generaltexth->allTexts[733];
  988. }
  989. showYesNoDialog(text, scs, onYes, onNo, true);
  990. }
  991. void CPlayerInterface::requestRealized( PackageApplied *pa )
  992. {
  993. if(stillMoveHero.get() == DURING_MOVE)
  994. stillMoveHero.setn(CONTINUE_MOVE);
  995. }
  996. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  997. {
  998. boost::unique_lock<boost::recursive_mutex> un(*pim);
  999. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1000. }
  1001. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1002. {
  1003. //redraw minimap if owner changed
  1004. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1005. if(sop->what == ObjProperty::OWNER)
  1006. {
  1007. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1008. std::set<int3> pos = obj->getBlockedPos();
  1009. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1010. {
  1011. if(cb->isVisible(*it))
  1012. adventureInt->minimap.showTile(*it);
  1013. }
  1014. if(obj->ID == TOWNI_TYPE)
  1015. if(obj->tempOwner == playerID)
  1016. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1017. else
  1018. towns -= obj;
  1019. assert(cb->getTownsInfo().size() == towns.size());
  1020. }
  1021. }
  1022. void CPlayerInterface::recreateHeroTownList()
  1023. {
  1024. wanderingHeroes.clear();
  1025. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1026. for(size_t i = 0; i < heroes.size(); i++)
  1027. if(!heroes[i]->inTownGarrison)
  1028. wanderingHeroes.push_back(heroes[i]);
  1029. towns.clear();
  1030. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1031. for(size_t i = 0; i < townInfo.size(); i++)
  1032. towns.push_back(townInfo[i]);
  1033. }
  1034. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1035. {
  1036. if(pos < 0 || pos >= wanderingHeroes.size())
  1037. return NULL;
  1038. return wanderingHeroes[pos];
  1039. }
  1040. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1041. {
  1042. waitWhileDialog();
  1043. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1044. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1045. GH.pushInt(cr);
  1046. }
  1047. void CPlayerInterface::waitWhileDialog()
  1048. {
  1049. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1050. while(showingDialog->data)
  1051. showingDialog->cond.wait(un);
  1052. }
  1053. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1054. {
  1055. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1056. int state = obj->state();
  1057. std::vector<si32> cost;
  1058. obj->getBoatCost(cost);
  1059. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1060. GH.pushInt(csw);
  1061. }
  1062. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1063. {
  1064. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1065. CGI->mh->printObject(obj);
  1066. //we might have built a boat in shipyard in opened town screen
  1067. if(obj->ID == 8
  1068. && LOCPLINT->castleInt
  1069. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1070. {
  1071. CGI->soundh->playSound(soundBase::newBuilding);
  1072. LOCPLINT->castleInt->recreateBuildings();
  1073. }
  1074. }
  1075. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1076. {
  1077. adventureInt->centerOn (pos);
  1078. if(focusTime)
  1079. {
  1080. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1081. if(activeAdv)
  1082. adventureInt->deactivate();
  1083. SDL_Delay(focusTime);
  1084. if(activeAdv)
  1085. adventureInt->activate();
  1086. }
  1087. }
  1088. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1089. {
  1090. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1091. {
  1092. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1093. heroKilled(h);
  1094. }
  1095. }
  1096. bool CPlayerInterface::ctrlPressed() const
  1097. {
  1098. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1099. }
  1100. void CPlayerInterface::update()
  1101. {
  1102. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1103. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1104. if(terminate_cond.get())
  1105. return;
  1106. //if there are any waiting dialogs, show them
  1107. if(dialogs.size() && !showingDialog->get())
  1108. {
  1109. showingDialog->set(true);
  1110. GH.pushInt(dialogs.front());
  1111. dialogs.pop_front();
  1112. }
  1113. GH.updateTime();
  1114. GH.handleEvents();
  1115. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1116. GH.totalRedraw();
  1117. else
  1118. GH.simpleRedraw();
  1119. CGI->curh->draw1();
  1120. CSDL_Ext::update(screen);
  1121. CGI->curh->draw2();
  1122. pim->unlock();
  1123. SDL_framerateDelay(mainFPSmng);
  1124. }
  1125. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1126. {
  1127. using namespace boost::filesystem;
  1128. using namespace boost::algorithm;
  1129. std::map<std::time_t, int> dates; //save number => datestamp
  1130. directory_iterator enddir;
  1131. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1132. {
  1133. if(is_regular(dir->status()))
  1134. {
  1135. std::string name = dir->path().leaf();
  1136. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1137. {
  1138. char nr = name[namePrefix.size()];
  1139. if(std::isdigit(nr))
  1140. {
  1141. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1142. }
  1143. }
  1144. }
  1145. }
  1146. if(dates.size())
  1147. return (--dates.end())->second; //return latest file number
  1148. return 0;
  1149. }
  1150. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1151. {
  1152. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1153. {
  1154. //ho->moveDir = 1;
  1155. ho->isStanding = false;
  1156. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1157. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1158. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1159. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1160. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1161. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1162. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1163. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1164. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1165. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1166. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1167. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1168. 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);
  1169. 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);
  1170. 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);
  1171. 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);
  1172. 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);
  1173. 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);
  1174. }
  1175. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1176. {
  1177. //ho->moveDir = 2;
  1178. ho->isStanding = false;
  1179. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1180. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1181. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1182. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1183. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1184. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1185. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1186. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1187. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1188. 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);
  1189. 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);
  1190. 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);
  1191. }
  1192. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1193. {
  1194. //ho->moveDir = 3;
  1195. ho->isStanding = false;
  1196. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1197. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1198. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1199. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1200. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1201. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1202. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1203. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1204. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1205. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1206. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1207. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1208. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1209. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1210. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1211. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1212. 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);
  1213. 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);
  1214. }
  1215. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1216. {
  1217. //ho->moveDir = 4;
  1218. ho->isStanding = false;
  1219. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1220. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1221. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1222. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1223. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1224. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1225. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1226. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1227. 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);
  1228. 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);
  1229. }
  1230. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1231. {
  1232. //ho->moveDir = 5;
  1233. ho->isStanding = false;
  1234. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1235. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1236. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1237. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1238. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1239. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1240. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1241. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1242. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1243. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1244. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1245. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1246. 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);
  1247. 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);
  1248. 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);
  1249. 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);
  1250. 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);
  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. }
  1253. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1254. {
  1255. //ho->moveDir = 6;
  1256. ho->isStanding = false;
  1257. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1258. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1259. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1260. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1261. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1262. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1263. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1264. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1265. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1266. 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);
  1267. 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);
  1268. 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);
  1269. }
  1270. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1271. {
  1272. //ho->moveDir = 7;
  1273. ho->isStanding = false;
  1274. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1275. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1276. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1277. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1278. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1279. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1280. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1281. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1282. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1283. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1284. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1285. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1286. 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);
  1287. 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);
  1288. 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);
  1289. 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);
  1290. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1291. 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);
  1292. }
  1293. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1294. {
  1295. //ho->moveDir = 8;
  1296. ho->isStanding = false;
  1297. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1298. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1299. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1300. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1301. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1302. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1303. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1304. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1305. 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);
  1306. 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);
  1307. }
  1308. }
  1309. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1310. {
  1311. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1312. {
  1313. //setting advmap shift
  1314. adventureInt->terrain.moveX = i-32;
  1315. adventureInt->terrain.moveY = i-32;
  1316. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1317. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1318. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1319. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1320. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1321. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1322. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1323. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1324. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1325. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1326. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1327. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1328. }
  1329. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1330. {
  1331. //setting advmap shift
  1332. adventureInt->terrain.moveY = i-32;
  1333. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1334. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1335. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1336. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1337. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1338. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1339. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1340. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1341. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1342. }
  1343. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1344. {
  1345. //setting advmap shift
  1346. adventureInt->terrain.moveX = -i+32;
  1347. adventureInt->terrain.moveY = i-32;
  1348. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1349. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1350. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1351. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1352. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1353. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1354. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1355. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1356. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1357. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1358. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1359. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1360. }
  1361. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1362. {
  1363. //setting advmap shift
  1364. adventureInt->terrain.moveX = -i+32;
  1365. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1366. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1367. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1368. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1369. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1370. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1371. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1372. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1373. }
  1374. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1375. {
  1376. //setting advmap shift
  1377. adventureInt->terrain.moveX = -i+32;
  1378. adventureInt->terrain.moveY = -i+32;
  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, 31-i), ho->id);
  1384. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1385. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1386. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1387. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1388. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1389. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1390. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1391. }
  1392. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1393. {
  1394. //setting advmap shift
  1395. adventureInt->terrain.moveY = -i+32;
  1396. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1397. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1398. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1399. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1400. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1401. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1402. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1403. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1404. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1405. }
  1406. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1407. {
  1408. //setting advmap shift
  1409. adventureInt->terrain.moveX = i-32;
  1410. adventureInt->terrain.moveY = -i+32;
  1411. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1412. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1413. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1414. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1415. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1416. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1417. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1418. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1419. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1420. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1421. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1422. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1423. }
  1424. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1425. {
  1426. //setting advmap shift
  1427. adventureInt->terrain.moveX = i-32;
  1428. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1429. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1430. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1431. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1432. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1433. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1434. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1435. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1436. }
  1437. }
  1438. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1439. {
  1440. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1441. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1442. {
  1443. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1444. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1445. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1446. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1447. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1448. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1449. }
  1450. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1451. {
  1452. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1453. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1454. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1455. }
  1456. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1457. {
  1458. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1459. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1460. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1461. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1462. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1463. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1464. }
  1465. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1466. {
  1467. delObjRect(hp.x-2, hp.y-1, 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-1 == details.start.y) //br
  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. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1475. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1476. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1477. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1478. }
  1479. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1480. {
  1481. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1482. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1483. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1484. }
  1485. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1486. {
  1487. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1488. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1489. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1490. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1491. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1492. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1493. }
  1494. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1495. {
  1496. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1497. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1498. }
  1499. //restoring good rects
  1500. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1501. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1502. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1503. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1504. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1505. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1506. //restoring good order of objects
  1507. 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);
  1508. 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);
  1509. 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);
  1510. 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);
  1511. 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);
  1512. 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);
  1513. }
  1514. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1515. {
  1516. if(LOCPLINT != this)
  1517. return;
  1518. if(player == playerID)
  1519. {
  1520. if(!victory)
  1521. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1522. // else
  1523. // showInfoDialog("Placeholder message: you won!");
  1524. makingTurn = true;
  1525. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1526. makingTurn = false;
  1527. //return to main menu
  1528. SDL_Event event;
  1529. event.type = SDL_USEREVENT;
  1530. event.user.code = 2;
  1531. SDL_PushEvent(&event);
  1532. }
  1533. else
  1534. {
  1535. if(!victory) //enemy has lost
  1536. {
  1537. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1538. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1539. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1540. }
  1541. }
  1542. }
  1543. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1544. {
  1545. }
  1546. void CPlayerInterface::showPuzzleMap()
  1547. {
  1548. waitWhileDialog();
  1549. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1550. //TODO: interface should not know the real position of Grail...
  1551. float ratio = 0;
  1552. int3 grailPos = cb->getGrailPos(ratio);
  1553. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1554. }
  1555. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1556. {
  1557. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1558. {
  1559. cb->recalculatePaths();
  1560. eraseCurrentPathOf(caster, false);
  1561. }
  1562. }
  1563. void SystemOptions::setMusicVolume( int newVolume )
  1564. {
  1565. musicVolume = newVolume;
  1566. CGI->musich->setVolume(newVolume);
  1567. settingsChanged();
  1568. }
  1569. void SystemOptions::setSoundVolume( int newVolume )
  1570. {
  1571. soundVolume = newVolume;
  1572. CGI->soundh->setVolume(newVolume);
  1573. settingsChanged();
  1574. }
  1575. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1576. {
  1577. heroMoveSpeed = newSpeed;
  1578. settingsChanged();
  1579. }
  1580. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1581. {
  1582. mapScrollingSpeed = newSpeed;
  1583. settingsChanged();
  1584. }
  1585. void SystemOptions::settingsChanged()
  1586. {
  1587. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1588. if(settings.sfile)
  1589. settings << *this;
  1590. else
  1591. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1592. }
  1593. void SystemOptions::apply()
  1594. {
  1595. if(CGI->musich->getVolume() != musicVolume)
  1596. CGI->musich->setVolume(musicVolume);
  1597. if(CGI->soundh->getVolume() != soundVolume)
  1598. CGI->soundh->setVolume(soundVolume);
  1599. settingsChanged();
  1600. }
  1601. SystemOptions::SystemOptions()
  1602. {
  1603. heroMoveSpeed = 2;
  1604. mapScrollingSpeed = 2;
  1605. musicVolume = 88;
  1606. soundVolume = 88;
  1607. printCellBorders = true;
  1608. printStackRange = true;
  1609. animSpeed = 2;
  1610. printMouseShadow = true;
  1611. showQueue = true;
  1612. }
  1613. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1614. {
  1615. if(checkForExistanceOfPath)
  1616. {
  1617. assert(vstd::contains(paths, ho));
  1618. }
  1619. else if (!vstd::contains(paths, ho))
  1620. {
  1621. return;
  1622. }
  1623. assert(ho == adventureInt->selection);
  1624. paths.erase(ho);
  1625. adventureInt->terrain.currentPath = NULL;
  1626. }
  1627. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1628. {
  1629. if(vstd::contains(paths,h)) //hero has assigned path
  1630. {
  1631. CGPath &path = paths[h];
  1632. if(!path.nodes.size())
  1633. {
  1634. tlog3 << "Warning: empty path found...\n";
  1635. paths.erase(h);
  1636. }
  1637. else
  1638. {
  1639. assert(h->getPosition(false) == path.startPos());
  1640. //update the hero path in case of something has changed on map
  1641. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1642. return &path;
  1643. else
  1644. paths.erase(h);
  1645. }
  1646. }
  1647. return NULL;
  1648. }
  1649. void CPlayerInterface::acceptTurn()
  1650. {
  1651. waitWhileDialog();
  1652. if(howManyPeople > 1)
  1653. adventureInt->startTurn();
  1654. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1655. /* TODO: This isn't quite right. First day in game should play
  1656. * NEWDAY. And we don't play NEWMONTH. */
  1657. int day = cb->getDate(1);
  1658. if (day != 1)
  1659. CGI->soundh->playSound(soundBase::newDay);
  1660. else
  1661. CGI->soundh->playSound(soundBase::newWeek);
  1662. adventureInt->infoBar.newDay(day);
  1663. //select first hero if available.
  1664. //TODO: check if hero is slept
  1665. if(wanderingHeroes.size())
  1666. adventureInt->select(wanderingHeroes.front());
  1667. else
  1668. adventureInt->select(towns.front());
  1669. adventureInt->showAll(screen);
  1670. }
  1671. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1672. {
  1673. std::string hlp;
  1674. if(h->movement < h->maxMovePoints(true))
  1675. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1676. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1677. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1678. else
  1679. {
  1680. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1681. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1682. if(hlp.length() || t->blockingObjects.size() > 1)
  1683. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1684. else
  1685. cb->dig(h);
  1686. }
  1687. }
  1688. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1689. {
  1690. adventureInt->infoBar.updateSelection(specific);
  1691. // if (adventureInt->selection == specific)
  1692. // adventureInt->infoBar.showAll(screen);
  1693. }
  1694. void CPlayerInterface::battleNewRoundFirst( int round )
  1695. {
  1696. if(LOCPLINT != this)
  1697. { //another local interface should do this
  1698. return;
  1699. }
  1700. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1701. battleInt->newRoundFirst(round);
  1702. }
  1703. void CPlayerInterface::stopMovement()
  1704. {
  1705. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1706. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1707. }
  1708. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1709. {
  1710. CMarketplaceWindow *cmw = new CMarketplaceWindow(market, visitor, market->availableModes().front());
  1711. GH.pushInt(cmw);
  1712. }