CPlayerInterface.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. #include <boost/foreach.hpp>
  49. #ifdef min
  50. #undef min
  51. #endif
  52. #ifdef max
  53. #undef max
  54. #endif
  55. /*
  56. * CPlayerInterface.cpp, part of VCMI engine
  57. *
  58. * Authors: listed in file AUTHORS in main folder
  59. *
  60. * License: GNU General Public License v2.0 or later
  61. * Full text of license available in license.txt file, in main folder
  62. *
  63. */
  64. using namespace boost::assign;
  65. using namespace CSDL_Ext;
  66. void processCommand(const std::string &message, CClient *&client);
  67. extern Point screenLTmax;
  68. extern std::queue<SDL_Event*> events;
  69. extern boost::mutex eventsM;
  70. CPlayerInterface * LOCPLINT;
  71. CBattleInterface * CPlayerInterface::battleInt;
  72. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  73. CondSh<EMoveState> stillMoveHero; //used during hero movement
  74. int CPlayerInterface::howManyPeople = 0;
  75. struct OCM_HLP_CGIN
  76. {
  77. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  78. {
  79. return (*a.first)<(*b.first);
  80. }
  81. } ocmptwo_cgin ;
  82. CPlayerInterface::CPlayerInterface(int Player)
  83. {
  84. observerInDuelMode = false;
  85. howManyPeople++;
  86. GH.defActionsDef = 0;
  87. LOCPLINT = this;
  88. curAction = NULL;
  89. playerID=Player;
  90. human=true;
  91. castleInt = NULL;
  92. battleInt = NULL;
  93. pim = new boost::recursive_mutex;
  94. makingTurn = false;
  95. showingDialog = new CondSh<bool>(false);
  96. sysOpts = GDefaultOptions;
  97. //initializing framerate keeper
  98. mainFPSmng = new FPSmanager;
  99. SDL_initFramerate(mainFPSmng);
  100. SDL_setFramerate(mainFPSmng, 48);
  101. //framerate keeper initialized
  102. cingconsole = new CInGameConsole;
  103. terminate_cond.set(false);
  104. firstCall = 1; //if loading will be overwritten in serialize
  105. autosaveCount = 0;
  106. }
  107. CPlayerInterface::~CPlayerInterface()
  108. {
  109. howManyPeople--;
  110. delete pim;
  111. delete showingDialog;
  112. delete mainFPSmng;
  113. if(adventureInt)
  114. {
  115. if(adventureInt->active & CIntObject::KEYBOARD)
  116. adventureInt->deactivateKeys();
  117. delete adventureInt;
  118. adventureInt = NULL;
  119. }
  120. if(cingconsole->active) //TODO
  121. cingconsole->deactivate();
  122. delete cingconsole;
  123. LOCPLINT = NULL;
  124. }
  125. void CPlayerInterface::init(ICallback * CB)
  126. {
  127. cb = dynamic_cast<CCallback*>(CB);
  128. if(observerInDuelMode)
  129. {
  130. return;
  131. }
  132. if(!adventureInt)
  133. adventureInt = new CAdvMapInt();
  134. if(!towns.size() && !wanderingHeroes.size())
  135. {
  136. recreateHeroTownList();
  137. }
  138. }
  139. void CPlayerInterface::yourTurn()
  140. {
  141. {
  142. boost::unique_lock<boost::recursive_mutex> un(*pim);
  143. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  144. LOCPLINT = this;
  145. GH.curInt = this;
  146. adventureInt->selection = NULL;
  147. if(firstCall)
  148. {
  149. if(howManyPeople == 1)
  150. adventureInt->setPlayer(playerID);
  151. autosaveCount = getLastIndex("Autosave_");
  152. if(!GH.listInt.size())
  153. {
  154. GH.pushInt(adventureInt);
  155. adventureInt->activateKeys();
  156. }
  157. if(firstCall > 0) //new game, not loaded
  158. {
  159. int index = getLastIndex("Newgame_Autosave_");
  160. index %= SAVES_COUNT;
  161. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  162. }
  163. firstCall = 0;
  164. }
  165. else
  166. {
  167. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  168. autosaveCount %= 5;
  169. }
  170. if(adventureInt->player != playerID)
  171. adventureInt->setPlayer(playerID);
  172. if(howManyPeople > 1) //hot seat message
  173. {
  174. adventureInt->startHotSeatWait(playerID);
  175. makingTurn = true;
  176. std::string msg = CGI->generaltexth->allTexts[13];
  177. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  178. std::vector<SComponent*> cmp;
  179. cmp.push_back(new SComponent(SComponent::flag, playerID, 0));
  180. showInfoDialog(msg, cmp);
  181. }
  182. else
  183. {
  184. makingTurn = true;
  185. adventureInt->startTurn();
  186. }
  187. }
  188. acceptTurn();
  189. }
  190. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  191. {
  192. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  193. for(int h=0; h<hlp.objects.size(); ++h)
  194. if(hlp.objects[h].first->id==hid)
  195. {
  196. hlp.objects[h].second = r;
  197. return;
  198. }
  199. }
  200. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  201. {
  202. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  203. for(int h=0; h<hlp.objects.size(); ++h)
  204. if(hlp.objects[h].first->id==hid)
  205. {
  206. hlp.objects.erase(hlp.objects.begin()+h);
  207. return;
  208. }
  209. }
  210. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  211. {
  212. if(LOCPLINT != this)
  213. return;
  214. boost::unique_lock<boost::recursive_mutex> un(*pim);
  215. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  216. int3 hp = details.start;
  217. adventureInt->centerOn(ho); //actualizing screen pos
  218. adventureInt->minimap.draw(screen2);
  219. adventureInt->heroList.draw(screen2);
  220. bool directlyAttackingCreature =
  221. CGI->mh->map->isInTheMap(details.attackedFrom)
  222. && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
  223. && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
  224. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  225. {
  226. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  227. {
  228. if(adventureInt->terrain.currentPath)
  229. eraseCurrentPathOf(ho);
  230. return; //teleport - no fancy moving animation
  231. //TODO: smooth disappear / appear effect
  232. }
  233. if (details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED //hero didn't change tile but visit succeeded
  234. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  235. {
  236. eraseCurrentPathOf(ho);
  237. }
  238. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  239. {
  240. //remove one node from the path (the one we went)
  241. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  242. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  243. eraseCurrentPathOf(ho);
  244. }
  245. }
  246. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  247. {
  248. ho->isStanding = true;
  249. stillMoveHero.setn(STOP_MOVE);
  250. GH.totalRedraw();
  251. return;
  252. }
  253. initMovement(details, ho, hp);
  254. //first initializing done
  255. SDL_framerateDelay(mainFPSmng); // after first move
  256. //main moving
  257. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  258. {
  259. movementPxStep(details, i, hp, ho);
  260. adventureInt->updateScreen = true;
  261. adventureInt->show(screen);
  262. CSDL_Ext::update(screen);
  263. SDL_framerateDelay(mainFPSmng); //for animation purposes
  264. } //for(int i=1; i<32; i+=4)
  265. //main moving done
  266. //finishing move
  267. finishMovement(details, hp, ho);
  268. ho->isStanding = true;
  269. //move finished
  270. adventureInt->minimap.draw(screen2);
  271. adventureInt->heroList.updateMove(ho);
  272. //check if user cancelled movement
  273. {
  274. boost::unique_lock<boost::mutex> un(eventsM);
  275. while(events.size())
  276. {
  277. SDL_Event *ev = events.front();
  278. events.pop();
  279. switch(ev->type)
  280. {
  281. case SDL_MOUSEBUTTONDOWN:
  282. stillMoveHero.setn(STOP_MOVE);
  283. break;
  284. case SDL_KEYDOWN:
  285. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  286. stillMoveHero.setn(STOP_MOVE);
  287. break;
  288. }
  289. delete ev;
  290. }
  291. }
  292. if(stillMoveHero.get() == WAITING_MOVE)
  293. stillMoveHero.setn(DURING_MOVE);
  294. // Hero attacked creature directly, set direction to face it.
  295. if (directlyAttackingCreature) {
  296. // Get direction to attacker.
  297. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  298. static const ui8 dirLookup[3][3] = {
  299. { 1, 2, 3 },
  300. { 8, 0, 4 },
  301. { 7, 6, 5 }
  302. };
  303. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  304. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  305. }
  306. }
  307. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  308. {
  309. boost::unique_lock<boost::recursive_mutex> un(*pim);
  310. wanderingHeroes -= hero;
  311. adventureInt->heroList.updateHList(hero);
  312. }
  313. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  314. {
  315. boost::unique_lock<boost::recursive_mutex> un(*pim);
  316. wanderingHeroes.push_back(hero);
  317. adventureInt->heroList.updateHList();
  318. }
  319. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  320. {
  321. if (castleInt)
  322. GH.popIntTotally(castleInt);
  323. castleInt = new CCastleInterface(town);
  324. GH.pushInt(castleInt);
  325. }
  326. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  327. {
  328. if(!specific)
  329. specific = adventureInt->selection;
  330. assert(specific);
  331. switch(specific->ID)
  332. {
  333. case HEROI_TYPE:
  334. {
  335. InfoAboutHero iah;
  336. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  337. assert(gotInfo);
  338. return graphics->drawHeroInfoWin(iah);
  339. }
  340. case TOWNI_TYPE:
  341. case 33: // Garrison
  342. case 219:
  343. {
  344. InfoAboutTown iah;
  345. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  346. assert(gotInfo);
  347. return graphics->drawTownInfoWin(iah);
  348. }
  349. default:
  350. return NULL;
  351. }
  352. }
  353. int3 CPlayerInterface::repairScreenPos(int3 pos)
  354. {
  355. if(pos.x<-CGI->mh->frameW)
  356. pos.x = -CGI->mh->frameW;
  357. if(pos.y<-CGI->mh->frameH)
  358. pos.y = -CGI->mh->frameH;
  359. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  360. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  361. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  362. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  363. return pos;
  364. }
  365. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  366. {
  367. boost::unique_lock<boost::recursive_mutex> un(*pim);
  368. if(which == 4)
  369. {
  370. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  371. ctw->setExpToLevel();
  372. }
  373. else if(which < PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  374. updateInfo(hero);
  375. }
  376. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  377. {
  378. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  379. if(cuw) //university window is open
  380. {
  381. GH.totalRedraw();
  382. }
  383. }
  384. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  385. {
  386. boost::unique_lock<boost::recursive_mutex> un(*pim);
  387. updateInfo(hero);
  388. }
  389. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  390. {
  391. boost::unique_lock<boost::recursive_mutex> un(*pim);
  392. if(makingTurn && hero->tempOwner == playerID)
  393. adventureInt->heroList.redraw();
  394. }
  395. void CPlayerInterface::receivedResource(int type, int val)
  396. {
  397. boost::unique_lock<boost::recursive_mutex> un(*pim);
  398. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  399. mw->resourceChanged(type, val);
  400. GH.totalRedraw();
  401. }
  402. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  403. {
  404. waitWhileDialog();
  405. CCS->soundh->playSound(soundBase::heroNewLevel);
  406. boost::unique_lock<boost::recursive_mutex> un(*pim);
  407. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  408. GH.pushInt(lw);
  409. }
  410. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  411. {
  412. boost::unique_lock<boost::recursive_mutex> un(*pim);
  413. updateInfo(town);
  414. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  415. {
  416. CGI->mh->hideObject(town->garrisonHero);
  417. wanderingHeroes -= town->garrisonHero;
  418. }
  419. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  420. {
  421. CGI->mh->printObject(town->visitingHero);
  422. wanderingHeroes.push_back(town->visitingHero);
  423. }
  424. //adventureInt->heroList.updateHList();
  425. CCastleInterface *c = castleInt;
  426. if(c)
  427. {
  428. c->garr->highlighted = NULL;
  429. c->hslotup.hero = town->garrisonHero;
  430. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  431. c->garr->set2 = town->visitingHero;
  432. c->garr->ourUp = LOCPLINT->playerID==(town->garrisonHero ? town->garrisonHero->tempOwner : town->tempOwner);
  433. c->garr->ourDown = (town->visitingHero ? LOCPLINT->playerID==town->visitingHero->tempOwner : false);
  434. c->garr->recreateSlots();
  435. }
  436. GH.totalRedraw();
  437. }
  438. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  439. {
  440. if(hero->tempOwner != playerID )
  441. return;
  442. boost::unique_lock<boost::recursive_mutex> un(*pim);
  443. openTownWindow(town);
  444. }
  445. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  446. {
  447. boost::unique_lock<boost::recursive_mutex> un(*pim);
  448. updateInfo(obj);
  449. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  450. {
  451. if((*i)->type & IShowActivable::WITH_GARRISON)
  452. {
  453. CGarrisonHolder *cgh = dynamic_cast<CGarrisonHolder*>(*i);
  454. cgh->updateGarrisons();
  455. }
  456. else if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
  457. {
  458. if(obj == cmw->hero)
  459. cmw->garrisonChanged();
  460. }
  461. }
  462. GH.totalRedraw();
  463. }
  464. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  465. {
  466. boost::unique_lock<boost::recursive_mutex> un(*pim);
  467. switch (buildingID)
  468. {
  469. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  470. updateInfo(town);
  471. break;
  472. }
  473. if(!castleInt)
  474. return;
  475. if(castleInt->town!=town)
  476. return;
  477. switch(what)
  478. {
  479. case 1:
  480. CCS->soundh->playSound(soundBase::newBuilding);
  481. castleInt->addBuilding(buildingID);
  482. break;
  483. case 2:
  484. castleInt->removeBuilding(buildingID);
  485. break;
  486. }
  487. }
  488. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  489. {
  490. if(LOCPLINT != this)
  491. { //another local interface should do this
  492. return;
  493. }
  494. while(showingDialog->get())
  495. SDL_Delay(20);
  496. boost::unique_lock<boost::recursive_mutex> un(*pim);
  497. CCS->musich->playMusicFromSet(CCS->musich->battleMusics, -1);
  498. GH.pushInt(battleInt);
  499. }
  500. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  501. {
  502. if(LOCPLINT != this)
  503. { //another local interface should do this
  504. return;
  505. }
  506. for(int b=0; b<healedStacks.size(); ++b)
  507. {
  508. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  509. if(battleInt->creAnims[healed->ID]->getType() == 5)
  510. {
  511. //stack has been resurrected
  512. battleInt->creAnims[healed->ID]->setType(2);
  513. }
  514. }
  515. if (lifeDrain)
  516. {
  517. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  518. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  519. if (attacker)
  520. {
  521. battleInt->displayEffect(50, attacker->position);
  522. }
  523. //print info about life drain
  524. int textOff = 0;
  525. if (attacker->count > 1)
  526. {
  527. textOff += 1;
  528. }
  529. char textBuf[1000];
  530. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  531. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  532. battleInt->console->addText(textBuf);
  533. }
  534. }
  535. void CPlayerInterface::battleNewStackAppeared(const CStack * stack)
  536. {
  537. if(LOCPLINT != this)
  538. { //another local interface should do this
  539. return;
  540. }
  541. //changing necessary things in battle interface
  542. battleInt->newStack(stack);
  543. }
  544. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  545. {
  546. if(LOCPLINT != this)
  547. { //another local interface should do this
  548. return;
  549. }
  550. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  551. {
  552. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  553. {
  554. if(itBat->first == *it) //remove this obstacle
  555. {
  556. battleInt->idToObstacle.erase(itBat);
  557. break;
  558. }
  559. }
  560. }
  561. //update accessible hexes
  562. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  563. }
  564. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  565. {
  566. if(LOCPLINT != this)
  567. { //another local interface should do this
  568. return;
  569. }
  570. battleInt->stackIsCatapulting(ca);
  571. }
  572. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  573. {
  574. if(LOCPLINT != this)
  575. { //another local interface should do this
  576. return;
  577. }
  578. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  579. {
  580. battleInt->stackRemoved(LOCPLINT->cb->battleGetStackByID(*it));
  581. }
  582. }
  583. 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
  584. {
  585. if(LOCPLINT != this)
  586. { //another local interface should do this
  587. return;
  588. }
  589. boost::unique_lock<boost::recursive_mutex> un(*pim);
  590. battleInt->newRound(round);
  591. }
  592. void CPlayerInterface::actionStarted(const BattleAction* action)
  593. {
  594. if(LOCPLINT != this)
  595. { //another local interface should do this
  596. return;
  597. }
  598. boost::unique_lock<boost::recursive_mutex> un(*pim);
  599. curAction = new BattleAction(*action);
  600. battleInt->startAction(action);
  601. }
  602. void CPlayerInterface::actionFinished(const BattleAction* action)
  603. {
  604. if(LOCPLINT != this)
  605. { //another local interface should do this
  606. return;
  607. }
  608. boost::unique_lock<boost::recursive_mutex> un(*pim);
  609. delete curAction;
  610. curAction = NULL;
  611. battleInt->endAction(action);
  612. }
  613. BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
  614. {
  615. CBattleInterface *b = battleInt;
  616. {
  617. boost::unique_lock<boost::recursive_mutex> un(*pim);
  618. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  619. {
  620. std::string hlp = CGI->generaltexth->allTexts[33];
  621. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl : stack->getCreature()->nameSing);
  622. battleInt->displayEffect(20,stack->position);
  623. battleInt->console->addText(hlp);
  624. }
  625. b->stackActivated(stack);
  626. }
  627. //wait till BattleInterface sets its command
  628. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  629. while(!b->givenCommand->data)
  630. b->givenCommand->cond.wait(lock);
  631. //tidy up
  632. BattleAction ret = *(b->givenCommand->data);
  633. delete b->givenCommand->data;
  634. b->givenCommand->data = NULL;
  635. //return command
  636. return ret;
  637. }
  638. void CPlayerInterface::battleEnd(const BattleResult *br)
  639. {
  640. if(LOCPLINT != this)
  641. { //another local interface should do this
  642. return;
  643. }
  644. boost::unique_lock<boost::recursive_mutex> un(*pim);
  645. battleInt->battleFinished(*br);
  646. }
  647. void CPlayerInterface::battleStackMoved(const CStack * stack, THex dest, int distance, bool end)
  648. {
  649. if(LOCPLINT != this)
  650. { //another local interface should do this
  651. return;
  652. }
  653. boost::unique_lock<boost::recursive_mutex> un(*pim);
  654. battleInt->stackMoved(stack, dest, end, distance);
  655. }
  656. void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
  657. {
  658. if(LOCPLINT != this)
  659. { //another local interface should do this
  660. return;
  661. }
  662. boost::unique_lock<boost::recursive_mutex> un(*pim);
  663. battleInt->spellCast(sc);
  664. }
  665. void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
  666. {
  667. if(LOCPLINT != this)
  668. { //another local interface should do this
  669. return;
  670. }
  671. boost::unique_lock<boost::recursive_mutex> un(*pim);
  672. battleInt->battleStacksEffectsSet(sse);
  673. }
  674. void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  675. {
  676. if(LOCPLINT != this)
  677. { //another local interface should do this
  678. return;
  679. }
  680. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  681. boost::unique_lock<boost::recursive_mutex> un(*pim);
  682. tlog5 << "done!\n";
  683. std::vector<SStackAttackedInfo> arg;
  684. for(std::vector<BattleStackAttacked>::const_iterator i = bsa.begin(); i != bsa.end(); i++)
  685. {
  686. const CStack *defender = cb->battleGetStackByID(i->stackAttacked, false);
  687. const CStack *attacker = cb->battleGetStackByID(i->attackerID, false);
  688. if(i->isEffect() && i->effect != 12) //and not armageddon
  689. {
  690. if (defender != NULL)
  691. battleInt->displayEffect(i->effect, defender->position);
  692. }
  693. SStackAttackedInfo to_put = {defender, i->damageAmount, i->killedAmount, attacker, LOCPLINT->curAction->actionType==7, i->killed()};
  694. arg.push_back(to_put);
  695. }
  696. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  697. {
  698. battleInt->displayEffect(bsa.begin()->effect, -1);
  699. }
  700. battleInt->stacksAreAttacked(arg);
  701. }
  702. void CPlayerInterface::battleAttack(const BattleAttack *ba)
  703. {
  704. if(LOCPLINT != this)
  705. { //another local interface should do this
  706. return;
  707. }
  708. tlog5 << "CPlayerInterface::battleAttack - locking...";
  709. boost::unique_lock<boost::recursive_mutex> un(*pim);
  710. tlog5 << "done!\n";
  711. assert(curAction);
  712. if(ba->lucky()) //lucky hit
  713. {
  714. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  715. std::string hlp = CGI->generaltexth->allTexts[45];
  716. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  717. battleInt->console->addText(hlp);
  718. battleInt->displayEffect(18,stack->position);
  719. }
  720. //TODO: bad luck?
  721. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  722. if(ba->shot())
  723. {
  724. for(std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  725. {
  726. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  727. battleInt->stackAttacking(attacker, cb->battleGetPos(i->stackAttacked), attacked, true);
  728. }
  729. }
  730. else
  731. {//WARNING: does not support multiple attacked creatures
  732. int shift = 0;
  733. if(ba->counter() && THex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  734. {
  735. int distp = THex::getDistance(curAction->destinationTile + 1, attacker->position);
  736. int distm = THex::getDistance(curAction->destinationTile - 1, attacker->position);
  737. if( distp < distm )
  738. shift = 1;
  739. else
  740. shift = -1;
  741. }
  742. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  743. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  744. }
  745. }
  746. void CPlayerInterface::showComp(SComponent comp)
  747. {
  748. boost::unique_lock<boost::recursive_mutex> un(*pim);
  749. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  750. adventureInt->infoBar.showComp(&comp,4000);
  751. }
  752. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  753. {
  754. std::vector<SComponent*> intComps;
  755. for(int i=0;i<components.size();i++)
  756. intComps.push_back(new SComponent(*components[i]));
  757. showInfoDialog(text,intComps,soundID);
  758. }
  759. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID, bool delComps)
  760. {
  761. waitWhileDialog();
  762. boost::unique_lock<boost::recursive_mutex> un(*pim);
  763. stopMovement();
  764. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  765. temp->setDelComps(delComps);
  766. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  767. {
  768. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  769. showingDialog->set(true);
  770. GH.pushInt(temp);
  771. }
  772. else
  773. {
  774. dialogs.push_back(temp);
  775. }
  776. }
  777. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  778. {
  779. boost::unique_lock<boost::recursive_mutex> un(*pim);
  780. stopMovement();
  781. LOCPLINT->showingDialog->setn(true);
  782. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  783. }
  784. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  785. {
  786. waitWhileDialog();
  787. boost::unique_lock<boost::recursive_mutex> un(*pim);
  788. stopMovement();
  789. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  790. if(!selection && cancel) //simple yes/no dialog
  791. {
  792. std::vector<SComponent*> intComps;
  793. for(int i=0;i<components.size();i++)
  794. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  795. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  796. }
  797. else if(selection)
  798. {
  799. std::vector<CSelectableComponent*> intComps;
  800. for(int i=0;i<components.size();i++)
  801. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  802. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  803. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  804. if(cancel)
  805. {
  806. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  807. }
  808. int charperline = 35;
  809. if (pom.size() > 1)
  810. charperline = 50;
  811. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  812. GH.pushInt(temp);
  813. intComps[0]->clickLeft(true, false);
  814. }
  815. }
  816. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  817. {
  818. boost::unique_lock<boost::recursive_mutex> un(*pim);
  819. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  820. adventureInt->minimap.showTile(*i);
  821. if(pos.size())
  822. GH.totalRedraw();
  823. }
  824. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  825. {
  826. boost::unique_lock<boost::recursive_mutex> un(*pim);
  827. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  828. adventureInt->minimap.hideTile(*i);
  829. if(pos.size())
  830. GH.totalRedraw();
  831. }
  832. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  833. {
  834. boost::unique_lock<boost::recursive_mutex> un(*pim);
  835. GH.pushInt(new CHeroWindow(hero));
  836. }
  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. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1826. garrisonChanged(location.army);
  1827. }
  1828. void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
  1829. {
  1830. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1831. garrisonChanged(location.army);
  1832. }
  1833. void CPlayerInterface::stacksErased(const StackLocation &location)
  1834. {
  1835. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1836. garrisonChanged(location.army);
  1837. }
  1838. void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  1839. {
  1840. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1841. garrisonChanged(loc1.army);
  1842. if(loc2.army != loc1.army)
  1843. garrisonChanged(loc2.army);
  1844. }
  1845. void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  1846. {
  1847. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1848. garrisonChanged(location.army);
  1849. }
  1850. void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  1851. {
  1852. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1853. garrisonChanged(src.army);
  1854. if(dst.army != src.army)
  1855. garrisonChanged(dst.army);
  1856. }
  1857. void CPlayerInterface::artifactPut(const ArtifactLocation &al)
  1858. {
  1859. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1860. }
  1861. void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
  1862. {
  1863. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1864. }
  1865. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  1866. {
  1867. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1868. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1869. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1870. BOOST_FOREACH(CArtifactsOfHero *aoh, (dynamic_cast<CWindowWithArtifacts*>(isa))->artSets)
  1871. aoh->artifactMoved(src, dst);
  1872. }
  1873. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  1874. {
  1875. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  1876. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  1877. }