CPlayerInterface.cpp 58 KB

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