CPlayerInterface.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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 "CConfigHandler.h"
  15. #include "CCreatureAnimation.h"
  16. #include "Graphics.h"
  17. #include "../lib/CArtHandler.h"
  18. #include "../lib/CGeneralTextHandler.h"
  19. #include "../lib/CHeroHandler.h"
  20. #include "../lib/CLodHandler.h"
  21. #include "../lib/CObjectHandler.h"
  22. #include "../lib/Connection.h"
  23. #include "../lib/CSpellHandler.h"
  24. #include "../lib/CTownHandler.h"
  25. #include "../lib/BattleState.h"
  26. #include "CMusicHandler.h"
  27. #include "../lib/CondSh.h"
  28. #include "../lib/NetPacks.h"
  29. #include "../lib/map.h"
  30. #include "../lib/VCMIDirs.h"
  31. #include "mapHandler.h"
  32. #include "../timeHandler.h"
  33. #include <boost/lexical_cast.hpp>
  34. #include <boost/format.hpp>
  35. #include <boost/algorithm/string.hpp>
  36. #include <boost/algorithm/string/replace.hpp>
  37. #include <boost/assign/std/vector.hpp>
  38. #include <boost/assign/list_of.hpp>
  39. #include <boost/date_time/posix_time/posix_time.hpp>
  40. #include <boost/thread.hpp>
  41. #include <cmath>
  42. #include <queue>
  43. #include <sstream>
  44. #include <boost/filesystem.hpp>
  45. #include "../StartInfo.h"
  46. #include <boost/foreach.hpp>
  47. #include "../lib/CGameState.h"
  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 std::queue<SDL_Event*> events;
  67. extern boost::mutex eventsM;
  68. CPlayerInterface * LOCPLINT;
  69. CBattleInterface * CPlayerInterface::battleInt;
  70. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  71. CondSh<EMoveState> stillMoveHero; //used during hero movement
  72. int CPlayerInterface::howManyPeople = 0;
  73. struct OCM_HLP_CGIN
  74. {
  75. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  76. {
  77. return (*a.first)<(*b.first);
  78. }
  79. } ocmptwo_cgin ;
  80. CPlayerInterface::CPlayerInterface(int Player)
  81. {
  82. observerInDuelMode = false;
  83. howManyPeople++;
  84. GH.defActionsDef = 0;
  85. LOCPLINT = this;
  86. curAction = NULL;
  87. playerID=Player;
  88. human=true;
  89. castleInt = NULL;
  90. battleInt = NULL;
  91. //pim = new boost::recursive_mutex;
  92. makingTurn = false;
  93. showingDialog = new CondSh<bool>(false);
  94. sysOpts = GDefaultOptions;
  95. cingconsole = new CInGameConsole;
  96. terminate_cond.set(false);
  97. firstCall = 1; //if loading will be overwritten in serialize
  98. autosaveCount = 0;
  99. }
  100. CPlayerInterface::~CPlayerInterface()
  101. {
  102. howManyPeople--;
  103. //delete pim;
  104. //delNull(pim);
  105. delete showingDialog;
  106. if(adventureInt)
  107. {
  108. if(adventureInt->active & CIntObject::KEYBOARD)
  109. adventureInt->deactivateKeys();
  110. delete adventureInt;
  111. adventureInt = NULL;
  112. }
  113. if(cingconsole->active) //TODO
  114. cingconsole->deactivate();
  115. delete cingconsole;
  116. LOCPLINT = NULL;
  117. }
  118. void CPlayerInterface::init(CCallback * CB)
  119. {
  120. cb = dynamic_cast<CCallback*>(CB);
  121. if(observerInDuelMode)
  122. {
  123. return;
  124. }
  125. if(!adventureInt)
  126. adventureInt = new CAdvMapInt();
  127. if(!towns.size() && !wanderingHeroes.size())
  128. {
  129. recreateHeroTownList();
  130. }
  131. }
  132. void CPlayerInterface::yourTurn()
  133. {
  134. {
  135. boost::unique_lock<boost::recursive_mutex> un(*pim);
  136. boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
  137. LOCPLINT = this;
  138. GH.curInt = this;
  139. adventureInt->selection = NULL;
  140. if(firstCall)
  141. {
  142. if(howManyPeople == 1)
  143. adventureInt->setPlayer(playerID);
  144. autosaveCount = getLastIndex("Autosave_");
  145. if(!GH.listInt.size())
  146. {
  147. GH.pushInt(adventureInt);
  148. adventureInt->activateKeys();
  149. }
  150. if(firstCall > 0) //new game, not loaded
  151. {
  152. int index = getLastIndex("Newgame_Autosave_");
  153. index %= SAVES_COUNT;
  154. cb->save("Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
  155. }
  156. firstCall = 0;
  157. }
  158. else
  159. {
  160. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  161. autosaveCount %= 5;
  162. }
  163. if(adventureInt->player != playerID)
  164. adventureInt->setPlayer(playerID);
  165. if(howManyPeople > 1) //hot seat message
  166. {
  167. adventureInt->startHotSeatWait(playerID);
  168. makingTurn = true;
  169. std::string msg = CGI->generaltexth->allTexts[13];
  170. boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
  171. std::vector<SComponent*> cmp;
  172. cmp.push_back(new SComponent(SComponent::flag, playerID, 0));
  173. showInfoDialog(msg, cmp);
  174. }
  175. else
  176. {
  177. makingTurn = true;
  178. adventureInt->startTurn();
  179. }
  180. }
  181. acceptTurn();
  182. }
  183. STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  184. {
  185. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  186. for(int h=0; h<hlp.objects.size(); ++h)
  187. if(hlp.objects[h].first->id==hid)
  188. {
  189. hlp.objects[h].second = r;
  190. return;
  191. }
  192. }
  193. STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  194. {
  195. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  196. for(int h=0; h<hlp.objects.size(); ++h)
  197. if(hlp.objects[h].first->id==hid)
  198. {
  199. hlp.objects.erase(hlp.objects.begin()+h);
  200. return;
  201. }
  202. }
  203. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  204. {
  205. if(LOCPLINT != this)
  206. return;
  207. boost::unique_lock<boost::recursive_mutex> un(*pim);
  208. const CGHeroInstance * ho = cb->getHero(details.id); //object representing this hero
  209. int3 hp = details.start;
  210. adventureInt->centerOn(ho); //actualizing screen pos
  211. adventureInt->minimap.draw(screen2);
  212. adventureInt->heroList.draw(screen2);
  213. bool directlyAttackingCreature =
  214. CGI->mh->map->isInTheMap(details.attackedFrom)
  215. && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
  216. && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
  217. if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
  218. {
  219. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  220. {
  221. if(adventureInt->terrain.currentPath)
  222. eraseCurrentPathOf(ho);
  223. return; //teleport - no fancy moving animation
  224. //TODO: smooth disappear / appear effect
  225. }
  226. if ((details.result != TryMoveHero::SUCCESS && details.result != TryMoveHero::FAILED) //hero didn't change tile but visit succeeded
  227. || directlyAttackingCreature) // or creature was attacked from endangering tile.
  228. {
  229. eraseCurrentPathOf(ho);
  230. }
  231. else if(adventureInt->terrain.currentPath && details.result == TryMoveHero::SUCCESS) //&& hero is moving
  232. {
  233. //remove one node from the path (the one we went)
  234. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  235. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  236. eraseCurrentPathOf(ho);
  237. }
  238. }
  239. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  240. {
  241. ho->isStanding = true;
  242. stillMoveHero.setn(STOP_MOVE);
  243. GH.totalRedraw();
  244. return;
  245. }
  246. initMovement(details, ho, hp);
  247. //first initializing done
  248. GH.mainFPSmng->framerateDelay(); // after first move
  249. //main moving
  250. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  251. {
  252. movementPxStep(details, i, hp, ho);
  253. adventureInt->updateScreen = true;
  254. adventureInt->show(screen);
  255. CSDL_Ext::update(screen);
  256. GH.mainFPSmng->framerateDelay(); //for animation purposes
  257. } //for(int i=1; i<32; i+=4)
  258. //main moving done
  259. //finishing move
  260. finishMovement(details, hp, ho);
  261. ho->isStanding = true;
  262. //move finished
  263. adventureInt->minimap.draw(screen2);
  264. adventureInt->heroList.updateMove(ho);
  265. //check if user cancelled movement
  266. {
  267. boost::unique_lock<boost::mutex> un(eventsM);
  268. while(events.size())
  269. {
  270. SDL_Event *ev = events.front();
  271. events.pop();
  272. switch(ev->type)
  273. {
  274. case SDL_MOUSEBUTTONDOWN:
  275. stillMoveHero.setn(STOP_MOVE);
  276. break;
  277. case SDL_KEYDOWN:
  278. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  279. stillMoveHero.setn(STOP_MOVE);
  280. break;
  281. }
  282. delete ev;
  283. }
  284. }
  285. if(stillMoveHero.get() == WAITING_MOVE)
  286. stillMoveHero.setn(DURING_MOVE);
  287. // Hero attacked creature directly, set direction to face it.
  288. if (directlyAttackingCreature) {
  289. // Get direction to attacker.
  290. int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
  291. static const ui8 dirLookup[3][3] = {
  292. { 1, 2, 3 },
  293. { 8, 0, 4 },
  294. { 7, 6, 5 }
  295. };
  296. // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  297. const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
  298. }
  299. }
  300. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  301. {
  302. boost::unique_lock<boost::recursive_mutex> un(*pim);
  303. wanderingHeroes -= hero;
  304. if(vstd::contains(paths, hero))
  305. paths.erase(hero);
  306. adventureInt->heroList.updateHList(hero);
  307. }
  308. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  309. {
  310. boost::unique_lock<boost::recursive_mutex> un(*pim);
  311. wanderingHeroes.push_back(hero);
  312. adventureInt->heroList.updateHList();
  313. }
  314. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  315. {
  316. if (castleInt)
  317. GH.popIntTotally(castleInt);
  318. castleInt = new CCastleInterface(town);
  319. GH.pushInt(castleInt);
  320. }
  321. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  322. {
  323. if(!specific)
  324. specific = adventureInt->selection;
  325. assert(specific);
  326. switch(specific->ID)
  327. {
  328. case HEROI_TYPE:
  329. {
  330. InfoAboutHero iah;
  331. bool gotInfo = LOCPLINT->cb->getHeroInfo(specific, iah);
  332. assert(gotInfo);
  333. return graphics->drawHeroInfoWin(iah);
  334. }
  335. case TOWNI_TYPE:
  336. case 33: // Garrison
  337. case 219:
  338. {
  339. InfoAboutTown iah;
  340. bool gotInfo = LOCPLINT->cb->getTownInfo(specific, iah);
  341. assert(gotInfo);
  342. return graphics->drawTownInfoWin(iah);
  343. }
  344. default:
  345. return NULL;
  346. }
  347. }
  348. int3 CPlayerInterface::repairScreenPos(int3 pos)
  349. {
  350. if(pos.x<-CGI->mh->frameW)
  351. pos.x = -CGI->mh->frameW;
  352. if(pos.y<-CGI->mh->frameH)
  353. pos.y = -CGI->mh->frameH;
  354. if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
  355. pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
  356. if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
  357. pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
  358. return pos;
  359. }
  360. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  361. {
  362. boost::unique_lock<boost::recursive_mutex> un(*pim);
  363. if(which == 4)
  364. {
  365. if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
  366. ctw->setExpToLevel();
  367. }
  368. else if(which < PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  369. updateInfo(hero);
  370. }
  371. void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  372. {
  373. CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
  374. if(cuw) //university window is open
  375. {
  376. GH.totalRedraw();
  377. }
  378. }
  379. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  380. {
  381. boost::unique_lock<boost::recursive_mutex> un(*pim);
  382. updateInfo(hero);
  383. }
  384. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  385. {
  386. boost::unique_lock<boost::recursive_mutex> un(*pim);
  387. if(makingTurn && hero->tempOwner == playerID)
  388. adventureInt->heroList.redraw();
  389. }
  390. void CPlayerInterface::receivedResource(int type, int val)
  391. {
  392. boost::unique_lock<boost::recursive_mutex> un(*pim);
  393. if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
  394. mw->resourceChanged(type, val);
  395. GH.totalRedraw();
  396. }
  397. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  398. {
  399. waitWhileDialog();
  400. CCS->soundh->playSound(soundBase::heroNewLevel);
  401. boost::unique_lock<boost::recursive_mutex> un(*pim);
  402. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  403. GH.pushInt(lw);
  404. }
  405. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  406. {
  407. boost::unique_lock<boost::recursive_mutex> un(*pim);
  408. updateInfo(town);
  409. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  410. {
  411. CGI->mh->hideObject(town->garrisonHero);
  412. wanderingHeroes -= town->garrisonHero;
  413. }
  414. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  415. {
  416. CGI->mh->printObject(town->visitingHero);
  417. wanderingHeroes.push_back(town->visitingHero);
  418. }
  419. if(CCastleInterface *c = castleInt)
  420. {
  421. c->garr->highlighted = NULL;
  422. c->garr->setArmy(town->getUpperArmy(), 0);
  423. c->garr->setArmy(town->visitingHero, 1);
  424. c->garr->recreateSlots();
  425. c->heroes->update();
  426. }
  427. GH.totalRedraw();
  428. }
  429. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  430. {
  431. if(hero->tempOwner != playerID )
  432. return;
  433. waitWhileDialog();
  434. boost::unique_lock<boost::recursive_mutex> un(*pim);
  435. openTownWindow(town);
  436. }
  437. void CPlayerInterface::garrisonChanged( const CGObjectInstance * obj, bool updateInfobox /*= true*/ )
  438. {
  439. boost::unique_lock<boost::recursive_mutex> un(*pim);
  440. if(updateInfobox)
  441. updateInfo(obj);
  442. for(std::list<IShowActivable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
  443. {
  444. if((*i)->type & IShowActivable::WITH_GARRISON)
  445. {
  446. CGarrisonHolder *cgh = dynamic_cast<CGarrisonHolder*>(*i);
  447. cgh->updateGarrisons();
  448. }
  449. else if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
  450. {
  451. if(obj == cmw->hero)
  452. cmw->garrisonChanged();
  453. }
  454. }
  455. GH.totalRedraw();
  456. }
  457. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  458. {
  459. boost::unique_lock<boost::recursive_mutex> un(*pim);
  460. switch (buildingID)
  461. {
  462. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  463. updateInfo(town);
  464. break;
  465. }
  466. if(!castleInt)
  467. return;
  468. if(castleInt->town!=town)
  469. return;
  470. switch(what)
  471. {
  472. case 1:
  473. CCS->soundh->playSound(soundBase::newBuilding);
  474. castleInt->addBuilding(buildingID);
  475. break;
  476. case 2:
  477. castleInt->removeBuilding(buildingID);
  478. break;
  479. }
  480. }
  481. void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  482. {
  483. if(LOCPLINT != this)
  484. { //another local interface should do this
  485. return;
  486. }
  487. while(showingDialog->get())
  488. SDL_Delay(20);
  489. boost::unique_lock<boost::recursive_mutex> un(*pim);
  490. CCS->musich->playMusicFromSet(CCS->musich->battleMusics, -1);
  491. GH.pushInt(battleInt);
  492. }
  493. void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  494. {
  495. if(LOCPLINT != this)
  496. { //another local interface should do this
  497. return;
  498. }
  499. for(int b=0; b<healedStacks.size(); ++b)
  500. {
  501. const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
  502. if(battleInt->creAnims[healed->ID]->getType() == CCreatureAnim::DEATH)
  503. {
  504. //stack has been resurrected
  505. battleInt->creAnims[healed->ID]->setType(CCreatureAnim::HOLDING);
  506. }
  507. }
  508. if (lifeDrain)
  509. {
  510. const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
  511. const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
  512. int textOff = 0;
  513. if (attacker)
  514. {
  515. battleInt->displayEffect(50, attacker->position);
  516. if (attacker->count > 1)
  517. {
  518. textOff += 1;
  519. }
  520. }
  521. //print info about life drain
  522. char textBuf[1000];
  523. sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
  524. healedStacks[0].second, defender->getCreature()->namePl.c_str());
  525. battleInt->console->addText(textBuf);
  526. }
  527. }
  528. void CPlayerInterface::battleNewStackAppeared(const CStack * stack)
  529. {
  530. if(LOCPLINT != this)
  531. { //another local interface should do this
  532. return;
  533. }
  534. //changing necessary things in battle interface
  535. battleInt->newStack(stack);
  536. }
  537. void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  538. {
  539. if(LOCPLINT != this)
  540. { //another local interface should do this
  541. return;
  542. }
  543. for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
  544. {
  545. for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
  546. {
  547. if(itBat->first == *it) //remove this obstacle
  548. {
  549. battleInt->idToObstacle.erase(itBat);
  550. break;
  551. }
  552. }
  553. }
  554. //update accessible hexes
  555. battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
  556. }
  557. void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
  558. {
  559. if(LOCPLINT != this)
  560. { //another local interface should do this
  561. return;
  562. }
  563. boost::unique_lock<boost::recursive_mutex> un(*pim);
  564. battleInt->stackIsCatapulting(ca);
  565. }
  566. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  567. {
  568. if(LOCPLINT != this)
  569. { //another local interface should do this
  570. return;
  571. }
  572. boost::unique_lock<boost::recursive_mutex> un(*pim);
  573. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  574. {
  575. battleInt->stackRemoved(LOCPLINT->cb->battleGetStackByID(*it));
  576. }
  577. }
  578. 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
  579. {
  580. if(LOCPLINT != this)
  581. { //another local interface should do this
  582. return;
  583. }
  584. boost::unique_lock<boost::recursive_mutex> un(*pim);
  585. battleInt->newRound(round);
  586. }
  587. void CPlayerInterface::actionStarted(const BattleAction* action)
  588. {
  589. if(LOCPLINT != this)
  590. { //another local interface should do this
  591. return;
  592. }
  593. boost::unique_lock<boost::recursive_mutex> un(*pim);
  594. curAction = new BattleAction(*action);
  595. battleInt->startAction(action);
  596. }
  597. void CPlayerInterface::actionFinished(const BattleAction* action)
  598. {
  599. if(LOCPLINT != this)
  600. { //another local interface should do this
  601. return;
  602. }
  603. boost::unique_lock<boost::recursive_mutex> un(*pim);
  604. delete curAction;
  605. curAction = NULL;
  606. battleInt->endAction(action);
  607. }
  608. BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
  609. {
  610. CBattleInterface *b = battleInt;
  611. {
  612. boost::unique_lock<boost::recursive_mutex> un(*pim);
  613. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  614. {
  615. std::string hlp = CGI->generaltexth->allTexts[33];
  616. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl : stack->getCreature()->nameSing);
  617. battleInt->displayEffect(20,stack->position);
  618. battleInt->console->addText(hlp);
  619. }
  620. b->stackActivated(stack);
  621. }
  622. //wait till BattleInterface sets its command
  623. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  624. while(!b->givenCommand->data)
  625. b->givenCommand->cond.wait(lock);
  626. //tidy up
  627. BattleAction ret = *(b->givenCommand->data);
  628. delete b->givenCommand->data;
  629. b->givenCommand->data = NULL;
  630. //return command
  631. return ret;
  632. }
  633. void CPlayerInterface::battleEnd(const BattleResult *br)
  634. {
  635. if(LOCPLINT != this)
  636. { //another local interface should do this
  637. return;
  638. }
  639. boost::unique_lock<boost::recursive_mutex> un(*pim);
  640. battleInt->battleFinished(*br);
  641. }
  642. void CPlayerInterface::battleStackMoved(const CStack * stack, THex dest, int distance, bool end)
  643. {
  644. if(LOCPLINT != this)
  645. { //another local interface should do this
  646. return;
  647. }
  648. boost::unique_lock<boost::recursive_mutex> un(*pim);
  649. battleInt->stackMoved(stack, dest, end, distance);
  650. }
  651. void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
  652. {
  653. if(LOCPLINT != this)
  654. { //another local interface should do this
  655. return;
  656. }
  657. boost::unique_lock<boost::recursive_mutex> un(*pim);
  658. battleInt->spellCast(sc);
  659. }
  660. void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
  661. {
  662. if(LOCPLINT != this)
  663. { //another local interface should do this
  664. return;
  665. }
  666. boost::unique_lock<boost::recursive_mutex> un(*pim);
  667. battleInt->battleStacksEffectsSet(sse);
  668. }
  669. void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  670. {
  671. if(LOCPLINT != this)
  672. { //another local interface should do this
  673. return;
  674. }
  675. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  676. boost::unique_lock<boost::recursive_mutex> un(*pim);
  677. tlog5 << "done!\n";
  678. std::vector<SStackAttackedInfo> arg;
  679. for(std::vector<BattleStackAttacked>::const_iterator i = bsa.begin(); i != bsa.end(); i++)
  680. {
  681. const CStack *defender = cb->battleGetStackByID(i->stackAttacked, false);
  682. const CStack *attacker = cb->battleGetStackByID(i->attackerID, false);
  683. if(i->isEffect() && i->effect != 12) //and not armageddon
  684. {
  685. if (defender != NULL)
  686. battleInt->displayEffect(i->effect, defender->position);
  687. }
  688. SStackAttackedInfo to_put = {defender, i->damageAmount, i->killedAmount, attacker, LOCPLINT->curAction->actionType==7, i->killed()};
  689. arg.push_back(to_put);
  690. }
  691. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  692. {
  693. battleInt->displayEffect(bsa.begin()->effect, -1);
  694. }
  695. battleInt->stacksAreAttacked(arg);
  696. }
  697. void CPlayerInterface::battleAttack(const BattleAttack *ba)
  698. {
  699. if(LOCPLINT != this)
  700. { //another local interface should do this
  701. return;
  702. }
  703. tlog5 << "CPlayerInterface::battleAttack - locking...";
  704. boost::unique_lock<boost::recursive_mutex> un(*pim);
  705. tlog5 << "done!\n";
  706. assert(curAction);
  707. if(ba->lucky()) //lucky hit
  708. {
  709. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  710. std::string hlp = CGI->generaltexth->allTexts[45];
  711. boost::algorithm::replace_first(hlp,"%s",(stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  712. battleInt->console->addText(hlp);
  713. battleInt->displayEffect(18,stack->position);
  714. }
  715. //TODO: bad luck?
  716. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  717. if(ba->shot())
  718. {
  719. for(std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  720. {
  721. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  722. battleInt->stackAttacking(attacker, cb->battleGetPos(i->stackAttacked), attacked, true);
  723. }
  724. }
  725. else
  726. {//WARNING: does not support multiple attacked creatures
  727. int shift = 0;
  728. if(ba->counter() && THex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  729. {
  730. int distp = THex::getDistance(curAction->destinationTile + 1, attacker->position);
  731. int distm = THex::getDistance(curAction->destinationTile - 1, attacker->position);
  732. if( distp < distm )
  733. shift = 1;
  734. else
  735. shift = -1;
  736. }
  737. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  738. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  739. }
  740. }
  741. void CPlayerInterface::showComp(SComponent comp)
  742. {
  743. boost::unique_lock<boost::recursive_mutex> un(*pim);
  744. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  745. adventureInt->infoBar.showComp(&comp,4000);
  746. }
  747. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  748. {
  749. std::vector<SComponent*> intComps;
  750. for(int i=0;i<components.size();i++)
  751. intComps.push_back(new SComponent(*components[i]));
  752. showInfoDialog(text,intComps,soundID);
  753. }
  754. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID, bool delComps)
  755. {
  756. waitWhileDialog();
  757. boost::unique_lock<boost::recursive_mutex> un(*pim);
  758. stopMovement();
  759. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  760. temp->setDelComps(delComps);
  761. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  762. {
  763. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  764. showingDialog->set(true);
  765. GH.pushInt(temp);
  766. }
  767. else
  768. {
  769. dialogs.push_back(temp);
  770. }
  771. }
  772. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  773. {
  774. boost::unique_lock<boost::recursive_mutex> un(*pim);
  775. stopMovement();
  776. LOCPLINT->showingDialog->setn(true);
  777. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  778. }
  779. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  780. {
  781. waitWhileDialog();
  782. boost::unique_lock<boost::recursive_mutex> un(*pim);
  783. stopMovement();
  784. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  785. if(!selection && cancel) //simple yes/no dialog
  786. {
  787. std::vector<SComponent*> intComps;
  788. for(int i=0;i<components.size();i++)
  789. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  790. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  791. }
  792. else if(selection)
  793. {
  794. std::vector<CSelectableComponent*> intComps;
  795. for(int i=0;i<components.size();i++)
  796. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  797. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  798. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  799. if(cancel)
  800. {
  801. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  802. }
  803. int charperline = 35;
  804. if (pom.size() > 1)
  805. charperline = 50;
  806. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  807. GH.pushInt(temp);
  808. intComps[0]->clickLeft(true, false);
  809. }
  810. }
  811. void CPlayerInterface::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
  812. {
  813. boost::unique_lock<boost::recursive_mutex> un(*pim);
  814. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  815. adventureInt->minimap.showTile(*i);
  816. if(pos.size())
  817. GH.totalRedraw();
  818. }
  819. void CPlayerInterface::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
  820. {
  821. boost::unique_lock<boost::recursive_mutex> un(*pim);
  822. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  823. adventureInt->minimap.hideTile(*i);
  824. if(pos.size())
  825. GH.totalRedraw();
  826. }
  827. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  828. {
  829. boost::unique_lock<boost::recursive_mutex> un(*pim);
  830. GH.pushInt(new CHeroWindow(hero));
  831. }
  832. /*
  833. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  834. {
  835. boost::unique_lock<boost::recursive_mutex> un(*pim);
  836. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  837. {
  838. adventureInt->heroWindow->deactivate();
  839. adventureInt->heroWindow->setHero(hero);
  840. adventureInt->heroWindow->activate();
  841. }
  842. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  843. {
  844. cew->deactivate();
  845. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  846. {
  847. if(cew->heroInst[g]->id == hero->id)
  848. {
  849. cew->heroInst[g] = hero;
  850. cew->artifs[g]->updateState = true;
  851. cew->artifs[g]->setHero(hero);
  852. cew->artifs[g]->updateState = false;
  853. }
  854. }
  855. cew->prepareBackground();
  856. cew->activate();
  857. }
  858. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  859. {
  860. if(caw->arts)
  861. {
  862. caw->deactivate();
  863. caw->arts->updateState = true;
  864. caw->arts->setHero(hero);
  865. caw->arts->updateState = false;
  866. caw->activate();
  867. }
  868. }
  869. updateInfo(hero);
  870. }*/
  871. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  872. {
  873. boost::unique_lock<boost::recursive_mutex> un(*pim);
  874. if(castleInt && town->ID == TOWNI_TYPE)
  875. {
  876. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  877. if(fs)
  878. fs->creaturesChanged();
  879. }
  880. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  881. {
  882. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  883. if(crw)
  884. crw->initCres();
  885. }
  886. }
  887. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  888. {
  889. if(bonus.type == Bonus::NONE) return;
  890. boost::unique_lock<boost::recursive_mutex> un(*pim);
  891. updateInfo(hero);
  892. if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
  893. {
  894. //recalculate paths because hero has lost bonus influencing pathfinding
  895. cb->recalculatePaths();
  896. eraseCurrentPathOf(hero, false);
  897. }
  898. }
  899. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  900. {
  901. h & playerID;
  902. h & sysOpts;
  903. h & spellbookSettings;
  904. }
  905. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  906. {
  907. serializeTempl(h,version);
  908. }
  909. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  910. {
  911. serializeTempl(h,version);
  912. sysOpts.apply();
  913. firstCall = -1;
  914. }
  915. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  916. {
  917. if (!h)
  918. return false; //can't find hero
  919. eventsM.unlock();
  920. pim->unlock();
  921. bool result = false;
  922. {
  923. path.convert(0);
  924. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  925. stillMoveHero.data = CONTINUE_MOVE;
  926. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  927. enum TerrainTile::EterrainType newTerrain;
  928. int sh = -1;
  929. const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
  930. for(int i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  931. {
  932. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  933. if(path.nodes[i-1].turns)
  934. {
  935. stillMoveHero.data = STOP_MOVE;
  936. break;
  937. }
  938. // Start a new sound for the hero movement or let the existing one carry on.
  939. #if 0
  940. // TODO
  941. if (hero is flying && sh == -1)
  942. sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
  943. #endif
  944. {
  945. newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  946. if (newTerrain != currentTerrain) {
  947. CCS->soundh->stopSound(sh);
  948. sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
  949. currentTerrain = newTerrain;
  950. }
  951. }
  952. stillMoveHero.data = WAITING_MOVE;
  953. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  954. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  955. cb->moveHero(h,endpos);
  956. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  957. stillMoveHero.cond.wait(un);
  958. if (guarded) // Abort movement if a guard was fought.
  959. break;
  960. }
  961. CCS->soundh->stopSound(sh);
  962. cb->recalculatePaths();
  963. }
  964. pim->lock();
  965. eventsM.lock();
  966. return result;
  967. }
  968. bool CPlayerInterface::shiftPressed() const
  969. {
  970. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  971. }
  972. bool CPlayerInterface::altPressed() const
  973. {
  974. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  975. }
  976. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  977. {
  978. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  979. return;
  980. {
  981. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  982. while(showingDialog->data)
  983. showingDialog->cond.wait(un);
  984. }
  985. boost::unique_lock<boost::recursive_mutex> un(*pim);
  986. while(dialogs.size())
  987. {
  988. pim->unlock();
  989. SDL_Delay(20);
  990. pim->lock();
  991. }
  992. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  993. cgw->quit->callback += onEnd;
  994. GH.pushInt(cgw);
  995. }
  996. /**
  997. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  998. * into a combinational one on an artifact screen. Does not require the combination of
  999. * artifacts to be legal.
  1000. * @param artifactID ID of a constituent artifact.
  1001. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1002. * is false.
  1003. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1004. */
  1005. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1006. {
  1007. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1008. std::string text = artifact.Description();
  1009. text += "\n\n";
  1010. std::vector<SComponent*> scs;
  1011. if (assemble) {
  1012. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1013. // You possess all of the components to...
  1014. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1015. // Picture of assembled artifact at bottom.
  1016. SComponent* sc = new SComponent;
  1017. sc->type = SComponent::artifact;
  1018. sc->subtype = assembledArtifact.id;
  1019. sc->description = assembledArtifact.Description();
  1020. sc->subtitle = assembledArtifact.Name();
  1021. scs.push_back(sc);
  1022. } else {
  1023. // Do you wish to disassemble this artifact?
  1024. text += CGI->generaltexth->allTexts[733];
  1025. }
  1026. showYesNoDialog(text, scs, onYes, onNo, true);
  1027. }
  1028. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1029. {
  1030. if(stillMoveHero.get() == DURING_MOVE)
  1031. stillMoveHero.setn(CONTINUE_MOVE);
  1032. }
  1033. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1034. {
  1035. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1036. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1037. }
  1038. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1039. {
  1040. //redraw minimap if owner changed
  1041. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1042. if(sop->what == ObjProperty::OWNER)
  1043. {
  1044. const CGObjectInstance * obj = cb->getObj(sop->id);
  1045. std::set<int3> pos = obj->getBlockedPos();
  1046. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1047. {
  1048. if(cb->isVisible(*it))
  1049. adventureInt->minimap.showTile(*it);
  1050. }
  1051. if(obj->ID == TOWNI_TYPE)
  1052. {
  1053. if(obj->tempOwner == playerID)
  1054. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1055. else
  1056. towns -= obj;
  1057. }
  1058. assert(cb->getTownsInfo().size() == towns.size());
  1059. }
  1060. }
  1061. void CPlayerInterface::recreateHeroTownList()
  1062. {
  1063. wanderingHeroes.clear();
  1064. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1065. for(size_t i = 0; i < heroes.size(); i++)
  1066. if(!heroes[i]->inTownGarrison)
  1067. wanderingHeroes.push_back(heroes[i]);
  1068. towns.clear();
  1069. std::vector<const CGTownInstance*> townInfo = cb->getTownsInfo();
  1070. for(size_t i = 0; i < townInfo.size(); i++)
  1071. towns.push_back(townInfo[i]);
  1072. }
  1073. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1074. {
  1075. if(pos < 0 || pos >= wanderingHeroes.size())
  1076. return NULL;
  1077. return wanderingHeroes[pos];
  1078. }
  1079. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1080. {
  1081. waitWhileDialog();
  1082. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1083. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1084. GH.pushInt(cr);
  1085. }
  1086. void CPlayerInterface::waitWhileDialog()
  1087. {
  1088. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1089. while(showingDialog->data)
  1090. showingDialog->cond.wait(un);
  1091. }
  1092. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1093. {
  1094. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1095. int state = obj->state();
  1096. std::vector<si32> cost;
  1097. obj->getBoatCost(cost);
  1098. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1099. GH.pushInt(csw);
  1100. }
  1101. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1102. {
  1103. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1104. CGI->mh->printObject(obj);
  1105. //we might have built a boat in shipyard in opened town screen
  1106. if(obj->ID == 8
  1107. && LOCPLINT->castleInt
  1108. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1109. {
  1110. CCS->soundh->playSound(soundBase::newBuilding);
  1111. LOCPLINT->castleInt->addBuilding(20);
  1112. }
  1113. }
  1114. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1115. {
  1116. waitWhileDialog();
  1117. adventureInt->centerOn (pos);
  1118. if(focusTime)
  1119. {
  1120. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1121. if(activeAdv)
  1122. adventureInt->deactivate();
  1123. SDL_Delay(focusTime);
  1124. if(activeAdv)
  1125. adventureInt->activate();
  1126. }
  1127. }
  1128. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1129. {
  1130. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1131. {
  1132. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1133. heroKilled(h);
  1134. }
  1135. }
  1136. bool CPlayerInterface::ctrlPressed() const
  1137. {
  1138. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1139. }
  1140. void CPlayerInterface::update()
  1141. {
  1142. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1143. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1144. if(terminate_cond.get())
  1145. return;
  1146. //if there are any waiting dialogs, show them
  1147. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1148. {
  1149. showingDialog->set(true);
  1150. GH.pushInt(dialogs.front());
  1151. dialogs.pop_front();
  1152. }
  1153. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1154. if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
  1155. {
  1156. pim->unlock();
  1157. return;
  1158. }
  1159. // Handles mouse and key input
  1160. GH.updateTime();
  1161. GH.handleEvents();
  1162. if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1163. GH.totalRedraw();
  1164. else
  1165. GH.simpleRedraw();
  1166. if (conf.cc.showFPS)
  1167. GH.drawFPSCounter();
  1168. // draw the mouse cursor and update the screen
  1169. CCS->curh->draw1();
  1170. CSDL_Ext::update(screen);
  1171. CCS->curh->draw2();
  1172. pim->unlock();
  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->getTile(h->getPosition(false))->tertype == TerrainTile::water)
  1735. showInfoDialog(CGI->generaltexth->allTexts[60]); //Try looking on land!
  1736. else
  1737. {
  1738. const TerrainTile *t = cb->getTile(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. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1875. {
  1876. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1877. {
  1878. (dynamic_cast<CWindowWithArtifacts*>(isa))->artifactRemoved(al);
  1879. }
  1880. }
  1881. }
  1882. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  1883. {
  1884. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1885. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1886. {
  1887. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1888. {
  1889. (dynamic_cast<CWindowWithArtifacts*>(isa))->artifactMoved(src, dst);
  1890. }
  1891. }
  1892. }
  1893. void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
  1894. {
  1895. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1896. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1897. {
  1898. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1899. {
  1900. (dynamic_cast<CWindowWithArtifacts*>(isa))->artifactAssembled(al);
  1901. }
  1902. }
  1903. }
  1904. void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
  1905. {
  1906. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1907. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  1908. {
  1909. if(isa->type & IShowActivable::WITH_ARTIFACTS)
  1910. {
  1911. (dynamic_cast<CWindowWithArtifacts*>(isa))->artifactAssembled(al);
  1912. }
  1913. }
  1914. }
  1915. boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
  1916. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  1917. {
  1918. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  1919. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  1920. }