CPlayerInterface.cpp 90 KB

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