CPlayerInterface.cpp 65 KB

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