CPlayerInterface.cpp 81 KB

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