CPlayerInterface.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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 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->soundh->playSound(soundBase::newDay);
  140. else
  141. CGI->soundh->playSound(soundBase::newWeek);
  142. adventureInt->infoBar.newDay(day);
  143. //select first hero if available.
  144. //TODO: check if hero is slept
  145. if(wanderingHeroes.size())
  146. adventureInt->select(wanderingHeroes[0]);
  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. static 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 TryMoveHero & details)
  273. {
  274. boost::unique_lock<boost::recursive_mutex> un(*pim);
  275. const CGHeroInstance * ho = cb->getHeroInfo(details.id); //object representing this hero
  276. adventureInt->centerOn(ho->pos); //actualizing screen pos
  277. adventureInt->minimap.draw(screen2);
  278. adventureInt->heroList.draw(screen2);
  279. if(details.result == TryMoveHero::TELEPORTATION || details.start == details.end)
  280. return;
  281. //initializing objects and performing first step of move
  282. int3 hp = details.start;
  283. if (details.result != TryMoveHero::SUCCESS) //hero failed to move
  284. {
  285. if(details.result == TryMoveHero::BLOCKING_VISIT)
  286. {
  287. ho->moveDir = getDir(details.start,details.end);
  288. adventureInt->paths.erase(ho);
  289. adventureInt->terrain.currentPath = NULL;
  290. }
  291. ho->isStanding = true;
  292. stillMoveHero.setn(STOP_MOVE);
  293. return;
  294. }
  295. if (adventureInt->terrain.currentPath) //&& hero is moving
  296. {
  297. //remove one node from the path (the one we went)
  298. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  299. if(!adventureInt->terrain.currentPath->nodes.size()) //if it was the last one, remove entire path
  300. {
  301. adventureInt->paths.erase(ho);
  302. adventureInt->terrain.currentPath = NULL;
  303. }
  304. }
  305. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  306. {
  307. ho->moveDir = 1;
  308. ho->isStanding = false;
  309. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  310. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  311. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  312. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  313. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  314. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  315. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  316. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  317. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  318. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  319. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  320. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  321. 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);
  322. 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);
  323. 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);
  324. 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);
  325. 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);
  326. 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);
  327. }
  328. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  329. {
  330. ho->moveDir = 2;
  331. ho->isStanding = false;
  332. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  333. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  334. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  335. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  336. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  337. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  338. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  339. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  340. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  341. 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);
  342. 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);
  343. 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);
  344. }
  345. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  346. {
  347. ho->moveDir = 3;
  348. ho->isStanding = false;
  349. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  350. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  351. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  352. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  353. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  354. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  355. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  356. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  357. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  358. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  359. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  360. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  361. 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);
  362. 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);
  363. 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);
  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+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  366. 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);
  367. }
  368. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  369. {
  370. ho->moveDir = 4;
  371. ho->isStanding = false;
  372. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  373. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  374. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  375. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  376. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  377. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  378. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  379. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  380. 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);
  381. 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);
  382. }
  383. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  384. {
  385. ho->moveDir = 5;
  386. ho->isStanding = false;
  387. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  388. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  389. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  390. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  391. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  392. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  393. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  394. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  395. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  396. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  397. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  398. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  399. 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);
  400. 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);
  401. 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);
  402. 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);
  403. 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);
  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. }
  406. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  407. {
  408. ho->moveDir = 6;
  409. ho->isStanding = false;
  410. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  411. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  412. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  413. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  414. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  415. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  416. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  417. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  418. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  419. 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);
  420. 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);
  421. 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);
  422. }
  423. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  424. {
  425. ho->moveDir = 7;
  426. ho->isStanding = false;
  427. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  428. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  429. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  430. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  431. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  432. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  433. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  434. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  435. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  436. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  437. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  438. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  439. 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);
  440. 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);
  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-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  443. 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);
  444. 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);
  445. }
  446. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  447. {
  448. ho->moveDir = 8;
  449. ho->isStanding = false;
  450. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  451. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  452. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  453. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  454. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  455. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  456. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  457. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  458. 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);
  459. 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);
  460. }
  461. //first initializing done
  462. SDL_framerateDelay(mainFPSmng); // after first move
  463. //main moving
  464. for(int i=1; i<32; i+=2*heroMoveSpeed)
  465. {
  466. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  467. {
  468. //setting advmap shift
  469. adventureInt->terrain.moveX = i-32;
  470. adventureInt->terrain.moveY = i-32;
  471. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  472. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  473. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  474. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  475. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  476. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  477. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  478. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  479. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  480. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  481. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  482. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  483. }
  484. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  485. {
  486. //setting advmap shift
  487. adventureInt->terrain.moveY = i-32;
  488. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  489. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  490. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  491. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  492. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  493. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  494. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  495. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  496. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  497. }
  498. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  499. {
  500. //setting advmap shift
  501. adventureInt->terrain.moveX = -i+32;
  502. adventureInt->terrain.moveY = i-32;
  503. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  504. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  505. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  506. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  507. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  508. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  509. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  510. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  511. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  512. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  513. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  514. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  515. }
  516. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  517. {
  518. //setting advmap shift
  519. adventureInt->terrain.moveX = -i+32;
  520. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  521. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  522. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  523. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  524. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  525. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  526. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  527. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  528. }
  529. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  530. {
  531. //setting advmap shift
  532. adventureInt->terrain.moveX = -i+32;
  533. adventureInt->terrain.moveY = -i+32;
  534. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  535. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  536. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  537. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  538. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  539. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  540. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  541. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  542. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  543. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  544. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  545. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  546. }
  547. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  548. {
  549. //setting advmap shift
  550. adventureInt->terrain.moveY = -i+32;
  551. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  552. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  553. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  554. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  555. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  556. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  557. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  558. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  559. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  560. }
  561. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  562. {
  563. //setting advmap shift
  564. adventureInt->terrain.moveX = i-32;
  565. adventureInt->terrain.moveY = -i+32;
  566. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  567. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  568. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  569. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  570. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  571. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  572. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  573. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  574. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  575. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  576. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  577. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  578. }
  579. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  580. {
  581. //setting advmap shift
  582. adventureInt->terrain.moveX = i-32;
  583. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  584. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  585. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  586. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  587. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  588. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  589. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  590. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  591. }
  592. adventureInt->updateScreen = true;
  593. adventureInt->show(screen);
  594. //LOCPLINT->adventureInt->show(); //updating screen
  595. CSDL_Ext::update(screen);
  596. SDL_Delay(5);
  597. SDL_framerateDelay(mainFPSmng); //for animation purposes
  598. } //for(int i=1; i<32; i+=4)
  599. //main moving done
  600. //finishing move
  601. //restoring adventureInt->terrain.move*
  602. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  603. if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
  604. {
  605. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  606. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  607. delObjRect(hp.x, hp.y, hp.z, ho->id);
  608. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  609. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  610. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  611. }
  612. else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
  613. {
  614. delObjRect(hp.x, hp.y, hp.z, ho->id);
  615. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  616. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  617. }
  618. else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
  619. {
  620. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  621. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  622. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  623. delObjRect(hp.x, hp.y, hp.z, ho->id);
  624. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  625. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  626. }
  627. else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
  628. {
  629. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  630. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  631. }
  632. else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
  633. {
  634. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  635. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  636. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  637. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  638. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  639. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  640. }
  641. else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
  642. {
  643. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  644. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  645. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  646. }
  647. else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
  648. {
  649. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  650. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  651. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  652. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  653. delObjRect(hp.x, hp.y, hp.z, ho->id);
  654. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  655. }
  656. else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
  657. {
  658. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  659. delObjRect(hp.x, hp.y, hp.z, ho->id);
  660. }
  661. //restoring good rects
  662. subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
  663. subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
  664. subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
  665. subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
  666. subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
  667. subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
  668. //restoring good order of objects
  669. 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);
  670. 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);
  671. 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);
  672. 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);
  673. 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);
  674. 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);
  675. ho->isStanding = true;
  676. //move finished
  677. adventureInt->minimap.draw(screen2);
  678. adventureInt->heroList.updateMove(ho);
  679. //check if user cancelled movement
  680. {
  681. boost::unique_lock<boost::mutex> un(eventsM);
  682. while(events.size())
  683. {
  684. SDL_Event *ev = events.front();
  685. events.pop();
  686. switch(ev->type)
  687. {
  688. case SDL_MOUSEBUTTONDOWN:
  689. stillMoveHero.setn(STOP_MOVE);
  690. break;
  691. case SDL_KEYDOWN:
  692. if(ev->key.keysym.sym < SDLK_F1)
  693. stillMoveHero.setn(STOP_MOVE);
  694. break;
  695. }
  696. delete ev;
  697. }
  698. }
  699. if(stillMoveHero.get() == 1)
  700. stillMoveHero.setn(DURING_MOVE);
  701. }
  702. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  703. {
  704. boost::unique_lock<boost::recursive_mutex> un(*pim);
  705. graphics->heroWins.erase(hero->ID);
  706. wanderingHeroes -= hero;
  707. adventureInt->heroList.updateHList(hero);
  708. }
  709. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  710. {
  711. boost::unique_lock<boost::recursive_mutex> un(*pim);
  712. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  713. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  714. wanderingHeroes.push_back(hero);
  715. adventureInt->heroList.updateHList();
  716. }
  717. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  718. {
  719. castleInt = new CCastleInterface(town);
  720. CGI->musich->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] && adventureInt->active)
  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<-CGI->mh->frameW)
  914. pos.x = -CGI->mh->frameW;
  915. if(pos.y<-CGI->mh->frameH)
  916. pos.y = -CGI->mh->frameH;
  917. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW)
  918. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + CGI->mh->frameW;
  919. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH)
  920. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + CGI->mh->frameH;
  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->soundh->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 && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
  964. {
  965. CGI->mh->hideObject(town->garrisonHero);
  966. wanderingHeroes -= town->garrisonHero;
  967. }
  968. if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
  969. {
  970. CGI->mh->printObject(town->visitingHero);
  971. wanderingHeroes.push_back(town->visitingHero);
  972. }
  973. //adventureInt->heroList.updateHList();
  974. CCastleInterface *c = castleInt;
  975. if(c)
  976. {
  977. c->garr->highlighted = NULL;
  978. c->hslotup.hero = town->garrisonHero;
  979. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  980. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  981. c->garr->recreateSlots();
  982. }
  983. LOCPLINT->totalRedraw();
  984. }
  985. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  986. {
  987. if(hero->tempOwner != town->tempOwner)
  988. return;
  989. boost::unique_lock<boost::recursive_mutex> un(*pim);
  990. openTownWindow(town);
  991. }
  992. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  993. {
  994. boost::unique_lock<boost::recursive_mutex> un(*pim);
  995. if(obj->ID == HEROI_TYPE) //hero
  996. {
  997. const CGHeroInstance * hh;
  998. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  999. {
  1000. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  1001. graphics->heroWins[hh->subID] = infoWin(hh);
  1002. }
  1003. }
  1004. else if (obj->ID == TOWNI_TYPE) //town
  1005. {
  1006. const CGTownInstance * tt;
  1007. if(tt = static_cast<const CGTownInstance*>(obj))
  1008. {
  1009. SDL_FreeSurface(graphics->townWins[tt->id]);
  1010. graphics->townWins[tt->id] = infoWin(tt);
  1011. }
  1012. if(tt->visitingHero)
  1013. {
  1014. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  1015. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  1016. }
  1017. }
  1018. bool wasGarrison = false;
  1019. for(std::list<IShowActivable*>::iterator i = listInt.begin(); i != listInt.end(); i++)
  1020. {
  1021. if((*i)->type & IShowActivable::WITH_GARRISON)
  1022. {
  1023. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  1024. wwg->garr->recreateSlots();
  1025. wasGarrison = true;
  1026. }
  1027. }
  1028. LOCPLINT->totalRedraw();
  1029. }
  1030. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  1031. {
  1032. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1033. switch (buildingID)
  1034. {
  1035. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  1036. {
  1037. SDL_FreeSurface(graphics->townWins[town->id]);
  1038. graphics->townWins[town->id] = infoWin(town);
  1039. break;
  1040. }
  1041. }
  1042. if(!castleInt)
  1043. return;
  1044. if(castleInt->town!=town)
  1045. return;
  1046. switch(what)
  1047. {
  1048. case 1:
  1049. CGI->soundh->playSound(soundBase::newBuilding);
  1050. castleInt->addBuilding(buildingID);
  1051. break;
  1052. case 2:
  1053. castleInt->removeBuilding(buildingID);
  1054. break;
  1055. }
  1056. }
  1057. 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
  1058. {
  1059. while(showingDialog->get())
  1060. SDL_Delay(20);
  1061. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1062. battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  1063. CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
  1064. pushInt(battleInt);
  1065. }
  1066. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1067. {
  1068. }
  1069. 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
  1070. {
  1071. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1072. battleInt->newRound(round);
  1073. }
  1074. void CPlayerInterface::actionStarted(const BattleAction* action)
  1075. {
  1076. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1077. curAction = new BattleAction(*action);
  1078. if( (action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber))) )
  1079. {
  1080. battleInt->moveStarted = true;
  1081. if(battleInt->creAnims[action->stackNumber]->framesInGroup(20))
  1082. {
  1083. battleInt->creAnims[action->stackNumber]->setType(20);
  1084. }
  1085. }
  1086. battleInt->deactivate();
  1087. CStack *stack = cb->battleGetStackByID(action->stackNumber);
  1088. char txt[400];
  1089. if(action->actionType == 1)
  1090. {
  1091. if(action->side)
  1092. battleInt->defendingHero->setPhase(4);
  1093. else
  1094. battleInt->attackingHero->setPhase(4);
  1095. return;
  1096. }
  1097. if(!stack)
  1098. {
  1099. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  1100. return;
  1101. }
  1102. int txtid = 0;
  1103. switch(action->actionType)
  1104. {
  1105. case 3: //defend
  1106. txtid = 120;
  1107. break;
  1108. case 8: //wait
  1109. txtid = 136;
  1110. break;
  1111. case 11: //bad morale
  1112. txtid = -34; //negative -> no separate singular/plural form
  1113. battleInt->displayEffect(30,stack->position);
  1114. break;
  1115. }
  1116. if(txtid > 0 && stack->amount != 1)
  1117. txtid++; //move to plural text
  1118. else if(txtid < 0)
  1119. txtid = -txtid;
  1120. if(txtid)
  1121. {
  1122. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str(), 0);
  1123. LOCPLINT->battleInt->console->addText(txt);
  1124. }
  1125. }
  1126. void CPlayerInterface::actionFinished(const BattleAction* action)
  1127. {
  1128. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1129. delete curAction;
  1130. curAction = NULL;
  1131. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  1132. {
  1133. battleInt->activate();
  1134. }
  1135. if(action->actionType == 1)
  1136. {
  1137. if(action->side)
  1138. battleInt->defendingHero->setPhase(0);
  1139. else
  1140. battleInt->attackingHero->setPhase(0);
  1141. }
  1142. }
  1143. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  1144. {
  1145. CBattleInterface *b = battleInt;
  1146. {
  1147. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1148. CStack *stack = cb->battleGetStackByID(stackID);
  1149. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  1150. {
  1151. std::string hlp = CGI->generaltexth->allTexts[33];
  1152. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  1153. battleInt->displayEffect(20,stack->position);
  1154. battleInt->console->addText(hlp);
  1155. }
  1156. b->stackActivated(stackID);
  1157. }
  1158. //wait till BattleInterface sets its command
  1159. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  1160. while(!b->givenCommand->data)
  1161. b->givenCommand->cond.wait(lock);
  1162. //tidy up
  1163. BattleAction ret = *(b->givenCommand->data);
  1164. delete b->givenCommand->data;
  1165. b->givenCommand->data = NULL;
  1166. //return command
  1167. return ret;
  1168. }
  1169. void CPlayerInterface::battleEnd(BattleResult *br)
  1170. {
  1171. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1172. battleInt->battleFinished(*br);
  1173. }
  1174. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  1175. {
  1176. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1177. battleInt->stackMoved(ID, dest, end, distance);
  1178. }
  1179. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  1180. {
  1181. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1182. battleInt->spellCast(sc);
  1183. }
  1184. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  1185. {
  1186. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1187. battleInt->battleStacksEffectsSet(sse);
  1188. }
  1189. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  1190. {
  1191. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  1192. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1193. tlog5 << "done!\n";
  1194. std::vector<CBattleInterface::SStackAttackedInfo> arg;
  1195. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  1196. {
  1197. if(i->isEffect() && i->effect != 12) //and not armageddon
  1198. {
  1199. battleInt->displayEffect(i->effect, cb->battleGetStackByID(i->stackAttacked)->position);
  1200. }
  1201. CBattleInterface::SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, LOCPLINT->curAction->stackNumber, LOCPLINT->curAction->actionType==7, i->killed()};
  1202. arg.push_back(to_put);
  1203. }
  1204. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  1205. {
  1206. battleInt->displayEffect(bsa.begin()->effect, -1);
  1207. }
  1208. battleInt->stacksAreAttacked(arg);
  1209. }
  1210. void CPlayerInterface::battleAttack(BattleAttack *ba)
  1211. {
  1212. tlog5 << "CPlayerInterface::battleAttack - locking...";
  1213. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1214. tlog5 << "done!\n";
  1215. assert(curAction);
  1216. if(ba->lucky()) //lucky hit
  1217. {
  1218. CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  1219. std::string hlp = CGI->generaltexth->allTexts[45];
  1220. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  1221. battleInt->console->addText(hlp);
  1222. battleInt->displayEffect(18,stack->position);
  1223. }
  1224. //TODO: bad luck?
  1225. if(ba->shot())
  1226. {
  1227. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  1228. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  1229. }
  1230. else
  1231. {
  1232. CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  1233. int shift = 0;
  1234. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  1235. {
  1236. if(attacker->attackerOwned)
  1237. shift = 1;
  1238. else
  1239. shift = -1;
  1240. }
  1241. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  1242. }
  1243. }
  1244. void CPlayerInterface::showComp(SComponent comp)
  1245. {
  1246. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1247. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  1248. adventureInt->infoBar.showComp(&comp,4000);
  1249. }
  1250. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  1251. {
  1252. std::vector<SComponent*> intComps;
  1253. for(int i=0;i<components.size();i++)
  1254. intComps.push_back(new SComponent(*components[i]));
  1255. showInfoDialog(text,intComps,soundID);
  1256. }
  1257. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  1258. {
  1259. {
  1260. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1261. while(showingDialog->data)
  1262. showingDialog->cond.wait(un);
  1263. }
  1264. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1265. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1266. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1267. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1268. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1269. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  1270. if(makingTurn && listInt.size())
  1271. {
  1272. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1273. showingDialog->set(true);
  1274. pushInt(temp);
  1275. }
  1276. else
  1277. {
  1278. dialogs.push_back(temp);
  1279. }
  1280. }
  1281. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  1282. {
  1283. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1284. LOCPLINT->showingDialog->setn(true);
  1285. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1286. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1287. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1288. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,DelComps);
  1289. temp->delComps = DelComps;
  1290. for(int i=0;i<onYes.funcs.size();i++)
  1291. temp->buttons[0]->callback += onYes.funcs[i];
  1292. for(int i=0;i<onNo.funcs.size();i++)
  1293. temp->buttons[1]->callback += onNo.funcs[i];
  1294. LOCPLINT->pushInt(temp);
  1295. }
  1296. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  1297. {
  1298. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1299. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1300. if(!selection && cancel) //simple yes/no dialog
  1301. {
  1302. std::vector<SComponent*> intComps;
  1303. for(int i=0;i<components.size();i++)
  1304. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  1305. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  1306. }
  1307. else if(selection)
  1308. {
  1309. std::vector<CSelectableComponent*> intComps;
  1310. for(int i=0;i<components.size();i++)
  1311. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  1312. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1313. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1314. if(cancel)
  1315. {
  1316. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1317. }
  1318. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  1319. pushInt(temp);
  1320. intComps[0]->clickLeft(true);
  1321. }
  1322. }
  1323. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  1324. {
  1325. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1326. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1327. adventureInt->minimap.showTile(*i);
  1328. }
  1329. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  1330. {
  1331. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1332. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1333. adventureInt->minimap.hideTile(*i);
  1334. }
  1335. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  1336. {
  1337. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1338. adventureInt->heroWindow->setHero(hero);
  1339. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  1340. pushInt(adventureInt->heroWindow);
  1341. }
  1342. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  1343. {
  1344. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1345. if(adventureInt->heroWindow->curHero) //hero window is opened
  1346. {
  1347. adventureInt->heroWindow->deactivate();
  1348. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  1349. adventureInt->heroWindow->activate();
  1350. return;
  1351. }
  1352. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(listInt.front());
  1353. if(cew) //exchange window is open
  1354. {
  1355. cew->deactivate();
  1356. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  1357. {
  1358. if(cew->heroInst[g] == hero)
  1359. {
  1360. cew->artifs[g]->setHero(hero);
  1361. }
  1362. }
  1363. cew->prepareBackground();
  1364. cew->activate();
  1365. }
  1366. }
  1367. void CPlayerInterface::updateWater()
  1368. {
  1369. }
  1370. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  1371. {
  1372. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1373. if(castleInt)
  1374. {
  1375. CFortScreen *fs = dynamic_cast<CFortScreen*>(listInt.front());
  1376. if(fs)
  1377. fs->draw(castleInt,false);
  1378. }
  1379. }
  1380. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  1381. {
  1382. if(bonus.type == HeroBonus::NONE) return;
  1383. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1384. redrawHeroWin(hero);
  1385. }
  1386. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1387. {
  1388. h & playerID & serialID;
  1389. h & heroMoveSpeed & mapScrollingSpeed;
  1390. h & CBattleInterface::settings;
  1391. }
  1392. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1393. {
  1394. serializeTempl(h,version);
  1395. }
  1396. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1397. {
  1398. serializeTempl(h,version);
  1399. }
  1400. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  1401. {
  1402. if(!vstd::contains(graphics->heroWins,hero->subID))
  1403. {
  1404. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  1405. return;
  1406. }
  1407. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  1408. graphics->heroWins[hero->subID] = infoWin(hero);
  1409. if (adventureInt->selection == hero)
  1410. adventureInt->infoBar.draw(screen);
  1411. }
  1412. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CPath path )
  1413. {
  1414. if (!h)
  1415. return false; //can't find hero
  1416. bool result = false;
  1417. path.convert(0);
  1418. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1419. stillMoveHero.data = CONTINUE_MOVE;
  1420. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1421. enum TerrainTile::EterrainType newTerrain;
  1422. int sh = -1;
  1423. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  1424. {
  1425. // Start a new sound for the hero movement or let the existing one carry on.
  1426. #if 0
  1427. // TODO
  1428. if (hero is flying && sh == -1)
  1429. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  1430. }
  1431. else if (hero is in a boat && sh = -1) {
  1432. sh = CGI->soundh->playSound(soundBase::sound_todo, -1);
  1433. } else
  1434. #endif
  1435. {
  1436. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1437. if (newTerrain != currentTerrain) {
  1438. CGI->soundh->stopSound(sh);
  1439. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  1440. currentTerrain = newTerrain;
  1441. }
  1442. }
  1443. stillMoveHero.data = WAITING_MOVE;
  1444. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1445. cb->moveHero(h,endpos);
  1446. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1447. stillMoveHero.cond.wait(un);
  1448. }
  1449. CGI->soundh->stopSound(sh);
  1450. //stillMoveHero = false;
  1451. return result;
  1452. }
  1453. bool CPlayerInterface::shiftPressed() const
  1454. {
  1455. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1456. }
  1457. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd )
  1458. {
  1459. {
  1460. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1461. while(showingDialog->data)
  1462. showingDialog->cond.wait(un);
  1463. }
  1464. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1465. while(dialogs.size())
  1466. {
  1467. pim->unlock();
  1468. SDL_Delay(20);
  1469. pim->lock();
  1470. }
  1471. CGarrisonWindow *cgw = new CGarrisonWindow(up,down);
  1472. cgw->quit->callback += onEnd;
  1473. pushInt(cgw);
  1474. }
  1475. void CPlayerInterface::popInt( IShowActivable *top )
  1476. {
  1477. assert(listInt.front() == top);
  1478. top->deactivate();
  1479. listInt.pop_front();
  1480. objsToBlit -= top;
  1481. if(listInt.size())
  1482. listInt.front()->activate();
  1483. totalRedraw();
  1484. }
  1485. void CPlayerInterface::popIntTotally( IShowActivable *top )
  1486. {
  1487. assert(listInt.front() == top);
  1488. popInt(top);
  1489. delete top;
  1490. }
  1491. void CPlayerInterface::pushInt( IShowActivable *newInt )
  1492. {
  1493. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  1494. screenBuf = screen2;
  1495. if(listInt.size())
  1496. listInt.front()->deactivate();
  1497. listInt.push_front(newInt);
  1498. newInt->activate();
  1499. objsToBlit += newInt;
  1500. LOCPLINT->totalRedraw();
  1501. }
  1502. void CPlayerInterface::popInts( int howMany )
  1503. {
  1504. if(!howMany) return; //senseless but who knows...
  1505. assert(listInt.size() > howMany);
  1506. listInt.front()->deactivate();
  1507. for(int i=0; i < howMany; i++)
  1508. {
  1509. objsToBlit -= listInt.front();
  1510. delete listInt.front();
  1511. listInt.pop_front();
  1512. }
  1513. listInt.front()->activate();
  1514. totalRedraw();
  1515. }
  1516. IShowActivable * CPlayerInterface::topInt()
  1517. {
  1518. if(!listInt.size())
  1519. return NULL;
  1520. else
  1521. return listInt.front();
  1522. }
  1523. void CPlayerInterface::totalRedraw()
  1524. {
  1525. for(int i=0;i<objsToBlit.size();i++)
  1526. objsToBlit[i]->showAll(screen2);
  1527. blitAt(screen2,0,0,screen);
  1528. if(objsToBlit.size())
  1529. objsToBlit.back()->showAll(screen);
  1530. }
  1531. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1532. {
  1533. if(stillMoveHero.get() == DURING_MOVE)
  1534. stillMoveHero.setn(CONTINUE_MOVE);
  1535. }
  1536. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1537. {
  1538. pushInt(new CExchangeWindow(hero1, hero2));
  1539. }
  1540. void CPlayerInterface::recreateWanderingHeroes()
  1541. {
  1542. wanderingHeroes.clear();
  1543. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1544. for(size_t i = 0; i < heroes.size(); i++)
  1545. if(!heroes[i]->inTownGarrison)
  1546. wanderingHeroes.push_back(heroes[i]);
  1547. }
  1548. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1549. {
  1550. if(pos < 0 || pos >= wanderingHeroes.size())
  1551. return NULL;
  1552. return wanderingHeroes[pos];
  1553. }
  1554. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, int level)
  1555. {
  1556. std::vector<std::pair<int,int> > cres;
  1557. for(int i = 0; i < dwelling->creatures.size(); i++)
  1558. {
  1559. if(i == level || level < 0)
  1560. for(size_t j = 0; j < dwelling->creatures[i].second.size(); j++)
  1561. cres.push_back( std::make_pair(dwelling->creatures[i].second[j],dwelling->creatures[i].first));
  1562. }
  1563. CRecruitmentWindow *cr = new CRecruitmentWindow(cres, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1564. pushInt(cr);
  1565. }