CPlayerInterface.cpp 72 KB

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