CPlayerInterface.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. #include "../stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "../CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CCursorHandler.h"
  7. #include "CGameInfo.h"
  8. #include "CHeroWindow.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. //#include "SDL_Extensions.h"
  12. #include "SDL_Extensions.h"
  13. //#include "SDL_framerate.h"
  14. #include "SDL_framerate.h"
  15. #include "CConfigHandler.h"
  16. #include "CCreatureAnimation.h"
  17. #include "Graphics.h"
  18. #include "../hch/CArtHandler.h"
  19. #include "../hch/CGeneralTextHandler.h"
  20. #include "../hch/CHeroHandler.h"
  21. #include "../hch/CLodHandler.h"
  22. #include "../hch/CObjectHandler.h"
  23. #include "../lib/Connection.h"
  24. #include "../hch/CSpellHandler.h"
  25. #include "../hch/CTownHandler.h"
  26. #include "../lib/CondSh.h"
  27. #include "../lib/NetPacks.h"
  28. #include "../lib/map.h"
  29. #include "../lib/VCMIDirs.h"
  30. #include "../mapHandler.h"
  31. #include "../timeHandler.h"
  32. #include <boost/lexical_cast.hpp>
  33. #include <boost/algorithm/string.hpp>
  34. #include <boost/algorithm/string/replace.hpp>
  35. #include <boost/assign/std/vector.hpp>
  36. #include <boost/assign/list_of.hpp>
  37. #include <boost/date_time/posix_time/posix_time.hpp>
  38. #include <boost/thread.hpp>
  39. #include <cmath>
  40. #include <queue>
  41. #include <sstream>
  42. #ifdef min
  43. #undef min
  44. #endif
  45. #ifdef max
  46. #undef max
  47. #endif
  48. /*
  49. * CPlayerInterface.cpp, part of VCMI engine
  50. *
  51. * Authors: listed in file AUTHORS in main folder
  52. *
  53. * License: GNU General Public License v2.0 or later
  54. * Full text of license available in license.txt file, in main folder
  55. *
  56. */
  57. using namespace boost::assign;
  58. using namespace CSDL_Ext;
  59. void processCommand(const std::string &message, CClient *&client);
  60. extern std::queue<SDL_Event*> events;
  61. extern boost::mutex eventsM;
  62. CPlayerInterface * LOCPLINT;
  63. enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
  64. CondSh<EMoveState> stillMoveHero; //used during hero movement
  65. struct OCM_HLP_CGIN
  66. {
  67. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  68. {
  69. return (*a.first)<(*b.first);
  70. }
  71. } ocmptwo_cgin ;
  72. CPlayerInterface::CPlayerInterface(int Player, int serial)
  73. {
  74. GH.defActionsDef = 0;
  75. LOCPLINT = this;
  76. curAction = NULL;
  77. playerID=Player;
  78. serialID=serial;
  79. human=true;
  80. castleInt = NULL;
  81. adventureInt = NULL;
  82. battleInt = NULL;
  83. pim = new boost::recursive_mutex;
  84. makingTurn = false;
  85. showingDialog = new CondSh<bool>(false);
  86. sysOpts = GDefaultOptions;
  87. //initializing framerate keeper
  88. mainFPSmng = new FPSmanager;
  89. SDL_initFramerate(mainFPSmng);
  90. SDL_setFramerate(mainFPSmng, 48);
  91. //framerate keeper initialized
  92. cingconsole = new CInGameConsole;
  93. }
  94. CPlayerInterface::~CPlayerInterface()
  95. {
  96. delete pim;
  97. delete showingDialog;
  98. delete mainFPSmng;
  99. delete adventureInt;
  100. delete cingconsole;
  101. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
  102. SDL_FreeSurface(i->second);
  103. for(std::map<int,SDL_Surface*>::iterator i=graphics->townWins.begin(); i!= graphics->townWins.end(); i++)
  104. SDL_FreeSurface(i->second);
  105. }
  106. void CPlayerInterface::init(ICallback * CB)
  107. {
  108. cb = dynamic_cast<CCallback*>(CB);
  109. adventureInt = new CAdvMapInt(playerID);
  110. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  111. for(int i=0;i<tt.size();i++)
  112. {
  113. SDL_Surface * pom = infoWin(tt[i]);
  114. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  115. }
  116. recreateWanderingHeroes();
  117. }
  118. void CPlayerInterface::yourTurn()
  119. {
  120. try
  121. {
  122. LOCPLINT = this;
  123. makingTurn = true;
  124. static bool firstCall = true;
  125. static int autosaveCount = 0;
  126. if(firstCall)
  127. firstCall = false;
  128. else
  129. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  130. autosaveCount %= 5;
  131. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  132. SDL_FreeSurface(i->second);
  133. graphics->heroWins.clear();
  134. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  135. for(int i=0;i<hh.size();i++)
  136. {
  137. SDL_Surface * pom = infoWin(hh[i]);
  138. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  139. }
  140. /* TODO: This isn't quite right. First day in game should play
  141. * NEWDAY. And we don't play NEWMONTH. */
  142. int day = cb->getDate(1);
  143. if (day != 1)
  144. CGI->soundh->playSound(soundBase::newDay);
  145. else
  146. CGI->soundh->playSound(soundBase::newWeek);
  147. adventureInt->infoBar.newDay(day);
  148. //select first hero if available.
  149. //TODO: check if hero is slept
  150. if(wanderingHeroes.size())
  151. adventureInt->select(wanderingHeroes[0]);
  152. else
  153. adventureInt->select(adventureInt->townList.items[0]);
  154. adventureInt->showAll(screen);
  155. GH.pushInt(adventureInt);
  156. adventureInt->activateKeys();
  157. while(makingTurn) // main loop
  158. {
  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::battleCatapultAttacked(const CatapultAttack & ca)
  881. {
  882. battleInt->stackIsCatapulting(ca);
  883. }
  884. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  885. {
  886. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  887. {
  888. battleInt->stackRemoved(*it);
  889. }
  890. }
  891. 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
  892. {
  893. boost::unique_lock<boost::recursive_mutex> un(*pim);
  894. battleInt->newRound(round);
  895. }
  896. void CPlayerInterface::actionStarted(const BattleAction* action)
  897. {
  898. boost::unique_lock<boost::recursive_mutex> un(*pim);
  899. curAction = new BattleAction(*action);
  900. battleInt->startAction(action);
  901. }
  902. void CPlayerInterface::actionFinished(const BattleAction* action)
  903. {
  904. boost::unique_lock<boost::recursive_mutex> un(*pim);
  905. delete curAction;
  906. curAction = NULL;
  907. battleInt->endAction(action);
  908. }
  909. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  910. {
  911. CBattleInterface *b = battleInt;
  912. {
  913. boost::unique_lock<boost::recursive_mutex> un(*pim);
  914. const CStack *stack = cb->battleGetStackByID(stackID);
  915. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  916. {
  917. std::string hlp = CGI->generaltexth->allTexts[33];
  918. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  919. battleInt->displayEffect(20,stack->position);
  920. battleInt->console->addText(hlp);
  921. }
  922. b->stackActivated(stackID);
  923. }
  924. //wait till BattleInterface sets its command
  925. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  926. while(!b->givenCommand->data)
  927. b->givenCommand->cond.wait(lock);
  928. //tidy up
  929. BattleAction ret = *(b->givenCommand->data);
  930. delete b->givenCommand->data;
  931. b->givenCommand->data = NULL;
  932. //return command
  933. return ret;
  934. }
  935. void CPlayerInterface::battleEnd(BattleResult *br)
  936. {
  937. boost::unique_lock<boost::recursive_mutex> un(*pim);
  938. battleInt->battleFinished(*br);
  939. }
  940. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  941. {
  942. boost::unique_lock<boost::recursive_mutex> un(*pim);
  943. battleInt->stackMoved(ID, dest, end, distance);
  944. }
  945. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  946. {
  947. boost::unique_lock<boost::recursive_mutex> un(*pim);
  948. battleInt->spellCast(sc);
  949. }
  950. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  951. {
  952. boost::unique_lock<boost::recursive_mutex> un(*pim);
  953. battleInt->battleStacksEffectsSet(sse);
  954. }
  955. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  956. {
  957. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  958. boost::unique_lock<boost::recursive_mutex> un(*pim);
  959. tlog5 << "done!\n";
  960. std::vector<SStackAttackedInfo> arg;
  961. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  962. {
  963. if(i->isEffect() && i->effect != 12) //and not armageddon
  964. {
  965. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  966. if (stack != NULL)
  967. battleInt->displayEffect(i->effect, stack->position);
  968. }
  969. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  970. arg.push_back(to_put);
  971. }
  972. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  973. {
  974. battleInt->displayEffect(bsa.begin()->effect, -1);
  975. }
  976. battleInt->stacksAreAttacked(arg);
  977. }
  978. void CPlayerInterface::battleAttack(BattleAttack *ba)
  979. {
  980. tlog5 << "CPlayerInterface::battleAttack - locking...";
  981. boost::unique_lock<boost::recursive_mutex> un(*pim);
  982. tlog5 << "done!\n";
  983. assert(curAction);
  984. if(ba->lucky()) //lucky hit
  985. {
  986. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  987. std::string hlp = CGI->generaltexth->allTexts[45];
  988. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  989. battleInt->console->addText(hlp);
  990. battleInt->displayEffect(18,stack->position);
  991. }
  992. //TODO: bad luck?
  993. if(ba->shot())
  994. {
  995. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  996. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  997. }
  998. else
  999. {
  1000. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  1001. int shift = 0;
  1002. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  1003. {
  1004. if( BattleInfo::mutualPosition(curAction->destinationTile + 1, attacker->position) >= 0 )
  1005. shift = 1;
  1006. else
  1007. shift = -1;
  1008. }
  1009. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  1010. }
  1011. }
  1012. void CPlayerInterface::showComp(SComponent comp)
  1013. {
  1014. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1015. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  1016. adventureInt->infoBar.showComp(&comp,4000);
  1017. }
  1018. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  1019. {
  1020. std::vector<SComponent*> intComps;
  1021. for(int i=0;i<components.size();i++)
  1022. intComps.push_back(new SComponent(*components[i]));
  1023. showInfoDialog(text,intComps,soundID);
  1024. }
  1025. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  1026. {
  1027. waitWhileDialog();
  1028. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1029. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1030. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1031. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1032. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1033. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  1034. if(makingTurn && GH.listInt.size())
  1035. {
  1036. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1037. showingDialog->set(true);
  1038. GH.pushInt(temp);
  1039. }
  1040. else
  1041. {
  1042. dialogs.push_back(temp);
  1043. }
  1044. }
  1045. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  1046. {
  1047. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1048. LOCPLINT->showingDialog->setn(true);
  1049. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1050. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1051. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1052. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,DelComps);
  1053. temp->delComps = DelComps;
  1054. for(int i=0;i<onYes.funcs.size();i++)
  1055. temp->buttons[0]->callback += onYes.funcs[i];
  1056. for(int i=0;i<onNo.funcs.size();i++)
  1057. temp->buttons[1]->callback += onNo.funcs[i];
  1058. GH.pushInt(temp);
  1059. }
  1060. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  1061. {
  1062. waitWhileDialog();
  1063. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1064. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1065. if(!selection && cancel) //simple yes/no dialog
  1066. {
  1067. std::vector<SComponent*> intComps;
  1068. for(int i=0;i<components.size();i++)
  1069. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  1070. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  1071. }
  1072. else if(selection)
  1073. {
  1074. std::vector<CSelectableComponent*> intComps;
  1075. for(int i=0;i<components.size();i++)
  1076. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  1077. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1078. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1079. if(cancel)
  1080. {
  1081. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1082. }
  1083. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  1084. GH.pushInt(temp);
  1085. intComps[0]->clickLeft(true, false);
  1086. }
  1087. }
  1088. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  1089. {
  1090. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1091. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1092. adventureInt->minimap.showTile(*i);
  1093. }
  1094. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  1095. {
  1096. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1097. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1098. adventureInt->minimap.hideTile(*i);
  1099. }
  1100. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  1101. {
  1102. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1103. adventureInt->heroWindow->setHero(hero);
  1104. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  1105. GH.pushInt(adventureInt->heroWindow);
  1106. }
  1107. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  1108. {
  1109. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1110. if(adventureInt->heroWindow->curHero) //hero window is opened
  1111. {
  1112. adventureInt->heroWindow->deactivate();
  1113. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  1114. adventureInt->heroWindow->activate();
  1115. return;
  1116. }
  1117. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  1118. if(cew) //exchange window is open
  1119. {
  1120. cew->deactivate();
  1121. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  1122. {
  1123. if(cew->heroInst[g] == hero)
  1124. {
  1125. cew->artifs[g]->setHero(hero);
  1126. }
  1127. }
  1128. cew->prepareBackground();
  1129. cew->activate();
  1130. }
  1131. }
  1132. void CPlayerInterface::updateWater()
  1133. {
  1134. }
  1135. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  1136. {
  1137. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1138. if(castleInt && town->ID == TOWNI_TYPE)
  1139. {
  1140. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  1141. if(fs)
  1142. fs->draw(castleInt,false);
  1143. }
  1144. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20)) //external dwelling
  1145. {
  1146. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  1147. if(crw)
  1148. crw->initCres();
  1149. }
  1150. }
  1151. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  1152. {
  1153. if(bonus.type == HeroBonus::NONE) return;
  1154. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1155. redrawHeroWin(hero);
  1156. }
  1157. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1158. {
  1159. h & playerID & serialID;
  1160. h & sysOpts;
  1161. }
  1162. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1163. {
  1164. serializeTempl(h,version);
  1165. }
  1166. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1167. {
  1168. serializeTempl(h,version);
  1169. sysOpts.apply();
  1170. }
  1171. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  1172. {
  1173. if(!vstd::contains(graphics->heroWins,hero->subID))
  1174. {
  1175. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  1176. return;
  1177. }
  1178. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  1179. graphics->heroWins[hero->subID] = infoWin(hero);
  1180. if (adventureInt->selection == hero)
  1181. adventureInt->infoBar.draw(screen);
  1182. }
  1183. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  1184. {
  1185. if (!h)
  1186. return false; //can't find hero
  1187. bool result = false;
  1188. path.convert(0);
  1189. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1190. stillMoveHero.data = CONTINUE_MOVE;
  1191. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1192. enum TerrainTile::EterrainType newTerrain;
  1193. int sh = -1;
  1194. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  1195. {
  1196. //stop sending move requests if hero exhausted all his move points
  1197. if(!h->movement)
  1198. {
  1199. stillMoveHero.data = STOP_MOVE;
  1200. break;
  1201. }
  1202. // Start a new sound for the hero movement or let the existing one carry on.
  1203. #if 0
  1204. // TODO
  1205. if (hero is flying && sh == -1)
  1206. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  1207. #endif
  1208. {
  1209. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1210. if (newTerrain != currentTerrain) {
  1211. CGI->soundh->stopSound(sh);
  1212. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  1213. currentTerrain = newTerrain;
  1214. }
  1215. }
  1216. stillMoveHero.data = WAITING_MOVE;
  1217. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1218. cb->moveHero(h,endpos);
  1219. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1220. stillMoveHero.cond.wait(un);
  1221. }
  1222. CGI->soundh->stopSound(sh);
  1223. //stillMoveHero = false;
  1224. cb->recalculatePaths();
  1225. return result;
  1226. }
  1227. bool CPlayerInterface::shiftPressed() const
  1228. {
  1229. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1230. }
  1231. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  1232. {
  1233. {
  1234. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1235. while(showingDialog->data)
  1236. showingDialog->cond.wait(un);
  1237. }
  1238. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1239. while(dialogs.size())
  1240. {
  1241. pim->unlock();
  1242. SDL_Delay(20);
  1243. pim->lock();
  1244. }
  1245. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  1246. cgw->quit->callback += onEnd;
  1247. GH.pushInt(cgw);
  1248. }
  1249. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1250. {
  1251. if(stillMoveHero.get() == DURING_MOVE)
  1252. stillMoveHero.setn(CONTINUE_MOVE);
  1253. }
  1254. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1255. {
  1256. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1257. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1258. }
  1259. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1260. {
  1261. //redraw minimap if owner changed
  1262. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1263. if(sop->what == 1)
  1264. {
  1265. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1266. std::set<int3> pos = obj->getBlockedPos();
  1267. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1268. {
  1269. if(cb->isVisible(*it))
  1270. adventureInt->minimap.showTile(*it);
  1271. }
  1272. if(obj->ID == TOWNI_TYPE)
  1273. adventureInt->townList.genList();
  1274. }
  1275. }
  1276. void CPlayerInterface::recreateWanderingHeroes()
  1277. {
  1278. wanderingHeroes.clear();
  1279. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1280. for(size_t i = 0; i < heroes.size(); i++)
  1281. if(!heroes[i]->inTownGarrison)
  1282. wanderingHeroes.push_back(heroes[i]);
  1283. }
  1284. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1285. {
  1286. if(pos < 0 || pos >= wanderingHeroes.size())
  1287. return NULL;
  1288. return wanderingHeroes[pos];
  1289. }
  1290. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1291. {
  1292. waitWhileDialog();
  1293. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1294. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1295. GH.pushInt(cr);
  1296. }
  1297. void CPlayerInterface::waitWhileDialog()
  1298. {
  1299. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1300. while(showingDialog->data)
  1301. showingDialog->cond.wait(un);
  1302. }
  1303. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1304. {
  1305. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1306. int state = obj->state();
  1307. std::vector<si32> cost;
  1308. obj->getBoatCost(cost);
  1309. CShipyardWindow *csw = new CShipyardWindow(cost, state, boost::bind(&CCallback::buildBoat, cb, obj));
  1310. GH.pushInt(csw);
  1311. }
  1312. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1313. {
  1314. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1315. CGI->mh->printObject(obj);
  1316. //we might have built a boat in shipyard in opened town screen
  1317. if(obj->ID == 8
  1318. && LOCPLINT->castleInt
  1319. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1320. {
  1321. CGI->soundh->playSound(soundBase::newBuilding);
  1322. LOCPLINT->castleInt->recreateBuildings();
  1323. }
  1324. }
  1325. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1326. {
  1327. LOCPLINT->adventureInt->centerOn (pos);
  1328. if(focusTime)
  1329. {
  1330. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->active);
  1331. if(activeAdv)
  1332. adventureInt->deactivate();
  1333. SDL_Delay(focusTime);
  1334. if(activeAdv)
  1335. adventureInt->activate();
  1336. }
  1337. }
  1338. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1339. {
  1340. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1341. {
  1342. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1343. heroKilled(h);
  1344. }
  1345. }
  1346. void SystemOptions::setMusicVolume( int newVolume )
  1347. {
  1348. musicVolume = newVolume;
  1349. CGI->musich->setVolume(newVolume);
  1350. settingsChanged();
  1351. }
  1352. void SystemOptions::setSoundVolume( int newVolume )
  1353. {
  1354. soundVolume = newVolume;
  1355. CGI->soundh->setVolume(newVolume);
  1356. settingsChanged();
  1357. }
  1358. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1359. {
  1360. heroMoveSpeed = newSpeed;
  1361. settingsChanged();
  1362. }
  1363. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1364. {
  1365. mapScrollingSpeed = newSpeed;
  1366. settingsChanged();
  1367. }
  1368. void SystemOptions::settingsChanged()
  1369. {
  1370. CSaveFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  1371. if(settings.sfile)
  1372. settings << *this;
  1373. else
  1374. tlog1 << "Cannot save settings to config/sysopts.bin!\n";
  1375. }
  1376. void SystemOptions::apply()
  1377. {
  1378. if(CGI->musich->getVolume() != musicVolume)
  1379. CGI->musich->setVolume(musicVolume);
  1380. if(CGI->soundh->getVolume() != soundVolume)
  1381. CGI->soundh->setVolume(soundVolume);
  1382. settingsChanged();
  1383. }
  1384. SystemOptions::SystemOptions()
  1385. {
  1386. heroMoveSpeed = 2;
  1387. mapScrollingSpeed = 2;
  1388. musicVolume = 88;
  1389. soundVolume = 88;
  1390. printCellBorders = true;
  1391. printStackRange = true;
  1392. animSpeed = 2;
  1393. printMouseShadow = true;
  1394. showQueue = true;
  1395. }