CPlayerInterface.cpp 79 KB

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