CPlayerInterface.cpp 58 KB

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