CPlayerInterface.cpp 75 KB

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