CPlayerInterface.cpp 75 KB

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