CPlayerInterface.cpp 74 KB

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