CPlayerInterface.cpp 85 KB

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