CPlayerInterface.cpp 72 KB

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