CPlayerInterface.cpp 78 KB

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