CPlayerInterface.cpp 58 KB

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