CPlayerInterface.cpp 83 KB

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