CPlayerInterface.cpp 70 KB

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