CPlayerInterface.cpp 67 KB

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