CPlayerInterface.cpp 88 KB

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