CPlayerInterface.cpp 59 KB

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