CPlayerInterface.cpp 78 KB

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