CPlayerInterface.cpp 59 KB

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