CPlayerInterface.cpp 65 KB

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