CPlayerInterface.cpp 88 KB

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