CPlayerInterface.cpp 84 KB

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