CPlayerInterface.cpp 60 KB

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