2
0

CPlayerInterface.cpp 67 KB

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