CPlayerInterface.cpp 69 KB

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