CPlayerInterface.cpp 79 KB

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