CPlayerInterface.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. for(std::set< std::pair<ui8, ui8> >::const_iterator it = ca.attackedParts.begin(); it != ca.attackedParts.end(); ++it)
  882. {
  883. SDL_FreeSurface(battleInt->siegeH->walls[it->first + 2]);
  884. battleInt->siegeH->walls[it->first + 2] = BitmapHandler::loadBitmap(
  885. battleInt->siegeH->getSiegeName(it->first + 2, cb->battleGetWallState(it->first)) );
  886. }
  887. }
  888. void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
  889. {
  890. for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
  891. {
  892. battleInt->stackRemoved(*it);
  893. }
  894. }
  895. 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
  896. {
  897. boost::unique_lock<boost::recursive_mutex> un(*pim);
  898. battleInt->newRound(round);
  899. }
  900. void CPlayerInterface::actionStarted(const BattleAction* action)
  901. {
  902. boost::unique_lock<boost::recursive_mutex> un(*pim);
  903. curAction = new BattleAction(*action);
  904. battleInt->startAction(action);
  905. }
  906. void CPlayerInterface::actionFinished(const BattleAction* action)
  907. {
  908. boost::unique_lock<boost::recursive_mutex> un(*pim);
  909. delete curAction;
  910. curAction = NULL;
  911. battleInt->endAction(action);
  912. }
  913. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  914. {
  915. CBattleInterface *b = battleInt;
  916. {
  917. boost::unique_lock<boost::recursive_mutex> un(*pim);
  918. const CStack *stack = cb->battleGetStackByID(stackID);
  919. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  920. {
  921. std::string hlp = CGI->generaltexth->allTexts[33];
  922. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  923. battleInt->displayEffect(20,stack->position);
  924. battleInt->console->addText(hlp);
  925. }
  926. b->stackActivated(stackID);
  927. }
  928. //wait till BattleInterface sets its command
  929. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  930. while(!b->givenCommand->data)
  931. b->givenCommand->cond.wait(lock);
  932. //tidy up
  933. BattleAction ret = *(b->givenCommand->data);
  934. delete b->givenCommand->data;
  935. b->givenCommand->data = NULL;
  936. //return command
  937. return ret;
  938. }
  939. void CPlayerInterface::battleEnd(BattleResult *br)
  940. {
  941. boost::unique_lock<boost::recursive_mutex> un(*pim);
  942. battleInt->battleFinished(*br);
  943. }
  944. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  945. {
  946. boost::unique_lock<boost::recursive_mutex> un(*pim);
  947. battleInt->stackMoved(ID, dest, end, distance);
  948. }
  949. void CPlayerInterface::battleSpellCast(SpellCast *sc)
  950. {
  951. boost::unique_lock<boost::recursive_mutex> un(*pim);
  952. battleInt->spellCast(sc);
  953. }
  954. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  955. {
  956. boost::unique_lock<boost::recursive_mutex> un(*pim);
  957. battleInt->battleStacksEffectsSet(sse);
  958. }
  959. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  960. {
  961. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  962. boost::unique_lock<boost::recursive_mutex> un(*pim);
  963. tlog5 << "done!\n";
  964. std::vector<SStackAttackedInfo> arg;
  965. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  966. {
  967. if(i->isEffect() && i->effect != 12) //and not armageddon
  968. {
  969. const CStack *stack = cb->battleGetStackByID(i->stackAttacked, false);
  970. if (stack != NULL)
  971. battleInt->displayEffect(i->effect, stack->position);
  972. }
  973. SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, i->attackerID, LOCPLINT->curAction->actionType==7, i->killed()};
  974. arg.push_back(to_put);
  975. }
  976. if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
  977. {
  978. battleInt->displayEffect(bsa.begin()->effect, -1);
  979. }
  980. battleInt->stacksAreAttacked(arg);
  981. }
  982. void CPlayerInterface::battleAttack(BattleAttack *ba)
  983. {
  984. tlog5 << "CPlayerInterface::battleAttack - locking...";
  985. boost::unique_lock<boost::recursive_mutex> un(*pim);
  986. tlog5 << "done!\n";
  987. assert(curAction);
  988. if(ba->lucky()) //lucky hit
  989. {
  990. const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  991. std::string hlp = CGI->generaltexth->allTexts[45];
  992. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  993. battleInt->console->addText(hlp);
  994. battleInt->displayEffect(18,stack->position);
  995. }
  996. //TODO: bad luck?
  997. if(ba->shot())
  998. {
  999. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  1000. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  1001. }
  1002. else
  1003. {
  1004. const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  1005. int shift = 0;
  1006. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  1007. {
  1008. if( BattleInfo::mutualPosition(curAction->destinationTile + 1, attacker->position) >= 0 )
  1009. shift = 1;
  1010. else
  1011. shift = -1;
  1012. }
  1013. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  1014. }
  1015. }
  1016. void CPlayerInterface::showComp(SComponent comp)
  1017. {
  1018. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1019. CGI->soundh->playSoundFromSet(CGI->soundh->pickupSounds);
  1020. adventureInt->infoBar.showComp(&comp,4000);
  1021. }
  1022. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  1023. {
  1024. std::vector<SComponent*> intComps;
  1025. for(int i=0;i<components.size();i++)
  1026. intComps.push_back(new SComponent(*components[i]));
  1027. showInfoDialog(text,intComps,soundID);
  1028. }
  1029. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, int soundID)
  1030. {
  1031. waitWhileDialog();
  1032. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1033. if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
  1034. stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
  1035. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1036. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1037. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,false);
  1038. if(makingTurn && GH.listInt.size())
  1039. {
  1040. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1041. showingDialog->set(true);
  1042. GH.pushInt(temp);
  1043. }
  1044. else
  1045. {
  1046. dialogs.push_back(temp);
  1047. }
  1048. }
  1049. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  1050. {
  1051. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1052. LOCPLINT->showingDialog->setn(true);
  1053. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1054. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1055. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1056. CInfoWindow * temp = new CInfoWindow(text,playerID,0,components,pom,DelComps);
  1057. temp->delComps = DelComps;
  1058. for(int i=0;i<onYes.funcs.size();i++)
  1059. temp->buttons[0]->callback += onYes.funcs[i];
  1060. for(int i=0;i<onNo.funcs.size();i++)
  1061. temp->buttons[1]->callback += onNo.funcs[i];
  1062. GH.pushInt(temp);
  1063. }
  1064. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
  1065. {
  1066. waitWhileDialog();
  1067. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1068. CGI->soundh->playSound(static_cast<soundBase::soundID>(soundID));
  1069. if(!selection && cancel) //simple yes/no dialog
  1070. {
  1071. std::vector<SComponent*> intComps;
  1072. for(int i=0;i<components.size();i++)
  1073. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  1074. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  1075. }
  1076. else if(selection)
  1077. {
  1078. std::vector<CSelectableComponent*> intComps;
  1079. for(int i=0;i<components.size();i++)
  1080. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  1081. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1082. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1083. if(cancel)
  1084. {
  1085. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1086. }
  1087. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  1088. GH.pushInt(temp);
  1089. intComps[0]->clickLeft(true, false);
  1090. }
  1091. }
  1092. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  1093. {
  1094. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1095. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1096. adventureInt->minimap.showTile(*i);
  1097. }
  1098. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  1099. {
  1100. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1101. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  1102. adventureInt->minimap.hideTile(*i);
  1103. }
  1104. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  1105. {
  1106. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1107. adventureInt->heroWindow->setHero(hero);
  1108. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  1109. GH.pushInt(adventureInt->heroWindow);
  1110. }
  1111. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  1112. {
  1113. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1114. if(adventureInt->heroWindow->curHero) //hero window is opened
  1115. {
  1116. adventureInt->heroWindow->deactivate();
  1117. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  1118. adventureInt->heroWindow->activate();
  1119. return;
  1120. }
  1121. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  1122. if(cew) //exchange window is open
  1123. {
  1124. cew->deactivate();
  1125. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  1126. {
  1127. if(cew->heroInst[g] == hero)
  1128. {
  1129. cew->artifs[g]->setHero(hero);
  1130. }
  1131. }
  1132. cew->prepareBackground();
  1133. cew->activate();
  1134. }
  1135. }
  1136. void CPlayerInterface::updateWater()
  1137. {
  1138. }
  1139. void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
  1140. {
  1141. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1142. if(castleInt && town->ID == TOWNI_TYPE)
  1143. {
  1144. CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
  1145. if(fs)
  1146. fs->draw(castleInt,false);
  1147. }
  1148. else if(GH.listInt.size() && (town->ID == 17 || town->ID == 20)) //external dwelling
  1149. {
  1150. CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
  1151. if(crw)
  1152. crw->initCres();
  1153. }
  1154. }
  1155. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  1156. {
  1157. if(bonus.type == HeroBonus::NONE) return;
  1158. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1159. redrawHeroWin(hero);
  1160. }
  1161. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  1162. {
  1163. h & playerID & serialID;
  1164. h & sysOpts;
  1165. }
  1166. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  1167. {
  1168. serializeTempl(h,version);
  1169. }
  1170. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  1171. {
  1172. serializeTempl(h,version);
  1173. sysOpts.apply();
  1174. }
  1175. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  1176. {
  1177. if(!vstd::contains(graphics->heroWins,hero->subID))
  1178. {
  1179. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  1180. return;
  1181. }
  1182. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  1183. graphics->heroWins[hero->subID] = infoWin(hero);
  1184. if (adventureInt->selection == hero)
  1185. adventureInt->infoBar.draw(screen);
  1186. }
  1187. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
  1188. {
  1189. if (!h)
  1190. return false; //can't find hero
  1191. bool result = false;
  1192. path.convert(0);
  1193. boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
  1194. stillMoveHero.data = CONTINUE_MOVE;
  1195. enum TerrainTile::EterrainType currentTerrain = TerrainTile::border; // not init yet
  1196. enum TerrainTile::EterrainType newTerrain;
  1197. int sh = -1;
  1198. for(int i=path.nodes.size()-1; i>0 && stillMoveHero.data == CONTINUE_MOVE; i--)
  1199. {
  1200. //stop sending move requests if hero exhausted all his move points
  1201. if(!h->movement)
  1202. {
  1203. stillMoveHero.data = STOP_MOVE;
  1204. break;
  1205. }
  1206. // Start a new sound for the hero movement or let the existing one carry on.
  1207. #if 0
  1208. // TODO
  1209. if (hero is flying && sh == -1)
  1210. sh = CGI->soundh->playSound(soundBase::horseFlying, -1);
  1211. #endif
  1212. {
  1213. newTerrain = cb->getTileInfo(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->tertype;
  1214. if (newTerrain != currentTerrain) {
  1215. CGI->soundh->stopSound(sh);
  1216. sh = CGI->soundh->playSound(CGI->soundh->horseSounds[newTerrain], -1);
  1217. currentTerrain = newTerrain;
  1218. }
  1219. }
  1220. stillMoveHero.data = WAITING_MOVE;
  1221. int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
  1222. cb->moveHero(h,endpos);
  1223. while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
  1224. stillMoveHero.cond.wait(un);
  1225. }
  1226. CGI->soundh->stopSound(sh);
  1227. //stillMoveHero = false;
  1228. cb->recalculatePaths();
  1229. return result;
  1230. }
  1231. bool CPlayerInterface::shiftPressed() const
  1232. {
  1233. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  1234. }
  1235. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  1236. {
  1237. {
  1238. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1239. while(showingDialog->data)
  1240. showingDialog->cond.wait(un);
  1241. }
  1242. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1243. while(dialogs.size())
  1244. {
  1245. pim->unlock();
  1246. SDL_Delay(20);
  1247. pim->lock();
  1248. }
  1249. CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
  1250. cgw->quit->callback += onEnd;
  1251. GH.pushInt(cgw);
  1252. }
  1253. void CPlayerInterface::requestRealized( PackageApplied *pa )
  1254. {
  1255. if(stillMoveHero.get() == DURING_MOVE)
  1256. stillMoveHero.setn(CONTINUE_MOVE);
  1257. }
  1258. void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
  1259. {
  1260. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1261. GH.pushInt(new CExchangeWindow(hero2, hero1));
  1262. }
  1263. void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
  1264. {
  1265. //redraw minimap if owner changed
  1266. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1267. if(sop->what == 1)
  1268. {
  1269. const CGObjectInstance * obj = cb->getObjectInfo(sop->id);
  1270. std::set<int3> pos = obj->getBlockedPos();
  1271. for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
  1272. {
  1273. if(cb->isVisible(*it))
  1274. adventureInt->minimap.showTile(*it);
  1275. }
  1276. if(obj->ID == TOWNI_TYPE)
  1277. adventureInt->townList.genList();
  1278. }
  1279. }
  1280. void CPlayerInterface::recreateWanderingHeroes()
  1281. {
  1282. wanderingHeroes.clear();
  1283. std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo();
  1284. for(size_t i = 0; i < heroes.size(); i++)
  1285. if(!heroes[i]->inTownGarrison)
  1286. wanderingHeroes.push_back(heroes[i]);
  1287. }
  1288. const CGHeroInstance * CPlayerInterface::getWHero( int pos )
  1289. {
  1290. if(pos < 0 || pos >= wanderingHeroes.size())
  1291. return NULL;
  1292. return wanderingHeroes[pos];
  1293. }
  1294. void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  1295. {
  1296. waitWhileDialog();
  1297. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1298. CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2));
  1299. GH.pushInt(cr);
  1300. }
  1301. void CPlayerInterface::waitWhileDialog()
  1302. {
  1303. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1304. while(showingDialog->data)
  1305. showingDialog->cond.wait(un);
  1306. }
  1307. void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
  1308. {
  1309. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1310. int state = obj->state();
  1311. std::vector<si32> cost;
  1312. obj->getBoatCost(cost);
  1313. CShipyardWindow *csw = new CShipyardWindow(cost, state, boost::bind(&CCallback::buildBoat, cb, obj));
  1314. GH.pushInt(csw);
  1315. }
  1316. void CPlayerInterface::newObject( const CGObjectInstance * obj )
  1317. {
  1318. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1319. CGI->mh->printObject(obj);
  1320. //we might have built a boat in shipyard in opened town screen
  1321. if(obj->ID == 8
  1322. && LOCPLINT->castleInt
  1323. && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
  1324. {
  1325. CGI->soundh->playSound(soundBase::newBuilding);
  1326. LOCPLINT->castleInt->recreateBuildings();
  1327. }
  1328. }
  1329. void CPlayerInterface::centerView (int3 pos, int focusTime)
  1330. {
  1331. LOCPLINT->adventureInt->centerOn (pos);
  1332. if(focusTime)
  1333. {
  1334. bool activeAdv = (GH.topInt() == adventureInt && adventureInt->active);
  1335. if(activeAdv)
  1336. adventureInt->deactivate();
  1337. SDL_Delay(focusTime);
  1338. if(activeAdv)
  1339. adventureInt->activate();
  1340. }
  1341. }
  1342. void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
  1343. {
  1344. if(obj->ID == HEROI_TYPE && obj->tempOwner == playerID)
  1345. {
  1346. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
  1347. heroKilled(h);
  1348. }
  1349. }
  1350. void SystemOptions::setMusicVolume( int newVolume )
  1351. {
  1352. musicVolume = newVolume;
  1353. CGI->musich->setVolume(newVolume);
  1354. settingsChanged();
  1355. }
  1356. void SystemOptions::setSoundVolume( int newVolume )
  1357. {
  1358. soundVolume = newVolume;
  1359. CGI->soundh->setVolume(newVolume);
  1360. settingsChanged();
  1361. }
  1362. void SystemOptions::setHeroMoveSpeed( int newSpeed )
  1363. {
  1364. heroMoveSpeed = newSpeed;
  1365. settingsChanged();
  1366. }
  1367. void SystemOptions::setMapScrollingSpeed( int newSpeed )
  1368. {
  1369. mapScrollingSpeed = newSpeed;
  1370. settingsChanged();
  1371. }
  1372. void SystemOptions::settingsChanged()
  1373. {
  1374. CSaveFile settings("config" PATHSEPARATOR "sysopts.bin");
  1375. if(settings.sfile)
  1376. settings << *this;
  1377. else
  1378. tlog1 << "Cannot save settings to config" PATHSEPARATOR "sysopts.bin!\n";
  1379. }
  1380. void SystemOptions::apply()
  1381. {
  1382. if(CGI->musich->getVolume() != musicVolume)
  1383. CGI->musich->setVolume(musicVolume);
  1384. if(CGI->soundh->getVolume() != soundVolume)
  1385. CGI->soundh->setVolume(soundVolume);
  1386. settingsChanged();
  1387. }
  1388. SystemOptions::SystemOptions()
  1389. {
  1390. heroMoveSpeed = 2;
  1391. mapScrollingSpeed = 2;
  1392. musicVolume = 88;
  1393. soundVolume = 88;
  1394. printCellBorders = true;
  1395. printStackRange = true;
  1396. animSpeed = 2;
  1397. printMouseShadow = true;
  1398. showQueue = true;
  1399. }