CPlayerInterface.cpp 91 KB

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