CPlayerInterface.cpp 77 KB

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