CPlayerInterface.cpp 88 KB

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