CPlayerInterface.cpp 80 KB

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