CPlayerInterface.cpp 75 KB

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