CPlayerInterface.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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. if (!i->isSecondary()) //display projectile only for primary target
  734. {
  735. battleInt->stackAttacking(attacker, cb->battleGetPos(i->stackAttacked), attacked, true);
  736. }
  737. }
  738. }
  739. else
  740. {//TODO: support multiple attacked creatures
  741. int shift = 0;
  742. if(ba->counter() && THex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  743. {
  744. int distp = THex::getDistance(curAction->destinationTile + 1, attacker->position);
  745. int distm = THex::getDistance(curAction->destinationTile - 1, attacker->position);
  746. if( distp < distm )
  747. shift = 1;
  748. else
  749. shift = -1;
  750. }
  751. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  752. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  753. }
  754. }
  755. void CPlayerInterface::showComp(SComponent comp)
  756. {
  757. boost::unique_lock<boost::recursive_mutex> un(*pim);
  758. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  759. adventureInt->infoBar.showComp(&comp,4000);
  760. }
  761. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  762. {
  763. std::vector<SComponent*> intComps;
  764. for(int i=0;i<components.size();i++)
  765. intComps.push_back(new SComponent(*components[i]));
  766. showInfoDialog(text,intComps,soundID);
  767. }
  768. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID, bool delComps)
  769. {
  770. waitWhileDialog();
  771. boost::unique_lock<boost::recursive_mutex> un(*pim);
  772. stopMovement();
  773. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  774. temp->setDelComps(delComps);
  775. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  776. {
  777. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  778. showingDialog->set(true);
  779. GH.pushInt(temp);
  780. }
  781. else
  782. {
  783. dialogs.push_back(temp);
  784. }
  785. }
  786. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  787. {
  788. boost::unique_lock<boost::recursive_mutex> un(*pim);
  789. stopMovement();
  790. LOCPLINT->showingDialog->setn(true);
  791. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  792. }
  793. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  794. {
  795. waitWhileDialog();
  796. boost::unique_lock<boost::recursive_mutex> un(*pim);
  797. stopMovement();
  798. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  799. if(!selection && cancel) //simple yes/no dialog
  800. {
  801. std::vector<SComponent*> intComps;
  802. for(int i=0;i<components.size();i++)
  803. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  804. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  805. }
  806. else if(selection)
  807. {
  808. std::vector<CSelectableComponent*> intComps;
  809. for(int i=0;i<components.size();i++)
  810. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  811. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  812. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  813. if(cancel)
  814. {
  815. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  816. }
  817. int charperline = 35;
  818. if (pom.size() > 1)
  819. charperline = 50;
  820. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  821. GH.pushInt(temp);
  822. intComps[0]->clickLeft(true, false);
  823. }
  824. }
  825. void CPlayerInterface::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
  826. {
  827. boost::unique_lock<boost::recursive_mutex> un(*pim);
  828. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  829. adventureInt->minimap.showTile(*i);
  830. if(pos.size())
  831. GH.totalRedraw();
  832. }
  833. void CPlayerInterface::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
  834. {
  835. boost::unique_lock<boost::recursive_mutex> un(*pim);
  836. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  837. adventureInt->minimap.hideTile(*i);
  838. if(pos.size())
  839. GH.totalRedraw();
  840. }
  841. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  842. {
  843. boost::unique_lock<boost::recursive_mutex> un(*pim);
  844. GH.pushInt(new CHeroWindow(hero));
  845. }
  846. /*
  847. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  848. {
  849. boost::unique_lock<boost::recursive_mutex> un(*pim);
  850. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  851. {
  852. adventureInt->heroWindow->deactivate();
  853. adventureInt->heroWindow->setHero(hero);
  854. adventureInt->heroWindow->activate();
  855. }
  856. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  857. {
  858. cew->deactivate();
  859. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  860. {
  861. if(cew->heroInst[g]->id == hero->id)
  862. {
  863. cew->heroInst[g] = hero;
  864. cew->artifs[g]->updateState = true;
  865. cew->artifs[g]->setHero(hero);
  866. cew->artifs[g]->updateState = false;
  867. }
  868. }
  869. cew->prepareBackground();
  870. cew->activate();
  871. }
  872. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  873. {
  874. if(caw->arts)
  875. {
  876. caw->deactivate();
  877. caw->arts->updateState = true;
  878. caw->arts->setHero(hero);
  879. caw->arts->updateState = false;
  880. caw->activate();
  881. }
  882. }
  883. updateInfo(hero);
  884. }*/
  885. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  886. {
  887. boost::unique_lock<boost::recursive_mutex> un(*pim);
  888. if(castleInt && town->ID == TOWNI_TYPE)
  889. {
  890. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  891. if(fs)
  892. fs->creaturesChanged();
  893. }
  894. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  895. {
  896. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  897. if(crw)
  898. crw->initCres();
  899. }
  900. }
  901. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  902. {
  903. if(bonus.type == Bonus::NONE) return;
  904. boost::unique_lock<boost::recursive_mutex> un(*pim);
  905. updateInfo(hero);
  906. if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
  907. {
  908. //recalculate paths because hero has lost bonus influencing pathfinding
  909. cb->recalculatePaths();
  910. eraseCurrentPathOf(hero, false);
  911. }
  912. }
  913. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  914. {
  915. h & playerID;
  916. h & sysOpts;
  917. h & spellbookSettings;
  918. }
  919. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  920. {
  921. serializeTempl(h,version);
  922. }
  923. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  924. {
  925. serializeTempl(h,version);
  926. sysOpts.apply();
  927. firstCall = -1;
  928. }
  929. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  930. {
  931. if(!LOCPLINT->makingTurn)
  932. return false;
  933. if (!h)
  934. return false; //can't find hero
  935. //evil...
  936. eventsM.unlock();
  937. pim->unlock();
  938. cb->getGsMutex().unlock_shared();
  939. bool result = false;
  940. {
  941. path.convert(0);
  942. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  943. stillMoveHero.data = CONTINUE_MOVE;
  944. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  945. enum TerrainTile::EterrainType newTerrain;
  946. int sh = -1;
  947. const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
  948. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  949. {
  950. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  951. if(path.nodes[i-1].turns)
  952. {
  953. stillMoveHero.data = STOP_MOVE;
  954. break;
  955. }
  956. // Start a new sound for the hero movement or let the existing one carry on.
  957. #if 0
  958. // TODO
  959. if (hero is flying && sh == -1)
  960. sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
  961. #endif
  962. {
  963. newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  964. if (newTerrain != currentTerrain) {
  965. CCS->soundh->stopSound(sh);
  966. sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
  967. currentTerrain = newTerrain;
  968. }
  969. }
  970. stillMoveHero.data = WAITING_MOVE;
  971. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  972. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  973. cb->moveHero(h,endpos);
  974. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  975. stillMoveHero.cond.wait(un);
  976. if (guarded) // Abort movement if a guard was fought.
  977. break;
  978. }
  979. CCS->soundh->stopSound(sh);
  980. }
  981. cb->getGsMutex().lock_shared();
  982. pim->lock();
  983. eventsM.lock();
  984. cb->recalculatePaths();
  985. return result;
  986. }
  987. bool CPlayerInterface::shiftPressed() const
  988. {
  989. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  990. }
  991. bool CPlayerInterface::altPressed() const
  992. {
  993. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  994. }
  995. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  996. {
  997. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  998. return;
  999. {
  1000. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1001. while(showingDialog->data)
  1002. showingDialog->cond.wait(un);
  1003. }
  1004. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1005. while(dialogs.size())
  1006. {
  1007. pim->unlock();
  1008. SDL_Delay(20);
  1009. pim->lock();
  1010. }
  1011. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  1012. cgw->quit->callback += onEnd;
  1013. GH.pushInt(cgw);
  1014. }
  1015. /**
  1016. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  1017. * into a combinational one on an artifact screen. Does not require the combination of
  1018. * artifacts to be legal.
  1019. * @param artifactID ID of a constituent artifact.
  1020. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1021. * is false.
  1022. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1023. */
  1024. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1025. {
  1026. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1027. std::string text = artifact.Description();
  1028. text += "\n\n";
  1029. std::vector<SComponent*> scs;
  1030. if (assemble) {
  1031. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1032. // You possess all of the components to...
  1033. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1034. // Picture of assembled artifact at bottom.
  1035. SComponent* sc = new SComponent;
  1036. sc->type = SComponent::artifact;
  1037. sc->subtype = assembledArtifact.id;
  1038. sc->description = assembledArtifact.Description();
  1039. sc->subtitle = assembledArtifact.Name();
  1040. scs.push_back(sc);
  1041. } else {
  1042. // Do you wish to disassemble this artifact?
  1043. text += CGI->generaltexth->allTexts[733];
  1044. }
  1045. showYesNoDialog(text, scs, onYes, onNo, true);
  1046. }
  1047. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1048. {
  1049. if(stillMoveHero.get() == DURING_MOVE)
  1050. stillMoveHero.setn(CONTINUE_MOVE);
  1051. }
  1052. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1053. {
  1054. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1055. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1056. }
  1057. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1058. {
  1059. //redraw minimap if owner changed
  1060. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1061. if(sop->what == ObjProperty::OWNER)
  1062. {
  1063. const CGObjectInstance * obj = cb->getObj(sop->id);
  1064. std::set<int3> pos = obj->getBlockedPos();
  1065. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1066. {
  1067. if(cb->isVisible(*it))
  1068. adventureInt->minimap.showTile(*it);
  1069. }
  1070. if(obj->ID == TOWNI_TYPE)
  1071. {
  1072. if(obj->tempOwner == playerID)
  1073. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1074. else
  1075. towns -= obj;
  1076. }
  1077. assert(cb->getTownsInfo().size() == towns.size());
  1078. }
  1079. }
  1080. void CPlayerInterface::recreateHeroTownList()
  1081. {
  1082. wanderingHeroes.clear();
  1083. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1084. for(size_t i = 0; i < heroes.size(); i++)
  1085. if(!heroes[i]->inTownGarrison)
  1086. wanderingHeroes.push_back(heroes[i]);
  1087. towns.clear();
  1088. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1089. for(size_t i = 0; i < townInfo.size(); i++)
  1090. towns.push_back(townInfo[i]);
  1091. }
  1092. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1093. {
  1094. if(pos < 0 || pos >= wanderingHeroes.size())
  1095. return NULL;
  1096. return wanderingHeroes[pos];
  1097. }
  1098. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1099. {
  1100. waitWhileDialog();
  1101. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1102. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1103. GH.pushInt(cr);
  1104. }
  1105. void CPlayerInterface::waitWhileDialog()
  1106. {
  1107. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1108. while(showingDialog->data)
  1109. showingDialog->cond.wait(un);
  1110. }
  1111. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1112. {
  1113. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1114. int state = obj->state();
  1115. std::vector<si32> cost;
  1116. obj->getBoatCost(cost);
  1117. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1118. GH.pushInt(csw);
  1119. }
  1120. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1121. {
  1122. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1123. CGI->mh->printObject(obj);
  1124. //we might have built a boat in shipyard in opened town screen
  1125. if(obj->ID == 8
  1126. && LOCPLINT->castleInt
  1127. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1128. {
  1129. CCS->soundh->playSound(soundBase::newBuilding);
  1130. LOCPLINT->castleInt->addBuilding(20);
  1131. }
  1132. }
  1133. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1134. {
  1135. waitWhileDialog();
  1136. adventureInt->centerOn (pos);
  1137. if(focusTime)
  1138. {
  1139. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1140. if(activeAdv)
  1141. adventureInt->deactivate();
  1142. SDL_Delay(focusTime);
  1143. if(activeAdv)
  1144. adventureInt->activate();
  1145. }
  1146. }
  1147. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1148. {
  1149. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1150. {
  1151. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1152. heroKilled(h);
  1153. }
  1154. }
  1155. bool CPlayerInterface::ctrlPressed() const
  1156. {
  1157. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1158. }
  1159. void CPlayerInterface::update()
  1160. {
  1161. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1162. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1163. if(terminate_cond.get())
  1164. return;
  1165. //make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
  1166. boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
  1167. //if there are any waiting dialogs, show them
  1168. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1169. {
  1170. showingDialog->set(true);
  1171. GH.pushInt(dialogs.front());
  1172. dialogs.pop_front();
  1173. }
  1174. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1175. if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
  1176. {
  1177. pim->unlock();
  1178. return;
  1179. }
  1180. // Handles mouse and key input
  1181. GH.updateTime();
  1182. GH.handleEvents();
  1183. if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1184. GH.totalRedraw();
  1185. else
  1186. GH.simpleRedraw();
  1187. if (conf.cc.showFPS)
  1188. GH.drawFPSCounter();
  1189. // draw the mouse cursor and update the screen
  1190. CCS->curh->draw1();
  1191. CSDL_Ext::update(screen);
  1192. CCS->curh->draw2();
  1193. pim->unlock();
  1194. }
  1195. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1196. {
  1197. using namespace boost::filesystem;
  1198. using namespace boost::algorithm;
  1199. std::map<std::time_t, int> dates; //save number => datestamp
  1200. directory_iterator enddir;
  1201. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1202. {
  1203. if(is_regular(dir->status()))
  1204. {
  1205. std::string name = dir->path().leaf();
  1206. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1207. {
  1208. char nr = name[namePrefix.size()];
  1209. if(std::isdigit(nr))
  1210. {
  1211. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1212. }
  1213. }
  1214. }
  1215. }
  1216. if(dates.size())
  1217. return (--dates.end())->second; //return latest file number
  1218. return 0;
  1219. }
  1220. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1221. {
  1222. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1223. {
  1224. //ho->moveDir = 1;
  1225. ho->isStanding = false;
  1226. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1227. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1228. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1229. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1230. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1231. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1232. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1233. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1234. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1235. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1236. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1237. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1238. 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);
  1239. 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);
  1240. 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);
  1241. 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);
  1242. 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);
  1243. 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);
  1244. }
  1245. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1246. {
  1247. //ho->moveDir = 2;
  1248. ho->isStanding = false;
  1249. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1250. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1251. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1252. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1253. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1254. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1255. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1256. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1257. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1258. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1259. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1260. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1261. }
  1262. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1263. {
  1264. //ho->moveDir = 3;
  1265. ho->isStanding = false;
  1266. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1267. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1268. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1269. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1270. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1271. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1272. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1273. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1274. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1275. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1276. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1277. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1278. 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);
  1279. 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);
  1280. 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);
  1281. 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);
  1282. 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);
  1283. 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);
  1284. }
  1285. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1286. {
  1287. //ho->moveDir = 4;
  1288. ho->isStanding = false;
  1289. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1290. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1291. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1292. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1293. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1294. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1295. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1296. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1297. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1298. 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);
  1299. }
  1300. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1301. {
  1302. //ho->moveDir = 5;
  1303. ho->isStanding = false;
  1304. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1305. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1306. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1307. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1308. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1309. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1310. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1311. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1312. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1313. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1314. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1315. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1316. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1317. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1318. 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);
  1319. 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);
  1320. 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);
  1321. 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);
  1322. }
  1323. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1324. {
  1325. //ho->moveDir = 6;
  1326. ho->isStanding = false;
  1327. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1328. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1329. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1330. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1331. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1332. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1333. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1334. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1335. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1336. 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);
  1337. 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);
  1338. 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);
  1339. }
  1340. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1341. {
  1342. //ho->moveDir = 7;
  1343. ho->isStanding = false;
  1344. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1345. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1346. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1347. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1348. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1349. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1350. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1351. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1352. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1353. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1354. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1355. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1356. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1357. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1358. 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);
  1359. 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);
  1360. 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);
  1361. 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);
  1362. }
  1363. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1364. {
  1365. //ho->moveDir = 8;
  1366. ho->isStanding = false;
  1367. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1368. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1369. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1370. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1371. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1372. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1373. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1374. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1375. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1376. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1377. }
  1378. }
  1379. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1380. {
  1381. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1382. {
  1383. //setting advmap shift
  1384. adventureInt->terrain.moveX = i-32;
  1385. adventureInt->terrain.moveY = i-32;
  1386. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1387. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1388. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1389. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1390. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1391. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1392. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1393. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1394. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1395. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1396. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1397. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1398. }
  1399. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1400. {
  1401. //setting advmap shift
  1402. adventureInt->terrain.moveY = i-32;
  1403. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1404. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1405. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1406. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1407. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1408. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1409. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1410. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1411. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1412. }
  1413. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1414. {
  1415. //setting advmap shift
  1416. adventureInt->terrain.moveX = -i+32;
  1417. adventureInt->terrain.moveY = i-32;
  1418. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1419. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1420. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1421. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1422. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1423. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1424. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1425. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1426. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1427. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1428. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1429. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1430. }
  1431. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1432. {
  1433. //setting advmap shift
  1434. adventureInt->terrain.moveX = -i+32;
  1435. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1436. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1437. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1438. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1439. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1440. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1441. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1442. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1443. }
  1444. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1445. {
  1446. //setting advmap shift
  1447. adventureInt->terrain.moveX = -i+32;
  1448. adventureInt->terrain.moveY = -i+32;
  1449. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1450. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1451. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1452. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1453. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1454. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1455. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1456. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1457. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1458. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1459. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1460. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1461. }
  1462. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1463. {
  1464. //setting advmap shift
  1465. adventureInt->terrain.moveY = -i+32;
  1466. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1467. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1468. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1469. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1470. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1471. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1472. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1473. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1474. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1475. }
  1476. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1477. {
  1478. //setting advmap shift
  1479. adventureInt->terrain.moveX = i-32;
  1480. adventureInt->terrain.moveY = -i+32;
  1481. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1482. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1483. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1484. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1485. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1486. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1487. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1488. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1489. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1490. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1491. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1492. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1493. }
  1494. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1495. {
  1496. //setting advmap shift
  1497. adventureInt->terrain.moveX = i-32;
  1498. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1499. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1500. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1501. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1502. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1503. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1504. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1505. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1506. }
  1507. }
  1508. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1509. {
  1510. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1511. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1512. {
  1513. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1514. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1515. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1516. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1517. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1518. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1519. }
  1520. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1521. {
  1522. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1523. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1524. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1525. }
  1526. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1527. {
  1528. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1529. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1530. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1531. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1532. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1533. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1534. }
  1535. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1536. {
  1537. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1538. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1539. }
  1540. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1541. {
  1542. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1543. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1544. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1545. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1546. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1547. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1548. }
  1549. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1550. {
  1551. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1552. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1553. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1554. }
  1555. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1556. {
  1557. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1558. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1559. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1560. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1561. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1562. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1563. }
  1564. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1565. {
  1566. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1567. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1568. }
  1569. //restoring good rects
  1570. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1571. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1572. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1573. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1574. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1575. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1576. //restoring good order of objects
  1577. 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);
  1578. 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);
  1579. 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);
  1580. 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);
  1581. 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);
  1582. 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);
  1583. }
  1584. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1585. {
  1586. if(LOCPLINT != this)
  1587. return;
  1588. if(player == playerID)
  1589. {
  1590. if(!victory)
  1591. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1592. // else
  1593. // showInfoDialog("Placeholder message: you won!");
  1594. makingTurn = true;
  1595. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1596. makingTurn = false;
  1597. howManyPeople--;
  1598. if(!howManyPeople) //all human players eliminated
  1599. {
  1600. if(cb->getStartInfo()->mode != StartInfo::CAMPAIGN)
  1601. requestReturningToMainMenu();
  1602. else
  1603. requestStoppingClient();
  1604. }
  1605. cb->unregisterMyInterface(); //we already won/lost, nothing else matters
  1606. }
  1607. else
  1608. {
  1609. if(!victory && cb->getPlayerStatus(playerID) == PlayerState::INGAME) //enemy has lost
  1610. {
  1611. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1612. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1613. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1614. }
  1615. }
  1616. }
  1617. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1618. {
  1619. }
  1620. void CPlayerInterface::showPuzzleMap()
  1621. {
  1622. waitWhileDialog();
  1623. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1624. //TODO: interface should not know the real position of Grail...
  1625. float ratio = 0;
  1626. int3 grailPos = cb->getGrailPos(ratio);
  1627. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1628. }
  1629. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1630. {
  1631. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1632. {
  1633. cb->recalculatePaths();
  1634. eraseCurrentPathOf(caster, false);
  1635. }
  1636. }
  1637. void SystemOptions::setMusicVolume( int newVolume )
  1638. {
  1639. musicVolume = newVolume;
  1640. CCS->musich->setVolume(newVolume);
  1641. settingsChanged();
  1642. }
  1643. void SystemOptions::setSoundVolume( int newVolume )
  1644. {
  1645. soundVolume = newVolume;
  1646. CCS->soundh->setVolume(newVolume);
  1647. settingsChanged();
  1648. }
  1649. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1650. {
  1651. heroMoveSpeed = newSpeed;
  1652. settingsChanged();
  1653. }
  1654. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1655. {
  1656. mapScrollingSpeed = newSpeed;
  1657. settingsChanged();
  1658. }
  1659. void SystemOptions::settingsChanged()
  1660. {
  1661. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1662. if(settings.sfile)
  1663. settings << *this;
  1664. else
  1665. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1666. }
  1667. void SystemOptions::apply()
  1668. {
  1669. if(CCS->musich->getVolume() != musicVolume)
  1670. CCS->musich->setVolume(musicVolume);
  1671. if(CCS->soundh->getVolume() != soundVolume)
  1672. CCS->soundh->setVolume(soundVolume);
  1673. settingsChanged();
  1674. }
  1675. SystemOptions::SystemOptions()
  1676. {
  1677. heroMoveSpeed = 2;
  1678. mapScrollingSpeed = 2;
  1679. musicVolume = 88;
  1680. soundVolume = 88;
  1681. printCellBorders = true;
  1682. printStackRange = true;
  1683. animSpeed = 2;
  1684. printMouseShadow = true;
  1685. showQueue = true;
  1686. playerName = "Player";
  1687. }
  1688. void SystemOptions::setPlayerName(const std::string &newPlayerName)
  1689. {
  1690. playerName = newPlayerName;
  1691. settingsChanged();
  1692. }
  1693. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1694. {
  1695. if(checkForExistanceOfPath)
  1696. {
  1697. assert(vstd::contains(paths, ho));
  1698. }
  1699. else if (!vstd::contains(paths, ho))
  1700. {
  1701. return;
  1702. }
  1703. assert(ho == adventureInt->selection);
  1704. paths.erase(ho);
  1705. adventureInt->terrain.currentPath = NULL;
  1706. }
  1707. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1708. {
  1709. if(vstd::contains(paths,h)) //hero has assigned path
  1710. {
  1711. CGPath &path = paths[h];
  1712. if(!path.nodes.size())
  1713. {
  1714. tlog3 << "Warning: empty path found...\n";
  1715. paths.erase(h);
  1716. }
  1717. else
  1718. {
  1719. assert(h->getPosition(false) == path.startPos());
  1720. //update the hero path in case of something has changed on map
  1721. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1722. return &path;
  1723. else
  1724. paths.erase(h);
  1725. }
  1726. }
  1727. return NULL;
  1728. }
  1729. void CPlayerInterface::acceptTurn()
  1730. {
  1731. waitWhileDialog();
  1732. if(howManyPeople > 1)
  1733. adventureInt->startTurn();
  1734. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1735. /* TODO: This isn't quite right. First day in game should play
  1736. * NEWDAY. And we don't play NEWMONTH. */
  1737. int day = cb->getDate(1);
  1738. if (day != 1)
  1739. CCS->soundh->playSound(soundBase::newDay);
  1740. else
  1741. CCS->soundh->playSound(soundBase::newWeek);
  1742. adventureInt->infoBar.newDay(day);
  1743. //select first hero if available.
  1744. //TODO: check if hero is slept
  1745. if(wanderingHeroes.size())
  1746. adventureInt->select(wanderingHeroes.front());
  1747. else
  1748. adventureInt->select(towns.front());
  1749. adventureInt->showAll(screen);
  1750. }
  1751. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1752. {
  1753. std::string hlp;
  1754. if(h->movement < h->maxMovePoints(true))
  1755. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1756. else if(cb->getTile(h->getPosition(false))->tertype == TerrainTile::water)
  1757. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1758. else
  1759. {
  1760. const TerrainTile *t = cb->getTile(h->getPosition());
  1761. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1762. if(hlp.length() || t->blockingObjects.size() > 1)
  1763. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1764. else
  1765. cb->dig(h);
  1766. }
  1767. }
  1768. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1769. {
  1770. adventureInt->infoBar.updateSelection(specific);
  1771. // if (adventureInt->selection == specific)
  1772. // adventureInt->infoBar.showAll(screen);
  1773. }
  1774. void CPlayerInterface::battleNewRoundFirst( int round )
  1775. {
  1776. if(LOCPLINT != this)
  1777. { //another local interface should do this
  1778. return;
  1779. }
  1780. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1781. battleInt->newRoundFirst(round);
  1782. }
  1783. void CPlayerInterface::stopMovement()
  1784. {
  1785. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1786. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1787. }
  1788. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1789. {
  1790. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1791. if(market->o->ID == 2) //Altar
  1792. {
  1793. //EMarketMode mode = market->availableModes().front();
  1794. if(market->allowsTrade(ARTIFACT_EXP) && visitor->getAlignment() != EVIL)
  1795. GH.pushInt(new CAltarWindow(market, visitor, ARTIFACT_EXP));
  1796. else if(market->allowsTrade(CREATURE_EXP) && visitor->getAlignment() != GOOD)
  1797. GH.pushInt(new CAltarWindow(market, visitor, CREATURE_EXP));
  1798. }
  1799. else
  1800. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1801. }
  1802. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1803. {
  1804. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1805. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1806. GH.pushInt(cuw);
  1807. }
  1808. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  1809. {
  1810. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1811. CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
  1812. GH.pushInt(chfw);
  1813. }
  1814. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1815. {
  1816. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1817. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1818. cmw->artifactsChanged(false);
  1819. }
  1820. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1821. {
  1822. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1823. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1824. GH.pushInt(tv);
  1825. }
  1826. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1827. {
  1828. if(obj->state())
  1829. {
  1830. MetaString txt;
  1831. obj->getProblemText(txt);
  1832. showInfoDialog(txt.toString());
  1833. }
  1834. else
  1835. showShipyardDialog(obj);
  1836. }
  1837. void CPlayerInterface::requestReturningToMainMenu()
  1838. {
  1839. sendCustomEvent(2);
  1840. }
  1841. void CPlayerInterface::requestStoppingClient()
  1842. {
  1843. sendCustomEvent(3);
  1844. }
  1845. void CPlayerInterface::sendCustomEvent( int code )
  1846. {
  1847. SDL_Event event;
  1848. event.type = SDL_USEREVENT;
  1849. event.user.code = code;
  1850. SDL_PushEvent(&event);
  1851. }
  1852. void CPlayerInterface::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
  1853. {
  1854. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1855. garrisonChanged(location.army);
  1856. }
  1857. void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
  1858. {
  1859. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1860. garrisonChanged(location.army);
  1861. }
  1862. void CPlayerInterface::stacksErased(const StackLocation &location)
  1863. {
  1864. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1865. garrisonChanged(location.army);
  1866. }
  1867. #define UPDATE_IF(LOC) static_cast<const CArmedInstance*>(adventureInt->infoBar.curSel) == LOC.army.get()
  1868. void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  1869. {
  1870. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1871. garrisonChanged(loc1.army, UPDATE_IF(loc1));
  1872. if(loc2.army != loc1.army)
  1873. garrisonChanged(loc2.army, UPDATE_IF(loc2));
  1874. }
  1875. void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  1876. {
  1877. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1878. garrisonChanged(location.army);
  1879. }
  1880. void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  1881. {
  1882. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1883. //bool updateInfobox = true;
  1884. garrisonChanged(src.army, UPDATE_IF(src));
  1885. if(dst.army != src.army)
  1886. garrisonChanged(dst.army, UPDATE_IF(dst));
  1887. }
  1888. #undef UPDATE_IF
  1889. void CPlayerInterface::artifactPut(const ArtifactLocation &al)
  1890. {
  1891. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1892. }
  1893. void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
  1894. {
  1895. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1896. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1897. {
  1898. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1899. {
  1900. (dynamic_cast<CArtifactHolder*>(isa))->artifactRemoved(al);
  1901. }
  1902. }
  1903. }
  1904. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  1905. {
  1906. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1907. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1908. {
  1909. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1910. {
  1911. (dynamic_cast<CArtifactHolder*>(isa))->artifactMoved(src, dst);
  1912. }
  1913. }
  1914. }
  1915. void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
  1916. {
  1917. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1918. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1919. {
  1920. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1921. {
  1922. (dynamic_cast<CArtifactHolder*>(isa))->artifactAssembled(al);
  1923. }
  1924. }
  1925. }
  1926. void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
  1927. {
  1928. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1929. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1930. {
  1931. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1932. {
  1933. (dynamic_cast<CArtifactHolder*>(isa))->artifactDisassembled(al);
  1934. }
  1935. }
  1936. }
  1937. boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
  1938. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  1939. {
  1940. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  1941. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  1942. }