CPlayerInterface.cpp 75 KB

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