CPlayerInterface.cpp 74 KB

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