CPlayerInterface.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. #include "../stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "../CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CCursorHandler.h"
  7. #include "CGameInfo.h"
  8. #include "CHeroWindow.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. //#include "SDL_Extensions.h"
  12. #include "SDL_Extensions.h"
  13. //#include "SDL_framerate.h"
  14. #include "SDL_framerate.h"
  15. #include "CConfigHandler.h"
  16. #include "CCreatureAnimation.h"
  17. #include "Graphics.h"
  18. #include "../hch/CArtHandler.h"
  19. #include "../hch/CGeneralTextHandler.h"
  20. #include "../hch/CHeroHandler.h"
  21. #include "../hch/CLodHandler.h"
  22. #include "../hch/CObjectHandler.h"
  23. #include "../lib/Connection.h"
  24. #include "../hch/CSpellHandler.h"
  25. #include "../hch/CTownHandler.h"
  26. #include "../lib/CondSh.h"
  27. #include "../lib/NetPacks.h"
  28. #include "../lib/map.h"
  29. #include "../mapHandler.h"
  30. #include "../timeHandler.h"
  31. #include <boost/lexical_cast.hpp>
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/algorithm/string/replace.hpp>
  34. #include <boost/assign/std/vector.hpp>
  35. #include <boost/assign/list_of.hpp>
  36. #include <boost/date_time/posix_time/posix_time.hpp>
  37. #include <boost/thread.hpp>
  38. #include <cmath>
  39. #include <queue>
  40. #include <sstream>
  41. #ifdef min
  42. #undef min
  43. #endif
  44. #ifdef max
  45. #undef max
  46. #endif
  47. /*
  48. * CPlayerInterface.cpp, part of VCMI engine
  49. *
  50. * Authors: listed in file AUTHORS in main folder
  51. *
  52. * License: GNU General Public License v2.0 or later
  53. * Full text of license available in license.txt file, in main folder
  54. *
  55. */
  56. using namespace boost::assign;
  57. using namespace CSDL_Ext;
  58. void processCommand(const std::string &message, CClient *&client);
  59. extern std::queue<SDL_Event*> events;
  60. extern boost::mutex eventsM;
  61. CPlayerInterface * LOCPLINT;
  62. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  63. CondSh<EMoveState> stillMoveHero; //used during hero movement
  64. struct OCM_HLP_CGIN
  65. {
  66. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  67. {
  68. return (*a.first)<(*b.first);
  69. }
  70. } ocmptwo_cgin ;
  71. CPlayerInterface::CPlayerInterface(int Player, int serial)
  72. {
  73. LOCPLINT = this;
  74. curAction = NULL;
  75. playerID=Player;
  76. serialID=serial;
  77. human=true;
  78. castleInt = NULL;
  79. adventureInt = NULL;
  80. battleInt = NULL;
  81. pim = new boost::recursive_mutex;
  82. makingTurn = false;
  83. showingDialog = new CondSh<bool>(false);
  84. sysOpts = GDefaultOptions;
  85. //initializing framerate keeper
  86. mainFPSmng = new FPSmanager;
  87. SDL_initFramerate(mainFPSmng);
  88. SDL_setFramerate(mainFPSmng, 48);
  89. //framerate keeper initialized
  90. cingconsole = new CInGameConsole;
  91. }
  92. CPlayerInterface::~CPlayerInterface()
  93. {
  94. delete pim;
  95. delete showingDialog;
  96. delete mainFPSmng;
  97. delete adventureInt;
  98. delete cingconsole;
  99. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
  100. SDL_FreeSurface(i->second);
  101. for(std::map<int,SDL_Surface*>::iterator i=graphics->townWins.begin(); i!= graphics->townWins.end(); i++)
  102. SDL_FreeSurface(i->second);
  103. }
  104. void CPlayerInterface::init(ICallback * CB)
  105. {
  106. cb = dynamic_cast<CCallback*>(CB);
  107. adventureInt = new CAdvMapInt(playerID);
  108. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  109. for(int i=0;i<tt.size();i++)
  110. {
  111. SDL_Surface * pom = infoWin(tt[i]);
  112. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  113. }
  114. recreateWanderingHeroes();
  115. }
  116. void CPlayerInterface::yourTurn()
  117. {
  118. try
  119. {
  120. LOCPLINT = this;
  121. makingTurn = true;
  122. static bool firstCall = true;
  123. static int autosaveCount = 0;
  124. if(firstCall)
  125. firstCall = false;
  126. else
  127. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  128. autosaveCount %= 5;
  129. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  130. SDL_FreeSurface(i->second);
  131. graphics->heroWins.clear();
  132. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  133. for(int i=0;i<hh.size();i++)
  134. {
  135. SDL_Surface * pom = infoWin(hh[i]);
  136. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  137. }
  138. /* TODO: This isn't quite right. First day in game should play
  139. * NEWDAY. And we don't play NEWMONTH. */
  140. int day = cb->getDate(1);
  141. if (day != 1)
  142. CGI->soundh->playSound(soundBase::newDay);
  143. else
  144. CGI->soundh->playSound(soundBase::newWeek);
  145. adventureInt->infoBar.newDay(day);
  146. //select first hero if available.
  147. //TODO: check if hero is slept
  148. if(wanderingHeroes.size())
  149. adventureInt->select(wanderingHeroes[0]);
  150. else
  151. adventureInt->select(adventureInt->townList.items[0]);
  152. adventureInt->showAll(screen);
  153. pushInt(adventureInt);
  154. adventureInt->KeyInterested::activate();
  155. timeHandler th;
  156. th.getDif();
  157. while(makingTurn) // main loop
  158. {
  159. updateWater();
  160. pim->lock();
  161. //if there are any waiting dialogs, show them
  162. if(dialogs.size() && !showingDialog->get())
  163. {
  164. showingDialog->set(true);
  165. pushInt(dialogs.front());
  166. dialogs.pop_front();
  167. }
  168. int tv = th.getDif();
  169. std::list<TimeInterested*> hlp = timeinterested;
  170. for (std::list<TimeInterested*>::iterator i=hlp.begin(); i != hlp.end();i++)
  171. {
  172. if(!vstd::contains(timeinterested,*i)) continue;
  173. if ((*i)->toNextTick>=0)
  174. (*i)->toNextTick-=tv;
  175. if ((*i)->toNextTick<0)
  176. (*i)->tick();
  177. }
  178. while(true)
  179. {
  180. SDL_Event *ev = NULL;
  181. {
  182. boost::unique_lock<boost::mutex> lock(eventsM);
  183. if(!events.size())
  184. {
  185. break;
  186. }
  187. else
  188. {
  189. ev = events.front();
  190. events.pop();
  191. }
  192. }
  193. handleEvent(ev);
  194. delete ev;
  195. }
  196. if(!adventureInt->active && adventureInt->scrollingDir) //player force map scrolling though interface is disabled
  197. {
  198. totalRedraw();
  199. }
  200. else
  201. {
  202. //update only top interface and draw background
  203. if(objsToBlit.size() > 1)
  204. blitAt(screen2,0,0,screen); //blit background
  205. objsToBlit.back()->show(screen); //blit active interface/window
  206. }
  207. CGI->curh->draw1();
  208. CSDL_Ext::update(screen);
  209. CGI->curh->draw2();
  210. pim->unlock();
  211. SDL_framerateDelay(mainFPSmng);
  212. }
  213. adventureInt->KeyInterested::deactivate();
  214. popInt(adventureInt);
  215. cb->endTurn();
  216. } HANDLE_EXCEPTION
  217. }
  218. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  219. {
  220. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  221. for(int h=0; h<hlp.objects.size(); ++h)
  222. if(hlp.objects[h].first->id==hid)
  223. {
  224. hlp.objects[h].second = r;
  225. return;
  226. }
  227. }
  228. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  229. {
  230. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  231. for(int h=0; h<hlp.objects.size(); ++h)
  232. if(hlp.objects[h].first->id==hid)
  233. {
  234. hlp.objects.erase(hlp.objects.begin()+h);
  235. return;
  236. }
  237. }
  238. void CPlayerInterface::heroMoved(const TryMoveHero & details)
  239. {
  240. boost::unique_lock<boost::recursive_mutex> un(*pim);
  241. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  242. adventureInt->centerOn(ho->pos); //actualizing screen pos
  243. adventureInt->minimap.draw(screen2);
  244. adventureInt->heroList.draw(screen2);
  245. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  246. return;
  247. //initializing objects and performing first step of move
  248. int3 hp = details.start;
  249. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  250. {
  251. if(details.result != TryMoveHero::FAILED)
  252. {
  253. adventureInt->paths.erase(ho);
  254. adventureInt->terrain.currentPath = NULL;
  255. }
  256. ho->isStanding = true;
  257. stillMoveHero.setn(STOP_MOVE);
  258. LOCPLINT->totalRedraw();
  259. return;
  260. }
  261. if (adventureInt->terrain.currentPath) //&& hero is moving
  262. {
  263. //remove one node from the path (the one we went)
  264. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  265. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  266. {
  267. adventureInt->paths.erase(ho);
  268. adventureInt->terrain.currentPath = NULL;
  269. }
  270. }
  271. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  272. {
  273. //ho->moveDir = 1;
  274. ho->isStanding = false;
  275. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  276. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  277. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  278. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  279. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  280. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  281. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  282. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  283. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  284. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  285. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  286. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  287. 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);
  288. 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);
  289. 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);
  290. 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);
  291. 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);
  292. 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);
  293. }
  294. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  295. {
  296. //ho->moveDir = 2;
  297. ho->isStanding = false;
  298. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  299. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  300. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  301. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  302. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  303. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  304. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  305. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  306. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  307. 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);
  308. 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);
  309. 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);
  310. }
  311. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  312. {
  313. //ho->moveDir = 3;
  314. ho->isStanding = false;
  315. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  316. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  317. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  318. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  319. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  320. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  321. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  322. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  323. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  324. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  325. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  326. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  327. 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);
  328. 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);
  329. 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);
  330. 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);
  331. 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);
  332. 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);
  333. }
  334. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  335. {
  336. //ho->moveDir = 4;
  337. ho->isStanding = false;
  338. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  339. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  340. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  341. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  342. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  343. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  344. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  345. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  346. 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);
  347. 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);
  348. }
  349. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  350. {
  351. //ho->moveDir = 5;
  352. ho->isStanding = false;
  353. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  354. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  355. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  356. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  357. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  358. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  359. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  360. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  361. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  362. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  363. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  364. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  365. 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);
  366. 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);
  367. 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);
  368. 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);
  369. 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);
  370. 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);
  371. }
  372. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  373. {
  374. //ho->moveDir = 6;
  375. ho->isStanding = false;
  376. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  377. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  378. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  379. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  380. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  381. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  382. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  383. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  384. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  385. 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);
  386. 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);
  387. 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);
  388. }
  389. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  390. {
  391. //ho->moveDir = 7;
  392. ho->isStanding = false;
  393. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  394. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  395. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  396. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  397. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  398. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  399. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  400. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  401. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  402. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  403. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  404. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  405. 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);
  406. 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);
  407. 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);
  408. 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);
  409. 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);
  410. 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);
  411. }
  412. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  413. {
  414. //ho->moveDir = 8;
  415. ho->isStanding = false;
  416. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  417. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  418. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  419. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  420. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  421. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  422. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  423. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  424. 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);
  425. 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);
  426. }
  427. //first initializing done
  428. SDL_framerateDelay(mainFPSmng); // after first move
  429. //main moving
  430. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  431. {
  432. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  433. {
  434. //setting advmap shift
  435. adventureInt->terrain.moveX = i-32;
  436. adventureInt->terrain.moveY = i-32;
  437. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  438. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  439. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  440. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  441. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  442. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  443. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  444. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  445. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  446. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  447. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  448. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  449. }
  450. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  451. {
  452. //setting advmap shift
  453. adventureInt->terrain.moveY = i-32;
  454. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  455. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  456. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  457. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  458. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  459. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  460. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  461. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  462. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  463. }
  464. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  465. {
  466. //setting advmap shift
  467. adventureInt->terrain.moveX = -i+32;
  468. adventureInt->terrain.moveY = i-32;
  469. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  470. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  471. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  472. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  473. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  474. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  475. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  476. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  477. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  478. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  479. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  480. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  481. }
  482. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  483. {
  484. //setting advmap shift
  485. adventureInt->terrain.moveX = -i+32;
  486. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  487. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  488. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  489. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  490. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  491. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  492. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  493. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  494. }
  495. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  496. {
  497. //setting advmap shift
  498. adventureInt->terrain.moveX = -i+32;
  499. adventureInt->terrain.moveY = -i+32;
  500. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  501. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  502. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  503. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  504. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  505. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  506. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  507. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  508. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  509. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  510. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  511. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  512. }
  513. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  514. {
  515. //setting advmap shift
  516. adventureInt->terrain.moveY = -i+32;
  517. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  518. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  519. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  520. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  521. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  522. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  523. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  524. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  525. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  526. }
  527. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  528. {
  529. //setting advmap shift
  530. adventureInt->terrain.moveX = i-32;
  531. adventureInt->terrain.moveY = -i+32;
  532. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  533. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  534. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  535. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  536. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  537. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  538. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  539. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  540. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  541. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  542. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  543. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  544. }
  545. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  546. {
  547. //setting advmap shift
  548. adventureInt->terrain.moveX = i-32;
  549. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  550. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  551. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  552. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  553. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  554. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  555. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  556. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  557. }
  558. adventureInt->updateScreen = true;
  559. adventureInt->show(screen);
  560. //LOCPLINT->adventureInt->show(); //updating screen
  561. CSDL_Ext::update(screen);
  562. SDL_Delay(5);
  563. SDL_framerateDelay(mainFPSmng); //for animation purposes
  564. } //for(int i=1; i<32; i+=4)
  565. //main moving done
  566. //finishing move
  567. //restoring adventureInt->terrain.move*
  568. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  569. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  570. {
  571. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  572. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  573. delObjRect(hp.x, hp.y, hp.z, ho->id);
  574. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  575. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  576. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  577. }
  578. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  579. {
  580. delObjRect(hp.x, hp.y, hp.z, ho->id);
  581. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  582. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  583. }
  584. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  585. {
  586. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  587. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  588. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  589. delObjRect(hp.x, hp.y, hp.z, ho->id);
  590. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  591. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  592. }
  593. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  594. {
  595. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  596. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  597. }
  598. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  599. {
  600. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  601. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  602. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  603. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  604. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  605. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  606. }
  607. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  608. {
  609. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  610. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  611. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  612. }
  613. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  614. {
  615. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  616. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  617. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  618. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  619. delObjRect(hp.x, hp.y, hp.z, ho->id);
  620. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  621. }
  622. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  623. {
  624. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  625. delObjRect(hp.x, hp.y, hp.z, ho->id);
  626. }
  627. //restoring good rects
  628. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  629. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  630. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  631. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  632. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  633. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  634. //restoring good order of objects
  635. 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);
  636. 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);
  637. 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);
  638. 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);
  639. 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);
  640. 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);
  641. ho->isStanding = true;
  642. //move finished
  643. adventureInt->minimap.draw(screen2);
  644. adventureInt->heroList.updateMove(ho);
  645. //check if user cancelled movement
  646. {
  647. boost::unique_lock<boost::mutex> un(eventsM);
  648. while(events.size())
  649. {
  650. SDL_Event *ev = events.front();
  651. events.pop();
  652. switch(ev->type)
  653. {
  654. case SDL_MOUSEBUTTONDOWN:
  655. stillMoveHero.setn(STOP_MOVE);
  656. break;
  657. case SDL_KEYDOWN:
  658. if(ev->key.keysym.sym < SDLK_F1 || ev->key.keysym.sym > SDLK_F15)
  659. stillMoveHero.setn(STOP_MOVE);
  660. break;
  661. }
  662. delete ev;
  663. }
  664. }
  665. if(stillMoveHero.get() == 1)
  666. stillMoveHero.setn(DURING_MOVE);
  667. }
  668. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  669. {
  670. boost::unique_lock<boost::recursive_mutex> un(*pim);
  671. graphics->heroWins.erase(hero->ID);
  672. wanderingHeroes -= hero;
  673. adventureInt->heroList.updateHList(hero);
  674. }
  675. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  676. {
  677. boost::unique_lock<boost::recursive_mutex> un(*pim);
  678. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  679. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  680. wanderingHeroes.push_back(hero);
  681. adventureInt->heroList.updateHList();
  682. }
  683. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  684. {
  685. castleInt = new CCastleInterface(town);
  686. CGI->musich->playMusic(castleInt->musicID, -1);
  687. LOCPLINT->pushInt(castleInt);
  688. }
  689. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  690. {
  691. if (specific)
  692. {
  693. switch (specific->ID)
  694. {
  695. case HEROI_TYPE:
  696. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  697. break;
  698. case TOWNI_TYPE:
  699. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  700. break;
  701. default:
  702. return NULL;
  703. break;
  704. }
  705. }
  706. else
  707. {
  708. switch (adventureInt->selection->ID)
  709. {
  710. case HEROI_TYPE:
  711. {
  712. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  713. return graphics->drawHeroInfoWin(curh);
  714. }
  715. case TOWNI_TYPE:
  716. {
  717. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  718. }
  719. default:
  720. tlog1 << "Strange... selection is neither hero nor town\n";
  721. return NULL;
  722. }
  723. }
  724. }
  725. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  726. {
  727. //sending active, hovered hoverable objects hover() call
  728. std::vector<Hoverable*> hlp;
  729. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  730. {
  731. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  732. {
  733. if (!(*i)->hovered)
  734. hlp.push_back((*i));
  735. }
  736. else if ((*i)->hovered)
  737. {
  738. (*i)->hover(false);
  739. }
  740. }
  741. for(int i=0; i<hlp.size();i++)
  742. hlp[i]->hover(true);
  743. //sending active, MotionInterested objects mouseMoved() call
  744. std::list<MotionInterested*> miCopy = motioninterested;
  745. for(std::list<MotionInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  746. {
  747. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  748. {
  749. (*i)->mouseMoved(sEvent->motion);
  750. }
  751. }
  752. //adventure map scrolling with mouse
  753. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && adventureInt->active)
  754. {
  755. if(sEvent->motion.x<15)
  756. {
  757. adventureInt->scrollingDir |= CAdvMapInt::LEFT;
  758. }
  759. else
  760. {
  761. adventureInt->scrollingDir &= ~CAdvMapInt::LEFT;
  762. }
  763. if(sEvent->motion.x>screen->w-15)
  764. {
  765. adventureInt->scrollingDir |= CAdvMapInt::RIGHT;
  766. }
  767. else
  768. {
  769. adventureInt->scrollingDir &= ~CAdvMapInt::RIGHT;
  770. }
  771. if(sEvent->motion.y<15)
  772. {
  773. adventureInt->scrollingDir |= CAdvMapInt::UP;
  774. }
  775. else
  776. {
  777. adventureInt->scrollingDir &= ~CAdvMapInt::UP;
  778. }
  779. if(sEvent->motion.y>screen->h-15)
  780. {
  781. adventureInt->scrollingDir |= CAdvMapInt::DOWN;
  782. }
  783. else
  784. {
  785. adventureInt->scrollingDir &= ~CAdvMapInt::DOWN;
  786. }
  787. }
  788. }
  789. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  790. {
  791. current = sEvent;
  792. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  793. {
  794. SDL_KeyboardEvent key = sEvent->key;
  795. //translate numpad keys
  796. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  797. {
  798. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  799. }
  800. else if(key.keysym.sym == SDLK_KP_ENTER)
  801. {
  802. key.keysym.sym = (SDLKey)SDLK_RETURN;
  803. }
  804. bool keysCaptured = false;
  805. for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  806. {
  807. if((*i)->captureAllKeys)
  808. {
  809. keysCaptured = true;
  810. break;
  811. }
  812. }
  813. std::list<KeyInterested*> miCopy = keyinterested;
  814. for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  815. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  816. (**i).keyPressed(key);
  817. }
  818. else if(sEvent->type==SDL_MOUSEMOTION)
  819. {
  820. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  821. handleMouseMotion(sEvent);
  822. }
  823. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  824. {
  825. std::list<ClickableL*> hlp = lclickable;
  826. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  827. {
  828. if(!vstd::contains(lclickable,*i)) continue;
  829. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  830. {
  831. (*i)->clickLeft(true);
  832. }
  833. }
  834. }
  835. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  836. {
  837. std::list<ClickableL*> hlp = lclickable;
  838. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  839. {
  840. if(!vstd::contains(lclickable,*i)) continue;
  841. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  842. {
  843. (*i)->clickLeft(false);
  844. }
  845. else
  846. (*i)->clickLeft(boost::logic::indeterminate);
  847. }
  848. }
  849. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  850. {
  851. std::list<ClickableR*> hlp = rclickable;
  852. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  853. {
  854. if(!vstd::contains(rclickable,*i)) continue;
  855. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  856. {
  857. (*i)->clickRight(true);
  858. }
  859. }
  860. }
  861. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  862. {
  863. std::list<ClickableR*> hlp = rclickable;
  864. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  865. {
  866. if(!vstd::contains(rclickable,*i)) continue;
  867. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  868. {
  869. (*i)->clickRight(false);
  870. }
  871. else
  872. (*i)->clickRight(boost::logic::indeterminate);
  873. }
  874. }
  875. current = NULL;
  876. } //event end
  877. int3 CPlayerInterface::repairScreenPos(int3 pos)
  878. {
  879. if(pos.x<-CGI->mh->frameW)
  880. pos.x = -CGI->mh->frameW;
  881. if(pos.y<-CGI->mh->frameH)
  882. pos.y = -CGI->mh->frameH;
  883. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW)
  884. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW;
  885. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH)
  886. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH;
  887. return pos;
  888. }
  889. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  890. {
  891. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  892. return;
  893. boost::unique_lock<boost::recursive_mutex> un(*pim);
  894. redrawHeroWin(hero);
  895. }
  896. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  897. {
  898. boost::unique_lock<boost::recursive_mutex> un(*pim);
  899. redrawHeroWin(hero);
  900. }
  901. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  902. {
  903. boost::unique_lock<boost::recursive_mutex> un(*pim);
  904. //adventureInt->heroList.draw();
  905. }
  906. void CPlayerInterface::receivedResource(int type, int val)
  907. {
  908. boost::unique_lock<boost::recursive_mutex> un(*pim);
  909. LOCPLINT->totalRedraw();
  910. }
  911. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  912. {
  913. waitWhileDialog();
  914. CGI->soundh->playSound(soundBase::heroNewLevel);
  915. boost::unique_lock<boost::recursive_mutex> un(*pim);
  916. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  917. LOCPLINT->pushInt(lw);
  918. }
  919. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  920. {
  921. boost::unique_lock<boost::recursive_mutex> un(*pim);
  922. //redraw infowindow
  923. SDL_FreeSurface(graphics->townWins[town->id]);
  924. graphics->townWins[town->id] = infoWin(town);
  925. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  926. {
  927. CGI->mh->hideObject(town->garrisonHero);
  928. wanderingHeroes -= town->garrisonHero;
  929. }
  930. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  931. {
  932. CGI->mh->printObject(town->visitingHero);
  933. wanderingHeroes.push_back(town->visitingHero);
  934. }
  935. //adventureInt->heroList.updateHList();
  936. CCastleInterface *c = castleInt;
  937. if(c)
  938. {
  939. c->garr->highlighted = NULL;
  940. c->hslotup.hero = town->garrisonHero;
  941. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  942. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  943. c->garr->recreateSlots();
  944. }
  945. LOCPLINT->totalRedraw();
  946. }
  947. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  948. {
  949. if(hero->tempOwner != town->tempOwner)
  950. return;
  951. boost::unique_lock<boost::recursive_mutex> un(*pim);
  952. openTownWindow(town);
  953. }
  954. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  955. {
  956. boost::unique_lock<boost::recursive_mutex> un(*pim);
  957. if(obj->ID == HEROI_TYPE) //hero
  958. {
  959. const CGHeroInstance * hh;
  960. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  961. {
  962. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  963. graphics->heroWins[hh->subID] = infoWin(hh);
  964. }
  965. }
  966. else if (obj->ID == TOWNI_TYPE) //town
  967. {
  968. const CGTownInstance * tt;
  969. if(tt = static_cast<const CGTownInstance*>(obj))
  970. {
  971. SDL_FreeSurface(graphics->townWins[tt->id]);
  972. graphics->townWins[tt->id] = infoWin(tt);
  973. }
  974. if(tt->visitingHero)
  975. {
  976. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  977. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  978. }
  979. }
  980. bool wasGarrison = false;
  981. for(std::list<IShowActivable*>::iterator i = listInt.begin(); i != listInt.end(); i++)
  982. {
  983. if((*i)->type & IShowActivable::WITH_GARRISON)
  984. {
  985. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  986. wwg->garr->recreateSlots();
  987. wasGarrison = true;
  988. }
  989. }
  990. LOCPLINT->totalRedraw();
  991. }
  992. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  993. {
  994. boost::unique_lock<boost::recursive_mutex> un(*pim);
  995. switch (buildingID)
  996. {
  997. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  998. {
  999. SDL_FreeSurface(graphics->townWins[town->id]);
  1000. graphics->townWins[town->id] = infoWin(town);
  1001. break;
  1002. }
  1003. }
  1004. if(!castleInt)
  1005. return;
  1006. if(castleInt->town!=town)
  1007. return;
  1008. switch(what)
  1009. {
  1010. case 1:
  1011. CGI->soundh->playSound(soundBase::newBuilding);
  1012. castleInt->addBuilding(buildingID);
  1013. break;
  1014. case 2:
  1015. castleInt->removeBuilding(buildingID);
  1016. break;
  1017. }
  1018. }
  1019. void CPlayerInterface::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) //called by engine when battle starts; side=0 - left, side=1 - right
  1020. {
  1021. while(showingDialog->get())
  1022. SDL_Delay(20);
  1023. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1024. battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  1025. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  1026. pushInt(battleInt);
  1027. }
  1028. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1029. {
  1030. }
  1031. 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
  1032. {
  1033. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1034. battleInt->newRound(round);
  1035. }
  1036. void CPlayerInterface::actionStarted(const BattleAction* action)
  1037. {
  1038. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1039. curAction = new BattleAction(*action);
  1040. if( (action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber))) )
  1041. {
  1042. battleInt->moveStarted = true;
  1043. if(battleInt->creAnims[action->stackNumber]->framesInGroup(20))
  1044. {
  1045. battleInt->creAnims[action->stackNumber]->setType(20);
  1046. }
  1047. }
  1048. battleInt->deactivate();
  1049. CStack *stack = cb->battleGetStackByID(action->stackNumber);
  1050. char txt[400];
  1051. if(action->actionType == 1)
  1052. {
  1053. if(action->side)
  1054. battleInt->defendingHero->setPhase(4);
  1055. else
  1056. battleInt->attackingHero->setPhase(4);
  1057. return;
  1058. }
  1059. if(!stack)
  1060. {
  1061. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  1062. return;
  1063. }
  1064. int txtid = 0;
  1065. switch(action->actionType)
  1066. {
  1067. case 3: //defend
  1068. txtid = 120;
  1069. break;
  1070. case 8: //wait
  1071. txtid = 136;
  1072. break;
  1073. case 11: //bad morale
  1074. txtid = -34; //negative -> no separate singular/plural form
  1075. battleInt->displayEffect(30,stack->position);
  1076. break;
  1077. }
  1078. if(txtid > 0 && stack->amount != 1)
  1079. txtid++; //move to plural text
  1080. else if(txtid < 0)
  1081. txtid = -txtid;
  1082. if(txtid)
  1083. {
  1084. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str(), 0);
  1085. LOCPLINT->battleInt->console->addText(txt);
  1086. }
  1087. }
  1088. void CPlayerInterface::actionFinished(const BattleAction* action)
  1089. {
  1090. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1091. delete curAction;
  1092. curAction = NULL;
  1093. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  1094. {
  1095. battleInt->activate();
  1096. }
  1097. if(action->actionType == 1)
  1098. {
  1099. if(action->side)
  1100. battleInt->defendingHero->setPhase(0);
  1101. else
  1102. battleInt->attackingHero->setPhase(0);
  1103. }
  1104. }
  1105. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  1106. {
  1107. CBattleInterface *b = battleInt;
  1108. {
  1109. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1110. CStack *stack = cb->battleGetStackByID(stackID);
  1111. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  1112. {
  1113. std::string hlp = CGI->generaltexth->allTexts[33];
  1114. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  1115. battleInt->displayEffect(20,stack->position);
  1116. battleInt->console->addText(hlp);
  1117. }
  1118. b->stackActivated(stackID);
  1119. }
  1120. //wait till BattleInterface sets its command
  1121. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  1122. while(!b->givenCommand->data)
  1123. b->givenCommand->cond.wait(lock);
  1124. //tidy up
  1125. BattleAction ret = *(b->givenCommand->data);
  1126. delete b->givenCommand->data;
  1127. b->givenCommand->data = NULL;
  1128. //return command
  1129. return ret;
  1130. }
  1131. void CPlayerInterface::battleEnd(BattleResult *br)
  1132. {
  1133. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1134. battleInt->battleFinished(*br);
  1135. }
  1136. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  1137. {
  1138. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1139. battleInt->stackMoved(ID, dest, end, distance);
  1140. }
  1141. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  1142. {
  1143. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1144. battleInt->spellCast(sc);
  1145. }
  1146. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  1147. {
  1148. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1149. battleInt->battleStacksEffectsSet(sse);
  1150. }
  1151. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  1152. {
  1153. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  1154. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1155. tlog5 << "done!\n";
  1156. std::vector<CBattleInterface::SStackAttackedInfo> arg;
  1157. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  1158. {
  1159. if(i->isEffect() && i->effect != 12) //and not armageddon
  1160. {
  1161. battleInt->displayEffect(i->effect, cb->battleGetStackByID(i->stackAttacked)->position);
  1162. }
  1163. CBattleInterface::SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, LOCPLINT->curAction->stackNumber, LOCPLINT->curAction->actionType==7, i->killed()};
  1164. arg.push_back(to_put);
  1165. }
  1166. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  1167. {
  1168. battleInt->displayEffect(bsa.begin()->effect, -1);
  1169. }
  1170. battleInt->stacksAreAttacked(arg);
  1171. }
  1172. void CPlayerInterface::battleAttack(BattleAttack *ba)
  1173. {
  1174. tlog5 << "CPlayerInterface::battleAttack - locking...";
  1175. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1176. tlog5 << "done!\n";
  1177. assert(curAction);
  1178. if(ba->lucky()) //lucky hit
  1179. {
  1180. CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  1181. std::string hlp = CGI->generaltexth->allTexts[45];
  1182. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  1183. battleInt->console->addText(hlp);
  1184. battleInt->displayEffect(18,stack->position);
  1185. }
  1186. //TODO: bad luck?
  1187. if(ba->shot())
  1188. {
  1189. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  1190. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  1191. }
  1192. else
  1193. {
  1194. CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  1195. int shift = 0;
  1196. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  1197. {
  1198. if(attacker->attackerOwned)
  1199. shift = 1;
  1200. else
  1201. shift = -1;
  1202. }
  1203. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  1204. }
  1205. }
  1206. void CPlayerInterface::showComp(SComponent comp)
  1207. {
  1208. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1209. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  1210. adventureInt->infoBar.showComp(&comp,4000);
  1211. }
  1212. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  1213. {
  1214. std::vector<SComponent*> intComps;
  1215. for(int i=0;i<components.size();i++)
  1216. intComps.push_back(new SComponent(*components[i]));
  1217. showInfoDialog(text,intComps,soundID);
  1218. }
  1219. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  1220. {
  1221. waitWhileDialog();
  1222. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1223. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1224. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1225. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1226. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1227. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  1228. if(makingTurn && listInt.size())
  1229. {
  1230. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1231. showingDialog->set(true);
  1232. pushInt(temp);
  1233. }
  1234. else
  1235. {
  1236. dialogs.push_back(temp);
  1237. }
  1238. }
  1239. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  1240. {
  1241. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1242. LOCPLINT->showingDialog->setn(true);
  1243. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1244. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1245. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1246. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,DelComps);
  1247. temp->delComps = DelComps;
  1248. for(int i=0;i<onYes.funcs.size();i++)
  1249. temp->buttons[0]->callback += onYes.funcs[i];
  1250. for(int i=0;i<onNo.funcs.size();i++)
  1251. temp->buttons[1]->callback += onNo.funcs[i];
  1252. LOCPLINT->pushInt(temp);
  1253. }
  1254. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  1255. {
  1256. waitWhileDialog();
  1257. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1258. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1259. if(!selection && cancel) //simple yes/no dialog
  1260. {
  1261. std::vector<SComponent*> intComps;
  1262. for(int i=0;i<components.size();i++)
  1263. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  1264. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  1265. }
  1266. else if(selection)
  1267. {
  1268. std::vector<CSelectableComponent*> intComps;
  1269. for(int i=0;i<components.size();i++)
  1270. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  1271. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1272. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1273. if(cancel)
  1274. {
  1275. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1276. }
  1277. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  1278. pushInt(temp);
  1279. intComps[0]->clickLeft(true);
  1280. }
  1281. }
  1282. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  1283. {
  1284. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1285. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1286. adventureInt->minimap.showTile(*i);
  1287. }
  1288. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  1289. {
  1290. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1291. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1292. adventureInt->minimap.hideTile(*i);
  1293. }
  1294. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  1295. {
  1296. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1297. adventureInt->heroWindow->setHero(hero);
  1298. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  1299. pushInt(adventureInt->heroWindow);
  1300. }
  1301. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  1302. {
  1303. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1304. if(adventureInt->heroWindow->curHero) //hero window is opened
  1305. {
  1306. adventureInt->heroWindow->deactivate();
  1307. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  1308. adventureInt->heroWindow->activate();
  1309. return;
  1310. }
  1311. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(listInt.front());
  1312. if(cew) //exchange window is open
  1313. {
  1314. cew->deactivate();
  1315. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  1316. {
  1317. if(cew->heroInst[g] == hero)
  1318. {
  1319. cew->artifs[g]->setHero(hero);
  1320. }
  1321. }
  1322. cew->prepareBackground();
  1323. cew->activate();
  1324. }
  1325. }
  1326. void CPlayerInterface::updateWater()
  1327. {
  1328. }
  1329. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  1330. {
  1331. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1332. if(castleInt)
  1333. {
  1334. CFortScreen *fs = dynamic_cast<CFortScreen*>(listInt.front());
  1335. if(fs)
  1336. fs->draw(castleInt,false);
  1337. }
  1338. }
  1339. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  1340. {
  1341. if(bonus.type == HeroBonus::NONE) return;
  1342. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1343. redrawHeroWin(hero);
  1344. }
  1345. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1346. {
  1347. h & playerID & serialID;
  1348. h & sysOpts;
  1349. h & CBattleInterface::settings;
  1350. }
  1351. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1352. {
  1353. serializeTempl(h,version);
  1354. }
  1355. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1356. {
  1357. serializeTempl(h,version);
  1358. sysOpts.apply();
  1359. }
  1360. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  1361. {
  1362. if(!vstd::contains(graphics->heroWins,hero->subID))
  1363. {
  1364. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  1365. return;
  1366. }
  1367. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  1368. graphics->heroWins[hero->subID] = infoWin(hero);
  1369. if (adventureInt->selection == hero)
  1370. adventureInt->infoBar.draw(screen);
  1371. }
  1372. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CPath path )
  1373. {
  1374. if (!h)
  1375. return false; //can't find hero
  1376. bool result = false;
  1377. path.convert(0);
  1378. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1379. stillMoveHero.data = CONTINUE_MOVE;
  1380. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1381. enum TerrainTile::EterrainType newTerrain;
  1382. int sh = -1;
  1383. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  1384. {
  1385. //stop sending move requests if hero exhausted all his move points
  1386. if(!h->movement)
  1387. {
  1388. stillMoveHero.data = STOP_MOVE;
  1389. break;
  1390. }
  1391. // Start a new sound for the hero movement or let the existing one carry on.
  1392. #if 0
  1393. // TODO
  1394. if (hero is flying && sh == -1)
  1395. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  1396. }
  1397. else if (hero is in a boat && sh = -1) {
  1398. sh = CGI->soundh->playSound(soundBase::sound_todo, -1);
  1399. } else
  1400. #endif
  1401. {
  1402. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1403. if (newTerrain != currentTerrain) {
  1404. CGI->soundh->stopSound(sh);
  1405. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  1406. currentTerrain = newTerrain;
  1407. }
  1408. }
  1409. stillMoveHero.data = WAITING_MOVE;
  1410. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1411. cb->moveHero(h,endpos);
  1412. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1413. stillMoveHero.cond.wait(un);
  1414. }
  1415. CGI->soundh->stopSound(sh);
  1416. //stillMoveHero = false;
  1417. return result;
  1418. }
  1419. bool CPlayerInterface::shiftPressed() const
  1420. {
  1421. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1422. }
  1423. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd )
  1424. {
  1425. {
  1426. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1427. while(showingDialog->data)
  1428. showingDialog->cond.wait(un);
  1429. }
  1430. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1431. while(dialogs.size())
  1432. {
  1433. pim->unlock();
  1434. SDL_Delay(20);
  1435. pim->lock();
  1436. }
  1437. CGarrisonWindow *cgw = new CGarrisonWindow(up,down);
  1438. cgw->quit->callback += onEnd;
  1439. pushInt(cgw);
  1440. }
  1441. void CPlayerInterface::popInt( IShowActivable *top )
  1442. {
  1443. assert(listInt.front() == top);
  1444. top->deactivate();
  1445. listInt.pop_front();
  1446. objsToBlit -= top;
  1447. if(listInt.size())
  1448. listInt.front()->activate();
  1449. totalRedraw();
  1450. }
  1451. void CPlayerInterface::popIntTotally( IShowActivable *top )
  1452. {
  1453. assert(listInt.front() == top);
  1454. popInt(top);
  1455. delete top;
  1456. }
  1457. void CPlayerInterface::pushInt( IShowActivable *newInt )
  1458. {
  1459. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  1460. screenBuf = screen2;
  1461. if(listInt.size())
  1462. listInt.front()->deactivate();
  1463. listInt.push_front(newInt);
  1464. newInt->activate();
  1465. objsToBlit += newInt;
  1466. LOCPLINT->totalRedraw();
  1467. }
  1468. void CPlayerInterface::popInts( int howMany )
  1469. {
  1470. if(!howMany) return; //senseless but who knows...
  1471. assert(listInt.size() > howMany);
  1472. listInt.front()->deactivate();
  1473. for(int i=0; i < howMany; i++)
  1474. {
  1475. objsToBlit -= listInt.front();
  1476. delete listInt.front();
  1477. listInt.pop_front();
  1478. }
  1479. listInt.front()->activate();
  1480. totalRedraw();
  1481. }
  1482. IShowActivable * CPlayerInterface::topInt()
  1483. {
  1484. if(!listInt.size())
  1485. return NULL;
  1486. else
  1487. return listInt.front();
  1488. }
  1489. void CPlayerInterface::totalRedraw()
  1490. {
  1491. for(int i=0;i<objsToBlit.size();i++)
  1492. objsToBlit[i]->showAll(screen2);
  1493. blitAt(screen2,0,0,screen);
  1494. if(objsToBlit.size())
  1495. objsToBlit.back()->showAll(screen);
  1496. }
  1497. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1498. {
  1499. if(stillMoveHero.get() == DURING_MOVE)
  1500. stillMoveHero.setn(CONTINUE_MOVE);
  1501. }
  1502. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1503. {
  1504. pushInt(new CExchangeWindow(hero1, hero2));
  1505. }
  1506. void CPlayerInterface::recreateWanderingHeroes()
  1507. {
  1508. wanderingHeroes.clear();
  1509. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1510. for(size_t i = 0; i < heroes.size(); i++)
  1511. if(!heroes[i]->inTownGarrison)
  1512. wanderingHeroes.push_back(heroes[i]);
  1513. }
  1514. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1515. {
  1516. if(pos < 0 || pos >= wanderingHeroes.size())
  1517. return NULL;
  1518. return wanderingHeroes[pos];
  1519. }
  1520. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, int level)
  1521. {
  1522. waitWhileDialog();
  1523. std::vector<std::pair<int,int> > cres;
  1524. for(int i = 0; i < dwelling->creatures.size(); i++)
  1525. {
  1526. if(i == level || level < 0)
  1527. for(size_t j = 0; j < dwelling->creatures[i].second.size(); j++)
  1528. cres.push_back( std::make_pair(dwelling->creatures[i].second[j],dwelling->creatures[i].first));
  1529. }
  1530. CRecruitmentWindow *cr = new CRecruitmentWindow(cres, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1531. pushInt(cr);
  1532. }
  1533. void CPlayerInterface::waitWhileDialog()
  1534. {
  1535. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1536. while(showingDialog->data)
  1537. showingDialog->cond.wait(un);
  1538. }
  1539. void SystemOptions::setMusicVolume( int newVolume )
  1540. {
  1541. musicVolume = newVolume;
  1542. CGI->musich->setVolume(newVolume);
  1543. settingsChanged();
  1544. }
  1545. void SystemOptions::setSoundVolume( int newVolume )
  1546. {
  1547. soundVolume = newVolume;
  1548. CGI->soundh->setVolume(newVolume);
  1549. settingsChanged();
  1550. }
  1551. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1552. {
  1553. heroMoveSpeed = newSpeed;
  1554. settingsChanged();
  1555. }
  1556. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1557. {
  1558. mapScrollingSpeed = newSpeed;
  1559. settingsChanged();
  1560. }
  1561. void SystemOptions::settingsChanged()
  1562. {
  1563. CSaveFile settings("config" PATHSEPARATOR "sysopts.bin");
  1564. if(settings.sfile)
  1565. settings << *this;
  1566. else
  1567. tlog1 << "Cannot save settings to config" PATHSEPARATOR "sysopts.bin!\n";
  1568. }
  1569. void SystemOptions::apply()
  1570. {
  1571. CGI->musich->setVolume(musicVolume);
  1572. CGI->soundh->setVolume(soundVolume);
  1573. settingsChanged();
  1574. }