CPlayerInterface.cpp 59 KB

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