CPlayerInterface.cpp 83 KB

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