CPlayerInterface.cpp 59 KB

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