CPlayerInterface.cpp 61 KB

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