CPlayerInterface.cpp 74 KB

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