CPlayerInterface.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. #include "../stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "../CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CKingdomInterface.h"
  7. #include "CCursorHandler.h"
  8. #include "CGameInfo.h"
  9. #include "CHeroWindow.h"
  10. #include "CMessage.h"
  11. #include "CPlayerInterface.h"
  12. //#include "SDL_Extensions.h"
  13. #include "SDL_Extensions.h"
  14. //#include "SDL_framerate.h"
  15. #include "SDL_framerate.h"
  16. #include "CConfigHandler.h"
  17. #include "CCreatureAnimation.h"
  18. #include "Graphics.h"
  19. #include "../hch/CArtHandler.h"
  20. #include "../hch/CGeneralTextHandler.h"
  21. #include "../hch/CHeroHandler.h"
  22. #include "../hch/CLodHandler.h"
  23. #include "../hch/CObjectHandler.h"
  24. #include "../lib/Connection.h"
  25. #include "../hch/CSpellHandler.h"
  26. #include "../hch/CTownHandler.h"
  27. #include "../lib/CondSh.h"
  28. #include "../lib/NetPacks.h"
  29. #include "../lib/map.h"
  30. #include "../lib/VCMIDirs.h"
  31. #include "mapHandler.h"
  32. #include "../timeHandler.h"
  33. #include <boost/lexical_cast.hpp>
  34. #include <boost/format.hpp>
  35. #include <boost/algorithm/string.hpp>
  36. #include <boost/algorithm/string/replace.hpp>
  37. #include <boost/assign/std/vector.hpp>
  38. #include <boost/assign/list_of.hpp>
  39. #include <boost/date_time/posix_time/posix_time.hpp>
  40. #include <boost/thread.hpp>
  41. #include <cmath>
  42. #include <queue>
  43. #include <sstream>
  44. #include <boost/filesystem.hpp>
  45. #include "../StartInfo.h"
  46. #ifdef min
  47. #undef min
  48. #endif
  49. #ifdef max
  50. #undef max
  51. #endif
  52. /*
  53. * CPlayerInterface.cpp, part of VCMI engine
  54. *
  55. * Authors: listed in file AUTHORS in main folder
  56. *
  57. * License: GNU General Public License v2.0 or later
  58. * Full text of license available in license.txt file, in main folder
  59. *
  60. */
  61. using namespace boost::assign;
  62. using namespace CSDL_Ext;
  63. void processCommand(const std::string &message, CClient *&client);
  64. extern std::queue<SDL_Event*> events;
  65. extern boost::mutex eventsM;
  66. CPlayerInterface * LOCPLINT;
  67. CBattleInterface * CPlayerInterface::battleInt;
  68. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  69. CondSh<EMoveState> stillMoveHero; //used during hero movement
  70. int CPlayerInterface::howManyPeople = 0;
  71. struct OCM_HLP_CGIN
  72. {
  73. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  74. {
  75. return (*a.first)<(*b.first);
  76. }
  77. } ocmptwo_cgin ;
  78. CPlayerInterface::CPlayerInterface(int Player)
  79. {
  80. howManyPeople++;
  81. GH.defActionsDef = 0;
  82. LOCPLINT = this;
  83. curAction = NULL;
  84. playerID=Player;
  85. human=true;
  86. castleInt = NULL;
  87. battleInt = NULL;
  88. pim = new boost::recursive_mutex;
  89. makingTurn = false;
  90. showingDialog = new CondSh<bool>(false);
  91. sysOpts = GDefaultOptions;
  92. //initializing framerate keeper
  93. mainFPSmng = new FPSmanager;
  94. SDL_initFramerate(mainFPSmng);
  95. SDL_setFramerate(mainFPSmng, 48);
  96. //framerate keeper initialized
  97. cingconsole = new CInGameConsole;
  98. terminate_cond.set(false);
  99. firstCall = 1; //if loading will be overwritten in serialize
  100. autosaveCount = 0;
  101. }
  102. CPlayerInterface::~CPlayerInterface()
  103. {
  104. howManyPeople--;
  105. delete pim;
  106. delete showingDialog;
  107. delete mainFPSmng;
  108. if(adventureInt)
  109. {
  110. if(adventureInt->active & CIntObject::KEYBOARD)
  111. adventureInt->deactivateKeys();
  112. delete adventureInt;
  113. adventureInt = NULL;
  114. }
  115. if(cingconsole->active) //TODO
  116. cingconsole->deactivate();
  117. delete cingconsole;
  118. LOCPLINT = NULL;
  119. }
  120. void CPlayerInterface::init(ICallback * CB)
  121. {
  122. cb = dynamic_cast<CCallback*>(CB);
  123. if(!adventureInt)
  124. adventureInt = new CAdvMapInt();
  125. if(!towns.size() && !wanderingHeroes.size())
  126. {
  127. recreateHeroTownList();
  128. }
  129. }
  130. void CPlayerInterface::yourTurn()
  131. {
  132. {
  133. boost::unique_lock<boost::recursive_mutex> un(*pim);
  134. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  135. LOCPLINT = this;
  136. GH.curInt = this;
  137. adventureInt->selection = NULL;
  138. if(firstCall)
  139. {
  140. if(howManyPeople == 1)
  141. adventureInt->setPlayer(playerID);
  142. autosaveCount = getLastIndex("Autosave_");
  143. if(!GH.listInt.size())
  144. {
  145. GH.pushInt(adventureInt);
  146. adventureInt->activateKeys();
  147. }
  148. if(firstCall > 0) //new game, not loaded
  149. {
  150. int index = getLastIndex("Newgame_Autosave_");
  151. index %= SAVES_COUNT;
  152. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  153. }
  154. firstCall = 0;
  155. }
  156. else
  157. {
  158. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  159. autosaveCount %= 5;
  160. }
  161. if(adventureInt->player != playerID)
  162. adventureInt->setPlayer(playerID);
  163. if(howManyPeople > 1) //hot seat message
  164. {
  165. adventureInt->startHotSeatWait(playerID);
  166. makingTurn = true;
  167. std::string msg = CGI->generaltexth->allTexts[13];
  168. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  169. std::vector<SComponent*> cmp;
  170. cmp.push_back(new SComponent(SComponent::flag, playerID, 0));
  171. showInfoDialog(msg, cmp);
  172. }
  173. else
  174. {
  175. makingTurn = true;
  176. adventureInt->startTurn();
  177. }
  178. }
  179. acceptTurn();
  180. }
  181. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  182. {
  183. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  184. for(int h=0; h<hlp.objects.size(); ++h)
  185. if(hlp.objects[h].first->id==hid)
  186. {
  187. hlp.objects[h].second = r;
  188. return;
  189. }
  190. }
  191. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  192. {
  193. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  194. for(int h=0; h<hlp.objects.size(); ++h)
  195. if(hlp.objects[h].first->id==hid)
  196. {
  197. hlp.objects.erase(hlp.objects.begin()+h);
  198. return;
  199. }
  200. }
  201. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  202. {
  203. if(LOCPLINT != this)
  204. return;
  205. boost::unique_lock<boost::recursive_mutex> un(*pim);
  206. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  207. int3 hp = details.start;
  208. adventureInt->centerOn(ho); //actualizing screen pos
  209. adventureInt->minimap.draw(screen2);
  210. adventureInt->heroList.draw(screen2);
  211. bool directlyAttackingCreature =
  212. CGI->mh->map->isInTheMap(details.attackedFrom)
  213. && adventureInt->terrain.currentPath->nodes.size() == 3;
  214. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  215. {
  216. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  217. {
  218. if(adventureInt->terrain.currentPath)
  219. eraseCurrentPathOf(ho);
  220. return; //teleport - no fancy moving animation
  221. //TODO: smooth disappear / appear effect
  222. }
  223. if (details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED //hero didn't change tile but visit succeeded
  224. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  225. {
  226. eraseCurrentPathOf(ho);
  227. }
  228. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  229. {
  230. //remove one node from the path (the one we went)
  231. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  232. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  233. eraseCurrentPathOf(ho);
  234. }
  235. }
  236. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  237. {
  238. ho->isStanding = true;
  239. stillMoveHero.setn(STOP_MOVE);
  240. GH.totalRedraw();
  241. return;
  242. }
  243. initMovement(details, ho, hp);
  244. //first initializing done
  245. SDL_framerateDelay(mainFPSmng); // after first move
  246. //main moving
  247. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  248. {
  249. movementPxStep(details, i, hp, ho);
  250. adventureInt->updateScreen = true;
  251. adventureInt->show(screen);
  252. CSDL_Ext::update(screen);
  253. SDL_framerateDelay(mainFPSmng); //for animation purposes
  254. } //for(int i=1; i<32; i+=4)
  255. //main moving done
  256. //finishing move
  257. finishMovement(details, hp, ho);
  258. ho->isStanding = true;
  259. //move finished
  260. adventureInt->minimap.draw(screen2);
  261. adventureInt->heroList.updateMove(ho);
  262. //check if user cancelled movement
  263. {
  264. boost::unique_lock<boost::mutex> un(eventsM);
  265. while(events.size())
  266. {
  267. SDL_Event *ev = events.front();
  268. events.pop();
  269. switch(ev->type)
  270. {
  271. case SDL_MOUSEBUTTONDOWN:
  272. stillMoveHero.setn(STOP_MOVE);
  273. break;
  274. case SDL_KEYDOWN:
  275. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  276. stillMoveHero.setn(STOP_MOVE);
  277. break;
  278. }
  279. delete ev;
  280. }
  281. }
  282. if(stillMoveHero.get() == WAITING_MOVE)
  283. stillMoveHero.setn(DURING_MOVE);
  284. // Hero attacked creature directly, set direction to face it.
  285. if (directlyAttackingCreature) {
  286. // Get direction to attacker.
  287. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  288. const ui8 dirLookup[3][3] = {
  289. 1, 2, 3,
  290. 8, 0, 4,
  291. 7, 6, 5
  292. };
  293. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  294. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  295. }
  296. }
  297. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  298. {
  299. boost::unique_lock<boost::recursive_mutex> un(*pim);
  300. wanderingHeroes -= hero;
  301. adventureInt->heroList.updateHList(hero);
  302. }
  303. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  304. {
  305. boost::unique_lock<boost::recursive_mutex> un(*pim);
  306. wanderingHeroes.push_back(hero);
  307. adventureInt->heroList.updateHList();
  308. }
  309. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  310. {
  311. if (castleInt)
  312. GH.popIntTotally(castleInt);
  313. castleInt = new CCastleInterface(town);
  314. GH.pushInt(castleInt);
  315. }
  316. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  317. {
  318. if(!specific)
  319. specific = adventureInt->selection;
  320. assert(specific);
  321. switch(specific->ID)
  322. {
  323. case HEROI_TYPE:
  324. {
  325. InfoAboutHero iah;
  326. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  327. assert(gotInfo);
  328. return graphics->drawHeroInfoWin(iah);
  329. }
  330. case TOWNI_TYPE:
  331. case 33: // Garrison
  332. case 219:
  333. {
  334. InfoAboutTown iah;
  335. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  336. assert(gotInfo);
  337. return graphics->drawTownInfoWin(iah);
  338. }
  339. default:
  340. return NULL;
  341. }
  342. }
  343. int3 CPlayerInterface::repairScreenPos(int3 pos)
  344. {
  345. if(pos.x<-CGI->mh->frameW)
  346. pos.x = -CGI->mh->frameW;
  347. if(pos.y<-CGI->mh->frameH)
  348. pos.y = -CGI->mh->frameH;
  349. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  350. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  351. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  352. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  353. return pos;
  354. }
  355. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  356. {
  357. boost::unique_lock<boost::recursive_mutex> un(*pim);
  358. if(which == 4)
  359. {
  360. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  361. ctw->setExpToLevel();
  362. }
  363. else if(which < PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  364. updateInfo(hero);
  365. }
  366. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  367. {
  368. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  369. if(cuw) //university window is open
  370. {
  371. GH.totalRedraw();
  372. }
  373. }
  374. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  375. {
  376. boost::unique_lock<boost::recursive_mutex> un(*pim);
  377. updateInfo(hero);
  378. }
  379. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  380. {
  381. boost::unique_lock<boost::recursive_mutex> un(*pim);
  382. if(makingTurn && hero->tempOwner == playerID)
  383. adventureInt->heroList.redraw();
  384. }
  385. void CPlayerInterface::receivedResource(int type, int val)
  386. {
  387. boost::unique_lock<boost::recursive_mutex> un(*pim);
  388. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  389. mw->resourceChanged(type, val);
  390. GH.totalRedraw();
  391. }
  392. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  393. {
  394. waitWhileDialog();
  395. CGI->soundh->playSound(soundBase::heroNewLevel);
  396. boost::unique_lock<boost::recursive_mutex> un(*pim);
  397. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  398. GH.pushInt(lw);
  399. }
  400. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  401. {
  402. boost::unique_lock<boost::recursive_mutex> un(*pim);
  403. updateInfo(town);
  404. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  405. {
  406. CGI->mh->hideObject(town->garrisonHero);
  407. wanderingHeroes -= town->garrisonHero;
  408. }
  409. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  410. {
  411. CGI->mh->printObject(town->visitingHero);
  412. wanderingHeroes.push_back(town->visitingHero);
  413. }
  414. //adventureInt->heroList.updateHList();
  415. CCastleInterface *c = castleInt;
  416. if(c)
  417. {
  418. c->garr->highlighted = NULL;
  419. c->hslotup.hero = town->garrisonHero;
  420. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  421. c->garr->set2 = town->visitingHero;
  422. c->garr->recreateSlots();
  423. }
  424. GH.totalRedraw();
  425. }
  426. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  427. {
  428. if(hero->tempOwner != playerID )
  429. return;
  430. boost::unique_lock<boost::recursive_mutex> un(*pim);
  431. openTownWindow(town);
  432. }
  433. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  434. {
  435. boost::unique_lock<boost::recursive_mutex> un(*pim);
  436. updateInfo(obj);
  437. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  438. {
  439. if((*i)->type & IShowActivable::WITH_GARRISON)
  440. {
  441. CGarrisonHolder *cgh = static_cast<CGarrisonHolder*>(*i);
  442. cgh->updateGarrisons();
  443. }
  444. else if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
  445. {
  446. if(obj == cmw->hero)
  447. cmw->garrisonChanged();
  448. }
  449. }
  450. GH.totalRedraw();
  451. }
  452. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  453. {
  454. boost::unique_lock<boost::recursive_mutex> un(*pim);
  455. switch (buildingID)
  456. {
  457. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  458. updateInfo(town);
  459. break;
  460. }
  461. if(!castleInt)
  462. return;
  463. if(castleInt->town!=town)
  464. return;
  465. switch(what)
  466. {
  467. case 1:
  468. CGI->soundh->playSound(soundBase::newBuilding);
  469. castleInt->addBuilding(buildingID);
  470. break;
  471. case 2:
  472. castleInt->removeBuilding(buildingID);
  473. break;
  474. }
  475. }
  476. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  477. {
  478. if(LOCPLINT != this)
  479. { //another local interface should do this
  480. return;
  481. }
  482. while(showingDialog->get())
  483. SDL_Delay(20);
  484. boost::unique_lock<boost::recursive_mutex> un(*pim);
  485. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  486. GH.pushInt(battleInt);
  487. }
  488. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  489. {
  490. }
  491. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  492. {
  493. if(LOCPLINT != this)
  494. { //another local interface should do this
  495. return;
  496. }
  497. for(int b=0; b<healedStacks.size(); ++b)
  498. {
  499. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  500. if(battleInt->creAnims[healed->ID]->getType() == 5)
  501. {
  502. //stack has been resurrected
  503. battleInt->creAnims[healed->ID]->setType(2);
  504. }
  505. }
  506. if (lifeDrain)
  507. {
  508. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  509. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  510. if (attacker)
  511. {
  512. battleInt->displayEffect(50, attacker->position);
  513. }
  514. //print info about life drain
  515. int textOff = 0;
  516. if (attacker->count > 1)
  517. {
  518. textOff += 1;
  519. }
  520. char textBuf[1000];
  521. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  522. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  523. battleInt->console->addText(textBuf);
  524. }
  525. }
  526. void CPlayerInterface::battleNewStackAppeared(int stackID)
  527. {
  528. if(LOCPLINT != this)
  529. { //another local interface should do this
  530. return;
  531. }
  532. //changing necessary things in battle interface
  533. battleInt->newStack(stackID);
  534. }
  535. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  536. {
  537. if(LOCPLINT != this)
  538. { //another local interface should do this
  539. return;
  540. }
  541. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  542. {
  543. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  544. {
  545. if(itBat->first == *it) //remove this obstacle
  546. {
  547. battleInt->idToObstacle.erase(itBat);
  548. break;
  549. }
  550. }
  551. }
  552. //update accessible hexes
  553. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  554. }
  555. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  556. {
  557. if(LOCPLINT != this)
  558. { //another local interface should do this
  559. return;
  560. }
  561. battleInt->stackIsCatapulting(ca);
  562. }
  563. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  564. {
  565. if(LOCPLINT != this)
  566. { //another local interface should do this
  567. return;
  568. }
  569. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  570. {
  571. battleInt->stackRemoved(*it);
  572. }
  573. }
  574. void CPlayerInterface::battleNewRound(int round) //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  575. {
  576. if(LOCPLINT != this)
  577. { //another local interface should do this
  578. return;
  579. }
  580. boost::unique_lock<boost::recursive_mutex> un(*pim);
  581. battleInt->newRound(round);
  582. }
  583. void CPlayerInterface::actionStarted(const BattleAction* action)
  584. {
  585. if(LOCPLINT != this)
  586. { //another local interface should do this
  587. return;
  588. }
  589. boost::unique_lock<boost::recursive_mutex> un(*pim);
  590. curAction = new BattleAction(*action);
  591. battleInt->startAction(action);
  592. }
  593. void CPlayerInterface::actionFinished(const BattleAction* action)
  594. {
  595. if(LOCPLINT != this)
  596. { //another local interface should do this
  597. return;
  598. }
  599. boost::unique_lock<boost::recursive_mutex> un(*pim);
  600. delete curAction;
  601. curAction = NULL;
  602. battleInt->endAction(action);
  603. }
  604. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  605. {
  606. CBattleInterface *b = battleInt;
  607. {
  608. boost::unique_lock<boost::recursive_mutex> un(*pim);
  609. const CStack *stack = cb->battleGetStackByID(stackID);
  610. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  611. {
  612. std::string hlp = CGI->generaltexth->allTexts[33];
  613. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->type->namePl : stack->type->nameSing);
  614. battleInt->displayEffect(20,stack->position);
  615. battleInt->console->addText(hlp);
  616. }
  617. b->stackActivated(stackID);
  618. }
  619. //wait till BattleInterface sets its command
  620. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  621. while(!b->givenCommand->data)
  622. b->givenCommand->cond.wait(lock);
  623. //tidy up
  624. BattleAction ret = *(b->givenCommand->data);
  625. delete b->givenCommand->data;
  626. b->givenCommand->data = NULL;
  627. //return command
  628. return ret;
  629. }
  630. void CPlayerInterface::battleEnd(BattleResult *br)
  631. {
  632. if(LOCPLINT != this)
  633. { //another local interface should do this
  634. return;
  635. }
  636. boost::unique_lock<boost::recursive_mutex> un(*pim);
  637. battleInt->battleFinished(*br);
  638. }
  639. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  640. {
  641. if(LOCPLINT != this)
  642. { //another local interface should do this
  643. return;
  644. }
  645. boost::unique_lock<boost::recursive_mutex> un(*pim);
  646. battleInt->stackMoved(ID, dest, end, distance);
  647. }
  648. void CPlayerInterface::battleSpellCast(BattleSpellCast *sc)
  649. {
  650. if(LOCPLINT != this)
  651. { //another local interface should do this
  652. return;
  653. }
  654. boost::unique_lock<boost::recursive_mutex> un(*pim);
  655. battleInt->spellCast(sc);
  656. }
  657. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  658. {
  659. if(LOCPLINT != this)
  660. { //another local interface should do this
  661. return;
  662. }
  663. boost::unique_lock<boost::recursive_mutex> un(*pim);
  664. battleInt->battleStacksEffectsSet(sse);
  665. }
  666. void CPlayerInterface::battleStacksAttacked(std::vector<BattleStackAttacked> & bsa)
  667. {
  668. if(LOCPLINT != this)
  669. { //another local interface should do this
  670. return;
  671. }
  672. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  673. boost::unique_lock<boost::recursive_mutex> un(*pim);
  674. tlog5 << "done!\n";
  675. std::vector<SStackAttackedInfo> arg;
  676. for(std::vector<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  677. {
  678. if(i->isEffect() && i->effect != 12) //and not armageddon
  679. {
  680. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  681. if (stack != NULL)
  682. battleInt->displayEffect(i->effect, stack->position);
  683. }
  684. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  685. arg.push_back(to_put);
  686. }
  687. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  688. {
  689. battleInt->displayEffect(bsa.begin()->effect, -1);
  690. }
  691. battleInt->stacksAreAttacked(arg);
  692. }
  693. void CPlayerInterface::battleAttack(BattleAttack *ba)
  694. {
  695. if(LOCPLINT != this)
  696. { //another local interface should do this
  697. return;
  698. }
  699. tlog5 << "CPlayerInterface::battleAttack - locking...";
  700. boost::unique_lock<boost::recursive_mutex> un(*pim);
  701. tlog5 << "done!\n";
  702. assert(curAction);
  703. if(ba->lucky()) //lucky hit
  704. {
  705. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  706. std::string hlp = CGI->generaltexth->allTexts[45];
  707. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->type->namePl.c_str() : stack->type->nameSing.c_str());
  708. battleInt->console->addText(hlp);
  709. battleInt->displayEffect(18,stack->position);
  710. }
  711. //TODO: bad luck?
  712. if(ba->shot())
  713. {
  714. for(std::vector<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  715. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked), i->stackAttacked);
  716. }
  717. else
  718. {//WARNING: does not support multiple attacked creatures
  719. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  720. int shift = 0;
  721. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  722. {
  723. int distp = BattleInfo::getDistance(curAction->destinationTile + 1, attacker->position);
  724. int distm = BattleInfo::getDistance(curAction->destinationTile - 1, attacker->position);
  725. if( distp < distm )
  726. shift = 1;
  727. else
  728. shift = -1;
  729. }
  730. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, ba->bsa.begin()->stackAttacked );
  731. }
  732. }
  733. void CPlayerInterface::showComp(SComponent comp)
  734. {
  735. boost::unique_lock<boost::recursive_mutex> un(*pim);
  736. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  737. adventureInt->infoBar.showComp(&comp,4000);
  738. }
  739. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  740. {
  741. std::vector<SComponent*> intComps;
  742. for(int i=0;i<components.size();i++)
  743. intComps.push_back(new SComponent(*components[i]));
  744. showInfoDialog(text,intComps,soundID);
  745. }
  746. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID, bool delComps)
  747. {
  748. waitWhileDialog();
  749. boost::unique_lock<boost::recursive_mutex> un(*pim);
  750. stopMovement();
  751. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  752. temp->setDelComps(delComps);
  753. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  754. {
  755. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  756. showingDialog->set(true);
  757. GH.pushInt(temp);
  758. }
  759. else
  760. {
  761. dialogs.push_back(temp);
  762. }
  763. }
  764. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  765. {
  766. boost::unique_lock<boost::recursive_mutex> un(*pim);
  767. stopMovement();
  768. LOCPLINT->showingDialog->setn(true);
  769. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  770. }
  771. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  772. {
  773. waitWhileDialog();
  774. boost::unique_lock<boost::recursive_mutex> un(*pim);
  775. stopMovement();
  776. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  777. if(!selection && cancel) //simple yes/no dialog
  778. {
  779. std::vector<SComponent*> intComps;
  780. for(int i=0;i<components.size();i++)
  781. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  782. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  783. }
  784. else if(selection)
  785. {
  786. std::vector<CSelectableComponent*> intComps;
  787. for(int i=0;i<components.size();i++)
  788. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  789. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  790. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  791. if(cancel)
  792. {
  793. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  794. }
  795. int charperline = 35;
  796. if (pom.size() > 1)
  797. charperline = 50;
  798. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  799. GH.pushInt(temp);
  800. intComps[0]->clickLeft(true, false);
  801. }
  802. else //only accept
  803. {
  804. std::vector<SComponent*> intComps;
  805. for(int i=0;i<components.size();i++)
  806. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  807. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),NULL,true);
  808. }
  809. }
  810. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  811. {
  812. boost::unique_lock<boost::recursive_mutex> un(*pim);
  813. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  814. adventureInt->minimap.showTile(*i);
  815. if(pos.size())
  816. GH.totalRedraw();
  817. }
  818. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  819. {
  820. boost::unique_lock<boost::recursive_mutex> un(*pim);
  821. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  822. adventureInt->minimap.hideTile(*i);
  823. if(pos.size())
  824. GH.totalRedraw();
  825. }
  826. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  827. {
  828. boost::unique_lock<boost::recursive_mutex> un(*pim);
  829. adventureInt->heroWindow->setHero(hero);
  830. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  831. GH.pushInt(adventureInt->heroWindow);
  832. }
  833. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  834. {
  835. boost::unique_lock<boost::recursive_mutex> un(*pim);
  836. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  837. {
  838. adventureInt->heroWindow->deactivate();
  839. adventureInt->heroWindow->setHero(hero);
  840. adventureInt->heroWindow->activate();
  841. }
  842. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  843. {
  844. cew->deactivate();
  845. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  846. {
  847. if(cew->heroInst[g]->id == hero->id)
  848. {
  849. cew->heroInst[g] = hero;
  850. cew->artifs[g]->updateState = true;
  851. cew->artifs[g]->setHero(hero);
  852. cew->artifs[g]->updateState = false;
  853. }
  854. }
  855. cew->prepareBackground();
  856. cew->activate();
  857. }
  858. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  859. {
  860. if(caw->arts)
  861. {
  862. caw->deactivate();
  863. caw->arts->updateState = true;
  864. caw->arts->setHero(hero);
  865. caw->arts->updateState = false;
  866. caw->activate();
  867. }
  868. }
  869. updateInfo(hero);
  870. }
  871. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  872. {
  873. boost::unique_lock<boost::recursive_mutex> un(*pim);
  874. if(castleInt && town->ID == TOWNI_TYPE)
  875. {
  876. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  877. if(fs)
  878. fs->draw(castleInt,false);
  879. }
  880. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  881. {
  882. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  883. if(crw)
  884. crw->initCres();
  885. }
  886. }
  887. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  888. {
  889. if(bonus.type == Bonus::NONE) return;
  890. boost::unique_lock<boost::recursive_mutex> un(*pim);
  891. updateInfo(hero);
  892. if (bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING && !gain)
  893. {
  894. //recalculate paths because hero has lost bonus influencing pathfinding
  895. cb->recalculatePaths();
  896. eraseCurrentPathOf(hero, false);
  897. }
  898. }
  899. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  900. {
  901. h & playerID;
  902. h & sysOpts;
  903. h & spellbookSettings;
  904. }
  905. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  906. {
  907. serializeTempl(h,version);
  908. }
  909. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  910. {
  911. serializeTempl(h,version);
  912. sysOpts.apply();
  913. firstCall = -1;
  914. }
  915. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  916. {
  917. if (!h)
  918. return false; //can't find hero
  919. pim->unlock();
  920. bool result = false;
  921. {
  922. path.convert(0);
  923. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  924. stillMoveHero.data = CONTINUE_MOVE;
  925. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  926. enum TerrainTile::EterrainType newTerrain;
  927. int sh = -1;
  928. const TerrainTile * curTile = cb->getTileInfo(CGHeroInstance::convertPosition(h->pos, false));
  929. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  930. {
  931. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  932. if(path.nodes[i-1].turns)
  933. {
  934. stillMoveHero.data = STOP_MOVE;
  935. break;
  936. }
  937. // Start a new sound for the hero movement or let the existing one carry on.
  938. #if 0
  939. // TODO
  940. if (hero is flying && sh == -1)
  941. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  942. #endif
  943. {
  944. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  945. if (newTerrain != currentTerrain) {
  946. CGI->soundh->stopSound(sh);
  947. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  948. currentTerrain = newTerrain;
  949. }
  950. }
  951. stillMoveHero.data = WAITING_MOVE;
  952. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  953. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  954. cb->moveHero(h,endpos);
  955. curTile = cb->getTileInfo(endpos);
  956. eventsM.unlock();
  957. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  958. stillMoveHero.cond.wait(un);
  959. eventsM.lock();
  960. if (guarded) // Abort movement if a guard was fought.
  961. break;
  962. }
  963. CGI->soundh->stopSound(sh);
  964. cb->recalculatePaths();
  965. }
  966. pim->lock();
  967. return result;
  968. }
  969. bool CPlayerInterface::shiftPressed() const
  970. {
  971. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  972. }
  973. bool CPlayerInterface::altPressed() const
  974. {
  975. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  976. }
  977. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  978. {
  979. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  980. return;
  981. {
  982. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  983. while(showingDialog->data)
  984. showingDialog->cond.wait(un);
  985. }
  986. boost::unique_lock<boost::recursive_mutex> un(*pim);
  987. while(dialogs.size())
  988. {
  989. pim->unlock();
  990. SDL_Delay(20);
  991. pim->lock();
  992. }
  993. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  994. cgw->quit->callback += onEnd;
  995. GH.pushInt(cgw);
  996. }
  997. /**
  998. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  999. * into a combinational one on an artifact screen. Does not require the combination of
  1000. * artifacts to be legal.
  1001. * @param artifactID ID of a constituent artifact.
  1002. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1003. * is false.
  1004. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1005. */
  1006. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1007. {
  1008. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1009. std::string text = artifact.Description();
  1010. text += "\n\n";
  1011. std::vector<SComponent*> scs;
  1012. if (assemble) {
  1013. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1014. // You possess all of the components to...
  1015. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1016. // Picture of assembled artifact at bottom.
  1017. SComponent* sc = new SComponent;
  1018. sc->type = SComponent::artifact;
  1019. sc->subtype = assembledArtifact.id;
  1020. sc->description = assembledArtifact.Description();
  1021. sc->subtitle = assembledArtifact.Name();
  1022. scs.push_back(sc);
  1023. } else {
  1024. // Do you wish to disassemble this artifact?
  1025. text += CGI->generaltexth->allTexts[733];
  1026. }
  1027. showYesNoDialog(text, scs, onYes, onNo, true);
  1028. }
  1029. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1030. {
  1031. if(stillMoveHero.get() == DURING_MOVE)
  1032. stillMoveHero.setn(CONTINUE_MOVE);
  1033. }
  1034. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1035. {
  1036. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1037. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1038. }
  1039. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1040. {
  1041. //redraw minimap if owner changed
  1042. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1043. if(sop->what == ObjProperty::OWNER)
  1044. {
  1045. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1046. std::set<int3> pos = obj->getBlockedPos();
  1047. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1048. {
  1049. if(cb->isVisible(*it))
  1050. adventureInt->minimap.showTile(*it);
  1051. }
  1052. if(obj->ID == TOWNI_TYPE)
  1053. if(obj->tempOwner == playerID)
  1054. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1055. else
  1056. towns -= obj;
  1057. assert(cb->getTownsInfo().size() == towns.size());
  1058. }
  1059. }
  1060. void CPlayerInterface::recreateHeroTownList()
  1061. {
  1062. wanderingHeroes.clear();
  1063. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1064. for(size_t i = 0; i < heroes.size(); i++)
  1065. if(!heroes[i]->inTownGarrison)
  1066. wanderingHeroes.push_back(heroes[i]);
  1067. towns.clear();
  1068. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1069. for(size_t i = 0; i < townInfo.size(); i++)
  1070. towns.push_back(townInfo[i]);
  1071. }
  1072. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1073. {
  1074. if(pos < 0 || pos >= wanderingHeroes.size())
  1075. return NULL;
  1076. return wanderingHeroes[pos];
  1077. }
  1078. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1079. {
  1080. waitWhileDialog();
  1081. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1082. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1083. GH.pushInt(cr);
  1084. }
  1085. void CPlayerInterface::waitWhileDialog()
  1086. {
  1087. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1088. while(showingDialog->data)
  1089. showingDialog->cond.wait(un);
  1090. }
  1091. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1092. {
  1093. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1094. int state = obj->state();
  1095. std::vector<si32> cost;
  1096. obj->getBoatCost(cost);
  1097. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1098. GH.pushInt(csw);
  1099. }
  1100. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1101. {
  1102. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1103. CGI->mh->printObject(obj);
  1104. //we might have built a boat in shipyard in opened town screen
  1105. if(obj->ID == 8
  1106. && LOCPLINT->castleInt
  1107. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1108. {
  1109. CGI->soundh->playSound(soundBase::newBuilding);
  1110. LOCPLINT->castleInt->recreateBuildings();
  1111. }
  1112. }
  1113. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1114. {
  1115. adventureInt->centerOn (pos);
  1116. if(focusTime)
  1117. {
  1118. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1119. if(activeAdv)
  1120. adventureInt->deactivate();
  1121. SDL_Delay(focusTime);
  1122. if(activeAdv)
  1123. adventureInt->activate();
  1124. }
  1125. }
  1126. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1127. {
  1128. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1129. {
  1130. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1131. heroKilled(h);
  1132. }
  1133. }
  1134. bool CPlayerInterface::ctrlPressed() const
  1135. {
  1136. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1137. }
  1138. void CPlayerInterface::update()
  1139. {
  1140. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1141. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1142. if(terminate_cond.get())
  1143. return;
  1144. //if there are any waiting dialogs, show them
  1145. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1146. {
  1147. showingDialog->set(true);
  1148. GH.pushInt(dialogs.front());
  1149. dialogs.pop_front();
  1150. }
  1151. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1152. if(!adventureInt->selection && GH.topInt() == adventureInt)
  1153. return;
  1154. GH.updateTime();
  1155. GH.handleEvents();
  1156. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1157. GH.totalRedraw();
  1158. else
  1159. GH.simpleRedraw();
  1160. CGI->curh->draw1();
  1161. CSDL_Ext::update(screen);
  1162. CGI->curh->draw2();
  1163. pim->unlock();
  1164. SDL_framerateDelay(mainFPSmng);
  1165. }
  1166. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1167. {
  1168. using namespace boost::filesystem;
  1169. using namespace boost::algorithm;
  1170. std::map<std::time_t, int> dates; //save number => datestamp
  1171. directory_iterator enddir;
  1172. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1173. {
  1174. if(is_regular(dir->status()))
  1175. {
  1176. std::string name = dir->path().leaf();
  1177. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1178. {
  1179. char nr = name[namePrefix.size()];
  1180. if(std::isdigit(nr))
  1181. {
  1182. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1183. }
  1184. }
  1185. }
  1186. }
  1187. if(dates.size())
  1188. return (--dates.end())->second; //return latest file number
  1189. return 0;
  1190. }
  1191. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1192. {
  1193. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1194. {
  1195. //ho->moveDir = 1;
  1196. ho->isStanding = false;
  1197. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1198. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1199. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1200. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1201. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1202. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1203. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1204. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1205. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1206. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1207. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1208. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1209. 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);
  1210. 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);
  1211. 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);
  1212. 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);
  1213. 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);
  1214. 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);
  1215. }
  1216. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1217. {
  1218. //ho->moveDir = 2;
  1219. ho->isStanding = false;
  1220. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1221. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1222. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1223. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1224. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1225. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1226. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1227. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1228. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1229. 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);
  1230. 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);
  1231. 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);
  1232. }
  1233. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1234. {
  1235. //ho->moveDir = 3;
  1236. ho->isStanding = false;
  1237. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1238. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1239. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1240. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1241. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1242. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1243. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1244. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1245. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1246. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1247. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1248. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1249. 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);
  1250. 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);
  1251. 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);
  1252. 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);
  1253. 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);
  1254. 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);
  1255. }
  1256. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1257. {
  1258. //ho->moveDir = 4;
  1259. ho->isStanding = false;
  1260. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1261. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1262. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1263. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1264. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1265. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1266. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1267. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1268. 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);
  1269. 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);
  1270. }
  1271. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1272. {
  1273. //ho->moveDir = 5;
  1274. ho->isStanding = false;
  1275. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1276. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1277. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1278. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1279. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1280. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1281. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1282. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1283. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1284. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1285. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1286. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1287. 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);
  1288. 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);
  1289. 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);
  1290. 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);
  1291. 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);
  1292. 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);
  1293. }
  1294. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1295. {
  1296. //ho->moveDir = 6;
  1297. ho->isStanding = false;
  1298. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1299. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1300. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1301. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1302. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1303. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1304. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1305. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1306. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1307. 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);
  1308. 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);
  1309. 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);
  1310. }
  1311. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1312. {
  1313. //ho->moveDir = 7;
  1314. ho->isStanding = false;
  1315. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1316. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1317. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1318. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1319. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1320. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1321. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1322. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1323. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1324. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1325. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1326. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1327. 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);
  1328. 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);
  1329. 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);
  1330. 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);
  1331. 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);
  1332. 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);
  1333. }
  1334. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1335. {
  1336. //ho->moveDir = 8;
  1337. ho->isStanding = false;
  1338. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1339. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1340. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1341. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1342. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1343. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1344. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1345. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1346. 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);
  1347. 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);
  1348. }
  1349. }
  1350. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1351. {
  1352. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1353. {
  1354. //setting advmap shift
  1355. adventureInt->terrain.moveX = i-32;
  1356. adventureInt->terrain.moveY = i-32;
  1357. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1358. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1359. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1360. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1361. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1362. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1363. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1364. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1365. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1366. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1367. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1368. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1369. }
  1370. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1371. {
  1372. //setting advmap shift
  1373. adventureInt->terrain.moveY = i-32;
  1374. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1375. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1376. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1377. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1378. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1379. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1380. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1381. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1382. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1383. }
  1384. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1385. {
  1386. //setting advmap shift
  1387. adventureInt->terrain.moveX = -i+32;
  1388. adventureInt->terrain.moveY = i-32;
  1389. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1390. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1391. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1392. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1393. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1394. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1395. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1396. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1397. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1398. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1399. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1400. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1401. }
  1402. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1403. {
  1404. //setting advmap shift
  1405. adventureInt->terrain.moveX = -i+32;
  1406. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1407. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1408. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1409. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1410. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1411. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1412. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1413. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1414. }
  1415. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1416. {
  1417. //setting advmap shift
  1418. adventureInt->terrain.moveX = -i+32;
  1419. adventureInt->terrain.moveY = -i+32;
  1420. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1421. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1422. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1423. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1424. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1425. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1426. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1427. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1428. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1429. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1430. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1431. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1432. }
  1433. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1434. {
  1435. //setting advmap shift
  1436. adventureInt->terrain.moveY = -i+32;
  1437. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1438. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1439. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1440. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1441. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1442. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1443. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1444. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1445. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1446. }
  1447. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1448. {
  1449. //setting advmap shift
  1450. adventureInt->terrain.moveX = i-32;
  1451. adventureInt->terrain.moveY = -i+32;
  1452. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1453. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1454. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1455. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1456. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1457. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1458. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1459. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1460. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1461. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1462. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1463. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1464. }
  1465. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1466. {
  1467. //setting advmap shift
  1468. adventureInt->terrain.moveX = i-32;
  1469. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1470. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1471. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1472. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1473. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1474. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1475. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1476. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1477. }
  1478. }
  1479. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1480. {
  1481. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1482. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1483. {
  1484. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1485. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1486. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1487. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1488. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1489. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1490. }
  1491. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1492. {
  1493. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1494. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1495. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1496. }
  1497. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1498. {
  1499. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1500. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1501. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1502. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1503. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1504. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1505. }
  1506. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1507. {
  1508. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1509. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1510. }
  1511. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1512. {
  1513. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1514. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1515. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1516. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1517. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1518. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1519. }
  1520. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1521. {
  1522. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1523. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1524. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1525. }
  1526. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1527. {
  1528. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1529. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1530. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1531. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1532. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1533. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1534. }
  1535. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1536. {
  1537. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1538. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1539. }
  1540. //restoring good rects
  1541. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1542. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1543. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1544. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1545. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1546. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1547. //restoring good order of objects
  1548. 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);
  1549. 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);
  1550. 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);
  1551. 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);
  1552. 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);
  1553. 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);
  1554. }
  1555. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1556. {
  1557. if(LOCPLINT != this)
  1558. return;
  1559. if(player == playerID)
  1560. {
  1561. if(!victory)
  1562. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1563. // else
  1564. // showInfoDialog("Placeholder message: you won!");
  1565. makingTurn = true;
  1566. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1567. makingTurn = false;
  1568. howManyPeople--;
  1569. if(!howManyPeople) //all human players eliminated
  1570. {
  1571. if(cb->getStartInfo()->mode != StartInfo::CAMPAIGN)
  1572. requestReturningToMainMenu();
  1573. //TODO next campaign scenario
  1574. }
  1575. }
  1576. else
  1577. {
  1578. if(!victory) //enemy has lost
  1579. {
  1580. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1581. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1582. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1583. }
  1584. }
  1585. }
  1586. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1587. {
  1588. }
  1589. void CPlayerInterface::showPuzzleMap()
  1590. {
  1591. waitWhileDialog();
  1592. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1593. //TODO: interface should not know the real position of Grail...
  1594. float ratio = 0;
  1595. int3 grailPos = cb->getGrailPos(ratio);
  1596. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1597. }
  1598. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1599. {
  1600. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1601. {
  1602. cb->recalculatePaths();
  1603. eraseCurrentPathOf(caster, false);
  1604. }
  1605. }
  1606. void SystemOptions::setMusicVolume( int newVolume )
  1607. {
  1608. musicVolume = newVolume;
  1609. CGI->musich->setVolume(newVolume);
  1610. settingsChanged();
  1611. }
  1612. void SystemOptions::setSoundVolume( int newVolume )
  1613. {
  1614. soundVolume = newVolume;
  1615. CGI->soundh->setVolume(newVolume);
  1616. settingsChanged();
  1617. }
  1618. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1619. {
  1620. heroMoveSpeed = newSpeed;
  1621. settingsChanged();
  1622. }
  1623. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1624. {
  1625. mapScrollingSpeed = newSpeed;
  1626. settingsChanged();
  1627. }
  1628. void SystemOptions::settingsChanged()
  1629. {
  1630. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1631. if(settings.sfile)
  1632. settings << *this;
  1633. else
  1634. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1635. }
  1636. void SystemOptions::apply()
  1637. {
  1638. if(CGI->musich->getVolume() != musicVolume)
  1639. CGI->musich->setVolume(musicVolume);
  1640. if(CGI->soundh->getVolume() != soundVolume)
  1641. CGI->soundh->setVolume(soundVolume);
  1642. settingsChanged();
  1643. }
  1644. SystemOptions::SystemOptions()
  1645. {
  1646. heroMoveSpeed = 2;
  1647. mapScrollingSpeed = 2;
  1648. musicVolume = 88;
  1649. soundVolume = 88;
  1650. printCellBorders = true;
  1651. printStackRange = true;
  1652. animSpeed = 2;
  1653. printMouseShadow = true;
  1654. showQueue = true;
  1655. }
  1656. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1657. {
  1658. if(checkForExistanceOfPath)
  1659. {
  1660. assert(vstd::contains(paths, ho));
  1661. }
  1662. else if (!vstd::contains(paths, ho))
  1663. {
  1664. return;
  1665. }
  1666. assert(ho == adventureInt->selection);
  1667. paths.erase(ho);
  1668. adventureInt->terrain.currentPath = NULL;
  1669. }
  1670. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1671. {
  1672. if(vstd::contains(paths,h)) //hero has assigned path
  1673. {
  1674. CGPath &path = paths[h];
  1675. if(!path.nodes.size())
  1676. {
  1677. tlog3 << "Warning: empty path found...\n";
  1678. paths.erase(h);
  1679. }
  1680. else
  1681. {
  1682. assert(h->getPosition(false) == path.startPos());
  1683. //update the hero path in case of something has changed on map
  1684. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1685. return &path;
  1686. else
  1687. paths.erase(h);
  1688. }
  1689. }
  1690. return NULL;
  1691. }
  1692. void CPlayerInterface::acceptTurn()
  1693. {
  1694. waitWhileDialog();
  1695. if(howManyPeople > 1)
  1696. adventureInt->startTurn();
  1697. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1698. /* TODO: This isn't quite right. First day in game should play
  1699. * NEWDAY. And we don't play NEWMONTH. */
  1700. int day = cb->getDate(1);
  1701. if (day != 1)
  1702. CGI->soundh->playSound(soundBase::newDay);
  1703. else
  1704. CGI->soundh->playSound(soundBase::newWeek);
  1705. adventureInt->infoBar.newDay(day);
  1706. //select first hero if available.
  1707. //TODO: check if hero is slept
  1708. if(wanderingHeroes.size())
  1709. adventureInt->select(wanderingHeroes.front());
  1710. else
  1711. adventureInt->select(towns.front());
  1712. adventureInt->showAll(screen);
  1713. }
  1714. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1715. {
  1716. std::string hlp;
  1717. if(h->movement < h->maxMovePoints(true))
  1718. showInfoDialog(CGI->generaltexth->allTexts[56]); //"Digging for artifacts requires a whole day, try again tomorrow."
  1719. else if(cb->getTileInfo(h->getPosition(false))->tertype == TerrainTile::water)
  1720. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1721. else
  1722. {
  1723. const TerrainTile *t = cb->getTileInfo(h->getPosition());
  1724. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1725. if(hlp.length() || t->blockingObjects.size() > 1)
  1726. showInfoDialog(CGI->generaltexth->allTexts[97]); //Try searching on clear ground.
  1727. else
  1728. cb->dig(h);
  1729. }
  1730. }
  1731. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1732. {
  1733. adventureInt->infoBar.updateSelection(specific);
  1734. // if (adventureInt->selection == specific)
  1735. // adventureInt->infoBar.showAll(screen);
  1736. }
  1737. void CPlayerInterface::battleNewRoundFirst( int round )
  1738. {
  1739. if(LOCPLINT != this)
  1740. { //another local interface should do this
  1741. return;
  1742. }
  1743. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1744. battleInt->newRoundFirst(round);
  1745. }
  1746. void CPlayerInterface::stopMovement()
  1747. {
  1748. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1749. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1750. }
  1751. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1752. {
  1753. if(market->o->ID == 2) //Altar
  1754. {
  1755. EMarketMode mode = market->availableModes().front();
  1756. if(market->allowsTrade(ARTIFACT_EXP) && visitor->getAlignment() != EVIL)
  1757. GH.pushInt(new CAltarWindow(market, visitor, ARTIFACT_EXP));
  1758. else if(market->allowsTrade(CREATURE_EXP) && visitor->getAlignment() != GOOD)
  1759. GH.pushInt(new CAltarWindow(market, visitor, CREATURE_EXP));
  1760. }
  1761. else
  1762. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1763. }
  1764. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1765. {
  1766. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1767. GH.pushInt(cuw);
  1768. }
  1769. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  1770. {
  1771. CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
  1772. GH.pushInt(chfw);
  1773. }
  1774. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1775. {
  1776. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1777. cmw->artifactsChanged(false);
  1778. }
  1779. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1780. {
  1781. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1782. GH.pushInt(tv);
  1783. }
  1784. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1785. {
  1786. if(obj->state())
  1787. {
  1788. MetaString txt;
  1789. obj->getProblemText(txt);
  1790. showInfoDialog(txt.toString());
  1791. }
  1792. else
  1793. showShipyardDialog(obj);
  1794. }
  1795. void CPlayerInterface::requestReturningToMainMenu()
  1796. {
  1797. SDL_Event event;
  1798. event.type = SDL_USEREVENT;
  1799. event.user.code = 2;
  1800. SDL_PushEvent(&event);
  1801. }
  1802. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  1803. {
  1804. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  1805. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  1806. }