CPlayerInterface.cpp 61 KB

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