2
0

CPlayerInterface.cpp 88 KB

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