CPlayerInterface.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395
  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. CBattleInterface *b = battleInt;
  644. {
  645. boost::unique_lock<boost::recursive_mutex> un(*pim);
  646. b->stackActivated(stack);
  647. //Regeneration & mana drain go there
  648. }
  649. //wait till BattleInterface sets its command
  650. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  651. while(!b->givenCommand->data)
  652. {
  653. b->givenCommand->cond.wait(lock);
  654. if(!battleInt) //batle ended while we were waiting for movement (eg. because of spell)
  655. throw boost::thread_interrupted(); //will shut the thread peacefully
  656. }
  657. //tidy up
  658. BattleAction ret = *(b->givenCommand->data);
  659. delete b->givenCommand->data;
  660. b->givenCommand->data = NULL;
  661. //return command
  662. return ret;
  663. }
  664. void CPlayerInterface::battleEnd(const BattleResult *br)
  665. {
  666. if(LOCPLINT != this)
  667. { //another local interface should do this
  668. return;
  669. }
  670. boost::unique_lock<boost::recursive_mutex> un(*pim);
  671. battleInt->battleFinished(*br);
  672. }
  673. void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  674. {
  675. if(LOCPLINT != this)
  676. { //another local interface should do this
  677. return;
  678. }
  679. boost::unique_lock<boost::recursive_mutex> un(*pim);
  680. battleInt->stackMoved(stack, dest, distance);
  681. }
  682. void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
  683. {
  684. if(LOCPLINT != this)
  685. { //another local interface should do this
  686. return;
  687. }
  688. boost::unique_lock<boost::recursive_mutex> un(*pim);
  689. battleInt->spellCast(sc);
  690. }
  691. void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
  692. {
  693. if(LOCPLINT != this)
  694. { //another local interface should do this
  695. return;
  696. }
  697. boost::unique_lock<boost::recursive_mutex> un(*pim);
  698. battleInt->battleStacksEffectsSet(sse);
  699. }
  700. void CPlayerInterface::battleTriggerEffect (const BattleTriggerEffect & bte)
  701. {
  702. battleInt->battleTriggerEffect(bte);
  703. }
  704. void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  705. {
  706. if(LOCPLINT != this)
  707. { //another local interface should do this
  708. return;
  709. }
  710. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  711. boost::unique_lock<boost::recursive_mutex> un(*pim);
  712. tlog5 << "done!\n";
  713. std::vector<StackAttackedInfo> arg;
  714. for(std::vector<BattleStackAttacked>::const_iterator i = bsa.begin(); i != bsa.end(); i++)
  715. {
  716. const CStack *defender = cb->battleGetStackByID(i->stackAttacked, false);
  717. const CStack *attacker = cb->battleGetStackByID(i->attackerID, false);
  718. if(i->isEffect() && i->effect != 12) //and not armageddon
  719. {
  720. if (defender && !i->isSecondary())
  721. battleInt->displayEffect(i->effect, defender->position);
  722. }
  723. StackAttackedInfo to_put = {defender, i->damageAmount, i->killedAmount, attacker, LOCPLINT->curAction->actionType==7, i->killed(), i->willRebirth(), i->cloneKilled()};
  724. arg.push_back(to_put);
  725. }
  726. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  727. {
  728. battleInt->displayEffect(bsa.begin()->effect, -1);
  729. }
  730. battleInt->stacksAreAttacked(arg);
  731. }
  732. void CPlayerInterface::battleAttack(const BattleAttack *ba)
  733. {
  734. if(LOCPLINT != this)
  735. { //another local interface should do this
  736. return;
  737. }
  738. tlog5 << "CPlayerInterface::battleAttack - locking...";
  739. boost::unique_lock<boost::recursive_mutex> un(*pim);
  740. tlog5 << "done!\n";
  741. assert(curAction);
  742. if(ba->lucky()) //lucky hit
  743. {
  744. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  745. std::string hlp = CGI->generaltexth->allTexts[45];
  746. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  747. battleInt->console->addText(hlp);
  748. battleInt->displayEffect(18, stack->position);
  749. }
  750. //TODO: bad luck?
  751. if (ba->deathBlow())
  752. {
  753. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  754. std::string hlp = CGI->generaltexth->allTexts[(stack->count != 1) ? 366 : 365];
  755. boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
  756. battleInt->console->addText(hlp);
  757. for (std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  758. {
  759. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  760. battleInt->displayEffect(73, attacked->position);
  761. }
  762. }
  763. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  764. if(ba->shot())
  765. {
  766. for(std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  767. {
  768. if (!i->isSecondary()) //display projectile only for primary target
  769. {
  770. const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
  771. battleInt->stackAttacking(attacker, cb->battleGetPos(i->stackAttacked), attacked, true);
  772. }
  773. }
  774. }
  775. else
  776. {
  777. int shift = 0;
  778. if(ba->counter() && BattleHex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  779. {
  780. int distp = BattleHex::getDistance(curAction->destinationTile + 1, attacker->position);
  781. int distm = BattleHex::getDistance(curAction->destinationTile - 1, attacker->position);
  782. if( distp < distm )
  783. shift = 1;
  784. else
  785. shift = -1;
  786. }
  787. const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
  788. battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
  789. }
  790. }
  791. void CPlayerInterface::yourTacticPhase(int distance)
  792. {
  793. while(battleInt && battleInt->tacticsMode)
  794. boost::this_thread::sleep(boost::posix_time::millisec(1));
  795. }
  796. void CPlayerInterface::showComp(CComponent comp)
  797. {
  798. waitWhileDialog();//Fix for mantis #98
  799. boost::unique_lock<boost::recursive_mutex> un(*pim);
  800. CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
  801. adventureInt->infoBar.showComp(&comp,4000);
  802. }
  803. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  804. {
  805. std::vector<CComponent*> intComps;
  806. for(int i=0;i<components.size();i++)
  807. intComps.push_back(new CComponent(*components[i]));
  808. showInfoDialog(text,intComps,soundID);
  809. }
  810. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
  811. {
  812. waitWhileDialog();
  813. boost::unique_lock<boost::recursive_mutex> un(*pim);
  814. stopMovement();
  815. CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
  816. temp->setDelComps(delComps);
  817. if(makingTurn && GH.listInt.size() && LOCPLINT == this)
  818. {
  819. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  820. showingDialog->set(true);
  821. GH.pushInt(temp);
  822. }
  823. else
  824. {
  825. dialogs.push_back(temp);
  826. }
  827. }
  828. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<CComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  829. {
  830. boost::unique_lock<boost::recursive_mutex> un(*pim);
  831. stopMovement();
  832. LOCPLINT->showingDialog->setn(true);
  833. CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
  834. }
  835. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  836. {
  837. waitWhileDialog();
  838. boost::unique_lock<boost::recursive_mutex> un(*pim);
  839. stopMovement();
  840. CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  841. if(!selection && cancel) //simple yes/no dialog
  842. {
  843. std::vector<CComponent*> intComps;
  844. for(int i=0;i<components.size();i++)
  845. intComps.push_back(new CComponent(components[i])); //will be deleted by close in window
  846. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  847. }
  848. else if(selection)
  849. {
  850. std::vector<CSelectableComponent*> intComps;
  851. for(int i=0;i<components.size();i++)
  852. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  853. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  854. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  855. if(cancel)
  856. {
  857. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  858. }
  859. int charperline = 35;
  860. if (pom.size() > 1)
  861. charperline = 50;
  862. CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
  863. GH.pushInt(temp);
  864. intComps[0]->clickLeft(true, false);
  865. }
  866. }
  867. void CPlayerInterface::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
  868. {
  869. boost::unique_lock<boost::recursive_mutex> un(*pim);
  870. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  871. adventureInt->minimap.showTile(*i);
  872. if(pos.size())
  873. GH.totalRedraw();
  874. }
  875. void CPlayerInterface::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
  876. {
  877. boost::unique_lock<boost::recursive_mutex> un(*pim);
  878. for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  879. adventureInt->minimap.hideTile(*i);
  880. if(pos.size())
  881. GH.totalRedraw();
  882. }
  883. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  884. {
  885. boost::unique_lock<boost::recursive_mutex> un(*pim);
  886. GH.pushInt(new CHeroWindow(hero));
  887. }
  888. /*
  889. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  890. {
  891. boost::unique_lock<boost::recursive_mutex> un(*pim);
  892. if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
  893. {
  894. adventureInt->heroWindow->deactivate();
  895. adventureInt->heroWindow->setHero(hero);
  896. adventureInt->heroWindow->activate();
  897. }
  898. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
  899. {
  900. cew->deactivate();
  901. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  902. {
  903. if(cew->heroInst[g]->id == hero->id)
  904. {
  905. cew->heroInst[g] = hero;
  906. cew->artifs[g]->updateState = true;
  907. cew->artifs[g]->setHero(hero);
  908. cew->artifs[g]->updateState = false;
  909. }
  910. }
  911. cew->prepareBackground();
  912. cew->activate();
  913. }
  914. else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
  915. {
  916. if(caw->arts)
  917. {
  918. caw->deactivate();
  919. caw->arts->updateState = true;
  920. caw->arts->setHero(hero);
  921. caw->arts->updateState = false;
  922. caw->activate();
  923. }
  924. }
  925. updateInfo(hero);
  926. }*/
  927. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  928. {
  929. boost::unique_lock<boost::recursive_mutex> un(*pim);
  930. if(const CGTownInstance * townObj = dynamic_cast<const CGTownInstance*>(town))
  931. {
  932. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  933. if(fs)
  934. fs->creaturesChanged();
  935. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  936. {
  937. CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
  938. if (ki && townObj)
  939. ki->townChanged(townObj);
  940. }
  941. }
  942. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20 || town->ID == 106)) //external dwelling
  943. {
  944. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  945. if(crw)
  946. crw->initCres();
  947. }
  948. }
  949. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
  950. {
  951. if(bonus.type == Bonus::NONE) return;
  952. boost::unique_lock<boost::recursive_mutex> un(*pim);
  953. updateInfo(hero);
  954. if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
  955. {
  956. //recalculate paths because hero has lost bonus influencing pathfinding
  957. eraseCurrentPathOf(hero, false);
  958. }
  959. }
  960. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  961. {
  962. h & playerID;
  963. h & spellbookSettings;
  964. //sleeping heroes
  965. ui8 sleepingSize;
  966. if (h.saving)
  967. sleepingSize = sleepingHeroes.size();
  968. h & sleepingSize;
  969. for (int i = 0; i < sleepingSize; i++)
  970. {
  971. si32 hid;
  972. if (h.saving)
  973. hid = sleepingHeroes[i]->id;
  974. h & hid;
  975. if (!h.saving)
  976. {
  977. const CGHeroInstance *hero = cb->getHero(hid);
  978. sleepingHeroes += hero;
  979. }
  980. }
  981. //hero list order
  982. ui8 heroListSize;
  983. if (h.saving)
  984. heroListSize = wanderingHeroes.size();
  985. else
  986. wanderingHeroes.clear();
  987. h & heroListSize;
  988. for (int i = 0; i < heroListSize; i++)
  989. {
  990. si32 hid;
  991. if (h.saving)
  992. hid = wanderingHeroes[i]->id;
  993. h & hid;
  994. if (!h.saving)
  995. {
  996. const CGHeroInstance *hero = cb->getHero(hid);
  997. wanderingHeroes += hero;
  998. }
  999. }
  1000. }
  1001. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1002. {
  1003. serializeTempl(h,version);
  1004. }
  1005. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1006. {
  1007. serializeTempl(h,version);
  1008. firstCall = -1;
  1009. }
  1010. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  1011. {
  1012. if(!LOCPLINT->makingTurn)
  1013. return false;
  1014. if (!h)
  1015. return false; //can't find hero
  1016. if (adventureInt && adventureInt->isHeroSleeping(h))
  1017. {
  1018. adventureInt->sleepWake.clickLeft(true, false);
  1019. adventureInt->sleepWake.clickLeft(false, true);
  1020. //could've just called
  1021. //adventureInt->fsleepWake();
  1022. //but no authentic button click/sound ;-)
  1023. }
  1024. int i = 1;
  1025. bool result = false; //TODO why not set to true anywhere?
  1026. {
  1027. //evil...
  1028. auto unlockEvents = vstd::makeUnlockGuard(eventsM);
  1029. auto unlockPim = vstd::makeUnlockGuard(*pim);
  1030. auto unlockGs = vstd::makeUnlockSharedGuard(cb->getGsMutex());
  1031. {
  1032. path.convert(0);
  1033. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1034. stillMoveHero.data = CONTINUE_MOVE;
  1035. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1036. enum TerrainTile::EterrainType newTerrain;
  1037. int sh = -1;
  1038. const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
  1039. for(i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
  1040. {
  1041. //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
  1042. if(path.nodes[i-1].coord.z != path.nodes[i].coord.z)
  1043. continue;
  1044. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  1045. if(path.nodes[i-1].turns)
  1046. {
  1047. stillMoveHero.data = STOP_MOVE;
  1048. break;
  1049. }
  1050. // Start a new sound for the hero movement or let the existing one carry on.
  1051. #if 0
  1052. // TODO
  1053. if (hero is flying && sh == -1)
  1054. sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
  1055. #endif
  1056. {
  1057. newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1058. if (newTerrain != currentTerrain)
  1059. {
  1060. CCS->soundh->stopSound(sh);
  1061. sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
  1062. currentTerrain = newTerrain;
  1063. }
  1064. }
  1065. stillMoveHero.data = WAITING_MOVE;
  1066. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1067. bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
  1068. cb->moveHero(h,endpos);
  1069. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1070. stillMoveHero.cond.wait(un);
  1071. if (guarded) // Abort movement if a guard was fought.
  1072. break;
  1073. }
  1074. CCS->soundh->stopSound(sh);
  1075. }
  1076. //RAII unlocks
  1077. }
  1078. if (adventureInt)
  1079. {
  1080. // (i == 0) means hero went through all the path
  1081. adventureInt->updateMoveHero(h, (i != 0));
  1082. adventureInt->updateNextHero(h);
  1083. }
  1084. return result;
  1085. }
  1086. bool CPlayerInterface::shiftPressed() const
  1087. {
  1088. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1089. }
  1090. bool CPlayerInterface::altPressed() const
  1091. {
  1092. return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
  1093. }
  1094. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  1095. {
  1096. if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
  1097. { //FIXME: why there is no currentPath at game start?
  1098. onEnd();
  1099. return;
  1100. }
  1101. waitWhileDialog();
  1102. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1103. while(dialogs.size())
  1104. {
  1105. auto unlock = vstd::makeUnlockGuard(*pim);
  1106. SDL_Delay(20);
  1107. }
  1108. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  1109. cgw->quit->callback += onEnd;
  1110. GH.pushInt(cgw);
  1111. }
  1112. /**
  1113. * Shows the dialog that appears when right-clicking an artifact that can be assembled
  1114. * into a combinational one on an artifact screen. Does not require the combination of
  1115. * artifacts to be legal.
  1116. * @param artifactID ID of a constituent artifact.
  1117. * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
  1118. * is false.
  1119. * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
  1120. */
  1121. void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
  1122. {
  1123. const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
  1124. std::string text = artifact.Description();
  1125. text += "\n\n";
  1126. std::vector<CComponent*> scs;
  1127. if (assemble) {
  1128. const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
  1129. // You possess all of the components to...
  1130. text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
  1131. // Picture of assembled artifact at bottom.
  1132. CComponent* sc = new CComponent;
  1133. sc->type = CComponent::artifact;
  1134. sc->subtype = assembledArtifact.id;
  1135. sc->description = assembledArtifact.Description();
  1136. sc->subtitle = assembledArtifact.Name();
  1137. scs.push_back(sc);
  1138. } else {
  1139. // Do you wish to disassemble this artifact?
  1140. text += CGI->generaltexth->allTexts[733];
  1141. }
  1142. showYesNoDialog(text, scs, onYes, onNo, true);
  1143. }
  1144. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1145. {
  1146. if(stillMoveHero.get() == DURING_MOVE)
  1147. stillMoveHero.setn(CONTINUE_MOVE);
  1148. }
  1149. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1150. {
  1151. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1152. GH.pushInt(new CExchangeWindow(hero1, hero2));
  1153. }
  1154. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1155. {
  1156. //redraw minimap if owner changed
  1157. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1158. if(sop->what == ObjProperty::OWNER)
  1159. {
  1160. const CGObjectInstance * obj = cb->getObj(sop->id);
  1161. std::set<int3> pos = obj->getBlockedPos();
  1162. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1163. {
  1164. if(cb->isVisible(*it))
  1165. adventureInt->minimap.showTile(*it);
  1166. }
  1167. if(obj->ID == GameConstants::TOWNI_TYPE)
  1168. {
  1169. if(obj->tempOwner == playerID)
  1170. towns.push_back(static_cast<const CGTownInstance *>(obj));
  1171. else
  1172. towns -= obj;
  1173. }
  1174. assert(cb->getTownsInfo().size() == towns.size());
  1175. }
  1176. }
  1177. void CPlayerInterface::recreateHeroTownList()
  1178. {
  1179. std::vector <const CGHeroInstance *> newWanderingHeroes;
  1180. std::vector<const CGHeroInstance*> allHeroes = cb->getHeroesInfo();
  1181. //applying current heroes order to new heroes info
  1182. int j;
  1183. for (int i = 0; i < wanderingHeroes.size(); i++)
  1184. if ((j = vstd::find_pos(allHeroes, wanderingHeroes[i])) >= 0)
  1185. if (!allHeroes[j]->inTownGarrison)
  1186. {
  1187. newWanderingHeroes += allHeroes[j];
  1188. allHeroes -= allHeroes[j];
  1189. }
  1190. //all the rest of new heroes go the end of the list
  1191. wanderingHeroes.clear();
  1192. wanderingHeroes = newWanderingHeroes;
  1193. newWanderingHeroes.clear();
  1194. for (int i = 0; i < allHeroes.size(); i++)
  1195. if (!allHeroes[i]->inTownGarrison)
  1196. wanderingHeroes += allHeroes[i];
  1197. std::vector<const CGTownInstance*> newTowns;
  1198. std::vector<const CGTownInstance*> allTowns = cb->getTownsInfo();
  1199. for (int i = 0; i < towns.size(); i++)
  1200. if ((j = vstd::find_pos(allTowns, towns[i])) >= 0)
  1201. {
  1202. newTowns += allTowns[j];
  1203. allTowns -= allTowns[j];
  1204. }
  1205. towns.clear();
  1206. towns = newTowns;
  1207. newTowns.clear();
  1208. for(int i = 0; i < allTowns.size(); i++)
  1209. towns.push_back(allTowns[i]);
  1210. adventureInt->updateNextHero(NULL);
  1211. }
  1212. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1213. {
  1214. if(pos < 0 || pos >= wanderingHeroes.size())
  1215. return NULL;
  1216. return wanderingHeroes[pos];
  1217. }
  1218. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1219. {
  1220. waitWhileDialog();
  1221. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1222. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
  1223. GH.pushInt(cr);
  1224. }
  1225. void CPlayerInterface::waitWhileDialog()
  1226. {
  1227. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1228. while(showingDialog->data)
  1229. showingDialog->cond.wait(un);
  1230. }
  1231. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1232. {
  1233. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1234. int state = obj->state();
  1235. std::vector<si32> cost;
  1236. obj->getBoatCost(cost);
  1237. CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
  1238. GH.pushInt(csw);
  1239. }
  1240. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1241. {
  1242. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1243. //we might have built a boat in shipyard in opened town screen
  1244. if(obj->ID == 8
  1245. && LOCPLINT->castleInt
  1246. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1247. {
  1248. CCS->soundh->playSound(soundBase::newBuilding);
  1249. LOCPLINT->castleInt->addBuilding(20);
  1250. }
  1251. }
  1252. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1253. {
  1254. waitWhileDialog();
  1255. adventureInt->centerOn (pos);
  1256. if(focusTime)
  1257. {
  1258. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
  1259. if(activeAdv)
  1260. adventureInt->deactivate();
  1261. SDL_Delay(focusTime);
  1262. if(activeAdv)
  1263. adventureInt->activate();
  1264. }
  1265. }
  1266. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1267. {
  1268. if(obj->ID == GameConstants::HEROI_TYPE && obj->tempOwner == playerID)
  1269. {
  1270. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1271. heroKilled(h);
  1272. }
  1273. }
  1274. bool CPlayerInterface::ctrlPressed() const
  1275. {
  1276. return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
  1277. }
  1278. void CPlayerInterface::update()
  1279. {
  1280. while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
  1281. boost::this_thread::sleep(boost::posix_time::milliseconds(15));
  1282. if(terminate_cond.get())
  1283. return;
  1284. boost::unique_lock<boost::recursive_mutex> un(*pim, boost::adopt_lock); //create lock from already owned mutex
  1285. //make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
  1286. boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
  1287. //if there are any waiting dialogs, show them
  1288. if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
  1289. {
  1290. showingDialog->set(true);
  1291. GH.pushInt(dialogs.front());
  1292. dialogs.pop_front();
  1293. }
  1294. //in some conditions we may receive calls before selection is initialized - we must ignore them
  1295. if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
  1296. {
  1297. return;
  1298. }
  1299. // Handles mouse and key input
  1300. GH.updateTime();
  1301. GH.handleEvents();
  1302. if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
  1303. GH.totalRedraw();
  1304. else
  1305. GH.simpleRedraw();
  1306. if (settings["general"]["showfps"].Bool())
  1307. GH.drawFPSCounter();
  1308. // draw the mouse cursor and update the screen
  1309. CCS->curh->draw1();
  1310. CSDL_Ext::update(screen);
  1311. CCS->curh->draw2();
  1312. }
  1313. int CPlayerInterface::getLastIndex( std::string namePrefix)
  1314. {
  1315. using namespace boost::filesystem;
  1316. using namespace boost::algorithm;
  1317. std::map<std::time_t, int> dates; //save number => datestamp
  1318. directory_iterator enddir;
  1319. for (directory_iterator dir(GVCMIDirs.UserPath + "/Games"); dir!=enddir; dir++)
  1320. {
  1321. if(is_regular(dir->status()))
  1322. {
  1323. std::string name = dir->path().leaf();
  1324. if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
  1325. {
  1326. char nr = name[namePrefix.size()];
  1327. if(std::isdigit(nr))
  1328. {
  1329. dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
  1330. }
  1331. }
  1332. }
  1333. }
  1334. if(dates.size())
  1335. return (--dates.end())->second; //return latest file number
  1336. return 0;
  1337. }
  1338. void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
  1339. {
  1340. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1341. {
  1342. //ho->moveDir = 1;
  1343. ho->isStanding = false;
  1344. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1345. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1346. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1347. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1348. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1349. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1350. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1351. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1352. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1353. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1354. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1355. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1356. 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);
  1357. 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);
  1358. 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);
  1359. 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);
  1360. 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);
  1361. 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);
  1362. }
  1363. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1364. {
  1365. //ho->moveDir = 2;
  1366. ho->isStanding = false;
  1367. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1368. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1369. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1370. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1371. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1372. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1373. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1374. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1375. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1376. 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);
  1377. 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);
  1378. 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);
  1379. }
  1380. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1381. {
  1382. //ho->moveDir = 3;
  1383. ho->isStanding = false;
  1384. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1385. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1386. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1387. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1388. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1389. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1390. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1391. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1392. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1393. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1394. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1395. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1396. 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);
  1397. 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);
  1398. 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);
  1399. 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);
  1400. 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);
  1401. 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);
  1402. }
  1403. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1404. {
  1405. //ho->moveDir = 4;
  1406. ho->isStanding = false;
  1407. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1408. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1409. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1410. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1411. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1412. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1413. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1414. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1415. 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);
  1416. 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);
  1417. }
  1418. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1419. {
  1420. //ho->moveDir = 5;
  1421. ho->isStanding = false;
  1422. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1423. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1424. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1425. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1426. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1427. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1428. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1429. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1430. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1431. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1432. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1433. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1434. 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);
  1435. 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);
  1436. 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);
  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][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1439. 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);
  1440. }
  1441. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1442. {
  1443. //ho->moveDir = 6;
  1444. ho->isStanding = false;
  1445. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1446. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1447. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1448. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1449. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1450. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1451. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1452. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1453. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1454. 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);
  1455. 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);
  1456. 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);
  1457. }
  1458. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1459. {
  1460. //ho->moveDir = 7;
  1461. ho->isStanding = false;
  1462. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1463. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1464. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1465. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1466. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1467. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1468. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1469. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1470. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1471. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1472. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1473. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1474. 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);
  1475. 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);
  1476. 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);
  1477. 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);
  1478. 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);
  1479. 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);
  1480. }
  1481. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1482. {
  1483. //ho->moveDir = 8;
  1484. ho->isStanding = false;
  1485. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1486. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1487. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1488. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1489. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1490. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1491. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1492. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1493. 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);
  1494. 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);
  1495. }
  1496. }
  1497. void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
  1498. {
  1499. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1500. {
  1501. //setting advmap shift
  1502. adventureInt->terrain.moveX = i-32;
  1503. adventureInt->terrain.moveY = i-32;
  1504. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1505. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1506. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1507. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1508. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1509. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1510. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1511. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1512. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1513. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1514. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1515. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1516. }
  1517. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1518. {
  1519. //setting advmap shift
  1520. adventureInt->terrain.moveY = i-32;
  1521. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1522. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1523. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1524. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1525. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1526. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1527. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1528. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1529. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1530. }
  1531. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1532. {
  1533. //setting advmap shift
  1534. adventureInt->terrain.moveX = -i+32;
  1535. adventureInt->terrain.moveY = i-32;
  1536. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1537. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1538. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1539. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1540. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1541. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1542. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1543. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1544. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1545. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1546. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1547. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1548. }
  1549. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1550. {
  1551. //setting advmap shift
  1552. adventureInt->terrain.moveX = -i+32;
  1553. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1554. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1555. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1556. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1557. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1558. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1559. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1560. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1561. }
  1562. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1563. {
  1564. //setting advmap shift
  1565. adventureInt->terrain.moveX = -i+32;
  1566. adventureInt->terrain.moveY = -i+32;
  1567. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1568. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1569. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1570. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1571. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1572. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1573. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1574. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1575. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1576. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1577. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1578. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1579. }
  1580. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1581. {
  1582. //setting advmap shift
  1583. adventureInt->terrain.moveY = -i+32;
  1584. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1585. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1586. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1587. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1588. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1589. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1590. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1591. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1592. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1593. }
  1594. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1595. {
  1596. //setting advmap shift
  1597. adventureInt->terrain.moveX = i-32;
  1598. adventureInt->terrain.moveY = -i+32;
  1599. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1600. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1601. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1602. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1603. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1604. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1605. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1606. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1607. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1608. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1609. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1610. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1611. }
  1612. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1613. {
  1614. //setting advmap shift
  1615. adventureInt->terrain.moveX = i-32;
  1616. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1617. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1618. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1619. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1620. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1621. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1622. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1623. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1624. }
  1625. }
  1626. void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
  1627. {
  1628. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1629. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  1630. {
  1631. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1632. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1633. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1634. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1635. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1636. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1637. }
  1638. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  1639. {
  1640. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1641. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1642. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1643. }
  1644. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  1645. {
  1646. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1647. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1648. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1649. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1650. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1651. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1652. }
  1653. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  1654. {
  1655. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1656. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1657. }
  1658. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  1659. {
  1660. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1661. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1662. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1663. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1664. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1665. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1666. }
  1667. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  1668. {
  1669. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1670. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1671. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1672. }
  1673. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  1674. {
  1675. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1676. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1677. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1678. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1679. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1680. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1681. }
  1682. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  1683. {
  1684. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1685. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1686. }
  1687. //restoring good rects
  1688. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  1689. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  1690. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  1691. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  1692. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  1693. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  1694. //restoring good order of objects
  1695. 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);
  1696. 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);
  1697. 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);
  1698. 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);
  1699. 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);
  1700. 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);
  1701. }
  1702. void CPlayerInterface::gameOver(ui8 player, bool victory )
  1703. {
  1704. if(LOCPLINT != this)
  1705. return;
  1706. if(player == playerID)
  1707. {
  1708. if(!victory)
  1709. showInfoDialog(CGI->generaltexth->allTexts[95]);
  1710. // else
  1711. // showInfoDialog("Placeholder message: you won!");
  1712. makingTurn = true;
  1713. while(showingDialog->get() || dialogs.size()); //wait till all dialogs are displayed and closed
  1714. makingTurn = false;
  1715. howManyPeople--;
  1716. if(!howManyPeople) //all human players eliminated
  1717. {
  1718. if(cb->getStartInfo()->mode != StartInfo::CAMPAIGN)
  1719. requestReturningToMainMenu();
  1720. else
  1721. requestStoppingClient();
  1722. }
  1723. cb->unregisterMyInterface(); //we already won/lost, nothing else matters
  1724. }
  1725. else
  1726. {
  1727. if(!victory && cb->getPlayerStatus(playerID) == PlayerState::INGAME) //enemy has lost
  1728. {
  1729. std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
  1730. boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
  1731. showInfoDialog(txt,std::vector<CComponent*>(1, new CComponent(CComponent::flag, player, 0)));
  1732. }
  1733. }
  1734. }
  1735. void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
  1736. {
  1737. }
  1738. void CPlayerInterface::showPuzzleMap()
  1739. {
  1740. waitWhileDialog();
  1741. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1742. //TODO: interface should not know the real position of Grail...
  1743. double ratio = 0;
  1744. int3 grailPos = cb->getGrailPos(ratio);
  1745. GH.pushInt(new CPuzzleWindow(grailPos, ratio));
  1746. }
  1747. void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  1748. {
  1749. if (spellID == Spells::FLY || spellID == Spells::WATER_WALK)
  1750. {
  1751. eraseCurrentPathOf(caster, false);
  1752. }
  1753. }
  1754. void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
  1755. {
  1756. if(checkForExistanceOfPath)
  1757. {
  1758. assert(vstd::contains(paths, ho));
  1759. }
  1760. else if (!vstd::contains(paths, ho))
  1761. {
  1762. return;
  1763. }
  1764. assert(ho == adventureInt->selection);
  1765. paths.erase(ho);
  1766. adventureInt->terrain.currentPath = NULL;
  1767. adventureInt->updateMoveHero(ho, false);
  1768. }
  1769. void CPlayerInterface::removeLastNodeFromPath(const CGHeroInstance *ho)
  1770. {
  1771. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1772. 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
  1773. eraseCurrentPathOf(ho);
  1774. }
  1775. CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
  1776. {
  1777. if(vstd::contains(paths,h)) //hero has assigned path
  1778. {
  1779. CGPath &path = paths[h];
  1780. if(!path.nodes.size())
  1781. {
  1782. tlog3 << "Warning: empty path found...\n";
  1783. paths.erase(h);
  1784. }
  1785. else
  1786. {
  1787. assert(h->getPosition(false) == path.startPos());
  1788. //update the hero path in case of something has changed on map
  1789. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1790. return &path;
  1791. else
  1792. paths.erase(h);
  1793. }
  1794. }
  1795. return NULL;
  1796. }
  1797. void CPlayerInterface::acceptTurn()
  1798. {
  1799. if(settings["session"]["autoSkip"].Bool())
  1800. {
  1801. while(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1802. iw->close();
  1803. }
  1804. waitWhileDialog();
  1805. if(howManyPeople > 1)
  1806. adventureInt->startTurn();
  1807. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1808. //Select sound for day start
  1809. int totalDays = cb->getDate();
  1810. int day = cb->getDate(1);
  1811. int week = cb->getDate(2);
  1812. if (totalDays == 1)
  1813. CCS->soundh->playSound(soundBase::newDay);
  1814. else if (day != 1)
  1815. CCS->soundh->playSound(soundBase::newDay);
  1816. else if (week != 1)
  1817. CCS->soundh->playSound(soundBase::newWeek);
  1818. else
  1819. CCS->soundh->playSound(soundBase::newMonth);
  1820. adventureInt->infoBar.newDay(day);
  1821. //select first hero if available.
  1822. //TODO: check if hero is slept
  1823. if(wanderingHeroes.size())
  1824. adventureInt->select(wanderingHeroes.front());
  1825. else
  1826. adventureInt->select(towns.front());
  1827. adventureInt->showAll(screen);
  1828. if(settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
  1829. {
  1830. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
  1831. iw->close();
  1832. adventureInt->endTurn.callback();
  1833. }
  1834. }
  1835. void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
  1836. {
  1837. std::string hlp;
  1838. CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1839. int msgToShow = -1;
  1840. CGHeroInstance::ECanDig isDiggingPossible = h->diggingStatus();
  1841. if(hlp.length())
  1842. isDiggingPossible = CGHeroInstance::TILE_OCCUPIED; //TODO integrate with canDig
  1843. switch(isDiggingPossible)
  1844. {
  1845. case CGHeroInstance::CAN_DIG:
  1846. break;
  1847. case CGHeroInstance::LACK_OF_MOVEMENT:
  1848. msgToShow = 56; //"Digging for artifacts requires a whole day, try again tomorrow."
  1849. break;
  1850. case CGHeroInstance::TILE_OCCUPIED:
  1851. msgToShow = 97; //Try searching on clear ground.
  1852. break;
  1853. case CGHeroInstance::WRONG_TERRAIN:
  1854. msgToShow = 60; ////Try looking on land!
  1855. break;
  1856. default:
  1857. assert(0);
  1858. }
  1859. if(msgToShow < 0)
  1860. cb->dig(h);
  1861. else
  1862. showInfoDialog(CGI->generaltexth->allTexts[msgToShow]);
  1863. }
  1864. void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
  1865. {
  1866. adventureInt->infoBar.updateSelection(specific);
  1867. // if (adventureInt->selection == specific)
  1868. // adventureInt->infoBar.showAll(screen);
  1869. }
  1870. void CPlayerInterface::battleNewRoundFirst( int round )
  1871. {
  1872. if(LOCPLINT != this)
  1873. { //another local interface should do this
  1874. return;
  1875. }
  1876. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1877. battleInt->newRoundFirst(round);
  1878. }
  1879. void CPlayerInterface::stopMovement()
  1880. {
  1881. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1882. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1883. }
  1884. void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  1885. {
  1886. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1887. if(market->o->ID == 2) //Altar
  1888. {
  1889. //EEMarketMode mode = market->availableModes().front();
  1890. if(market->allowsTrade(EMarketMode::ARTIFACT_EXP) && visitor->getAlignment() != EAlignment::EVIL)
  1891. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::ARTIFACT_EXP));
  1892. else if(market->allowsTrade(EMarketMode::CREATURE_EXP) && visitor->getAlignment() != EAlignment::GOOD)
  1893. GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::CREATURE_EXP));
  1894. }
  1895. else
  1896. GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
  1897. }
  1898. void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  1899. {
  1900. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1901. CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
  1902. GH.pushInt(cuw);
  1903. }
  1904. void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  1905. {
  1906. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1907. CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
  1908. GH.pushInt(chfw);
  1909. }
  1910. void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  1911. {
  1912. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1913. if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
  1914. cmw->artifactsChanged(false);
  1915. }
  1916. void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
  1917. {
  1918. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1919. CTavernWindow *tv = new CTavernWindow(townOrTavern);
  1920. GH.pushInt(tv);
  1921. }
  1922. void CPlayerInterface::showThievesGuildWindow (const CGObjectInstance * obj)
  1923. {
  1924. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1925. CThievesGuildWindow *tgw = new CThievesGuildWindow(obj);
  1926. GH.pushInt(tgw);
  1927. }
  1928. void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
  1929. {
  1930. if(obj->state())
  1931. {
  1932. MetaString txt;
  1933. obj->getProblemText(txt);
  1934. showInfoDialog(txt.toString());
  1935. }
  1936. else
  1937. showShipyardDialog(obj);
  1938. }
  1939. void CPlayerInterface::requestReturningToMainMenu()
  1940. {
  1941. sendCustomEvent(2);
  1942. }
  1943. void CPlayerInterface::requestStoppingClient()
  1944. {
  1945. sendCustomEvent(3);
  1946. }
  1947. void CPlayerInterface::sendCustomEvent( int code )
  1948. {
  1949. SDL_Event event;
  1950. event.type = SDL_USEREVENT;
  1951. event.user.code = code;
  1952. SDL_PushEvent(&event);
  1953. }
  1954. void CPlayerInterface::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
  1955. {
  1956. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1957. garrisonChanged(location.army);
  1958. }
  1959. void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
  1960. {
  1961. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1962. garrisonChanged(location.army);
  1963. }
  1964. void CPlayerInterface::stacksErased(const StackLocation &location)
  1965. {
  1966. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1967. garrisonChanged(location.army);
  1968. }
  1969. #define UPDATE_IF(LOC) static_cast<const CArmedInstance*>(adventureInt->infoBar.curSel) == LOC.army.get()
  1970. void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  1971. {
  1972. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1973. garrisonChanged(loc1.army, UPDATE_IF(loc1));
  1974. if(loc2.army != loc1.army)
  1975. garrisonChanged(loc2.army, UPDATE_IF(loc2));
  1976. }
  1977. void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  1978. {
  1979. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1980. garrisonChanged(location.army);
  1981. }
  1982. void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  1983. {
  1984. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1985. //bool updateInfobox = true;
  1986. garrisonChanged(src.army, UPDATE_IF(src));
  1987. if(dst.army != src.army)
  1988. garrisonChanged(dst.army, UPDATE_IF(dst));
  1989. }
  1990. #undef UPDATE_IF
  1991. void CPlayerInterface::artifactPut(const ArtifactLocation &al)
  1992. {
  1993. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1994. }
  1995. void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
  1996. {
  1997. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1998. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  1999. {
  2000. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2001. {
  2002. (dynamic_cast<CArtifactHolder*>(isa))->artifactRemoved(al);
  2003. }
  2004. }
  2005. }
  2006. void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  2007. {
  2008. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2009. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2010. {
  2011. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2012. {
  2013. (dynamic_cast<CArtifactHolder*>(isa))->artifactMoved(src, dst);
  2014. }
  2015. }
  2016. }
  2017. void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
  2018. {
  2019. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2020. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2021. {
  2022. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2023. {
  2024. (dynamic_cast<CArtifactHolder*>(isa))->artifactAssembled(al);
  2025. }
  2026. }
  2027. }
  2028. void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
  2029. {
  2030. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2031. BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
  2032. {
  2033. if(isa->type & IShowActivatable::WITH_ARTIFACTS)
  2034. {
  2035. (dynamic_cast<CArtifactHolder*>(isa))->artifactDisassembled(al);
  2036. }
  2037. }
  2038. }
  2039. void CPlayerInterface::playerStartsTurn(ui8 player)
  2040. {
  2041. {
  2042. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2043. if(!GH.listInt.size())
  2044. {
  2045. GH.pushInt(adventureInt);
  2046. adventureInt->activateKeys();
  2047. }
  2048. if(howManyPeople == 1)
  2049. {
  2050. GH.curInt = this;
  2051. adventureInt->startTurn();
  2052. }
  2053. }
  2054. if(player != playerID && this == LOCPLINT)
  2055. {
  2056. waitWhileDialog();
  2057. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2058. adventureInt->aiTurnStarted();
  2059. }
  2060. }
  2061. void CPlayerInterface::waitForAllDialogs()
  2062. {
  2063. {
  2064. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2065. while(dialogs.size())
  2066. {
  2067. auto unlock = vstd::makeUnlockGuard(*pim);
  2068. SDL_Delay(5);
  2069. }
  2070. }
  2071. waitWhileDialog();
  2072. }
  2073. CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
  2074. {
  2075. spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
  2076. spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
  2077. }