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