CPlayerInterface.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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/algorithm/string.hpp>
  35. #include <boost/algorithm/string/replace.hpp>
  36. #include <boost/assign/std/vector.hpp>
  37. #include <boost/assign/list_of.hpp>
  38. #include <boost/date_time/posix_time/posix_time.hpp>
  39. #include <boost/thread.hpp>
  40. #include <cmath>
  41. #include <queue>
  42. #include <sstream>
  43. #include <boost/filesystem.hpp>
  44. #ifdef min
  45. #undef min
  46. #endif
  47. #ifdef max
  48. #undef max
  49. #endif
  50. /*
  51. * CPlayerInterface.cpp, part of VCMI engine
  52. *
  53. * Authors: listed in file AUTHORS in main folder
  54. *
  55. * License: GNU General Public License v2.0 or later
  56. * Full text of license available in license.txt file, in main folder
  57. *
  58. */
  59. using namespace boost::assign;
  60. using namespace CSDL_Ext;
  61. void processCommand(const std::string &message, CClient *&client);
  62. extern std::queue<SDL_Event*> events;
  63. extern boost::mutex eventsM;
  64. CPlayerInterface * LOCPLINT;
  65. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  66. CondSh<EMoveState> stillMoveHero; //used during hero movement
  67. struct OCM_HLP_CGIN
  68. {
  69. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  70. {
  71. return (*a.first)<(*b.first);
  72. }
  73. } ocmptwo_cgin ;
  74. CPlayerInterface::CPlayerInterface(int Player, int serial)
  75. {
  76. GH.defActionsDef = 0;
  77. LOCPLINT = this;
  78. curAction = NULL;
  79. playerID=Player;
  80. serialID=serial;
  81. human=true;
  82. castleInt = NULL;
  83. adventureInt = NULL;
  84. battleInt = NULL;
  85. pim = new boost::recursive_mutex;
  86. makingTurn = false;
  87. showingDialog = new CondSh<bool>(false);
  88. sysOpts = GDefaultOptions;
  89. //initializing framerate keeper
  90. mainFPSmng = new FPSmanager;
  91. SDL_initFramerate(mainFPSmng);
  92. SDL_setFramerate(mainFPSmng, 48);
  93. //framerate keeper initialized
  94. cingconsole = new CInGameConsole;
  95. terminate_cond.set(false);
  96. firstCall = 1; //if loading will be overwritten in serialize
  97. autosaveCount = 0;
  98. }
  99. CPlayerInterface::~CPlayerInterface()
  100. {
  101. delete pim;
  102. delete showingDialog;
  103. delete mainFPSmng;
  104. if(adventureInt->active & CIntObject::KEYBOARD)
  105. adventureInt->deactivateKeys();
  106. delete adventureInt;
  107. delete cingconsole;
  108. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
  109. SDL_FreeSurface(i->second);
  110. graphics->heroWins.clear();
  111. for(std::map<int,SDL_Surface*>::iterator i=graphics->townWins.begin(); i!= graphics->townWins.end(); i++)
  112. SDL_FreeSurface(i->second);
  113. graphics->townWins.clear();
  114. LOCPLINT = NULL;
  115. }
  116. void CPlayerInterface::init(ICallback * CB)
  117. {
  118. cb = dynamic_cast<CCallback*>(CB);
  119. adventureInt = new CAdvMapInt(playerID);
  120. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  121. for(int i=0;i<tt.size();i++)
  122. {
  123. SDL_Surface * pom = infoWin(tt[i]);
  124. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  125. }
  126. recreateWanderingHeroes();
  127. }
  128. void CPlayerInterface::yourTurn()
  129. {
  130. LOCPLINT = this;
  131. makingTurn = true;
  132. if(firstCall)
  133. {
  134. autosaveCount = getLastIndex("Autosave_");
  135. GH.pushInt(adventureInt);
  136. adventureInt->activateKeys();
  137. if(firstCall > 0) //new game, not laoded
  138. {
  139. int index = getLastIndex("Newgame_Autosave_");
  140. index %= SAVES_COUNT;
  141. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  142. }
  143. firstCall = 0;
  144. }
  145. else
  146. {
  147. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  148. autosaveCount %= 5;
  149. }
  150. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  151. SDL_FreeSurface(i->second);
  152. graphics->heroWins.clear();
  153. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  154. for(int i=0;i<hh.size();i++)
  155. {
  156. SDL_Surface * pom = infoWin(hh[i]);
  157. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  158. }
  159. /* TODO: This isn't quite right. First day in game should play
  160. * NEWDAY. And we don't play NEWMONTH. */
  161. int day = cb->getDate(1);
  162. if (day != 1)
  163. CGI->soundh->playSound(soundBase::newDay);
  164. else
  165. CGI->soundh->playSound(soundBase::newWeek);
  166. adventureInt->infoBar.newDay(day);
  167. //select first hero if available.
  168. //TODO: check if hero is slept
  169. if(wanderingHeroes.size())
  170. adventureInt->select(wanderingHeroes[0]);
  171. else
  172. adventureInt->select(adventureInt->townList.items[0]);
  173. adventureInt->showAll(screen);
  174. GH.curInt = this;
  175. }
  176. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  177. {
  178. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  179. for(int h=0; h<hlp.objects.size(); ++h)
  180. if(hlp.objects[h].first->id==hid)
  181. {
  182. hlp.objects[h].second = r;
  183. return;
  184. }
  185. }
  186. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  187. {
  188. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  189. for(int h=0; h<hlp.objects.size(); ++h)
  190. if(hlp.objects[h].first->id==hid)
  191. {
  192. hlp.objects.erase(hlp.objects.begin()+h);
  193. return;
  194. }
  195. }
  196. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  197. {
  198. boost::unique_lock<boost::recursive_mutex> un(*pim);
  199. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  200. int3 hp = details.start;
  201. adventureInt->centerOn(ho->pos); //actualizing screen pos
  202. adventureInt->minimap.draw(screen2);
  203. adventureInt->heroList.draw(screen2);
  204. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  205. {
  206. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  207. {
  208. adventureInt->eraseCurrentPathOf(ho);
  209. return; //teleport - no fancy moving animation
  210. //TODO: smooth disappear / appear effect
  211. }
  212. if (details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED) //hero didn't change tile but visit succeeded
  213. {
  214. adventureInt->eraseCurrentPathOf(ho);
  215. }
  216. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  217. {
  218. //remove one node from the path (the one we went)
  219. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  220. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  221. adventureInt->eraseCurrentPathOf(ho);
  222. }
  223. }
  224. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  225. {
  226. ho->isStanding = true;
  227. stillMoveHero.setn(STOP_MOVE);
  228. GH.totalRedraw();
  229. return;
  230. }
  231. initMovement(details, ho, hp);
  232. //first initializing done
  233. SDL_framerateDelay(mainFPSmng); // after first move
  234. //main moving
  235. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  236. {
  237. movementPxStep(details, i, hp, ho);
  238. adventureInt->updateScreen = true;
  239. adventureInt->show(screen);
  240. CSDL_Ext::update(screen);
  241. SDL_framerateDelay(mainFPSmng); //for animation purposes
  242. } //for(int i=1; i<32; i+=4)
  243. //main moving done
  244. //finishing move
  245. finishMovement(details, hp, ho);
  246. ho->isStanding = true;
  247. //move finished
  248. adventureInt->minimap.draw(screen2);
  249. adventureInt->heroList.updateMove(ho);
  250. //check if user cancelled movement
  251. {
  252. boost::unique_lock<boost::mutex> un(eventsM);
  253. while(events.size())
  254. {
  255. SDL_Event *ev = events.front();
  256. events.pop();
  257. switch(ev->type)
  258. {
  259. case SDL_MOUSEBUTTONDOWN:
  260. stillMoveHero.setn(STOP_MOVE);
  261. break;
  262. case SDL_KEYDOWN:
  263. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  264. stillMoveHero.setn(STOP_MOVE);
  265. break;
  266. }
  267. delete ev;
  268. }
  269. }
  270. if(stillMoveHero.get() == WAITING_MOVE)
  271. stillMoveHero.setn(DURING_MOVE);
  272. }
  273. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  274. {
  275. boost::unique_lock<boost::recursive_mutex> un(*pim);
  276. graphics->heroWins.erase(hero->ID);
  277. wanderingHeroes -= hero;
  278. adventureInt->heroList.updateHList(hero);
  279. }
  280. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  281. {
  282. boost::unique_lock<boost::recursive_mutex> un(*pim);
  283. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  284. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  285. wanderingHeroes.push_back(hero);
  286. adventureInt->heroList.updateHList();
  287. }
  288. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  289. {
  290. castleInt = new CCastleInterface(town);
  291. GH.pushInt(castleInt);
  292. }
  293. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  294. {
  295. if (specific)
  296. {
  297. switch (specific->ID)
  298. {
  299. case HEROI_TYPE:
  300. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  301. break;
  302. case TOWNI_TYPE:
  303. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  304. break;
  305. default:
  306. return NULL;
  307. break;
  308. }
  309. }
  310. else
  311. {
  312. switch (adventureInt->selection->ID)
  313. {
  314. case HEROI_TYPE:
  315. {
  316. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  317. return graphics->drawHeroInfoWin(curh);
  318. }
  319. case TOWNI_TYPE:
  320. {
  321. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  322. }
  323. default:
  324. tlog1 << "Strange... selection is neither hero nor town\n";
  325. return NULL;
  326. }
  327. }
  328. }
  329. int3 CPlayerInterface::repairScreenPos(int3 pos)
  330. {
  331. if(pos.x<-CGI->mh->frameW)
  332. pos.x = -CGI->mh->frameW;
  333. if(pos.y<-CGI->mh->frameH)
  334. pos.y = -CGI->mh->frameH;
  335. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW)
  336. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW;
  337. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH)
  338. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH;
  339. return pos;
  340. }
  341. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  342. {
  343. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  344. return;
  345. boost::unique_lock<boost::recursive_mutex> un(*pim);
  346. redrawHeroWin(hero);
  347. }
  348. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  349. {
  350. boost::unique_lock<boost::recursive_mutex> un(*pim);
  351. redrawHeroWin(hero);
  352. }
  353. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  354. {
  355. boost::unique_lock<boost::recursive_mutex> un(*pim);
  356. //adventureInt->heroList.draw();
  357. }
  358. void CPlayerInterface::receivedResource(int type, int val)
  359. {
  360. boost::unique_lock<boost::recursive_mutex> un(*pim);
  361. GH.totalRedraw();
  362. }
  363. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  364. {
  365. waitWhileDialog();
  366. CGI->soundh->playSound(soundBase::heroNewLevel);
  367. boost::unique_lock<boost::recursive_mutex> un(*pim);
  368. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  369. GH.pushInt(lw);
  370. }
  371. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  372. {
  373. boost::unique_lock<boost::recursive_mutex> un(*pim);
  374. //redraw infowindow
  375. SDL_FreeSurface(graphics->townWins[town->id]);
  376. graphics->townWins[town->id] = infoWin(town);
  377. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  378. {
  379. CGI->mh->hideObject(town->garrisonHero);
  380. wanderingHeroes -= town->garrisonHero;
  381. }
  382. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  383. {
  384. CGI->mh->printObject(town->visitingHero);
  385. wanderingHeroes.push_back(town->visitingHero);
  386. }
  387. //adventureInt->heroList.updateHList();
  388. CCastleInterface *c = castleInt;
  389. if(c)
  390. {
  391. c->garr->highlighted = NULL;
  392. c->hslotup.hero = town->garrisonHero;
  393. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  394. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  395. c->garr->recreateSlots();
  396. }
  397. GH.totalRedraw();
  398. }
  399. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  400. {
  401. if(hero->tempOwner != town->tempOwner)
  402. return;
  403. boost::unique_lock<boost::recursive_mutex> un(*pim);
  404. openTownWindow(town);
  405. }
  406. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  407. {
  408. boost::unique_lock<boost::recursive_mutex> un(*pim);
  409. if(obj->ID == HEROI_TYPE) //hero
  410. {
  411. const CGHeroInstance * hh;
  412. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  413. {
  414. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  415. graphics->heroWins[hh->subID] = infoWin(hh);
  416. }
  417. }
  418. else if (obj->ID == TOWNI_TYPE) //town
  419. {
  420. const CGTownInstance * tt;
  421. if(tt = static_cast<const CGTownInstance*>(obj))
  422. {
  423. SDL_FreeSurface(graphics->townWins[tt->id]);
  424. graphics->townWins[tt->id] = infoWin(tt);
  425. }
  426. if(tt->visitingHero)
  427. {
  428. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  429. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  430. }
  431. }
  432. bool wasGarrison = false;
  433. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  434. {
  435. if((*i)->type & IShowActivable::WITH_GARRISON)
  436. {
  437. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  438. wwg->garr->recreateSlots();
  439. wasGarrison = true;
  440. }
  441. else
  442. {//a cheat for Kingdom Overview window (it has CWindowWithGarrison-childrens which are not present in ListInt)
  443. CKingdomInterface *cki = dynamic_cast<CKingdomInterface*>(*i);//need to create "Garrison Holder" class thingy
  444. if (cki)
  445. cki->updateAllGarrisons();
  446. }
  447. }
  448. GH.totalRedraw();
  449. }
  450. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  451. {
  452. boost::unique_lock<boost::recursive_mutex> un(*pim);
  453. switch (buildingID)
  454. {
  455. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  456. {
  457. SDL_FreeSurface(graphics->townWins[town->id]);
  458. graphics->townWins[town->id] = infoWin(town);
  459. break;
  460. }
  461. }
  462. if(!castleInt)
  463. return;
  464. if(castleInt->town!=town)
  465. return;
  466. switch(what)
  467. {
  468. case 1:
  469. CGI->soundh->playSound(soundBase::newBuilding);
  470. castleInt->addBuilding(buildingID);
  471. break;
  472. case 2:
  473. castleInt->removeBuilding(buildingID);
  474. break;
  475. }
  476. }
  477. void CPlayerInterface::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) //called by engine when battle starts; side=0 - left, side=1 - right
  478. {
  479. while(showingDialog->get())
  480. SDL_Delay(20);
  481. boost::unique_lock<boost::recursive_mutex> un(*pim);
  482. /*battleInt = */new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  483. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  484. GH.pushInt(battleInt);
  485. }
  486. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  487. {
  488. }
  489. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks)
  490. {
  491. for(int b=0; b<healedStacks.size(); ++b)
  492. {
  493. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  494. if(battleInt->creAnims[healed->ID]->getType() == 5)
  495. {
  496. //stack has been resurrected
  497. battleInt->creAnims[healed->ID]->setType(2);
  498. }
  499. }
  500. }
  501. void CPlayerInterface::battleNewStackAppeared(int stackID)
  502. {
  503. //changing necessary things in battle interface
  504. battleInt->newStack(stackID);
  505. }
  506. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  507. {
  508. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  509. {
  510. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  511. {
  512. if(itBat->first == *it) //remove this obstacle
  513. {
  514. battleInt->idToObstacle.erase(itBat);
  515. break;
  516. }
  517. }
  518. }
  519. //update accessible hexes
  520. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  521. }
  522. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  523. {
  524. battleInt->stackIsCatapulting(ca);
  525. }
  526. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  527. {
  528. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  529. {
  530. battleInt->stackRemoved(*it);
  531. }
  532. }
  533. 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
  534. {
  535. boost::unique_lock<boost::recursive_mutex> un(*pim);
  536. battleInt->newRound(round);
  537. }
  538. void CPlayerInterface::actionStarted(const BattleAction* action)
  539. {
  540. boost::unique_lock<boost::recursive_mutex> un(*pim);
  541. curAction = new BattleAction(*action);
  542. battleInt->startAction(action);
  543. }
  544. void CPlayerInterface::actionFinished(const BattleAction* action)
  545. {
  546. boost::unique_lock<boost::recursive_mutex> un(*pim);
  547. delete curAction;
  548. curAction = NULL;
  549. battleInt->endAction(action);
  550. }
  551. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  552. {
  553. CBattleInterface *b = battleInt;
  554. {
  555. boost::unique_lock<boost::recursive_mutex> un(*pim);
  556. const CStack *stack = cb->battleGetStackByID(stackID);
  557. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  558. {
  559. std::string hlp = CGI->generaltexth->allTexts[33];
  560. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  561. battleInt->displayEffect(20,stack->position);
  562. battleInt->console->addText(hlp);
  563. }
  564. b->stackActivated(stackID);
  565. }
  566. //wait till BattleInterface sets its command
  567. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  568. while(!b->givenCommand->data)
  569. b->givenCommand->cond.wait(lock);
  570. //tidy up
  571. BattleAction ret = *(b->givenCommand->data);
  572. delete b->givenCommand->data;
  573. b->givenCommand->data = NULL;
  574. //return command
  575. return ret;
  576. }
  577. void CPlayerInterface::battleEnd(BattleResult *br)
  578. {
  579. boost::unique_lock<boost::recursive_mutex> un(*pim);
  580. battleInt->battleFinished(*br);
  581. }
  582. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  583. {
  584. boost::unique_lock<boost::recursive_mutex> un(*pim);
  585. battleInt->stackMoved(ID, dest, end, distance);
  586. }
  587. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  588. {
  589. boost::unique_lock<boost::recursive_mutex> un(*pim);
  590. battleInt->spellCast(sc);
  591. }
  592. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  593. {
  594. boost::unique_lock<boost::recursive_mutex> un(*pim);
  595. battleInt->battleStacksEffectsSet(sse);
  596. }
  597. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  598. {
  599. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  600. boost::unique_lock<boost::recursive_mutex> un(*pim);
  601. tlog5 << "done!\n";
  602. std::vector<SStackAttackedInfo> arg;
  603. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  604. {
  605. if(i->isEffect() && i->effect != 12) //and not armageddon
  606. {
  607. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  608. if (stack != NULL)
  609. battleInt->displayEffect(i->effect, stack->position);
  610. }
  611. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  612. arg.push_back(to_put);
  613. }
  614. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  615. {
  616. battleInt->displayEffect(bsa.begin()->effect, -1);
  617. }
  618. battleInt->stacksAreAttacked(arg);
  619. }
  620. void CPlayerInterface::battleAttack(BattleAttack *ba)
  621. {
  622. tlog5 << "CPlayerInterface::battleAttack - locking...";
  623. boost::unique_lock<boost::recursive_mutex> un(*pim);
  624. tlog5 << "done!\n";
  625. assert(curAction);
  626. if(ba->lucky()) //lucky hit
  627. {
  628. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  629. std::string hlp = CGI->generaltexth->allTexts[45];
  630. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  631. battleInt->console->addText(hlp);
  632. battleInt->displayEffect(18,stack->position);
  633. }
  634. //TODO: bad luck?
  635. if(ba->shot())
  636. {
  637. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  638. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked), i->stackAttacked);
  639. }
  640. else
  641. {//WARNING: does not support multiple attacked creatures
  642. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  643. int shift = 0;
  644. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  645. {
  646. if( BattleInfo::mutualPosition(curAction->destinationTile + 1, attacker->position) >= 0 )
  647. shift = 1;
  648. else
  649. shift = -1;
  650. }
  651. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, ba->bsa.begin()->stackAttacked );
  652. }
  653. }
  654. void CPlayerInterface::showComp(SComponent comp)
  655. {
  656. boost::unique_lock<boost::recursive_mutex> un(*pim);
  657. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  658. adventureInt->infoBar.showComp(&comp,4000);
  659. }
  660. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  661. {
  662. std::vector<SComponent*> intComps;
  663. for(int i=0;i<components.size();i++)
  664. intComps.push_back(new SComponent(*components[i]));
  665. showInfoDialog(text,intComps,soundID);
  666. }
  667. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  668. {
  669. waitWhileDialog();
  670. boost::unique_lock<boost::recursive_mutex> un(*pim);
  671. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  672. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  673. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  674. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  675. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  676. if(/*makingTurn && */GH.listInt.size())
  677. {
  678. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  679. showingDialog->set(true);
  680. GH.pushInt(temp);
  681. }
  682. else
  683. {
  684. dialogs.push_back(temp);
  685. }
  686. }
  687. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  688. {
  689. boost::unique_lock<boost::recursive_mutex> un(*pim);
  690. LOCPLINT->showingDialog->setn(true);
  691. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  692. }
  693. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  694. {
  695. waitWhileDialog();
  696. boost::unique_lock<boost::recursive_mutex> un(*pim);
  697. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  698. if(!selection && cancel) //simple yes/no dialog
  699. {
  700. std::vector<SComponent*> intComps;
  701. for(int i=0;i<components.size();i++)
  702. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  703. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  704. }
  705. else if(selection)
  706. {
  707. std::vector<CSelectableComponent*> intComps;
  708. for(int i=0;i<components.size();i++)
  709. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  710. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  711. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  712. if(cancel)
  713. {
  714. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  715. }
  716. int charperline = 35;
  717. if (pom.size() > 1)
  718. charperline = 50;
  719. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  720. GH.pushInt(temp);
  721. intComps[0]->clickLeft(true, false);
  722. }
  723. }
  724. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  725. {
  726. boost::unique_lock<boost::recursive_mutex> un(*pim);
  727. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  728. adventureInt->minimap.showTile(*i);
  729. if(pos.size())
  730. GH.totalRedraw();
  731. }
  732. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  733. {
  734. boost::unique_lock<boost::recursive_mutex> un(*pim);
  735. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  736. adventureInt->minimap.hideTile(*i);
  737. }
  738. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  739. {
  740. boost::unique_lock<boost::recursive_mutex> un(*pim);
  741. adventureInt->heroWindow->setHero(hero);
  742. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  743. GH.pushInt(adventureInt->heroWindow);
  744. }
  745. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  746. {
  747. boost::unique_lock<boost::recursive_mutex> un(*pim);
  748. if(adventureInt->heroWindow->curHero) //hero window is opened
  749. {
  750. adventureInt->heroWindow->deactivate();
  751. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  752. adventureInt->heroWindow->activate();
  753. return;
  754. }
  755. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  756. if(cew) //exchange window is open
  757. {
  758. cew->deactivate();
  759. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  760. {
  761. if(cew->heroInst[g] == hero)
  762. {
  763. cew->artifs[g]->setHero(hero);
  764. }
  765. }
  766. cew->prepareBackground();
  767. cew->activate();
  768. }
  769. }
  770. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  771. {
  772. boost::unique_lock<boost::recursive_mutex> un(*pim);
  773. if(castleInt && town->ID == TOWNI_TYPE)
  774. {
  775. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  776. if(fs)
  777. fs->draw(castleInt,false);
  778. }
  779. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20)) //external dwelling
  780. {
  781. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  782. if(crw)
  783. crw->initCres();
  784. }
  785. }
  786. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  787. {
  788. if(bonus.type == HeroBonus::NONE) return;
  789. boost::unique_lock<boost::recursive_mutex> un(*pim);
  790. redrawHeroWin(hero);
  791. }
  792. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  793. {
  794. h & playerID & serialID;
  795. h & sysOpts;
  796. }
  797. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  798. {
  799. serializeTempl(h,version);
  800. }
  801. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  802. {
  803. serializeTempl(h,version);
  804. sysOpts.apply();
  805. firstCall = -1;
  806. }
  807. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  808. {
  809. if(!vstd::contains(graphics->heroWins,hero->subID))
  810. {
  811. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  812. return;
  813. }
  814. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  815. graphics->heroWins[hero->subID] = infoWin(hero);
  816. if (adventureInt->selection == hero)
  817. adventureInt->infoBar.draw(screen);
  818. }
  819. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  820. {
  821. if (!h)
  822. return false; //can't find hero
  823. bool result = false;
  824. path.convert(0);
  825. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  826. stillMoveHero.data = CONTINUE_MOVE;
  827. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  828. enum TerrainTile::EterrainType newTerrain;
  829. int sh = -1;
  830. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  831. {
  832. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  833. if(path.nodes[i-1].turns)
  834. {
  835. stillMoveHero.data = STOP_MOVE;
  836. break;
  837. }
  838. // Start a new sound for the hero movement or let the existing one carry on.
  839. #if 0
  840. // TODO
  841. if (hero is flying && sh == -1)
  842. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  843. #endif
  844. {
  845. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  846. if (newTerrain != currentTerrain) {
  847. CGI->soundh->stopSound(sh);
  848. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  849. currentTerrain = newTerrain;
  850. }
  851. }
  852. stillMoveHero.data = WAITING_MOVE;
  853. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  854. cb->moveHero(h,endpos);
  855. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  856. stillMoveHero.cond.wait(un);
  857. }
  858. CGI->soundh->stopSound(sh);
  859. //stillMoveHero = false;
  860. cb->recalculatePaths();
  861. return result;
  862. }
  863. bool CPlayerInterface::shiftPressed() const
  864. {
  865. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  866. }
  867. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  868. {
  869. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  870. return;
  871. {
  872. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  873. while(showingDialog->data)
  874. showingDialog->cond.wait(un);
  875. }
  876. boost::unique_lock<boost::recursive_mutex> un(*pim);
  877. while(dialogs.size())
  878. {
  879. pim->unlock();
  880. SDL_Delay(20);
  881. pim->lock();
  882. }
  883. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  884. cgw->quit->callback += onEnd;
  885. GH.pushInt(cgw);
  886. }
  887. void CPlayerInterface::requestRealized( PackageApplied *pa )
  888. {
  889. if(stillMoveHero.get() == DURING_MOVE)
  890. stillMoveHero.setn(CONTINUE_MOVE);
  891. }
  892. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  893. {
  894. boost::unique_lock<boost::recursive_mutex> un(*pim);
  895. GH.pushInt(new CExchangeWindow(hero2, hero1));
  896. }
  897. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  898. {
  899. //redraw minimap if owner changed
  900. boost::unique_lock<boost::recursive_mutex> un(*pim);
  901. if(sop->what == 1)
  902. {
  903. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  904. std::set<int3> pos = obj->getBlockedPos();
  905. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  906. {
  907. if(cb->isVisible(*it))
  908. adventureInt->minimap.showTile(*it);
  909. }
  910. if(obj->ID == TOWNI_TYPE)
  911. adventureInt->townList.genList();
  912. }
  913. }
  914. void CPlayerInterface::recreateWanderingHeroes()
  915. {
  916. wanderingHeroes.clear();
  917. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  918. for(size_t i = 0; i < heroes.size(); i++)
  919. if(!heroes[i]->inTownGarrison)
  920. wanderingHeroes.push_back(heroes[i]);
  921. }
  922. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  923. {
  924. if(pos < 0 || pos >= wanderingHeroes.size())
  925. return NULL;
  926. return wanderingHeroes[pos];
  927. }
  928. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  929. {
  930. waitWhileDialog();
  931. boost::unique_lock<boost::recursive_mutex> un(*pim);
  932. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  933. GH.pushInt(cr);
  934. }
  935. void CPlayerInterface::waitWhileDialog()
  936. {
  937. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  938. while(showingDialog->data)
  939. showingDialog->cond.wait(un);
  940. }
  941. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  942. {
  943. boost::unique_lock<boost::recursive_mutex> un(*pim);
  944. int state = obj->state();
  945. std::vector<si32> cost;
  946. obj->getBoatCost(cost);
  947. CShipyardWindow *csw = new CShipyardWindow(cost, state, boost::bind(&CCallback::buildBoat, cb, obj));
  948. GH.pushInt(csw);
  949. }
  950. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  951. {
  952. boost::unique_lock<boost::recursive_mutex> un(*pim);
  953. CGI->mh->printObject(obj);
  954. //we might have built a boat in shipyard in opened town screen
  955. if(obj->ID == 8
  956. && LOCPLINT->castleInt
  957. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  958. {
  959. CGI->soundh->playSound(soundBase::newBuilding);
  960. LOCPLINT->castleInt->recreateBuildings();
  961. }
  962. }
  963. void CPlayerInterface::centerView (int3 pos, int focusTime)
  964. {
  965. LOCPLINT->adventureInt->centerOn (pos);
  966. if(focusTime)
  967. {
  968. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  969. if(activeAdv)
  970. adventureInt->deactivate();
  971. SDL_Delay(focusTime);
  972. if(activeAdv)
  973. adventureInt->activate();
  974. }
  975. }
  976. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  977. {
  978. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  979. {
  980. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  981. heroKilled(h);
  982. }
  983. }
  984. bool CPlayerInterface::ctrlPressed() const
  985. {
  986. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  987. }
  988. void CPlayerInterface::update()
  989. {
  990. pim->lock();
  991. //if there are any waiting dialogs, show them
  992. if(dialogs.size() && !showingDialog->get())
  993. {
  994. showingDialog->set(true);
  995. GH.pushInt(dialogs.front());
  996. dialogs.pop_front();
  997. }
  998. GH.updateTime();
  999. GH.handleEvents();
  1000. if(!adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1001. GH.totalRedraw();
  1002. else
  1003. GH.simpleRedraw();
  1004. CGI->curh->draw1();
  1005. CSDL_Ext::update(screen);
  1006. CGI->curh->draw2();
  1007. pim->unlock();
  1008. SDL_framerateDelay(mainFPSmng);
  1009. }
  1010. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1011. {
  1012. using namespace boost::filesystem;
  1013. using namespace boost::algorithm;
  1014. std::map<std::time_t, int> dates; //save number => datestamp
  1015. directory_iterator enddir;
  1016. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1017. {
  1018. if(is_regular(dir->status()))
  1019. {
  1020. std::string name = dir->path().leaf();
  1021. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1022. {
  1023. char nr = name[namePrefix.size()];
  1024. if(std::isdigit(nr))
  1025. {
  1026. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. if(dates.size())
  1032. return (--dates.end())->second; //return latest file number
  1033. return 0;
  1034. }
  1035. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1036. {
  1037. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1038. {
  1039. //ho->moveDir = 1;
  1040. ho->isStanding = false;
  1041. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1042. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1043. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1044. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1045. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1046. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1047. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1048. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1049. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1050. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1051. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1052. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1053. 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);
  1054. 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);
  1055. 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);
  1056. 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);
  1057. 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);
  1058. 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);
  1059. }
  1060. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1061. {
  1062. //ho->moveDir = 2;
  1063. ho->isStanding = false;
  1064. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1065. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1066. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1067. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1068. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1069. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1070. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1071. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1072. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1073. 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);
  1074. 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);
  1075. 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);
  1076. }
  1077. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1078. {
  1079. //ho->moveDir = 3;
  1080. ho->isStanding = false;
  1081. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1082. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1083. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1084. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1085. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1086. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1087. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1088. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1089. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1090. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1091. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1092. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1093. 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);
  1094. 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);
  1095. 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);
  1096. 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);
  1097. 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);
  1098. 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);
  1099. }
  1100. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1101. {
  1102. //ho->moveDir = 4;
  1103. ho->isStanding = false;
  1104. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1105. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1106. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1107. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1108. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1109. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1110. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1111. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1112. 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);
  1113. 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);
  1114. }
  1115. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1116. {
  1117. //ho->moveDir = 5;
  1118. ho->isStanding = false;
  1119. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1120. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1121. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1122. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1123. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1124. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1125. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1126. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1127. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1128. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1129. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1130. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1131. 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);
  1132. 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);
  1133. 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);
  1134. 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);
  1135. 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);
  1136. 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);
  1137. }
  1138. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1139. {
  1140. //ho->moveDir = 6;
  1141. ho->isStanding = false;
  1142. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1143. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1144. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1145. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1146. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1147. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1148. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1149. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1150. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1151. 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);
  1152. 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);
  1153. 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);
  1154. }
  1155. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1156. {
  1157. //ho->moveDir = 7;
  1158. ho->isStanding = false;
  1159. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1160. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1161. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1162. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1163. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1164. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1165. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1166. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1167. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1168. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1169. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1170. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1171. 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);
  1172. 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);
  1173. 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);
  1174. 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);
  1175. 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);
  1176. 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);
  1177. }
  1178. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1179. {
  1180. //ho->moveDir = 8;
  1181. ho->isStanding = false;
  1182. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1183. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1184. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1185. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1186. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1187. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1188. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1189. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1190. 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);
  1191. 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);
  1192. }
  1193. }
  1194. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1195. {
  1196. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1197. {
  1198. //setting advmap shift
  1199. adventureInt->terrain.moveX = i-32;
  1200. adventureInt->terrain.moveY = i-32;
  1201. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1202. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1203. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1204. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1205. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1206. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1207. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1208. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1209. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1210. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1211. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1212. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1213. }
  1214. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1215. {
  1216. //setting advmap shift
  1217. adventureInt->terrain.moveY = i-32;
  1218. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1219. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1220. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1221. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1222. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1223. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1224. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1225. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1226. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1227. }
  1228. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1229. {
  1230. //setting advmap shift
  1231. adventureInt->terrain.moveX = -i+32;
  1232. adventureInt->terrain.moveY = i-32;
  1233. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1234. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1235. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1236. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1237. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1238. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1239. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1240. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1241. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1242. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1243. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1244. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1245. }
  1246. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1247. {
  1248. //setting advmap shift
  1249. adventureInt->terrain.moveX = -i+32;
  1250. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1251. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1252. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1253. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1254. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1255. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1256. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1257. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1258. }
  1259. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1260. {
  1261. //setting advmap shift
  1262. adventureInt->terrain.moveX = -i+32;
  1263. adventureInt->terrain.moveY = -i+32;
  1264. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1265. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1266. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1267. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1268. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1269. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1270. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1271. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1272. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1273. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1274. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1275. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1276. }
  1277. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1278. {
  1279. //setting advmap shift
  1280. adventureInt->terrain.moveY = -i+32;
  1281. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1282. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1283. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1284. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1285. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1286. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1287. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1288. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1289. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1290. }
  1291. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1292. {
  1293. //setting advmap shift
  1294. adventureInt->terrain.moveX = i-32;
  1295. adventureInt->terrain.moveY = -i+32;
  1296. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1297. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1298. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1299. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1300. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1301. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1302. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1303. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1304. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1305. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1306. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1307. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1308. }
  1309. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1310. {
  1311. //setting advmap shift
  1312. adventureInt->terrain.moveX = i-32;
  1313. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1314. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1315. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1316. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1317. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1318. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1319. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1320. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1321. }
  1322. }
  1323. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1324. {
  1325. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1326. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1327. {
  1328. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1329. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1330. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1331. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1332. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1333. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1334. }
  1335. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1336. {
  1337. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1338. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1339. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1340. }
  1341. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1342. {
  1343. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1344. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1345. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1346. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1347. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1348. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1349. }
  1350. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1351. {
  1352. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1353. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1354. }
  1355. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1356. {
  1357. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1358. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1359. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1360. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1361. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1362. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1363. }
  1364. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1365. {
  1366. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1367. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1368. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1369. }
  1370. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1371. {
  1372. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1373. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1374. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1375. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1376. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1377. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1378. }
  1379. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1380. {
  1381. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1382. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1383. }
  1384. //restoring good rects
  1385. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1386. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1387. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1388. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1389. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1390. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1391. //restoring good order of objects
  1392. 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);
  1393. 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);
  1394. 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);
  1395. 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);
  1396. 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);
  1397. 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);
  1398. }
  1399. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1400. {
  1401. if(player == playerID)
  1402. {
  1403. if(!victory)
  1404. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1405. // else
  1406. // showInfoDialog("Placeholder message: you won!");
  1407. waitWhileDialog();
  1408. //return to main menu
  1409. SDL_Event event;
  1410. event.type = SDL_USEREVENT;
  1411. event.user.code = 2;
  1412. SDL_PushEvent(&event);
  1413. }
  1414. else
  1415. {
  1416. if(!victory) //enemy has lost
  1417. {
  1418. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1419. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1420. showInfoDialog(txt,std::vector<SComponent*>(1, new SComponent(SComponent::flag, player, 0)));
  1421. }
  1422. }
  1423. }
  1424. void SystemOptions::setMusicVolume( int newVolume )
  1425. {
  1426. musicVolume = newVolume;
  1427. CGI->musich->setVolume(newVolume);
  1428. settingsChanged();
  1429. }
  1430. void SystemOptions::setSoundVolume( int newVolume )
  1431. {
  1432. soundVolume = newVolume;
  1433. CGI->soundh->setVolume(newVolume);
  1434. settingsChanged();
  1435. }
  1436. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1437. {
  1438. heroMoveSpeed = newSpeed;
  1439. settingsChanged();
  1440. }
  1441. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1442. {
  1443. mapScrollingSpeed = newSpeed;
  1444. settingsChanged();
  1445. }
  1446. void SystemOptions::settingsChanged()
  1447. {
  1448. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1449. if(settings.sfile)
  1450. settings << *this;
  1451. else
  1452. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1453. }
  1454. void SystemOptions::apply()
  1455. {
  1456. if(CGI->musich->getVolume() != musicVolume)
  1457. CGI->musich->setVolume(musicVolume);
  1458. if(CGI->soundh->getVolume() != soundVolume)
  1459. CGI->soundh->setVolume(soundVolume);
  1460. settingsChanged();
  1461. }
  1462. SystemOptions::SystemOptions()
  1463. {
  1464. heroMoveSpeed = 2;
  1465. mapScrollingSpeed = 2;
  1466. musicVolume = 88;
  1467. soundVolume = 88;
  1468. printCellBorders = true;
  1469. printStackRange = true;
  1470. animSpeed = 2;
  1471. printMouseShadow = true;
  1472. showQueue = true;
  1473. }