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