2
0

CPlayerInterface.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  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. eventsM.unlock();
  935. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  936. stillMoveHero.cond.wait(un);
  937. eventsM.lock();
  938. }
  939. CGI->soundh->stopSound(sh);
  940. //stillMoveHero = false;
  941. cb->recalculatePaths();
  942. return result;
  943. }
  944. bool CPlayerInterface::shiftPressed() const
  945. {
  946. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  947. }
  948. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  949. {
  950. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  951. return;
  952. {
  953. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  954. while(showingDialog->data)
  955. showingDialog->cond.wait(un);
  956. }
  957. boost::unique_lock<boost::recursive_mutex> un(*pim);
  958. while(dialogs.size())
  959. {
  960. pim->unlock();
  961. SDL_Delay(20);
  962. pim->lock();
  963. }
  964. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  965. cgw->quit->callback += onEnd;
  966. GH.pushInt(cgw);
  967. }
  968. /**
  969. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  970. * into a combinational one on an artifact screen. Does not require the combination of
  971. * artifacts to be legal.
  972. * @param artifactID ID of a constituent artifact.
  973. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  974. * is false.
  975. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  976. */
  977. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  978. {
  979. const CArtifact &artifact = CGI->arth->artifacts[artifactID];
  980. std::string text = artifact.Description();
  981. text += "\n\n";
  982. std::vector<SComponent*> scs;
  983. if (assemble) {
  984. const CArtifact &assembledArtifact = CGI->arth->artifacts[assembleTo];
  985. // You possess all of the components to...
  986. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  987. // Picture of assembled artifact at bottom.
  988. SComponent* sc = new SComponent;
  989. sc->type = SComponent::artifact;
  990. sc->subtype = assembledArtifact.id;
  991. sc->description = assembledArtifact.Description();
  992. sc->subtitle = assembledArtifact.Name();
  993. scs.push_back(sc);
  994. } else {
  995. // Do you wish to disassemble this artifact?
  996. text += CGI->generaltexth->allTexts[733];
  997. }
  998. showYesNoDialog(text, scs, onYes, onNo, true);
  999. }
  1000. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1001. {
  1002. if(stillMoveHero.get() == DURING_MOVE)
  1003. stillMoveHero.setn(CONTINUE_MOVE);
  1004. }
  1005. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1006. {
  1007. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1008. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1009. }
  1010. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1011. {
  1012. //redraw minimap if owner changed
  1013. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1014. if(sop->what == 1)
  1015. {
  1016. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1017. std::set<int3> pos = obj->getBlockedPos();
  1018. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1019. {
  1020. if(cb->isVisible(*it))
  1021. adventureInt->minimap.showTile(*it);
  1022. }
  1023. if(obj->ID == TOWNI_TYPE)
  1024. if(obj->tempOwner == playerID)
  1025. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1026. else
  1027. towns -= obj;
  1028. assert(cb->getTownsInfo().size() == towns.size());
  1029. }
  1030. }
  1031. void CPlayerInterface::recreateHeroTownList()
  1032. {
  1033. wanderingHeroes.clear();
  1034. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1035. for(size_t i = 0; i < heroes.size(); i++)
  1036. if(!heroes[i]->inTownGarrison)
  1037. wanderingHeroes.push_back(heroes[i]);
  1038. towns.clear();
  1039. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1040. for(size_t i = 0; i < townInfo.size(); i++)
  1041. towns.push_back(townInfo[i]);
  1042. }
  1043. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1044. {
  1045. if(pos < 0 || pos >= wanderingHeroes.size())
  1046. return NULL;
  1047. return wanderingHeroes[pos];
  1048. }
  1049. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1050. {
  1051. waitWhileDialog();
  1052. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1053. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1054. GH.pushInt(cr);
  1055. }
  1056. void CPlayerInterface::waitWhileDialog()
  1057. {
  1058. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1059. while(showingDialog->data)
  1060. showingDialog->cond.wait(un);
  1061. }
  1062. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1063. {
  1064. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1065. int state = obj->state();
  1066. std::vector<si32> cost;
  1067. obj->getBoatCost(cost);
  1068. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1069. GH.pushInt(csw);
  1070. }
  1071. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1072. {
  1073. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1074. CGI->mh->printObject(obj);
  1075. //we might have built a boat in shipyard in opened town screen
  1076. if(obj->ID == 8
  1077. && LOCPLINT->castleInt
  1078. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1079. {
  1080. CGI->soundh->playSound(soundBase::newBuilding);
  1081. LOCPLINT->castleInt->recreateBuildings();
  1082. }
  1083. }
  1084. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1085. {
  1086. adventureInt->centerOn (pos);
  1087. if(focusTime)
  1088. {
  1089. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1090. if(activeAdv)
  1091. adventureInt->deactivate();
  1092. SDL_Delay(focusTime);
  1093. if(activeAdv)
  1094. adventureInt->activate();
  1095. }
  1096. }
  1097. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1098. {
  1099. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1100. {
  1101. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1102. heroKilled(h);
  1103. }
  1104. }
  1105. bool CPlayerInterface::ctrlPressed() const
  1106. {
  1107. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1108. }
  1109. void CPlayerInterface::update()
  1110. {
  1111. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1112. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1113. if(terminate_cond.get())
  1114. return;
  1115. //if there are any waiting dialogs, show them
  1116. if(dialogs.size() && !showingDialog->get())
  1117. {
  1118. showingDialog->set(true);
  1119. GH.pushInt(dialogs.front());
  1120. dialogs.pop_front();
  1121. }
  1122. GH.updateTime();
  1123. GH.handleEvents();
  1124. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1125. GH.totalRedraw();
  1126. else
  1127. GH.simpleRedraw();
  1128. CGI->curh->draw1();
  1129. CSDL_Ext::update(screen);
  1130. CGI->curh->draw2();
  1131. pim->unlock();
  1132. SDL_framerateDelay(mainFPSmng);
  1133. }
  1134. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1135. {
  1136. using namespace boost::filesystem;
  1137. using namespace boost::algorithm;
  1138. std::map<std::time_t, int> dates; //save number => datestamp
  1139. directory_iterator enddir;
  1140. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1141. {
  1142. if(is_regular(dir->status()))
  1143. {
  1144. std::string name = dir->path().leaf();
  1145. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1146. {
  1147. char nr = name[namePrefix.size()];
  1148. if(std::isdigit(nr))
  1149. {
  1150. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. if(dates.size())
  1156. return (--dates.end())->second; //return latest file number
  1157. return 0;
  1158. }
  1159. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1160. {
  1161. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1162. {
  1163. //ho->moveDir = 1;
  1164. ho->isStanding = false;
  1165. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1166. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1167. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1168. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1169. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1170. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1171. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1172. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1173. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1174. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1175. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1176. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1177. 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);
  1178. 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);
  1179. 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);
  1180. 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);
  1181. 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);
  1182. 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);
  1183. }
  1184. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1185. {
  1186. //ho->moveDir = 2;
  1187. ho->isStanding = false;
  1188. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1189. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1190. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1191. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1192. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1193. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1194. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1195. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1196. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1197. 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);
  1198. 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);
  1199. 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);
  1200. }
  1201. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1202. {
  1203. //ho->moveDir = 3;
  1204. ho->isStanding = false;
  1205. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1206. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1207. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1208. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1209. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1210. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1211. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1212. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1213. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1214. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1215. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1216. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1217. 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);
  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][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][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+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1222. 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);
  1223. }
  1224. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1225. {
  1226. //ho->moveDir = 4;
  1227. ho->isStanding = false;
  1228. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1229. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1230. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1231. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1232. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1233. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1234. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1235. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1236. 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);
  1237. 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);
  1238. }
  1239. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1240. {
  1241. //ho->moveDir = 5;
  1242. ho->isStanding = false;
  1243. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1244. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1245. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1246. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1247. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1248. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1249. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1250. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1251. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1252. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1253. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1254. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1255. 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);
  1256. 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);
  1257. 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);
  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. 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);
  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. }
  1262. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1263. {
  1264. //ho->moveDir = 6;
  1265. ho->isStanding = false;
  1266. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1267. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1268. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1269. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1270. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1271. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1272. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1273. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1274. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1275. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1276. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1277. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1278. }
  1279. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1280. {
  1281. //ho->moveDir = 7;
  1282. ho->isStanding = false;
  1283. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1284. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1285. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1286. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1287. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1288. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1289. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1290. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1291. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1292. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1293. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1294. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  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-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  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-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1299. 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);
  1300. 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);
  1301. }
  1302. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1303. {
  1304. //ho->moveDir = 8;
  1305. ho->isStanding = false;
  1306. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1307. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1308. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1309. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1310. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1311. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1312. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1313. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1314. 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);
  1315. 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);
  1316. }
  1317. }
  1318. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1319. {
  1320. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1321. {
  1322. //setting advmap shift
  1323. adventureInt->terrain.moveX = i-32;
  1324. adventureInt->terrain.moveY = i-32;
  1325. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1326. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1327. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1328. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1329. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1330. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1331. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1332. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1333. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1334. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1335. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1336. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1337. }
  1338. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1339. {
  1340. //setting advmap shift
  1341. adventureInt->terrain.moveY = i-32;
  1342. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1343. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1344. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1345. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1346. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1347. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1348. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1349. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1350. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1351. }
  1352. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1353. {
  1354. //setting advmap shift
  1355. adventureInt->terrain.moveX = -i+32;
  1356. adventureInt->terrain.moveY = i-32;
  1357. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1358. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1359. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1360. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1361. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1362. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1363. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1364. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1365. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1366. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1367. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1368. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1369. }
  1370. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1371. {
  1372. //setting advmap shift
  1373. adventureInt->terrain.moveX = -i+32;
  1374. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1375. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1376. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1377. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1378. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1379. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1380. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1381. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1382. }
  1383. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1384. {
  1385. //setting advmap shift
  1386. adventureInt->terrain.moveX = -i+32;
  1387. adventureInt->terrain.moveY = -i+32;
  1388. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1389. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1390. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1391. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1392. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1393. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1394. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1395. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1396. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1397. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1398. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1399. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1400. }
  1401. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1402. {
  1403. //setting advmap shift
  1404. adventureInt->terrain.moveY = -i+32;
  1405. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1406. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1407. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1408. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1409. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1410. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1411. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1412. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1413. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1414. }
  1415. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1416. {
  1417. //setting advmap shift
  1418. adventureInt->terrain.moveX = i-32;
  1419. adventureInt->terrain.moveY = -i+32;
  1420. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1421. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1422. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1423. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1424. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1425. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1426. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1427. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1428. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1429. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1430. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1431. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1432. }
  1433. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1434. {
  1435. //setting advmap shift
  1436. adventureInt->terrain.moveX = i-32;
  1437. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1438. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1439. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1440. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1441. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1442. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1443. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1444. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1445. }
  1446. }
  1447. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1448. {
  1449. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1450. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1451. {
  1452. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1453. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1454. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1455. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1456. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1457. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1458. }
  1459. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1460. {
  1461. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1462. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1463. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1464. }
  1465. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1466. {
  1467. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1468. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1469. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1470. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1471. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1472. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1473. }
  1474. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1475. {
  1476. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1477. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1478. }
  1479. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1480. {
  1481. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1482. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1483. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1484. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1485. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1486. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1487. }
  1488. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1489. {
  1490. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1491. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1492. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1493. }
  1494. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1495. {
  1496. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1497. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1498. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1499. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1500. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1501. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1502. }
  1503. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1504. {
  1505. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1506. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1507. }
  1508. //restoring good rects
  1509. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1510. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1511. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1512. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1513. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1514. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1515. //restoring good order of objects
  1516. 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);
  1517. 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);
  1518. 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);
  1519. 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);
  1520. 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);
  1521. 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);
  1522. }
  1523. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1524. {
  1525. if(LOCPLINT != this)
  1526. return;
  1527. if(player == playerID)
  1528. {
  1529. if(!victory)
  1530. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1531. // else
  1532. // showInfoDialog("Placeholder message: you won!");
  1533. makingTurn = true;
  1534. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1535. makingTurn = false;
  1536. //return to main menu
  1537. SDL_Event event;
  1538. event.type = SDL_USEREVENT;
  1539. event.user.code = 2;
  1540. SDL_PushEvent(&event);
  1541. }
  1542. else
  1543. {
  1544. if(!victory) //enemy has lost
  1545. {
  1546. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1547. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1548. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1549. }
  1550. }
  1551. }
  1552. void CPlayerInterface::playerBonusChanged( const HeroBonus &bonus, bool gain )
  1553. {
  1554. }
  1555. void CPlayerInterface::showPuzzleMap()
  1556. {
  1557. waitWhileDialog();
  1558. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1559. //TODO: interface should not know the real position of Grail...
  1560. float ratio = 0;
  1561. int3 grailPos = cb->getGrailPos(ratio);
  1562. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1563. }
  1564. void SystemOptions::setMusicVolume( int newVolume )
  1565. {
  1566. musicVolume = newVolume;
  1567. CGI->musich->setVolume(newVolume);
  1568. settingsChanged();
  1569. }
  1570. void SystemOptions::setSoundVolume( int newVolume )
  1571. {
  1572. soundVolume = newVolume;
  1573. CGI->soundh->setVolume(newVolume);
  1574. settingsChanged();
  1575. }
  1576. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1577. {
  1578. heroMoveSpeed = newSpeed;
  1579. settingsChanged();
  1580. }
  1581. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1582. {
  1583. mapScrollingSpeed = newSpeed;
  1584. settingsChanged();
  1585. }
  1586. void SystemOptions::settingsChanged()
  1587. {
  1588. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1589. if(settings.sfile)
  1590. settings << *this;
  1591. else
  1592. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1593. }
  1594. void SystemOptions::apply()
  1595. {
  1596. if(CGI->musich->getVolume() != musicVolume)
  1597. CGI->musich->setVolume(musicVolume);
  1598. if(CGI->soundh->getVolume() != soundVolume)
  1599. CGI->soundh->setVolume(soundVolume);
  1600. settingsChanged();
  1601. }
  1602. SystemOptions::SystemOptions()
  1603. {
  1604. heroMoveSpeed = 2;
  1605. mapScrollingSpeed = 2;
  1606. musicVolume = 88;
  1607. soundVolume = 88;
  1608. printCellBorders = true;
  1609. printStackRange = true;
  1610. animSpeed = 2;
  1611. printMouseShadow = true;
  1612. showQueue = true;
  1613. }
  1614. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho )
  1615. {
  1616. assert(vstd::contains(paths, ho));
  1617. assert(ho == adventureInt->selection);
  1618. paths.erase(ho);
  1619. adventureInt->terrain.currentPath = NULL;
  1620. }
  1621. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1622. {
  1623. if(vstd::contains(paths,h)) //hero has assigned path
  1624. {
  1625. CGPath &path = paths[h];
  1626. if(!path.nodes.size())
  1627. {
  1628. tlog3 << "Warning: empty path found...\n";
  1629. paths.erase(h);
  1630. }
  1631. else
  1632. {
  1633. assert(h->getPosition(false) == path.startPos());
  1634. //update the hero path in case of something has changed on map
  1635. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1636. return &path;
  1637. else
  1638. paths.erase(h);
  1639. }
  1640. }
  1641. return NULL;
  1642. }
  1643. void CPlayerInterface::acceptTurn()
  1644. {
  1645. waitWhileDialog();
  1646. if(howManyPeople > 1)
  1647. adventureInt->startTurn();
  1648. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1649. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  1650. SDL_FreeSurface(i->second);
  1651. graphics->heroWins.clear();
  1652. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  1653. for(int i=0;i<hh.size();i++)
  1654. {
  1655. SDL_Surface * pom = infoWin(hh[i]);
  1656. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  1657. }
  1658. /* TODO: This isn't quite right. First day in game should play
  1659. * NEWDAY. And we don't play NEWMONTH. */
  1660. int day = cb->getDate(1);
  1661. if (day != 1)
  1662. CGI->soundh->playSound(soundBase::newDay);
  1663. else
  1664. CGI->soundh->playSound(soundBase::newWeek);
  1665. adventureInt->infoBar.newDay(day);
  1666. //select first hero if available.
  1667. //TODO: check if hero is slept
  1668. if(wanderingHeroes.size())
  1669. adventureInt->select(wanderingHeroes.front());
  1670. else
  1671. adventureInt->select(towns.front());
  1672. adventureInt->showAll(screen);
  1673. }
  1674. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1675. {
  1676. std::string hlp;
  1677. if(h->movement < h->maxMovePoints(true))
  1678. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1679. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1680. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1681. else
  1682. {
  1683. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1684. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1685. if(hlp.length() || t->blockingObjects.size() > 1)
  1686. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1687. else
  1688. cb->dig(h);
  1689. }
  1690. }