CPlayerInterface.cpp 78 KB

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