CPlayerInterface.cpp 90 KB

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