CPlayerInterface.cpp 61 KB

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