CPlayerInterface.cpp 93 KB

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