CPlayerInterface.cpp 89 KB

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