CPlayerInterface.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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. boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
  60. CPlayerInterface * LOCPLINT;
  61. CBattleInterface * CPlayerInterface::battleInt;
  62. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  63. CondSh<EMoveState> stillMoveHero; //used during hero movement
  64. int CPlayerInterface::howManyPeople = 0;
  65. struct OCM_HLP_CGIN
  66. {
  67. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  68. {
  69. return (*a.first)<(*b.first);
  70. }
  71. } ocmptwo_cgin ;
  72. CPlayerInterface::CPlayerInterface(int Player)
  73. {
  74. observerInDuelMode = false;
  75. howManyPeople++;
  76. GH.defActionsDef = 0;
  77. LOCPLINT = this;
  78. curAction = NULL;
  79. playerID=Player;
  80. human=true;
  81. castleInt = NULL;
  82. battleInt = NULL;
  83. //pim = new boost::recursive_mutex;
  84. makingTurn = false;
  85. showingDialog = new CondSh<bool>(false);
  86. cingconsole = new CInGameConsole;
  87. terminate_cond.set(false);
  88. firstCall = 1; //if loading will be overwritten in serialize
  89. autosaveCount = 0;
  90. }
  91. CPlayerInterface::~CPlayerInterface()
  92. {
  93. howManyPeople--;
  94. //delete pim;
  95. //vstd::clear_pointer(pim);
  96. delete showingDialog;
  97. if(adventureInt)
  98. {
  99. if(adventureInt->active & CIntObject::KEYBOARD)
  100. adventureInt->deactivateKeys();
  101. delete adventureInt;
  102. adventureInt = NULL;
  103. }
  104. if(cingconsole->active) //TODO
  105. cingconsole->deactivate();
  106. delete cingconsole;
  107. LOCPLINT = NULL;
  108. }
  109. void CPlayerInterface::init(CCallback * CB)
  110. {
  111. cb = dynamic_cast<CCallback*>(CB);
  112. if(observerInDuelMode)
  113. {
  114. return;
  115. }
  116. if(!adventureInt)
  117. adventureInt = new CAdvMapInt();
  118. if(!towns.size() && !wanderingHeroes.size())
  119. {
  120. recreateHeroTownList();
  121. }
  122. }
  123. void CPlayerInterface::yourTurn()
  124. {
  125. {
  126. boost::unique_lock<boost::recursive_mutex> un(*pim);
  127. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  128. LOCPLINT = this;
  129. GH.curInt = this;
  130. adventureInt->selection = NULL;
  131. if(firstCall)
  132. {
  133. if(howManyPeople == 1)
  134. adventureInt->setPlayer(playerID);
  135. autosaveCount = getLastIndex("Autosave_");
  136. if(!GH.listInt.size())
  137. {
  138. GH.pushInt(adventureInt);
  139. adventureInt->activateKeys();
  140. }
  141. if(firstCall > 0) //new game, not loaded
  142. {
  143. int index = getLastIndex("Newgame_Autosave_");
  144. index %= SAVES_COUNT;
  145. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  146. }
  147. firstCall = 0;
  148. }
  149. else
  150. {
  151. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  152. autosaveCount %= 5;
  153. }
  154. if(adventureInt->player != playerID)
  155. adventureInt->setPlayer(playerID);
  156. if(howManyPeople > 1) //hot seat message
  157. {
  158. adventureInt->startHotSeatWait(playerID);
  159. makingTurn = true;
  160. std::string msg = CGI->generaltexth->allTexts[13];
  161. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  162. std::vector<CComponent*> cmp;
  163. cmp.push_back(new CComponent(CComponent::flag, playerID, 0));
  164. showInfoDialog(msg, cmp);
  165. }
  166. else
  167. {
  168. makingTurn = true;
  169. adventureInt->startTurn();
  170. }
  171. }
  172. acceptTurn();
  173. }
  174. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  175. {
  176. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  177. for(int h=0; h<hlp.objects.size(); ++h)
  178. if(hlp.objects[h].first->id==hid)
  179. {
  180. hlp.objects[h].second = r;
  181. return;
  182. }
  183. }
  184. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  185. {
  186. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  187. for(int h=0; h<hlp.objects.size(); ++h)
  188. if(hlp.objects[h].first->id==hid)
  189. {
  190. hlp.objects.erase(hlp.objects.begin()+h);
  191. return;
  192. }
  193. }
  194. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  195. {
  196. if(LOCPLINT != this)
  197. return;
  198. boost::unique_lock<boost::recursive_mutex> un(*pim);
  199. const CGHeroInstance * ho = cb->getHero(details.id); //object representing this hero
  200. int3 hp = details.start;
  201. if(!ho)
  202. {
  203. //AI hero left the visible area (we can't obtain info)
  204. //TODO very evil workaround -> retreive pointer to hero so we could animate it
  205. // TODO -> we should not need full CGHeroInstance structure to display animation or it should not be handled by playerint (but by the client itself)
  206. const TerrainTile2 &tile = CGI->mh->ttiles[hp.x-1][hp.y][hp.z];
  207. for(int i = 0; i < tile.objects.size(); i++)
  208. if(tile.objects[i].first->id == details.id)
  209. ho = dynamic_cast<const CGHeroInstance *>(tile.objects[i].first);
  210. if(!ho) //still nothing...
  211. return;
  212. }
  213. adventureInt->centerOn(ho); //actualizing screen pos
  214. adventureInt->minimap.draw(screen2);
  215. adventureInt->heroList.draw(screen2);
  216. bool directlyAttackingCreature =
  217. CGI->mh->map->isInTheMap(details.attackedFrom)
  218. && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
  219. && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
  220. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  221. {
  222. //We may need to change music - select new track, music handler will change it if needed
  223. CCS->musich->playMusic(CCS->musich->terrainMusics[LOCPLINT->cb->getTile(ho->visitablePos())->tertype]);
  224. if(details.result == TryMoveHero::TELEPORTATION)
  225. {
  226. if(adventureInt->terrain.currentPath->nodes.size() && adventureInt->terrain.currentPath->nodes.back().coord == CGHeroInstance::convertPosition(hp, false))
  227. removeLastNodeFromPath(ho);
  228. // else
  229. // eraseCurrentPathOf(ho);
  230. return; //teleport - no fancy moving animation
  231. //TODO: smooth disappear / appear effect
  232. }
  233. if (ho->pos != details.end //hero didn't change tile but visit succeeded
  234. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  235. {
  236. eraseCurrentPathOf(ho);
  237. }
  238. else if(adventureInt->terrain.currentPath && ho->pos == details.end) //&& hero is moving
  239. {
  240. if (adventureInt->terrain.currentPath->endPos() != details.end)
  241. removeLastNodeFromPath(ho);
  242. }
  243. }
  244. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  245. {
  246. ho->isStanding = true;
  247. stillMoveHero.setn(STOP_MOVE);
  248. GH.totalRedraw();
  249. return;
  250. }
  251. initMovement(details, ho, hp);
  252. //first initializing done
  253. GH.mainFPSmng->framerateDelay(); // after first move
  254. ui32 speed = settings["adventure"]["heroSpeed"].Float();
  255. //main moving
  256. for(int i=1; i<32; i+=2*speed)
  257. {
  258. movementPxStep(details, i, hp, ho);
  259. adventureInt->updateScreen = true;
  260. adventureInt->show(screen);
  261. CSDL_Ext::update(screen);
  262. GH.mainFPSmng->framerateDelay(); //for animation purposes
  263. } //for(int i=1; i<32; i+=4)
  264. //main moving done
  265. //finishing move
  266. finishMovement(details, hp, ho);
  267. ho->isStanding = true;
  268. //move finished
  269. adventureInt->minimap.draw(screen2);
  270. adventureInt->heroList.updateMove(ho);
  271. //check if user cancelled movement
  272. {
  273. boost::unique_lock<boost::mutex> un(eventsM);
  274. while(events.size())
  275. {
  276. SDL_Event *ev = events.front();
  277. events.pop();
  278. switch(ev->type)
  279. {
  280. case SDL_MOUSEBUTTONDOWN:
  281. stillMoveHero.setn(STOP_MOVE);
  282. break;
  283. case SDL_KEYDOWN:
  284. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  285. stillMoveHero.setn(STOP_MOVE);
  286. break;
  287. }
  288. delete ev;
  289. }
  290. }
  291. if(stillMoveHero.get() == WAITING_MOVE)
  292. stillMoveHero.setn(DURING_MOVE);
  293. // Hero attacked creature directly, set direction to face it.
  294. if (directlyAttackingCreature) {
  295. // Get direction to attacker.
  296. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  297. static const ui8 dirLookup[3][3] = {
  298. { 1, 2, 3 },
  299. { 8, 0, 4 },
  300. { 7, 6, 5 }
  301. };
  302. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  303. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  304. }
  305. }
  306. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  307. {
  308. boost::unique_lock<boost::recursive_mutex> un(*pim);
  309. wanderingHeroes -= hero;
  310. if(vstd::contains(paths, hero))
  311. paths.erase(hero);
  312. adventureInt->heroList.updateHList(hero);
  313. }
  314. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  315. {
  316. boost::unique_lock<boost::recursive_mutex> un(*pim);
  317. wanderingHeroes.push_back(hero);
  318. adventureInt->heroList.updateHList();
  319. }
  320. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  321. {
  322. if (castleInt)
  323. GH.popIntTotally(castleInt);
  324. castleInt = new CCastleInterface(town);
  325. GH.pushInt(castleInt);
  326. }
  327. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  328. {
  329. if(!specific)
  330. specific = adventureInt->selection;
  331. assert(specific);
  332. switch(specific->ID)
  333. {
  334. case GameConstants::HEROI_TYPE:
  335. {
  336. InfoAboutHero iah;
  337. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  338. assert(gotInfo);
  339. return graphics->drawHeroInfoWin(iah);
  340. }
  341. case GameConstants::TOWNI_TYPE:
  342. case 33: // Garrison
  343. case 219:
  344. {
  345. InfoAboutTown iah;
  346. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  347. assert(gotInfo);
  348. return graphics->drawTownInfoWin(iah);
  349. }
  350. default:
  351. return NULL;
  352. }
  353. }
  354. int3 CPlayerInterface::repairScreenPos(int3 pos)
  355. {
  356. if(pos.x<-CGI->mh->frameW)
  357. pos.x = -CGI->mh->frameW;
  358. if(pos.y<-CGI->mh->frameH)
  359. pos.y = -CGI->mh->frameH;
  360. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  361. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  362. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  363. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  364. return pos;
  365. }
  366. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  367. {
  368. boost::unique_lock<boost::recursive_mutex> un(*pim);
  369. if(which == 4)
  370. {
  371. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  372. ctw->setExpToLevel();
  373. }
  374. else if(which < GameConstants::PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  375. updateInfo(hero);
  376. }
  377. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  378. {
  379. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  380. if(cuw) //university window is open
  381. {
  382. GH.totalRedraw();
  383. }
  384. }
  385. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  386. {
  387. boost::unique_lock<boost::recursive_mutex> un(*pim);
  388. updateInfo(hero);
  389. }
  390. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  391. {
  392. boost::unique_lock<boost::recursive_mutex> un(*pim);
  393. if(makingTurn && hero->tempOwner == playerID)
  394. adventureInt->heroList.redraw();
  395. }
  396. void CPlayerInterface::receivedResource(int type, int val)
  397. {
  398. boost::unique_lock<boost::recursive_mutex> un(*pim);
  399. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  400. mw->resourceChanged(type, val);
  401. GH.totalRedraw();
  402. }
  403. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  404. {
  405. waitWhileDialog();
  406. CCS->soundh->playSound(soundBase::heroNewLevel);
  407. boost::unique_lock<boost::recursive_mutex> un(*pim);
  408. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  409. GH.pushInt(lw);
  410. }
  411. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  412. {
  413. boost::unique_lock<boost::recursive_mutex> un(*pim);
  414. updateInfo(town);
  415. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  416. {
  417. CGI->mh->hideObject(town->garrisonHero);
  418. wanderingHeroes -= town->garrisonHero;
  419. }
  420. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  421. {
  422. CGI->mh->printObject(town->visitingHero);
  423. wanderingHeroes.push_back(town->visitingHero);
  424. }
  425. adventureInt->updateNextHero(NULL);
  426. if(CCastleInterface *c = castleInt)
  427. {
  428. c->garr->highlighted = NULL;
  429. c->garr->setArmy(town->getUpperArmy(), 0);
  430. c->garr->setArmy(town->visitingHero, 1);
  431. c->garr->recreateSlots();
  432. c->heroes->update();
  433. }
  434. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  435. {
  436. CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
  437. if (ki)
  438. {
  439. ki->townChanged(town);
  440. ki->updateGarrisons();
  441. }
  442. }
  443. GH.totalRedraw();
  444. }
  445. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  446. {
  447. if(hero->tempOwner != playerID )
  448. return;
  449. waitWhileDialog();
  450. boost::unique_lock<boost::recursive_mutex> un(*pim);
  451. openTownWindow(town);
  452. }
  453. void CPlayerInterface::garrisonChanged( const CGObjectInstance * obj, bool updateInfobox /*= true*/ )
  454. {
  455. boost::unique_lock<boost::recursive_mutex> un(*pim);
  456. if(updateInfobox)
  457. updateInfo(obj);
  458. for(std::list<IShowActivatable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  459. {
  460. if((*i)->type & IShowActivatable::WITH_GARRISON)
  461. {
  462. CGarrisonHolder *cgh = dynamic_cast<CGarrisonHolder*>(*i);
  463. cgh->updateGarrisons();
  464. }
  465. else if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
  466. {
  467. if(obj == cmw->hero)
  468. cmw->garrisonChanged();
  469. }
  470. }
  471. GH.totalRedraw();
  472. }
  473. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  474. {
  475. boost::unique_lock<boost::recursive_mutex> un(*pim);
  476. switch (buildingID)
  477. {
  478. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  479. updateInfo(town);
  480. break;
  481. }
  482. if(!castleInt)
  483. return;
  484. if(castleInt->town!=town)
  485. return;
  486. switch(what)
  487. {
  488. case 1:
  489. CCS->soundh->playSound(soundBase::newBuilding);
  490. castleInt->addBuilding(buildingID);
  491. break;
  492. case 2:
  493. castleInt->removeBuilding(buildingID);
  494. break;
  495. }
  496. }
  497. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  498. {
  499. if(LOCPLINT != this)
  500. { //another local interface should do this
  501. return;
  502. }
  503. while(showingDialog->get())
  504. SDL_Delay(20);
  505. boost::unique_lock<boost::recursive_mutex> un(*pim);
  506. GH.pushInt(battleInt);
  507. }
  508. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  509. {
  510. if(LOCPLINT != this)
  511. { //another local interface should do this
  512. return;
  513. }
  514. for(int b=0; b<healedStacks.size(); ++b)
  515. {
  516. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  517. if(battleInt->creAnims[healed->ID]->getType() == CCreatureAnim::DEATH)
  518. {
  519. //stack has been resurrected
  520. battleInt->creAnims[healed->ID]->setType(CCreatureAnim::HOLDING);
  521. }
  522. }
  523. if (lifeDrain)
  524. {
  525. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  526. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  527. int textOff = 0;
  528. if (attacker)
  529. {
  530. battleInt->displayEffect(52, attacker->position); //TODO: transparency
  531. if (attacker->count > 1)
  532. {
  533. textOff += 1;
  534. }
  535. CCS->soundh->playSound(soundBase::DRAINLIF);
  536. }
  537. //print info about life drain
  538. char textBuf[1000];
  539. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  540. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  541. battleInt->console->addText(textBuf);
  542. }
  543. if (tentHeal)
  544. {
  545. std::string text = CGI->generaltexth->allTexts[414];
  546. boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(lifeDrainFrom, false)->getCreature()->nameSing);
  547. boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(healedStacks[0].first, false)->getCreature()->nameSing);
  548. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(healedStacks[0].second));
  549. battleInt->console->addText(text);
  550. }
  551. }
  552. void CPlayerInterface::battleNewStackAppeared(const CStack * stack)
  553. {
  554. if(LOCPLINT != this)
  555. { //another local interface should do this
  556. return;
  557. }
  558. //changing necessary things in battle interface
  559. battleInt->newStack(stack);
  560. //battleInt->addNewAnim(new CDummyAnim(battleInt, 2)); //wait a moment
  561. }
  562. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  563. {
  564. if(LOCPLINT != this)
  565. { //another local interface should do this
  566. return;
  567. }
  568. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  569. {
  570. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  571. {
  572. if(itBat->first == *it) //remove this obstacle
  573. {
  574. battleInt->idToObstacle.erase(itBat);
  575. break;
  576. }
  577. }
  578. }
  579. //update accessible hexes
  580. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  581. }
  582. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  583. {
  584. if(LOCPLINT != this)
  585. { //another local interface should do this
  586. return;
  587. }
  588. boost::unique_lock<boost::recursive_mutex> un(*pim);
  589. battleInt->stackIsCatapulting(ca);
  590. }
  591. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  592. {
  593. if(LOCPLINT != this)
  594. { //another local interface should do this
  595. return;
  596. }
  597. //boost::unique_lock<boost::recursive_mutex> un(*pim); //fixme: this one caused deadlock
  598. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  599. {
  600. battleInt->stackRemoved(*it);
  601. //battleInt->stackRemoved(LOCPLINT->cb->battleGetStackByID(*it));
  602. }
  603. }
  604. 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
  605. {
  606. if(LOCPLINT != this)
  607. { //another local interface should do this
  608. return;
  609. }
  610. boost::unique_lock<boost::recursive_mutex> un(*pim);
  611. battleInt->newRound(round);
  612. }
  613. void CPlayerInterface::actionStarted(const BattleAction* action)
  614. {
  615. if(LOCPLINT != this)
  616. { //another local interface should do this
  617. return;
  618. }
  619. boost::unique_lock<boost::recursive_mutex> un(*pim);
  620. curAction = new BattleAction(*action);
  621. battleInt->startAction(action);
  622. }
  623. void CPlayerInterface::actionFinished(const BattleAction* action)
  624. {
  625. if(LOCPLINT != this)
  626. { //another local interface should do this
  627. return;
  628. }
  629. boost::unique_lock<boost::recursive_mutex> un(*pim);
  630. delete curAction;
  631. curAction = NULL;
  632. battleInt->endAction(action);
  633. }
  634. BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
  635. {
  636. CBattleInterface *b = battleInt;
  637. {
  638. boost::unique_lock<boost::recursive_mutex> un(*pim);
  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. void CPlayerInterface::playerStartsTurn(ui8 player)
  2027. {
  2028. if(player != playerID && this == LOCPLINT)
  2029. {
  2030. adventureInt->minimap.redraw();
  2031. adventureInt->infoBar.enemyTurn(player, 0.5);
  2032. //TODO AI turn music
  2033. //TODO block GUI
  2034. }
  2035. }
  2036. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  2037. {
  2038. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  2039. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  2040. }