CPlayerInterface.cpp 59 KB

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