CPlayerInterface.cpp 65 KB

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