CPlayerInterface.cpp 74 KB

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