CPlayerInterface.cpp 83 KB

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