CPlayerInterface.cpp 78 KB

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