CPlayerInterface.cpp 81 KB

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