CPlayerInterface.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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::BLOCKING_VISIT)
  252. {
  253. adventureInt->paths.erase(ho);
  254. adventureInt->terrain.currentPath = NULL;
  255. }
  256. ho->isStanding = true;
  257. stillMoveHero.setn(STOP_MOVE);
  258. return;
  259. }
  260. if (adventureInt->terrain.currentPath) //&& hero is moving
  261. {
  262. //remove one node from the path (the one we went)
  263. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  264. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  265. {
  266. adventureInt->paths.erase(ho);
  267. adventureInt->terrain.currentPath = NULL;
  268. }
  269. }
  270. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  271. {
  272. //ho->moveDir = 1;
  273. ho->isStanding = false;
  274. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  275. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  276. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  277. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  278. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  279. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  280. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  281. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  282. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  283. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  284. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  285. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  286. 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);
  287. 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);
  288. 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);
  289. 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);
  290. 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);
  291. 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);
  292. }
  293. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  294. {
  295. //ho->moveDir = 2;
  296. ho->isStanding = false;
  297. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  298. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  299. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  300. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  301. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  302. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  303. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  304. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  305. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  306. 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);
  307. 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);
  308. 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);
  309. }
  310. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  311. {
  312. //ho->moveDir = 3;
  313. ho->isStanding = false;
  314. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  315. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  316. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  317. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  318. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  319. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  320. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  321. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  322. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  323. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  324. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  325. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  326. 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);
  327. 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);
  328. 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);
  329. 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);
  330. 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);
  331. 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);
  332. }
  333. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  334. {
  335. //ho->moveDir = 4;
  336. ho->isStanding = false;
  337. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  338. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  339. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  340. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  341. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  342. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  343. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  344. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  345. 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);
  346. 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);
  347. }
  348. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  349. {
  350. //ho->moveDir = 5;
  351. ho->isStanding = false;
  352. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  353. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  354. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  355. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  356. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  357. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  358. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  359. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  360. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  361. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  362. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  363. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  364. 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);
  365. 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);
  366. 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);
  367. 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);
  368. 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);
  369. 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);
  370. }
  371. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  372. {
  373. //ho->moveDir = 6;
  374. ho->isStanding = false;
  375. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  376. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  377. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  378. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  379. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  380. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  381. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  382. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  383. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  384. 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);
  385. 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);
  386. 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);
  387. }
  388. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  389. {
  390. //ho->moveDir = 7;
  391. ho->isStanding = false;
  392. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  393. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  394. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  395. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  396. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  397. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  398. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  399. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  400. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  401. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  402. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  403. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  404. 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);
  405. 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);
  406. 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);
  407. 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);
  408. 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);
  409. 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);
  410. }
  411. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  412. {
  413. //ho->moveDir = 8;
  414. ho->isStanding = false;
  415. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  416. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  417. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  418. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  419. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  420. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  421. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  422. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  423. 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);
  424. 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);
  425. }
  426. //first initializing done
  427. SDL_framerateDelay(mainFPSmng); // after first move
  428. //main moving
  429. for(int i=1; i<32; i+=2*sysOpts.heroMoveSpeed)
  430. {
  431. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  432. {
  433. //setting advmap shift
  434. adventureInt->terrain.moveX = i-32;
  435. adventureInt->terrain.moveY = i-32;
  436. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  437. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  438. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  439. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  440. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  441. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  442. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  443. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  444. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  445. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  446. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  447. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  448. }
  449. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  450. {
  451. //setting advmap shift
  452. adventureInt->terrain.moveY = i-32;
  453. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  454. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  455. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  456. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  457. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  458. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  459. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  460. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  461. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  462. }
  463. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  464. {
  465. //setting advmap shift
  466. adventureInt->terrain.moveX = -i+32;
  467. adventureInt->terrain.moveY = i-32;
  468. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  469. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  470. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  471. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  472. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  473. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  474. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  475. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  476. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  477. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  478. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  479. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  480. }
  481. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  482. {
  483. //setting advmap shift
  484. adventureInt->terrain.moveX = -i+32;
  485. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  486. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  487. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  488. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  489. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  490. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  491. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  492. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  493. }
  494. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  495. {
  496. //setting advmap shift
  497. adventureInt->terrain.moveX = -i+32;
  498. adventureInt->terrain.moveY = -i+32;
  499. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  500. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  501. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  502. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  503. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  504. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  505. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  506. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  507. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  508. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  509. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  510. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  511. }
  512. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  513. {
  514. //setting advmap shift
  515. adventureInt->terrain.moveY = -i+32;
  516. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  517. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  518. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  519. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  520. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  521. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  522. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  523. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  524. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  525. }
  526. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  527. {
  528. //setting advmap shift
  529. adventureInt->terrain.moveX = i-32;
  530. adventureInt->terrain.moveY = -i+32;
  531. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  532. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  533. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  534. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  535. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  536. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  537. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  538. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  539. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  540. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  541. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  542. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  543. }
  544. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  545. {
  546. //setting advmap shift
  547. adventureInt->terrain.moveX = i-32;
  548. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  549. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  550. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  551. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  552. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  553. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  554. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  555. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  556. }
  557. adventureInt->updateScreen = true;
  558. adventureInt->show(screen);
  559. //LOCPLINT->adventureInt->show(); //updating screen
  560. CSDL_Ext::update(screen);
  561. SDL_Delay(5);
  562. SDL_framerateDelay(mainFPSmng); //for animation purposes
  563. } //for(int i=1; i<32; i+=4)
  564. //main moving done
  565. //finishing move
  566. //restoring adventureInt->terrain.move*
  567. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  568. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  569. {
  570. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  571. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  572. delObjRect(hp.x, hp.y, hp.z, ho->id);
  573. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  574. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  575. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  576. }
  577. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  578. {
  579. delObjRect(hp.x, hp.y, hp.z, ho->id);
  580. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  581. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  582. }
  583. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  584. {
  585. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  586. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  587. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  588. delObjRect(hp.x, hp.y, hp.z, ho->id);
  589. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  590. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  591. }
  592. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  593. {
  594. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  595. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  596. }
  597. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  598. {
  599. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  600. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  601. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  602. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  603. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  604. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  605. }
  606. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  607. {
  608. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  609. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  610. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  611. }
  612. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  613. {
  614. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  615. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  616. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  617. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  618. delObjRect(hp.x, hp.y, hp.z, ho->id);
  619. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  620. }
  621. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  622. {
  623. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  624. delObjRect(hp.x, hp.y, hp.z, ho->id);
  625. }
  626. //restoring good rects
  627. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  628. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  629. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  630. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  631. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  632. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  633. //restoring good order of objects
  634. 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);
  635. 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);
  636. 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);
  637. 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);
  638. 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);
  639. 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);
  640. ho->isStanding = true;
  641. //move finished
  642. adventureInt->minimap.draw(screen2);
  643. adventureInt->heroList.updateMove(ho);
  644. //check if user cancelled movement
  645. {
  646. boost::unique_lock<boost::mutex> un(eventsM);
  647. while(events.size())
  648. {
  649. SDL_Event *ev = events.front();
  650. events.pop();
  651. switch(ev->type)
  652. {
  653. case SDL_MOUSEBUTTONDOWN:
  654. stillMoveHero.setn(STOP_MOVE);
  655. break;
  656. case SDL_KEYDOWN:
  657. if(ev->key.keysym.sym < SDLK_F1)
  658. stillMoveHero.setn(STOP_MOVE);
  659. break;
  660. }
  661. delete ev;
  662. }
  663. }
  664. if(stillMoveHero.get() == 1)
  665. stillMoveHero.setn(DURING_MOVE);
  666. }
  667. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  668. {
  669. boost::unique_lock<boost::recursive_mutex> un(*pim);
  670. graphics->heroWins.erase(hero->ID);
  671. wanderingHeroes -= hero;
  672. adventureInt->heroList.updateHList(hero);
  673. }
  674. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  675. {
  676. boost::unique_lock<boost::recursive_mutex> un(*pim);
  677. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  678. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  679. wanderingHeroes.push_back(hero);
  680. adventureInt->heroList.updateHList();
  681. }
  682. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  683. {
  684. castleInt = new CCastleInterface(town);
  685. CGI->musich->playMusic(castleInt->musicID, -1);
  686. LOCPLINT->pushInt(castleInt);
  687. }
  688. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  689. {
  690. if (specific)
  691. {
  692. switch (specific->ID)
  693. {
  694. case HEROI_TYPE:
  695. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  696. break;
  697. case TOWNI_TYPE:
  698. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  699. break;
  700. default:
  701. return NULL;
  702. break;
  703. }
  704. }
  705. else
  706. {
  707. switch (adventureInt->selection->ID)
  708. {
  709. case HEROI_TYPE:
  710. {
  711. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  712. return graphics->drawHeroInfoWin(curh);
  713. }
  714. case TOWNI_TYPE:
  715. {
  716. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  717. }
  718. default:
  719. tlog1 << "Strange... selection is neither hero nor town\n";
  720. return NULL;
  721. }
  722. }
  723. }
  724. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  725. {
  726. //sending active, hovered hoverable objects hover() call
  727. std::vector<Hoverable*> hlp;
  728. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  729. {
  730. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  731. {
  732. if (!(*i)->hovered)
  733. hlp.push_back((*i));
  734. }
  735. else if ((*i)->hovered)
  736. {
  737. (*i)->hover(false);
  738. }
  739. }
  740. for(int i=0; i<hlp.size();i++)
  741. hlp[i]->hover(true);
  742. //sending active, MotionInterested objects mouseMoved() call
  743. std::list<MotionInterested*> miCopy = motioninterested;
  744. for(std::list<MotionInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  745. {
  746. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  747. {
  748. (*i)->mouseMoved(sEvent->motion);
  749. }
  750. }
  751. //adventure map scrolling with mouse
  752. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && adventureInt->active)
  753. {
  754. if(sEvent->motion.x<15)
  755. {
  756. adventureInt->scrollingDir |= CAdvMapInt::LEFT;
  757. }
  758. else
  759. {
  760. adventureInt->scrollingDir &= ~CAdvMapInt::LEFT;
  761. }
  762. if(sEvent->motion.x>screen->w-15)
  763. {
  764. adventureInt->scrollingDir |= CAdvMapInt::RIGHT;
  765. }
  766. else
  767. {
  768. adventureInt->scrollingDir &= ~CAdvMapInt::RIGHT;
  769. }
  770. if(sEvent->motion.y<15)
  771. {
  772. adventureInt->scrollingDir |= CAdvMapInt::UP;
  773. }
  774. else
  775. {
  776. adventureInt->scrollingDir &= ~CAdvMapInt::UP;
  777. }
  778. if(sEvent->motion.y>screen->h-15)
  779. {
  780. adventureInt->scrollingDir |= CAdvMapInt::DOWN;
  781. }
  782. else
  783. {
  784. adventureInt->scrollingDir &= ~CAdvMapInt::DOWN;
  785. }
  786. }
  787. }
  788. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  789. {
  790. current = sEvent;
  791. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  792. {
  793. SDL_KeyboardEvent key = sEvent->key;
  794. //translate numpad keys
  795. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  796. {
  797. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  798. }
  799. else if(key.keysym.sym == SDLK_KP_ENTER)
  800. {
  801. key.keysym.sym = (SDLKey)SDLK_RETURN;
  802. }
  803. bool keysCaptured = false;
  804. for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  805. {
  806. if((*i)->captureAllKeys)
  807. {
  808. keysCaptured = true;
  809. break;
  810. }
  811. }
  812. std::list<KeyInterested*> miCopy = keyinterested;
  813. for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  814. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  815. (**i).keyPressed(key);
  816. }
  817. else if(sEvent->type==SDL_MOUSEMOTION)
  818. {
  819. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  820. handleMouseMotion(sEvent);
  821. }
  822. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  823. {
  824. std::list<ClickableL*> hlp = lclickable;
  825. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  826. {
  827. if(!vstd::contains(lclickable,*i)) continue;
  828. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  829. {
  830. (*i)->clickLeft(true);
  831. }
  832. }
  833. }
  834. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  835. {
  836. std::list<ClickableL*> hlp = lclickable;
  837. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  838. {
  839. if(!vstd::contains(lclickable,*i)) continue;
  840. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  841. {
  842. (*i)->clickLeft(false);
  843. }
  844. else
  845. (*i)->clickLeft(boost::logic::indeterminate);
  846. }
  847. }
  848. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  849. {
  850. std::list<ClickableR*> hlp = rclickable;
  851. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  852. {
  853. if(!vstd::contains(rclickable,*i)) continue;
  854. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  855. {
  856. (*i)->clickRight(true);
  857. }
  858. }
  859. }
  860. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  861. {
  862. std::list<ClickableR*> hlp = rclickable;
  863. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  864. {
  865. if(!vstd::contains(rclickable,*i)) continue;
  866. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  867. {
  868. (*i)->clickRight(false);
  869. }
  870. else
  871. (*i)->clickRight(boost::logic::indeterminate);
  872. }
  873. }
  874. current = NULL;
  875. } //event end
  876. int3 CPlayerInterface::repairScreenPos(int3 pos)
  877. {
  878. if(pos.x<-CGI->mh->frameW)
  879. pos.x = -CGI->mh->frameW;
  880. if(pos.y<-CGI->mh->frameH)
  881. pos.y = -CGI->mh->frameH;
  882. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW)
  883. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW;
  884. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH)
  885. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH;
  886. return pos;
  887. }
  888. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  889. {
  890. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  891. return;
  892. boost::unique_lock<boost::recursive_mutex> un(*pim);
  893. redrawHeroWin(hero);
  894. }
  895. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  896. {
  897. boost::unique_lock<boost::recursive_mutex> un(*pim);
  898. redrawHeroWin(hero);
  899. }
  900. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  901. {
  902. boost::unique_lock<boost::recursive_mutex> un(*pim);
  903. //adventureInt->heroList.draw();
  904. }
  905. void CPlayerInterface::receivedResource(int type, int val)
  906. {
  907. boost::unique_lock<boost::recursive_mutex> un(*pim);
  908. LOCPLINT->totalRedraw();
  909. }
  910. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  911. {
  912. {
  913. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  914. while(showingDialog->data)
  915. showingDialog->cond.wait(un);
  916. }
  917. CGI->soundh->playSound(soundBase::heroNewLevel);
  918. boost::unique_lock<boost::recursive_mutex> un(*pim);
  919. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  920. LOCPLINT->pushInt(lw);
  921. }
  922. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  923. {
  924. boost::unique_lock<boost::recursive_mutex> un(*pim);
  925. //redraw infowindow
  926. SDL_FreeSurface(graphics->townWins[town->id]);
  927. graphics->townWins[town->id] = infoWin(town);
  928. if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  929. {
  930. CGI->mh->hideObject(town->garrisonHero);
  931. wanderingHeroes -= town->garrisonHero;
  932. }
  933. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  934. {
  935. CGI->mh->printObject(town->visitingHero);
  936. wanderingHeroes.push_back(town->visitingHero);
  937. }
  938. //adventureInt->heroList.updateHList();
  939. CCastleInterface *c = castleInt;
  940. if(c)
  941. {
  942. c->garr->highlighted = NULL;
  943. c->hslotup.hero = town->garrisonHero;
  944. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  945. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  946. c->garr->recreateSlots();
  947. }
  948. LOCPLINT->totalRedraw();
  949. }
  950. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  951. {
  952. if(hero->tempOwner != town->tempOwner)
  953. return;
  954. boost::unique_lock<boost::recursive_mutex> un(*pim);
  955. openTownWindow(town);
  956. }
  957. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  958. {
  959. boost::unique_lock<boost::recursive_mutex> un(*pim);
  960. if(obj->ID == HEROI_TYPE) //hero
  961. {
  962. const CGHeroInstance * hh;
  963. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  964. {
  965. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  966. graphics->heroWins[hh->subID] = infoWin(hh);
  967. }
  968. }
  969. else if (obj->ID == TOWNI_TYPE) //town
  970. {
  971. const CGTownInstance * tt;
  972. if(tt = static_cast<const CGTownInstance*>(obj))
  973. {
  974. SDL_FreeSurface(graphics->townWins[tt->id]);
  975. graphics->townWins[tt->id] = infoWin(tt);
  976. }
  977. if(tt->visitingHero)
  978. {
  979. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  980. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  981. }
  982. }
  983. bool wasGarrison = false;
  984. for(std::list<IShowActivable*>::iterator i = listInt.begin(); i != listInt.end(); i++)
  985. {
  986. if((*i)->type & IShowActivable::WITH_GARRISON)
  987. {
  988. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  989. wwg->garr->recreateSlots();
  990. wasGarrison = true;
  991. }
  992. }
  993. LOCPLINT->totalRedraw();
  994. }
  995. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  996. {
  997. boost::unique_lock<boost::recursive_mutex> un(*pim);
  998. switch (buildingID)
  999. {
  1000. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  1001. {
  1002. SDL_FreeSurface(graphics->townWins[town->id]);
  1003. graphics->townWins[town->id] = infoWin(town);
  1004. break;
  1005. }
  1006. }
  1007. if(!castleInt)
  1008. return;
  1009. if(castleInt->town!=town)
  1010. return;
  1011. switch(what)
  1012. {
  1013. case 1:
  1014. CGI->soundh->playSound(soundBase::newBuilding);
  1015. castleInt->addBuilding(buildingID);
  1016. break;
  1017. case 2:
  1018. castleInt->removeBuilding(buildingID);
  1019. break;
  1020. }
  1021. }
  1022. 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
  1023. {
  1024. while(showingDialog->get())
  1025. SDL_Delay(20);
  1026. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1027. battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  1028. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  1029. pushInt(battleInt);
  1030. }
  1031. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1032. {
  1033. }
  1034. 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
  1035. {
  1036. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1037. battleInt->newRound(round);
  1038. }
  1039. void CPlayerInterface::actionStarted(const BattleAction* action)
  1040. {
  1041. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1042. curAction = new BattleAction(*action);
  1043. if( (action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber))) )
  1044. {
  1045. battleInt->moveStarted = true;
  1046. if(battleInt->creAnims[action->stackNumber]->framesInGroup(20))
  1047. {
  1048. battleInt->creAnims[action->stackNumber]->setType(20);
  1049. }
  1050. }
  1051. battleInt->deactivate();
  1052. CStack *stack = cb->battleGetStackByID(action->stackNumber);
  1053. char txt[400];
  1054. if(action->actionType == 1)
  1055. {
  1056. if(action->side)
  1057. battleInt->defendingHero->setPhase(4);
  1058. else
  1059. battleInt->attackingHero->setPhase(4);
  1060. return;
  1061. }
  1062. if(!stack)
  1063. {
  1064. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  1065. return;
  1066. }
  1067. int txtid = 0;
  1068. switch(action->actionType)
  1069. {
  1070. case 3: //defend
  1071. txtid = 120;
  1072. break;
  1073. case 8: //wait
  1074. txtid = 136;
  1075. break;
  1076. case 11: //bad morale
  1077. txtid = -34; //negative -> no separate singular/plural form
  1078. battleInt->displayEffect(30,stack->position);
  1079. break;
  1080. }
  1081. if(txtid > 0 && stack->amount != 1)
  1082. txtid++; //move to plural text
  1083. else if(txtid < 0)
  1084. txtid = -txtid;
  1085. if(txtid)
  1086. {
  1087. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str(), 0);
  1088. LOCPLINT->battleInt->console->addText(txt);
  1089. }
  1090. }
  1091. void CPlayerInterface::actionFinished(const BattleAction* action)
  1092. {
  1093. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1094. delete curAction;
  1095. curAction = NULL;
  1096. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  1097. {
  1098. battleInt->activate();
  1099. }
  1100. if(action->actionType == 1)
  1101. {
  1102. if(action->side)
  1103. battleInt->defendingHero->setPhase(0);
  1104. else
  1105. battleInt->attackingHero->setPhase(0);
  1106. }
  1107. }
  1108. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  1109. {
  1110. CBattleInterface *b = battleInt;
  1111. {
  1112. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1113. CStack *stack = cb->battleGetStackByID(stackID);
  1114. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  1115. {
  1116. std::string hlp = CGI->generaltexth->allTexts[33];
  1117. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  1118. battleInt->displayEffect(20,stack->position);
  1119. battleInt->console->addText(hlp);
  1120. }
  1121. b->stackActivated(stackID);
  1122. }
  1123. //wait till BattleInterface sets its command
  1124. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  1125. while(!b->givenCommand->data)
  1126. b->givenCommand->cond.wait(lock);
  1127. //tidy up
  1128. BattleAction ret = *(b->givenCommand->data);
  1129. delete b->givenCommand->data;
  1130. b->givenCommand->data = NULL;
  1131. //return command
  1132. return ret;
  1133. }
  1134. void CPlayerInterface::battleEnd(BattleResult *br)
  1135. {
  1136. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1137. battleInt->battleFinished(*br);
  1138. }
  1139. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  1140. {
  1141. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1142. battleInt->stackMoved(ID, dest, end, distance);
  1143. }
  1144. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  1145. {
  1146. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1147. battleInt->spellCast(sc);
  1148. }
  1149. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  1150. {
  1151. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1152. battleInt->battleStacksEffectsSet(sse);
  1153. }
  1154. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  1155. {
  1156. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  1157. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1158. tlog5 << "done!\n";
  1159. std::vector<CBattleInterface::SStackAttackedInfo> arg;
  1160. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  1161. {
  1162. if(i->isEffect() && i->effect != 12) //and not armageddon
  1163. {
  1164. battleInt->displayEffect(i->effect, cb->battleGetStackByID(i->stackAttacked)->position);
  1165. }
  1166. CBattleInterface::SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, LOCPLINT->curAction->stackNumber, LOCPLINT->curAction->actionType==7, i->killed()};
  1167. arg.push_back(to_put);
  1168. }
  1169. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  1170. {
  1171. battleInt->displayEffect(bsa.begin()->effect, -1);
  1172. }
  1173. battleInt->stacksAreAttacked(arg);
  1174. }
  1175. void CPlayerInterface::battleAttack(BattleAttack *ba)
  1176. {
  1177. tlog5 << "CPlayerInterface::battleAttack - locking...";
  1178. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1179. tlog5 << "done!\n";
  1180. assert(curAction);
  1181. if(ba->lucky()) //lucky hit
  1182. {
  1183. CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  1184. std::string hlp = CGI->generaltexth->allTexts[45];
  1185. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  1186. battleInt->console->addText(hlp);
  1187. battleInt->displayEffect(18,stack->position);
  1188. }
  1189. //TODO: bad luck?
  1190. if(ba->shot())
  1191. {
  1192. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  1193. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  1194. }
  1195. else
  1196. {
  1197. CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  1198. int shift = 0;
  1199. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  1200. {
  1201. if(attacker->attackerOwned)
  1202. shift = 1;
  1203. else
  1204. shift = -1;
  1205. }
  1206. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  1207. }
  1208. }
  1209. void CPlayerInterface::showComp(SComponent comp)
  1210. {
  1211. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1212. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  1213. adventureInt->infoBar.showComp(&comp,4000);
  1214. }
  1215. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  1216. {
  1217. std::vector<SComponent*> intComps;
  1218. for(int i=0;i<components.size();i++)
  1219. intComps.push_back(new SComponent(*components[i]));
  1220. showInfoDialog(text,intComps,soundID);
  1221. }
  1222. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  1223. {
  1224. {
  1225. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1226. while(showingDialog->data)
  1227. showingDialog->cond.wait(un);
  1228. }
  1229. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1230. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1231. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1232. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1233. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1234. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  1235. if(makingTurn && listInt.size())
  1236. {
  1237. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1238. showingDialog->set(true);
  1239. pushInt(temp);
  1240. }
  1241. else
  1242. {
  1243. dialogs.push_back(temp);
  1244. }
  1245. }
  1246. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  1247. {
  1248. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1249. LOCPLINT->showingDialog->setn(true);
  1250. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1251. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1252. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1253. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,DelComps);
  1254. temp->delComps = DelComps;
  1255. for(int i=0;i<onYes.funcs.size();i++)
  1256. temp->buttons[0]->callback += onYes.funcs[i];
  1257. for(int i=0;i<onNo.funcs.size();i++)
  1258. temp->buttons[1]->callback += onNo.funcs[i];
  1259. LOCPLINT->pushInt(temp);
  1260. }
  1261. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  1262. {
  1263. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1264. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1265. if(!selection && cancel) //simple yes/no dialog
  1266. {
  1267. std::vector<SComponent*> intComps;
  1268. for(int i=0;i<components.size();i++)
  1269. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  1270. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  1271. }
  1272. else if(selection)
  1273. {
  1274. std::vector<CSelectableComponent*> intComps;
  1275. for(int i=0;i<components.size();i++)
  1276. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  1277. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1278. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1279. if(cancel)
  1280. {
  1281. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1282. }
  1283. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  1284. pushInt(temp);
  1285. intComps[0]->clickLeft(true);
  1286. }
  1287. }
  1288. void CPlayerInterface::tileRevealed(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.showTile(*i);
  1293. }
  1294. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  1295. {
  1296. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1297. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1298. adventureInt->minimap.hideTile(*i);
  1299. }
  1300. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  1301. {
  1302. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1303. adventureInt->heroWindow->setHero(hero);
  1304. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  1305. pushInt(adventureInt->heroWindow);
  1306. }
  1307. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  1308. {
  1309. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1310. if(adventureInt->heroWindow->curHero) //hero window is opened
  1311. {
  1312. adventureInt->heroWindow->deactivate();
  1313. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  1314. adventureInt->heroWindow->activate();
  1315. return;
  1316. }
  1317. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(listInt.front());
  1318. if(cew) //exchange window is open
  1319. {
  1320. cew->deactivate();
  1321. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  1322. {
  1323. if(cew->heroInst[g] == hero)
  1324. {
  1325. cew->artifs[g]->setHero(hero);
  1326. }
  1327. }
  1328. cew->prepareBackground();
  1329. cew->activate();
  1330. }
  1331. }
  1332. void CPlayerInterface::updateWater()
  1333. {
  1334. }
  1335. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  1336. {
  1337. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1338. if(castleInt)
  1339. {
  1340. CFortScreen *fs = dynamic_cast<CFortScreen*>(listInt.front());
  1341. if(fs)
  1342. fs->draw(castleInt,false);
  1343. }
  1344. }
  1345. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  1346. {
  1347. if(bonus.type == HeroBonus::NONE) return;
  1348. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1349. redrawHeroWin(hero);
  1350. }
  1351. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1352. {
  1353. h & playerID & serialID;
  1354. h & sysOpts;
  1355. h & CBattleInterface::settings;
  1356. }
  1357. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1358. {
  1359. serializeTempl(h,version);
  1360. }
  1361. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1362. {
  1363. serializeTempl(h,version);
  1364. sysOpts.apply();
  1365. }
  1366. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  1367. {
  1368. if(!vstd::contains(graphics->heroWins,hero->subID))
  1369. {
  1370. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  1371. return;
  1372. }
  1373. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  1374. graphics->heroWins[hero->subID] = infoWin(hero);
  1375. if (adventureInt->selection == hero)
  1376. adventureInt->infoBar.draw(screen);
  1377. }
  1378. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CPath path )
  1379. {
  1380. if (!h)
  1381. return false; //can't find hero
  1382. bool result = false;
  1383. path.convert(0);
  1384. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1385. stillMoveHero.data = CONTINUE_MOVE;
  1386. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1387. enum TerrainTile::EterrainType newTerrain;
  1388. int sh = -1;
  1389. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  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. std::vector<std::pair<int,int> > cres;
  1523. for(int i = 0; i < dwelling->creatures.size(); i++)
  1524. {
  1525. if(i == level || level < 0)
  1526. for(size_t j = 0; j < dwelling->creatures[i].second.size(); j++)
  1527. cres.push_back( std::make_pair(dwelling->creatures[i].second[j],dwelling->creatures[i].first));
  1528. }
  1529. CRecruitmentWindow *cr = new CRecruitmentWindow(cres, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1530. pushInt(cr);
  1531. }
  1532. void SystemOptions::setMusicVolume( int newVolume )
  1533. {
  1534. musicVolume = newVolume;
  1535. CGI->musich->setVolume(newVolume);
  1536. settingsChanged();
  1537. }
  1538. void SystemOptions::setSoundVolume( int newVolume )
  1539. {
  1540. soundVolume = newVolume;
  1541. CGI->soundh->setVolume(newVolume);
  1542. settingsChanged();
  1543. }
  1544. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1545. {
  1546. heroMoveSpeed = newSpeed;
  1547. settingsChanged();
  1548. }
  1549. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1550. {
  1551. mapScrollingSpeed = newSpeed;
  1552. settingsChanged();
  1553. }
  1554. void SystemOptions::settingsChanged()
  1555. {
  1556. CSaveFile settings("config" PATHSEPARATOR "sysopts.bin");
  1557. if(settings.sfile)
  1558. settings << *this;
  1559. else
  1560. tlog1 << "Cannot save settings to config" PATHSEPARATOR "sysopts.bin!\n";
  1561. }
  1562. void SystemOptions::apply()
  1563. {
  1564. CGI->musich->setVolume(musicVolume);
  1565. CGI->soundh->setVolume(soundVolume);
  1566. settingsChanged();
  1567. }