CPlayerInterface.cpp 84 KB

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