CPlayerInterface.cpp 79 KB

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