CPlayerInterface.cpp 76 KB

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