CPlayerInterface.cpp 76 KB

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