CPlayerInterface.cpp 62 KB

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