CPlayerInterface.cpp 91 KB

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