CPlayerInterface.cpp 81 KB

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