CPlayerInterface.cpp 83 KB

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