CPlayerInterface.cpp 74 KB

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