CPlayerInterface.cpp 71 KB

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