CPlayerInterface.cpp 67 KB

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