CPlayerInterface.cpp 84 KB

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