2
0

CPlayerInterface.cpp 67 KB

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