CPlayerInterface.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  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. //changing necessary things in battle interface
  567. battleInt->newStack(stack);
  568. //battleInt->addNewAnim(new CDummyAnim(battleInt, 2)); //wait a moment
  569. }
  570. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  571. {
  572. if(LOCPLINT != this)
  573. { //another local interface should do this
  574. return;
  575. }
  576. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  577. {
  578. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  579. {
  580. if(itBat->first == *it) //remove this obstacle
  581. {
  582. battleInt->idToObstacle.erase(itBat);
  583. break;
  584. }
  585. }
  586. }
  587. //update accessible hexes
  588. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  589. }
  590. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  591. {
  592. if(LOCPLINT != this)
  593. { //another local interface should do this
  594. return;
  595. }
  596. boost::unique_lock<boost::recursive_mutex> un(*pim);
  597. battleInt->stackIsCatapulting(ca);
  598. }
  599. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  600. {
  601. if(LOCPLINT != this)
  602. { //another local interface should do this
  603. return;
  604. }
  605. //boost::unique_lock<boost::recursive_mutex> un(*pim); //fixme: this one caused deadlock
  606. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  607. {
  608. battleInt->stackRemoved(*it);
  609. //battleInt->stackRemoved(LOCPLINT->cb->battleGetStackByID(*it));
  610. }
  611. }
  612. void CPlayerInterface::battleNewRound(int round) //called at the beginning of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  613. {
  614. if(LOCPLINT != this)
  615. { //another local interface should do this
  616. return;
  617. }
  618. boost::unique_lock<boost::recursive_mutex> un(*pim);
  619. battleInt->newRound(round);
  620. }
  621. void CPlayerInterface::actionStarted(const BattleAction* action)
  622. {
  623. if(LOCPLINT != this)
  624. { //another local interface should do this
  625. return;
  626. }
  627. boost::unique_lock<boost::recursive_mutex> un(*pim);
  628. curAction = new BattleAction(*action);
  629. battleInt->startAction(action);
  630. }
  631. void CPlayerInterface::actionFinished(const BattleAction* action)
  632. {
  633. if(LOCPLINT != this)
  634. { //another local interface should do this
  635. return;
  636. }
  637. boost::unique_lock<boost::recursive_mutex> un(*pim);
  638. delete curAction;
  639. curAction = NULL;
  640. battleInt->endAction(action);
  641. }
  642. BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
  643. {
  644. CBattleInterface *b = battleInt;
  645. {
  646. boost::unique_lock<boost::recursive_mutex> un(*pim);
  647. b->stackActivated(stack);
  648. //Regeneration & mana drain go there
  649. }
  650. //wait till BattleInterface sets its command
  651. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  652. while(!b->givenCommand->data)
  653. b->givenCommand->cond.wait(lock);
  654. //tidy up
  655. BattleAction ret = *(b->givenCommand->data);
  656. delete b->givenCommand->data;
  657. b->givenCommand->data = NULL;
  658. //return command
  659. return ret;
  660. }
  661. void CPlayerInterface::battleEnd(const BattleResult *br)
  662. {
  663. if(LOCPLINT != this)
  664. { //another local interface should do this
  665. return;
  666. }
  667. boost::unique_lock<boost::recursive_mutex> un(*pim);
  668. battleInt->battleFinished(*br);
  669. }
  670. void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  671. {
  672. if(LOCPLINT != this)
  673. { //another local interface should do this
  674. return;
  675. }
  676. boost::unique_lock<boost::recursive_mutex> un(*pim);
  677. battleInt->stackMoved(stack, dest, distance);
  678. }
  679. void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
  680. {
  681. if(LOCPLINT != this)
  682. { //another local interface should do this
  683. return;
  684. }
  685. boost::unique_lock<boost::recursive_mutex> un(*pim);
  686. battleInt->spellCast(sc);
  687. }
  688. void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
  689. {
  690. if(LOCPLINT != this)
  691. { //another local interface should do this
  692. return;
  693. }
  694. boost::unique_lock<boost::recursive_mutex> un(*pim);
  695. battleInt->battleStacksEffectsSet(sse);
  696. }
  697. void CPlayerInterface::battleTriggerEffect (const BattleTriggerEffect & bte)
  698. {
  699. battleInt->battleTriggerEffect(bte);
  700. }
  701. void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  702. {
  703. if(LOCPLINT != this)
  704. { //another local interface should do this
  705. return;
  706. }
  707. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  708. boost::unique_lock<boost::recursive_mutex> un(*pim);
  709. tlog5 << "done!\n";
  710. std::vector<StackAttackedInfo> arg;
  711. for(std::vector<BattleStackAttacked>::const_iterator i = bsa.begin(); i != bsa.end(); i++)
  712. {
  713. const CStack *defender = cb->battleGetStackByID(i->stackAttacked, false);
  714. const CStack *attacker = cb->battleGetStackByID(i->attackerID, false);
  715. if(i->isEffect() && i->effect != 12) //and not armageddon
  716. {
  717. if (defender && !i->isSecondary())
  718. battleInt->displayEffect(i->effect, defender->position);
  719. }
  720. StackAttackedInfo to_put = {defender, i->damageAmount, i->killedAmount, attacker, LOCPLINT->curAction->actionType==7, i->killed(), i->willRebirth()};
  721. arg.push_back(to_put);
  722. }
  723. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  724. {
  725. battleInt->displayEffect(bsa.begin()->effect, -1);
  726. }
  727. battleInt->stacksAreAttacked(arg);
  728. }
  729. void CPlayerInterface::battleAttack(const BattleAttack *ba)
  730. {
  731. if(LOCPLINT != this)
  732. { //another local interface should do this
  733. return;
  734. }
  735. tlog5 << "CPlayerInterface::battleAttack - locking...";
  736. boost::unique_lock<boost::recursive_mutex> un(*pim);
  737. tlog5 << "done!\n";
  738. assert(curAction);
  739. if(ba->lucky()) //lucky hit
  740. {
  741. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  742. std::string hlp = CGI->generaltexth->allTexts[45];
  743. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  744. battleInt->console->addText(hlp);
  745. battleInt->displayEffect(18, stack->position);
  746. }
  747. //TODO: bad luck?
  748. if (ba->deathBlow())
  749. {
  750. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  751. std::string hlp = CGI->generaltexth->allTexts[(stack->count != 1) ? 366 : 365];
  752. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  753. battleInt->console->addText(hlp);
  754. for (std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  755. {
  756. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  757. battleInt->displayEffect(73, attacked->position);
  758. }
  759. }
  760. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  761. if(ba->shot())
  762. {
  763. for(std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  764. {
  765. if (!i->isSecondary()) //display projectile only for primary target
  766. {
  767. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  768. battleInt->stackAttacking(attacker, cb->battleGetPos(i->stackAttacked), attacked, true);
  769. }
  770. }
  771. }
  772. else
  773. {
  774. int shift = 0;
  775. if(ba->counter() && BattleHex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  776. {
  777. int distp = BattleHex::getDistance(curAction->destinationTile + 1, attacker->position);
  778. int distm = BattleHex::getDistance(curAction->destinationTile - 1, attacker->position);
  779. if( distp < distm )
  780. shift = 1;
  781. else
  782. shift = -1;
  783. }
  784. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  785. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  786. }
  787. }
  788. void CPlayerInterface::yourTacticPhase(int distance)
  789. {
  790. while(battleInt && battleInt->tacticsMode)
  791. boost::this_thread::sleep(boost::posix_time::millisec(1));
  792. }
  793. void CPlayerInterface::showComp(CComponent comp)
  794. {
  795. waitWhileDialog();//Fix for mantis #98
  796. boost::unique_lock<boost::recursive_mutex> un(*pim);
  797. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  798. adventureInt->infoBar.showComp(&comp,4000);
  799. }
  800. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  801. {
  802. std::vector<CComponent*> intComps;
  803. for(int i=0;i<components.size();i++)
  804. intComps.push_back(new CComponent(*components[i]));
  805. showInfoDialog(text,intComps,soundID);
  806. }
  807. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
  808. {
  809. waitWhileDialog();
  810. boost::unique_lock<boost::recursive_mutex> un(*pim);
  811. stopMovement();
  812. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  813. temp->setDelComps(delComps);
  814. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  815. {
  816. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  817. showingDialog->set(true);
  818. GH.pushInt(temp);
  819. }
  820. else
  821. {
  822. dialogs.push_back(temp);
  823. }
  824. }
  825. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<CComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  826. {
  827. boost::unique_lock<boost::recursive_mutex> un(*pim);
  828. stopMovement();
  829. LOCPLINT->showingDialog->setn(true);
  830. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  831. }
  832. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  833. {
  834. waitWhileDialog();
  835. boost::unique_lock<boost::recursive_mutex> un(*pim);
  836. stopMovement();
  837. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  838. if(!selection && cancel) //simple yes/no dialog
  839. {
  840. std::vector<CComponent*> intComps;
  841. for(int i=0;i<components.size();i++)
  842. intComps.push_back(new CComponent(components[i])); //will be deleted by close in window
  843. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  844. }
  845. else if(selection)
  846. {
  847. std::vector<CSelectableComponent*> intComps;
  848. for(int i=0;i<components.size();i++)
  849. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  850. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  851. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  852. if(cancel)
  853. {
  854. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  855. }
  856. int charperline = 35;
  857. if (pom.size() > 1)
  858. charperline = 50;
  859. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  860. GH.pushInt(temp);
  861. intComps[0]->clickLeft(true, false);
  862. }
  863. }
  864. void CPlayerInterface::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
  865. {
  866. boost::unique_lock<boost::recursive_mutex> un(*pim);
  867. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  868. adventureInt->minimap.showTile(*i);
  869. if(pos.size())
  870. GH.totalRedraw();
  871. }
  872. void CPlayerInterface::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
  873. {
  874. boost::unique_lock<boost::recursive_mutex> un(*pim);
  875. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  876. adventureInt->minimap.hideTile(*i);
  877. if(pos.size())
  878. GH.totalRedraw();
  879. }
  880. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  881. {
  882. boost::unique_lock<boost::recursive_mutex> un(*pim);
  883. GH.pushInt(new CHeroWindow(hero));
  884. }
  885. /*
  886. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  887. {
  888. boost::unique_lock<boost::recursive_mutex> un(*pim);
  889. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  890. {
  891. adventureInt->heroWindow->deactivate();
  892. adventureInt->heroWindow->setHero(hero);
  893. adventureInt->heroWindow->activate();
  894. }
  895. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  896. {
  897. cew->deactivate();
  898. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  899. {
  900. if(cew->heroInst[g]->id == hero->id)
  901. {
  902. cew->heroInst[g] = hero;
  903. cew->artifs[g]->updateState = true;
  904. cew->artifs[g]->setHero(hero);
  905. cew->artifs[g]->updateState = false;
  906. }
  907. }
  908. cew->prepareBackground();
  909. cew->activate();
  910. }
  911. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  912. {
  913. if(caw->arts)
  914. {
  915. caw->deactivate();
  916. caw->arts->updateState = true;
  917. caw->arts->setHero(hero);
  918. caw->arts->updateState = false;
  919. caw->activate();
  920. }
  921. }
  922. updateInfo(hero);
  923. }*/
  924. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  925. {
  926. boost::unique_lock<boost::recursive_mutex> un(*pim);
  927. if(const CGTownInstance * townObj = dynamic_cast<const CGTownInstance*>(town))
  928. {
  929. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  930. if(fs)
  931. fs->creaturesChanged();
  932. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  933. {
  934. CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
  935. if (ki && townObj)
  936. ki->townChanged(townObj);
  937. }
  938. }
  939. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  940. {
  941. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  942. if(crw)
  943. crw->initCres();
  944. }
  945. }
  946. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  947. {
  948. if(bonus.type == Bonus::NONE) return;
  949. boost::unique_lock<boost::recursive_mutex> un(*pim);
  950. updateInfo(hero);
  951. if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
  952. {
  953. //recalculate paths because hero has lost bonus influencing pathfinding
  954. eraseCurrentPathOf(hero, false);
  955. }
  956. }
  957. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  958. {
  959. h & playerID;
  960. h & spellbookSettings;
  961. //sleeping heroes
  962. ui8 sleepingSize;
  963. if (h.saving)
  964. sleepingSize = sleepingHeroes.size();
  965. h & sleepingSize;
  966. for (int i = 0; i < sleepingSize; i++)
  967. {
  968. si32 hid;
  969. if (h.saving)
  970. hid = sleepingHeroes[i]->id;
  971. h & hid;
  972. if (!h.saving)
  973. {
  974. const CGHeroInstance *hero = cb->getHero(hid);
  975. sleepingHeroes += hero;
  976. }
  977. }
  978. //hero list order
  979. ui8 heroListSize;
  980. if (h.saving)
  981. heroListSize = wanderingHeroes.size();
  982. else
  983. wanderingHeroes.clear();
  984. h & heroListSize;
  985. for (int i = 0; i < heroListSize; i++)
  986. {
  987. si32 hid;
  988. if (h.saving)
  989. hid = wanderingHeroes[i]->id;
  990. h & hid;
  991. if (!h.saving)
  992. {
  993. const CGHeroInstance *hero = cb->getHero(hid);
  994. wanderingHeroes += hero;
  995. }
  996. }
  997. }
  998. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  999. {
  1000. serializeTempl(h,version);
  1001. }
  1002. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1003. {
  1004. serializeTempl(h,version);
  1005. firstCall = -1;
  1006. }
  1007. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  1008. {
  1009. if(!LOCPLINT->makingTurn)
  1010. return false;
  1011. if (!h)
  1012. return false; //can't find hero
  1013. if (adventureInt && adventureInt->isHeroSleeping(h))
  1014. {
  1015. adventureInt->sleepWake.clickLeft(true, false);
  1016. adventureInt->sleepWake.clickLeft(false, true);
  1017. //could've just called
  1018. //adventureInt->fsleepWake();
  1019. //but no authentic button click/sound ;-)
  1020. }
  1021. int i = 1;
  1022. //evil...
  1023. eventsM.unlock();
  1024. pim->unlock();
  1025. cb->getGsMutex().unlock_shared();
  1026. bool result = false;
  1027. {
  1028. path.convert(0);
  1029. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1030. stillMoveHero.data = CONTINUE_MOVE;
  1031. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1032. enum TerrainTile::EterrainType newTerrain;
  1033. int sh = -1;
  1034. const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
  1035. for(i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  1036. {
  1037. //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
  1038. if(path.nodes[i-1].coord.z != path.nodes[i].coord.z)
  1039. continue;
  1040. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  1041. if(path.nodes[i-1].turns)
  1042. {
  1043. stillMoveHero.data = STOP_MOVE;
  1044. break;
  1045. }
  1046. // Start a new sound for the hero movement or let the existing one carry on.
  1047. #if 0
  1048. // TODO
  1049. if (hero is flying && sh == -1)
  1050. sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
  1051. #endif
  1052. {
  1053. newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1054. if (newTerrain != currentTerrain)
  1055. {
  1056. CCS->soundh->stopSound(sh);
  1057. sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
  1058. currentTerrain = newTerrain;
  1059. }
  1060. }
  1061. stillMoveHero.data = WAITING_MOVE;
  1062. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1063. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  1064. cb->moveHero(h,endpos);
  1065. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1066. stillMoveHero.cond.wait(un);
  1067. if (guarded) // Abort movement if a guard was fought.
  1068. break;
  1069. }
  1070. CCS->soundh->stopSound(sh);
  1071. }
  1072. cb->getGsMutex().lock_shared();
  1073. pim->lock();
  1074. eventsM.lock();
  1075. if (adventureInt)
  1076. {
  1077. // (i == 0) means hero went through all the path
  1078. adventureInt->updateMoveHero(h, (i != 0));
  1079. adventureInt->updateNextHero(h);
  1080. }
  1081. return result;
  1082. }
  1083. bool CPlayerInterface::shiftPressed() const
  1084. {
  1085. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1086. }
  1087. bool CPlayerInterface::altPressed() const
  1088. {
  1089. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  1090. }
  1091. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  1092. {
  1093. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  1094. {
  1095. onEnd();
  1096. return;
  1097. }
  1098. {
  1099. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1100. while(showingDialog->data)
  1101. showingDialog->cond.wait(un);
  1102. }
  1103. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1104. while(dialogs.size())
  1105. {
  1106. pim->unlock();
  1107. SDL_Delay(20);
  1108. pim->lock();
  1109. }
  1110. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  1111. cgw->quit->callback += onEnd;
  1112. GH.pushInt(cgw);
  1113. }
  1114. /**
  1115. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  1116. * into a combinational one on an artifact screen. Does not require the combination of
  1117. * artifacts to be legal.
  1118. * @param artifactID ID of a constituent artifact.
  1119. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1120. * is false.
  1121. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1122. */
  1123. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1124. {
  1125. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1126. std::string text = artifact.Description();
  1127. text += "\n\n";
  1128. std::vector<CComponent*> scs;
  1129. if (assemble) {
  1130. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1131. // You possess all of the components to...
  1132. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1133. // Picture of assembled artifact at bottom.
  1134. CComponent* sc = new CComponent;
  1135. sc->type = CComponent::artifact;
  1136. sc->subtype = assembledArtifact.id;
  1137. sc->description = assembledArtifact.Description();
  1138. sc->subtitle = assembledArtifact.Name();
  1139. scs.push_back(sc);
  1140. } else {
  1141. // Do you wish to disassemble this artifact?
  1142. text += CGI->generaltexth->allTexts[733];
  1143. }
  1144. showYesNoDialog(text, scs, onYes, onNo, true);
  1145. }
  1146. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1147. {
  1148. if(stillMoveHero.get() == DURING_MOVE)
  1149. stillMoveHero.setn(CONTINUE_MOVE);
  1150. }
  1151. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1152. {
  1153. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1154. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1155. }
  1156. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1157. {
  1158. //redraw minimap if owner changed
  1159. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1160. if(sop->what == ObjProperty::OWNER)
  1161. {
  1162. const CGObjectInstance * obj = cb->getObj(sop->id);
  1163. std::set<int3> pos = obj->getBlockedPos();
  1164. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1165. {
  1166. if(cb->isVisible(*it))
  1167. adventureInt->minimap.showTile(*it);
  1168. }
  1169. if(obj->ID == GameConstants::TOWNI_TYPE)
  1170. {
  1171. if(obj->tempOwner == playerID)
  1172. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1173. else
  1174. towns -= obj;
  1175. }
  1176. assert(cb->getTownsInfo().size() == towns.size());
  1177. }
  1178. }
  1179. void CPlayerInterface::recreateHeroTownList()
  1180. {
  1181. std::vector <const CGHeroInstance *> newWanderingHeroes;
  1182. std::vector<const CGHeroInstance*> allHeroes = cb->getHeroesInfo();
  1183. //applying current heroes order to new heroes info
  1184. int j;
  1185. for (int i = 0; i < wanderingHeroes.size(); i++)
  1186. if ((j = vstd::find_pos(allHeroes, wanderingHeroes[i])) >= 0)
  1187. if (!allHeroes[j]->inTownGarrison)
  1188. {
  1189. newWanderingHeroes += allHeroes[j];
  1190. allHeroes -= allHeroes[j];
  1191. }
  1192. //all the rest of new heroes go the end of the list
  1193. wanderingHeroes.clear();
  1194. wanderingHeroes = newWanderingHeroes;
  1195. newWanderingHeroes.clear();
  1196. for (int i = 0; i < allHeroes.size(); i++)
  1197. if (!allHeroes[i]->inTownGarrison)
  1198. wanderingHeroes += allHeroes[i];
  1199. std::vector<const CGTownInstance*> newTowns;
  1200. std::vector<const CGTownInstance*> allTowns = cb->getTownsInfo();
  1201. for (int i = 0; i < towns.size(); i++)
  1202. if ((j = vstd::find_pos(allTowns, towns[i])) >= 0)
  1203. {
  1204. newTowns += allTowns[j];
  1205. allTowns -= allTowns[j];
  1206. }
  1207. towns.clear();
  1208. towns = newTowns;
  1209. newTowns.clear();
  1210. for(int i = 0; i < allTowns.size(); i++)
  1211. towns.push_back(allTowns[i]);
  1212. adventureInt->updateNextHero(NULL);
  1213. }
  1214. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1215. {
  1216. if(pos < 0 || pos >= wanderingHeroes.size())
  1217. return NULL;
  1218. return wanderingHeroes[pos];
  1219. }
  1220. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1221. {
  1222. waitWhileDialog();
  1223. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1224. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1225. GH.pushInt(cr);
  1226. }
  1227. void CPlayerInterface::waitWhileDialog()
  1228. {
  1229. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1230. while(showingDialog->data)
  1231. showingDialog->cond.wait(un);
  1232. }
  1233. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1234. {
  1235. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1236. int state = obj->state();
  1237. std::vector<si32> cost;
  1238. obj->getBoatCost(cost);
  1239. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1240. GH.pushInt(csw);
  1241. }
  1242. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1243. {
  1244. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1245. //we might have built a boat in shipyard in opened town screen
  1246. if(obj->ID == 8
  1247. && LOCPLINT->castleInt
  1248. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1249. {
  1250. CCS->soundh->playSound(soundBase::newBuilding);
  1251. LOCPLINT->castleInt->addBuilding(20);
  1252. }
  1253. }
  1254. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1255. {
  1256. waitWhileDialog();
  1257. adventureInt->centerOn (pos);
  1258. if(focusTime)
  1259. {
  1260. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1261. if(activeAdv)
  1262. adventureInt->deactivate();
  1263. SDL_Delay(focusTime);
  1264. if(activeAdv)
  1265. adventureInt->activate();
  1266. }
  1267. }
  1268. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1269. {
  1270. if(obj->ID == GameConstants::HEROI_TYPE && obj->tempOwner == playerID)
  1271. {
  1272. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1273. heroKilled(h);
  1274. }
  1275. }
  1276. bool CPlayerInterface::ctrlPressed() const
  1277. {
  1278. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1279. }
  1280. void CPlayerInterface::update()
  1281. {
  1282. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1283. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1284. if(terminate_cond.get())
  1285. return;
  1286. //make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
  1287. boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
  1288. //if there are any waiting dialogs, show them
  1289. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1290. {
  1291. showingDialog->set(true);
  1292. GH.pushInt(dialogs.front());
  1293. dialogs.pop_front();
  1294. }
  1295. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1296. if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
  1297. {
  1298. pim->unlock();
  1299. return;
  1300. }
  1301. // Handles mouse and key input
  1302. GH.updateTime();
  1303. GH.handleEvents();
  1304. if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1305. GH.totalRedraw();
  1306. else
  1307. GH.simpleRedraw();
  1308. if (settings["general"]["showfps"].Bool())
  1309. GH.drawFPSCounter();
  1310. // draw the mouse cursor and update the screen
  1311. CCS->curh->draw1();
  1312. CSDL_Ext::update(screen);
  1313. CCS->curh->draw2();
  1314. pim->unlock();
  1315. }
  1316. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1317. {
  1318. using namespace boost::filesystem;
  1319. using namespace boost::algorithm;
  1320. std::map<std::time_t, int> dates; //save number => datestamp
  1321. directory_iterator enddir;
  1322. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1323. {
  1324. if(is_regular(dir->status()))
  1325. {
  1326. std::string name = dir->path().leaf();
  1327. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1328. {
  1329. char nr = name[namePrefix.size()];
  1330. if(std::isdigit(nr))
  1331. {
  1332. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. if(dates.size())
  1338. return (--dates.end())->second; //return latest file number
  1339. return 0;
  1340. }
  1341. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1342. {
  1343. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1344. {
  1345. //ho->moveDir = 1;
  1346. ho->isStanding = false;
  1347. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1348. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1349. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1350. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1351. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1352. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1353. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1354. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1355. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1356. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1357. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1358. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1359. 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);
  1360. 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);
  1361. 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);
  1362. 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);
  1363. 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);
  1364. 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);
  1365. }
  1366. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1367. {
  1368. //ho->moveDir = 2;
  1369. ho->isStanding = false;
  1370. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1371. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1372. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1373. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1374. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1375. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1376. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1377. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1378. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1379. 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);
  1380. 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);
  1381. 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);
  1382. }
  1383. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1384. {
  1385. //ho->moveDir = 3;
  1386. ho->isStanding = false;
  1387. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1388. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1389. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1390. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1391. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1392. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1393. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1394. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1395. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1396. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1397. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1398. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1399. 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);
  1400. 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);
  1401. 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);
  1402. 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);
  1403. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1404. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1405. }
  1406. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1407. {
  1408. //ho->moveDir = 4;
  1409. ho->isStanding = false;
  1410. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1411. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1412. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1413. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1414. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1415. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1416. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1417. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1418. 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);
  1419. 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);
  1420. }
  1421. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1422. {
  1423. //ho->moveDir = 5;
  1424. ho->isStanding = false;
  1425. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1426. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1427. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1428. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1429. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1430. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1431. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1432. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1433. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1434. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1435. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1436. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1437. 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);
  1438. 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);
  1439. 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);
  1440. 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);
  1441. 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);
  1442. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1443. }
  1444. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1445. {
  1446. //ho->moveDir = 6;
  1447. ho->isStanding = false;
  1448. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1449. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1450. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1451. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1452. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1453. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1454. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1455. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1456. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1457. 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);
  1458. 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);
  1459. 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);
  1460. }
  1461. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1462. {
  1463. //ho->moveDir = 7;
  1464. ho->isStanding = false;
  1465. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1466. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1467. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1468. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1469. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1470. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1471. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1472. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1473. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1474. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1475. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1476. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1477. 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);
  1478. 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);
  1479. 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);
  1480. 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);
  1481. 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);
  1482. 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);
  1483. }
  1484. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1485. {
  1486. //ho->moveDir = 8;
  1487. ho->isStanding = false;
  1488. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1489. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1490. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1491. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1492. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1493. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1494. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1495. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1496. 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);
  1497. 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);
  1498. }
  1499. }
  1500. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1501. {
  1502. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1503. {
  1504. //setting advmap shift
  1505. adventureInt->terrain.moveX = i-32;
  1506. adventureInt->terrain.moveY = i-32;
  1507. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1508. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1509. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1510. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1511. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1512. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1513. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1514. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1515. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1516. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1517. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1518. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1519. }
  1520. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1521. {
  1522. //setting advmap shift
  1523. adventureInt->terrain.moveY = i-32;
  1524. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1525. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1526. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1527. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1528. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1529. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1530. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1531. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1532. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1533. }
  1534. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1535. {
  1536. //setting advmap shift
  1537. adventureInt->terrain.moveX = -i+32;
  1538. adventureInt->terrain.moveY = i-32;
  1539. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1540. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1541. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1542. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1543. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1544. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1545. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1546. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1547. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1548. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1549. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1550. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1551. }
  1552. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1553. {
  1554. //setting advmap shift
  1555. adventureInt->terrain.moveX = -i+32;
  1556. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1557. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1558. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1559. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1560. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1561. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1562. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1563. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1564. }
  1565. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1566. {
  1567. //setting advmap shift
  1568. adventureInt->terrain.moveX = -i+32;
  1569. adventureInt->terrain.moveY = -i+32;
  1570. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1571. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1572. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1573. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1574. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1575. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1576. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1577. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1578. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1579. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1580. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1581. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1582. }
  1583. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1584. {
  1585. //setting advmap shift
  1586. adventureInt->terrain.moveY = -i+32;
  1587. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1588. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1589. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1590. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1591. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1592. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1593. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1594. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1595. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1596. }
  1597. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1598. {
  1599. //setting advmap shift
  1600. adventureInt->terrain.moveX = i-32;
  1601. adventureInt->terrain.moveY = -i+32;
  1602. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1603. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1604. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1605. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1606. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1607. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1608. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1609. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1610. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1611. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1612. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1613. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1614. }
  1615. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1616. {
  1617. //setting advmap shift
  1618. adventureInt->terrain.moveX = i-32;
  1619. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1620. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1621. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1622. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1623. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1624. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1625. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1626. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1627. }
  1628. }
  1629. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1630. {
  1631. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1632. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1633. {
  1634. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1635. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1636. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1637. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1638. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1639. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1640. }
  1641. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1642. {
  1643. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1644. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1645. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1646. }
  1647. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1648. {
  1649. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1650. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1651. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1652. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1653. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1654. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1655. }
  1656. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1657. {
  1658. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1659. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1660. }
  1661. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1662. {
  1663. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1664. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1665. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1666. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1667. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1668. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1669. }
  1670. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1671. {
  1672. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1673. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1674. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1675. }
  1676. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1677. {
  1678. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1679. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1680. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1681. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1682. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1683. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1684. }
  1685. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1686. {
  1687. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1688. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1689. }
  1690. //restoring good rects
  1691. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1692. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1693. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1694. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1695. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1696. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1697. //restoring good order of objects
  1698. 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);
  1699. 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);
  1700. 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);
  1701. 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);
  1702. 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);
  1703. 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);
  1704. }
  1705. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1706. {
  1707. if(LOCPLINT != this)
  1708. return;
  1709. if(player == playerID)
  1710. {
  1711. if(!victory)
  1712. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1713. // else
  1714. // showInfoDialog("Placeholder message: you won!");
  1715. makingTurn = true;
  1716. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1717. makingTurn = false;
  1718. howManyPeople--;
  1719. if(!howManyPeople) //all human players eliminated
  1720. {
  1721. if(cb->getStartInfo()->mode != StartInfo::CAMPAIGN)
  1722. requestReturningToMainMenu();
  1723. else
  1724. requestStoppingClient();
  1725. }
  1726. cb->unregisterMyInterface(); //we already won/lost, nothing else matters
  1727. }
  1728. else
  1729. {
  1730. if(!victory && cb->getPlayerStatus(playerID) == PlayerState::INGAME) //enemy has lost
  1731. {
  1732. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1733. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1734. showInfoDialog(txt,std::vector<CComponent*>(1, new CComponent(CComponent::flag, player, 0)));
  1735. }
  1736. }
  1737. }
  1738. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1739. {
  1740. }
  1741. void CPlayerInterface::showPuzzleMap()
  1742. {
  1743. waitWhileDialog();
  1744. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1745. //TODO: interface should not know the real position of Grail...
  1746. double ratio = 0;
  1747. int3 grailPos = cb->getGrailPos(ratio);
  1748. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1749. }
  1750. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1751. {
  1752. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1753. {
  1754. eraseCurrentPathOf(caster, false);
  1755. }
  1756. }
  1757. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1758. {
  1759. if(checkForExistanceOfPath)
  1760. {
  1761. assert(vstd::contains(paths, ho));
  1762. }
  1763. else if (!vstd::contains(paths, ho))
  1764. {
  1765. return;
  1766. }
  1767. assert(ho == adventureInt->selection);
  1768. paths.erase(ho);
  1769. adventureInt->terrain.currentPath = NULL;
  1770. adventureInt->updateMoveHero(ho, false);
  1771. }
  1772. void CPlayerInterface::removeLastNodeFromPath(const CGHeroInstance *ho)
  1773. {
  1774. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1775. 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
  1776. eraseCurrentPathOf(ho);
  1777. }
  1778. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1779. {
  1780. if(vstd::contains(paths,h)) //hero has assigned path
  1781. {
  1782. CGPath &path = paths[h];
  1783. if(!path.nodes.size())
  1784. {
  1785. tlog3 << "Warning: empty path found...\n";
  1786. paths.erase(h);
  1787. }
  1788. else
  1789. {
  1790. assert(h->getPosition(false) == path.startPos());
  1791. //update the hero path in case of something has changed on map
  1792. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1793. return &path;
  1794. else
  1795. paths.erase(h);
  1796. }
  1797. }
  1798. return NULL;
  1799. }
  1800. void CPlayerInterface::acceptTurn()
  1801. {
  1802. if(settings["session"]["autoSkip"].Bool())
  1803. {
  1804. while(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1805. iw->close();
  1806. }
  1807. waitWhileDialog();
  1808. if(howManyPeople > 1)
  1809. adventureInt->startTurn();
  1810. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1811. //Select sound for day start
  1812. int totalDays = cb->getDate();
  1813. int day = cb->getDate(1);
  1814. int week = cb->getDate(2);
  1815. if (totalDays == 1)
  1816. CCS->soundh->playSound(soundBase::newDay);
  1817. else if (day != 1)
  1818. CCS->soundh->playSound(soundBase::newDay);
  1819. else if (week != 1)
  1820. CCS->soundh->playSound(soundBase::newWeek);
  1821. else
  1822. CCS->soundh->playSound(soundBase::newMonth);
  1823. adventureInt->infoBar.newDay(day);
  1824. //select first hero if available.
  1825. //TODO: check if hero is slept
  1826. if(wanderingHeroes.size())
  1827. adventureInt->select(wanderingHeroes.front());
  1828. else
  1829. adventureInt->select(towns.front());
  1830. adventureInt->showAll(screen);
  1831. if(settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
  1832. {
  1833. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1834. iw->close();
  1835. adventureInt->endTurn.callback();
  1836. }
  1837. }
  1838. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1839. {
  1840. std::string hlp;
  1841. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1842. int msgToShow = -1;
  1843. CGHeroInstance::ECanDig isDiggingPossible = h->diggingStatus();
  1844. if(hlp.length())
  1845. isDiggingPossible = CGHeroInstance::TILE_OCCUPIED; //TODO integrate with canDig
  1846. switch(isDiggingPossible)
  1847. {
  1848. case CGHeroInstance::CAN_DIG:
  1849. break;
  1850. case CGHeroInstance::LACK_OF_MOVEMENT:
  1851. msgToShow = 56; //"Digging for artifacts requires a whole day, try again tomorrow."
  1852. break;
  1853. case CGHeroInstance::TILE_OCCUPIED:
  1854. msgToShow = 97; //Try searching on clear ground.
  1855. break;
  1856. case CGHeroInstance::WRONG_TERRAIN:
  1857. msgToShow = 60; ////Try looking on land!
  1858. break;
  1859. default:
  1860. assert(0);
  1861. }
  1862. if(msgToShow < 0)
  1863. cb->dig(h);
  1864. else
  1865. showInfoDialog(CGI->generaltexth->allTexts[msgToShow]);
  1866. }
  1867. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1868. {
  1869. adventureInt->infoBar.updateSelection(specific);
  1870. // if (adventureInt->selection == specific)
  1871. // adventureInt->infoBar.showAll(screen);
  1872. }
  1873. void CPlayerInterface::battleNewRoundFirst( int round )
  1874. {
  1875. if(LOCPLINT != this)
  1876. { //another local interface should do this
  1877. return;
  1878. }
  1879. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1880. battleInt->newRoundFirst(round);
  1881. }
  1882. void CPlayerInterface::stopMovement()
  1883. {
  1884. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1885. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1886. }
  1887. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1888. {
  1889. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1890. if(market->o->ID == 2) //Altar
  1891. {
  1892. //EEMarketMode mode = market->availableModes().front();
  1893. if(market->allowsTrade(EMarketMode::ARTIFACT_EXP) && visitor->getAlignment() != EAlignment::EVIL)
  1894. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::ARTIFACT_EXP));
  1895. else if(market->allowsTrade(EMarketMode::CREATURE_EXP) && visitor->getAlignment() != EAlignment::GOOD)
  1896. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::CREATURE_EXP));
  1897. }
  1898. else
  1899. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1900. }
  1901. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1902. {
  1903. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1904. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1905. GH.pushInt(cuw);
  1906. }
  1907. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  1908. {
  1909. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1910. CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
  1911. GH.pushInt(chfw);
  1912. }
  1913. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1914. {
  1915. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1916. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1917. cmw->artifactsChanged(false);
  1918. }
  1919. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1920. {
  1921. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1922. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1923. GH.pushInt(tv);
  1924. }
  1925. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1926. {
  1927. if(obj->state())
  1928. {
  1929. MetaString txt;
  1930. obj->getProblemText(txt);
  1931. showInfoDialog(txt.toString());
  1932. }
  1933. else
  1934. showShipyardDialog(obj);
  1935. }
  1936. void CPlayerInterface::requestReturningToMainMenu()
  1937. {
  1938. sendCustomEvent(2);
  1939. }
  1940. void CPlayerInterface::requestStoppingClient()
  1941. {
  1942. sendCustomEvent(3);
  1943. }
  1944. void CPlayerInterface::sendCustomEvent( int code )
  1945. {
  1946. SDL_Event event;
  1947. event.type = SDL_USEREVENT;
  1948. event.user.code = code;
  1949. SDL_PushEvent(&event);
  1950. }
  1951. void CPlayerInterface::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
  1952. {
  1953. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1954. garrisonChanged(location.army);
  1955. }
  1956. void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
  1957. {
  1958. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1959. garrisonChanged(location.army);
  1960. }
  1961. void CPlayerInterface::stacksErased(const StackLocation &location)
  1962. {
  1963. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1964. garrisonChanged(location.army);
  1965. }
  1966. #define UPDATE_IF(LOC) static_cast<const CArmedInstance*>(adventureInt->infoBar.curSel) == LOC.army.get()
  1967. void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  1968. {
  1969. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1970. garrisonChanged(loc1.army, UPDATE_IF(loc1));
  1971. if(loc2.army != loc1.army)
  1972. garrisonChanged(loc2.army, UPDATE_IF(loc2));
  1973. }
  1974. void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  1975. {
  1976. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1977. garrisonChanged(location.army);
  1978. }
  1979. void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  1980. {
  1981. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1982. //bool updateInfobox = true;
  1983. garrisonChanged(src.army, UPDATE_IF(src));
  1984. if(dst.army != src.army)
  1985. garrisonChanged(dst.army, UPDATE_IF(dst));
  1986. }
  1987. #undef UPDATE_IF
  1988. void CPlayerInterface::artifactPut(const ArtifactLocation &al)
  1989. {
  1990. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1991. }
  1992. void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
  1993. {
  1994. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1995. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  1996. {
  1997. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  1998. {
  1999. (dynamic_cast<CArtifactHolder*>(isa))->artifactRemoved(al);
  2000. }
  2001. }
  2002. }
  2003. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  2004. {
  2005. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2006. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2007. {
  2008. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2009. {
  2010. (dynamic_cast<CArtifactHolder*>(isa))->artifactMoved(src, dst);
  2011. }
  2012. }
  2013. }
  2014. void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
  2015. {
  2016. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2017. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2018. {
  2019. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2020. {
  2021. (dynamic_cast<CArtifactHolder*>(isa))->artifactAssembled(al);
  2022. }
  2023. }
  2024. }
  2025. void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
  2026. {
  2027. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2028. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2029. {
  2030. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2031. {
  2032. (dynamic_cast<CArtifactHolder*>(isa))->artifactDisassembled(al);
  2033. }
  2034. }
  2035. }
  2036. void CPlayerInterface::playerStartsTurn(ui8 player)
  2037. {
  2038. {
  2039. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2040. if(!GH.listInt.size())
  2041. {
  2042. GH.pushInt(adventureInt);
  2043. adventureInt->activateKeys();
  2044. }
  2045. if(howManyPeople == 1)
  2046. {
  2047. GH.curInt = this;
  2048. adventureInt->startTurn();
  2049. }
  2050. }
  2051. if(player != playerID && this == LOCPLINT)
  2052. {
  2053. waitWhileDialog();
  2054. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2055. adventureInt->minimap.redraw();
  2056. adventureInt->infoBar.enemyTurn(player, 0.5);
  2057. //TODO AI turn music
  2058. //TODO block GUI
  2059. }
  2060. }
  2061. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  2062. {
  2063. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  2064. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  2065. }