CPlayerInterface.cpp 84 KB

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