CPlayerInterface.cpp 97 KB

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