CPlayerInterface.cpp 80 KB

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