CBattleInterfaceClasses.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. #include "StdInc.h"
  2. #include "CBattleInterfaceClasses.h"
  3. #include "../UIFramework/SDL_Extensions.h"
  4. #include "CBattleInterface.h"
  5. #include "../CGameInfo.h"
  6. #include "../CDefHandler.h"
  7. #include "../UIFramework/CCursorHandler.h"
  8. #include "../CPlayerInterface.h"
  9. #include "../../CCallback.h"
  10. #include "../CSpellWindow.h"
  11. #include "../Graphics.h"
  12. #include "../CConfigHandler.h"
  13. #include "../UIFramework/CGuiHandler.h"
  14. #include "../UIFramework/CIntObjectClasses.h"
  15. #include "../../lib/CGeneralTextHandler.h"
  16. #include "../../lib/NetPacks.h"
  17. #include "../../lib/CCreatureHandler.h"
  18. #include "../../lib/CObjectHandler.h"
  19. #include "../../lib/BattleState.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CVideoHandler.h"
  22. #include "../../lib/CTownHandler.h"
  23. #include "../CBitmapHandler.h"
  24. #include "../CCreatureWindow.h"
  25. CBattleConsole::~CBattleConsole()
  26. {
  27. texts.clear();
  28. }
  29. void CBattleConsole::show(SDL_Surface * to)
  30. {
  31. if(ingcAlter.size())
  32. {
  33. CSDL_Ext::printAtMiddleWB(ingcAlter, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, Colors::Cornsilk, to);
  34. }
  35. else if(alterTxt.size())
  36. {
  37. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, Colors::Cornsilk, to);
  38. }
  39. else if(texts.size())
  40. {
  41. if(texts.size()==1)
  42. {
  43. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, Colors::Cornsilk, to);
  44. }
  45. else
  46. {
  47. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, Colors::Cornsilk, to);
  48. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 27, FONT_SMALL, 80, Colors::Cornsilk, to);
  49. }
  50. }
  51. }
  52. bool CBattleConsole::addText(const std::string & text)
  53. {
  54. if(text.size()>70)
  55. return false; //text too long!
  56. int firstInToken = 0;
  57. for(size_t i = 0; i < text.size(); ++i) //tokenize
  58. {
  59. if(text[i] == 10)
  60. {
  61. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  62. firstInToken = i+1;
  63. }
  64. }
  65. texts.push_back( text.substr(firstInToken, text.size()) );
  66. lastShown = texts.size()-1;
  67. return true;
  68. }
  69. void CBattleConsole::alterText(const std::string &text)
  70. {
  71. //char buf[500];
  72. //sprintf(buf, text.c_str());
  73. //alterTxt = buf;
  74. alterTxt = text;
  75. }
  76. void CBattleConsole::eraseText(ui32 pos)
  77. {
  78. if(pos < texts.size())
  79. {
  80. texts.erase(texts.begin() + pos);
  81. if(lastShown == texts.size())
  82. --lastShown;
  83. }
  84. }
  85. void CBattleConsole::changeTextAt(const std::string & text, ui32 pos)
  86. {
  87. if(pos >= texts.size()) //no such pos
  88. return;
  89. texts[pos] = text;
  90. }
  91. void CBattleConsole::scrollUp(ui32 by)
  92. {
  93. if(lastShown > static_cast<int>(by))
  94. lastShown -= by;
  95. }
  96. void CBattleConsole::scrollDown(ui32 by)
  97. {
  98. if(lastShown + by < texts.size())
  99. lastShown += by;
  100. }
  101. void CBattleHero::show(SDL_Surface * to)
  102. {
  103. //animation of flag
  104. if(flip)
  105. {
  106. SDL_Rect temp_rect = genRect(
  107. flag->ourImages[flagAnim].bitmap->h,
  108. flag->ourImages[flagAnim].bitmap->w,
  109. pos.x + 61,
  110. pos.y + 39);
  111. CSDL_Ext::blit8bppAlphaTo24bpp(
  112. flag->ourImages[flagAnim].bitmap,
  113. NULL,
  114. screen,
  115. &temp_rect);
  116. }
  117. else
  118. {
  119. SDL_Rect temp_rect = genRect(
  120. flag->ourImages[flagAnim].bitmap->h,
  121. flag->ourImages[flagAnim].bitmap->w,
  122. pos.x + 72,
  123. pos.y + 39);
  124. CSDL_Ext::blit8bppAlphaTo24bpp(
  125. flag->ourImages[flagAnim].bitmap,
  126. NULL,
  127. screen,
  128. &temp_rect);
  129. }
  130. ++flagAnimCount;
  131. if(flagAnimCount%4==0)
  132. {
  133. ++flagAnim;
  134. flagAnim %= flag->ourImages.size();
  135. }
  136. //animation of hero
  137. int tick=-1;
  138. for(size_t i = 0; i < dh->ourImages.size(); ++i)
  139. {
  140. if(dh->ourImages[i].groupNumber==phase)
  141. ++tick;
  142. if(tick==image)
  143. {
  144. SDL_Rect posb = pos;
  145. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  146. if(phase != 4 || nextPhase != -1 || image < 4)
  147. {
  148. if(flagAnimCount%2==0)
  149. {
  150. ++image;
  151. }
  152. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  153. {
  154. image = 0;
  155. }
  156. }
  157. if(phase == 4 && nextPhase != -1 && image == 7)
  158. {
  159. phase = nextPhase;
  160. nextPhase = -1;
  161. image = 0;
  162. }
  163. break;
  164. }
  165. }
  166. }
  167. void CBattleHero::activate()
  168. {
  169. activateLClick();
  170. }
  171. void CBattleHero::deactivate()
  172. {
  173. deactivateLClick();
  174. }
  175. void CBattleHero::setPhase(int newPhase)
  176. {
  177. if(phase != 4)
  178. {
  179. phase = newPhase;
  180. image = 0;
  181. }
  182. else
  183. {
  184. nextPhase = newPhase;
  185. }
  186. }
  187. void CBattleHero::clickLeft(tribool down, bool previousState)
  188. {
  189. if(myOwner->spellDestSelectMode) //we are casting a spell
  190. return;
  191. if(!down && myHero != NULL && myOwner->myTurn && myOwner->curInt->cb->battleCanCastSpell()) //check conditions
  192. {
  193. for(int it=0; it<GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  194. {
  195. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  196. return;
  197. }
  198. CCS->curh->changeGraphic(0,0);
  199. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), myHero, myOwner->curInt);
  200. GH.pushInt(spellWindow);
  201. }
  202. }
  203. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, ui8 player, const CGHeroInstance * hero, const CBattleInterface * owner): flip(flipG), myHero(hero), myOwner(owner), phase(phaseG), nextPhase(-1), image(imageG), flagAnim(0), flagAnimCount(0)
  204. {
  205. dh = CDefHandler::giveDef( defName );
  206. for(size_t i = 0; i < dh->ourImages.size(); ++i) //transforming images
  207. {
  208. if(flip)
  209. {
  210. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  211. SDL_FreeSurface(dh->ourImages[i].bitmap);
  212. dh->ourImages[i].bitmap = hlp;
  213. }
  214. CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  215. }
  216. if(flip)
  217. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  218. else
  219. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  220. //coloring flag and adding transparency
  221. for(size_t i = 0; i < flag->ourImages.size(); ++i)
  222. {
  223. CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  224. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  225. }
  226. }
  227. CBattleHero::~CBattleHero()
  228. {
  229. delete dh;
  230. delete flag;
  231. }
  232. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  233. {
  234. OBJ_CONSTRUCTION_CAPTURING_ALL;
  235. pos = position;
  236. background = new CPicture("comopbck.bmp");
  237. background->colorize(owner->curInt->playerID);
  238. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[427].first)(3,CGI->generaltexth->zelp[427].first), CGI->generaltexth->zelp[427].second, false, "sysopchk.def", NULL, 25, 56, false);
  239. viewGrid->select(settings["battle"]["cellBorders"].Bool());
  240. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[428].first)(3,CGI->generaltexth->zelp[428].first), CGI->generaltexth->zelp[428].second, false, "sysopchk.def", NULL, 25, 89, false);
  241. movementShadow->select(settings["battle"]["stackRange"].Bool());
  242. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[429].first)(3,CGI->generaltexth->zelp[429].first), CGI->generaltexth->zelp[429].second, false, "sysopchk.def", NULL, 25, 122, false);
  243. mouseShadow->select(settings["battle"]["mouseShadow"].Bool());
  244. animSpeeds = new CHighlightableButtonsGroup(0);
  245. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def", 28, 225, 1);
  246. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def", 92, 225, 2);
  247. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",156, 225, 4);
  248. animSpeeds->select(owner->getAnimSpeed(), 1);
  249. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  250. setToDefault = new CAdventureMapButton (CGI->generaltexth->zelp[393], boost::bind(&CBattleOptionsWindow::bDefaultf,this), 246, 359, "codefaul.def");
  251. setToDefault->swappedImages = true;
  252. setToDefault->update();
  253. exit = new CAdventureMapButton (CGI->generaltexth->zelp[392], boost::bind(&CBattleOptionsWindow::bExitf,this), 357, 359, "soretrn.def",SDLK_RETURN);
  254. exit->swappedImages = true;
  255. exit->update();
  256. //creating labels
  257. labels.push_back(new CLabel(242, 32, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[392]));//window title
  258. labels.push_back(new CLabel(122, 214, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[393]));//animation speed
  259. labels.push_back(new CLabel(122, 293, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[394]));//music volume
  260. labels.push_back(new CLabel(122, 359, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[395]));//effects' volume
  261. labels.push_back(new CLabel(353, 66, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[396]));//auto - combat options
  262. labels.push_back(new CLabel(353, 265, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[397]));//creature info
  263. //auto - combat options
  264. labels.push_back(new CLabel(283, 86, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[398]));//creatures
  265. labels.push_back(new CLabel(283, 116, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[399]));//spells
  266. labels.push_back(new CLabel(283, 146, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[400]));//catapult
  267. labels.push_back(new CLabel(283, 176, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[151]));//ballista
  268. labels.push_back(new CLabel(283, 206, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[401]));//first aid tent
  269. //creature info
  270. labels.push_back(new CLabel(283, 285, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[402]));//all stats
  271. labels.push_back(new CLabel(283, 315, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[403]));//spells only
  272. //general options
  273. labels.push_back(new CLabel(61, 57, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[404]));
  274. labels.push_back(new CLabel(61, 90, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[405]));
  275. labels.push_back(new CLabel(61, 123, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[406]));
  276. labels.push_back(new CLabel(61, 156, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[407]));
  277. }
  278. void CBattleOptionsWindow::bDefaultf()
  279. {
  280. }
  281. void CBattleOptionsWindow::bExitf()
  282. {
  283. GH.popIntTotally(this);
  284. }
  285. CBattleResultWindow::CBattleResultWindow(const BattleResult &br, const SDL_Rect & pos, CBattleInterface * _owner)
  286. : owner(_owner)
  287. {
  288. this->pos = pos;
  289. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  290. graphics->blueToPlayersAdv(background, owner->curInt->playerID);
  291. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  292. SDL_FreeSurface(background);
  293. background = pom;
  294. exit = new CAdventureMapButton (std::string(), std::string(), boost::bind(&CBattleResultWindow::bExitf,this), 384 + pos.x, 505 + pos.y, "iok6432.def", SDLK_RETURN);
  295. exit->borderColor = Colors::MetallicGold;
  296. exit->borderEnabled = true;
  297. if(br.winner==0) //attacker won
  298. {
  299. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 59, 124, FONT_SMALL, Colors::Cornsilk, background);
  300. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 408, 124, FONT_SMALL, Colors::Cornsilk, background);
  301. }
  302. else //if(br.winner==1)
  303. {
  304. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 59, 124, FONT_SMALL, Colors::Cornsilk, background);
  305. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 412, 124, FONT_SMALL, Colors::Cornsilk, background);
  306. }
  307. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 232, 302, FONT_BIG, Colors::Jasmine, background);
  308. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 232, 332, FONT_BIG, Colors::Cornsilk, background);
  309. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 237, 428, FONT_BIG, Colors::Cornsilk, background);
  310. std::string attackerName, defenderName;
  311. if(owner->attackingHeroInstance) //a hero attacked
  312. {
  313. SDL_Rect temp_rect = genRect(64, 58, 21, 38);
  314. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &temp_rect);
  315. //setting attackerName
  316. attackerName = owner->attackingHeroInstance->name;
  317. }
  318. else //a monster attacked
  319. {
  320. int bestMonsterID = -1;
  321. ui32 bestPower = 0;
  322. for(TSlots::const_iterator it = owner->army1->Slots().begin(); it!=owner->army1->Slots().end(); ++it)
  323. {
  324. if(it->second->type->AIValue > bestPower)
  325. {
  326. bestPower = it->second->type->AIValue;
  327. bestMonsterID = it->second->type->idNumber;
  328. }
  329. }
  330. SDL_Rect temp_rect = genRect(64, 58, 21, 38);
  331. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &temp_rect);
  332. //setting attackerName
  333. attackerName = CGI->creh->creatures[bestMonsterID]->namePl;
  334. }
  335. if(owner->defendingHeroInstance) //a hero defended
  336. {
  337. SDL_Rect temp_rect = genRect(64, 58, 392, 38);
  338. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &temp_rect);
  339. //setting defenderName
  340. defenderName = owner->defendingHeroInstance->name;
  341. }
  342. else //a monster defended
  343. {
  344. int bestMonsterID = -1;
  345. ui32 bestPower = 0;
  346. for(TSlots::const_iterator it = owner->army2->Slots().begin(); it!=owner->army2->Slots().end(); ++it)
  347. {
  348. if( it->second->type->AIValue > bestPower)
  349. {
  350. bestPower = it->second->type->AIValue;
  351. bestMonsterID = it->second->type->idNumber;
  352. }
  353. }
  354. SDL_Rect temp_rect = genRect(64, 58, 392, 38);
  355. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &temp_rect);
  356. //setting defenderName
  357. defenderName = CGI->creh->creatures[bestMonsterID]->namePl;
  358. }
  359. //printing attacker and defender's names
  360. CSDL_Ext::printAt(attackerName, 89, 37, FONT_SMALL, Colors::Cornsilk, background);
  361. CSDL_Ext::printTo(defenderName, 381, 53, FONT_SMALL, Colors::Cornsilk, background);
  362. //printing casualities
  363. for(int step = 0; step < 2; ++step)
  364. {
  365. if(br.casualties[step].size()==0)
  366. {
  367. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, FONT_SMALL, Colors::Cornsilk, background);
  368. }
  369. else
  370. {
  371. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  372. int yPos = 344 + step*97;
  373. for(std::map<ui32,si32>::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  374. {
  375. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  376. std::ostringstream amount;
  377. amount<<it->second;
  378. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, FONT_SMALL, Colors::Cornsilk, background);
  379. xPos += 42;
  380. }
  381. }
  382. }
  383. //printing result description
  384. bool weAreAttacker = (owner->curInt->playerID == owner->attackingHeroInstance->tempOwner);
  385. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  386. {
  387. int text=-1;
  388. switch(br.result)
  389. {
  390. case 0: text = 304; break;
  391. case 1: text = 303; break;
  392. case 2: text = 302; break;
  393. }
  394. CCS->musich->playMusic(musicBase::winBattle);
  395. CCS->videoh->open(VIDEO_WIN);
  396. std::string str = CGI->generaltexth->allTexts[text];
  397. const CGHeroInstance * ourHero = weAreAttacker? owner->attackingHeroInstance : owner->defendingHeroInstance;
  398. if (ourHero)
  399. {
  400. str += CGI->generaltexth->allTexts[305];
  401. boost::algorithm::replace_first(str,"%s",ourHero->name);
  402. boost::algorithm::replace_first(str,"%d",boost::lexical_cast<std::string>(br.exp[weAreAttacker?0:1]));
  403. }
  404. CSDL_Ext::printAtMiddleWB(str, 235, 235, FONT_SMALL, 55, Colors::Cornsilk, background);
  405. }
  406. else // we lose
  407. {
  408. switch(br.result)
  409. {
  410. case 0: //normal victory
  411. {
  412. CCS->musich->playMusic(musicBase::loseCombat);
  413. CCS->videoh->open(VIDEO_LOSE_BATTLE_START);
  414. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, FONT_SMALL, Colors::Cornsilk, background);
  415. break;
  416. }
  417. case 1: //flee
  418. {
  419. CCS->musich->playMusic(musicBase::retreatBattle);
  420. CCS->videoh->open(VIDEO_RETREAT_START);
  421. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, FONT_SMALL, Colors::Cornsilk, background);
  422. break;
  423. }
  424. case 2: //surrender
  425. {
  426. CCS->musich->playMusic(musicBase::surrenderBattle);
  427. CCS->videoh->open(VIDEO_SURRENDER);
  428. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 220, FONT_SMALL, Colors::Cornsilk, background);
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. CBattleResultWindow::~CBattleResultWindow()
  435. {
  436. SDL_FreeSurface(background);
  437. }
  438. void CBattleResultWindow::activate()
  439. {
  440. owner->curInt->showingDialog->set(true);
  441. exit->activate();
  442. }
  443. void CBattleResultWindow::deactivate()
  444. {
  445. exit->deactivate();
  446. }
  447. void CBattleResultWindow::show(SDL_Surface * to)
  448. {
  449. //evaluating to
  450. if(!to)
  451. to = screen;
  452. CCS->videoh->update(107, 70, background, false, true);
  453. SDL_BlitSurface(background, NULL, to, &pos);
  454. exit->showAll(to);
  455. }
  456. void CBattleResultWindow::bExitf()
  457. {
  458. if(LOCPLINT->cb->getStartInfo()->mode == StartInfo::DUEL)
  459. {
  460. std::exit(0);
  461. }
  462. CPlayerInterface * intTmp = owner->curInt;
  463. GH.popInts(2); //first - we; second - battle interface
  464. intTmp->showingDialog->setn(false);
  465. CCS->videoh->close();
  466. }
  467. Point CClickableHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
  468. {
  469. Point ret(-500, -500); //returned value
  470. if(stack && stack->position < 0) //creatures in turrets
  471. {
  472. switch(stack->position)
  473. {
  474. case -2: //keep
  475. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][17];
  476. break;
  477. case -3: //lower turret
  478. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][18];
  479. break;
  480. case -4: //upper turret
  481. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][19];
  482. break;
  483. }
  484. }
  485. else
  486. {
  487. ret.y = -139 + 42 * (hexNum/GameConstants::BFIELD_WIDTH); //counting y
  488. //counting x
  489. if(attacker)
  490. {
  491. ret.x = -160 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  492. }
  493. else
  494. {
  495. ret.x = -219 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  496. }
  497. //shifting position for double - hex creatures
  498. if(stack && stack->doubleWide())
  499. {
  500. if(attacker)
  501. {
  502. ret.x -= 44;
  503. }
  504. else
  505. {
  506. ret.x += 45;
  507. }
  508. }
  509. }
  510. //returning
  511. return ret +CPlayerInterface::battleInt->pos;
  512. }
  513. void CClickableHex::activate()
  514. {
  515. activateHover();
  516. activateMouseMove();
  517. activateLClick();
  518. activateRClick();
  519. }
  520. void CClickableHex::deactivate()
  521. {
  522. deactivateHover();
  523. deactivateMouseMove();
  524. deactivateLClick();
  525. deactivateRClick();
  526. }
  527. void CClickableHex::hover(bool on)
  528. {
  529. hovered = on;
  530. //Hoverable::hover(on);
  531. if(!on && setAlterText)
  532. {
  533. myInterface->console->alterTxt = std::string();
  534. setAlterText = false;
  535. }
  536. }
  537. CClickableHex::CClickableHex() : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(NULL)
  538. {
  539. }
  540. void CClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  541. {
  542. if(myInterface->cellShade)
  543. {
  544. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  545. {
  546. strictHovered = false;
  547. }
  548. else //hovered pixel is inside hex
  549. {
  550. strictHovered = true;
  551. }
  552. }
  553. if(hovered && strictHovered) //print attacked creature to console
  554. {
  555. const CStack * attackedStack = myInterface->curInt->cb->battleGetStackByPos(myNumber);
  556. if(myInterface->console->alterTxt.size() == 0 &&attackedStack != NULL &&
  557. attackedStack->owner != myInterface->curInt->playerID &&
  558. attackedStack->alive())
  559. {
  560. char tabh[160];
  561. const std::string & attackedName = attackedStack->count == 1 ? attackedStack->getCreature()->nameSing : attackedStack->getCreature()->namePl;
  562. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  563. myInterface->console->alterTxt = std::string(tabh);
  564. setAlterText = true;
  565. }
  566. }
  567. else if(setAlterText)
  568. {
  569. myInterface->console->alterTxt = std::string();
  570. setAlterText = false;
  571. }
  572. }
  573. void CClickableHex::clickLeft(tribool down, bool previousState)
  574. {
  575. if(!down && hovered && strictHovered) //we've been really clicked!
  576. {
  577. myInterface->hexLclicked(myNumber);
  578. }
  579. }
  580. void CClickableHex::clickRight(tribool down, bool previousState)
  581. {
  582. const CStack * myst = myInterface->curInt->cb->battleGetStackByPos(myNumber); //stack info
  583. if(hovered && strictHovered && myst!=NULL)
  584. {
  585. if(!myst->alive()) return;
  586. if(down)
  587. {
  588. GH.pushInt(createCreWindow(myst));
  589. }
  590. }
  591. }
  592. void CStackQueue::update()
  593. {
  594. stacksSorted.clear();
  595. owner->curInt->cb->getStackQueue(stacksSorted, QUEUE_SIZE);
  596. for (int i = 0; i < QUEUE_SIZE ; i++)
  597. {
  598. stackBoxes[i]->setStack(stacksSorted[i]);
  599. }
  600. }
  601. CStackQueue::CStackQueue(bool Embedded, CBattleInterface * _owner)
  602. :embedded(Embedded), owner(_owner)
  603. {
  604. OBJ_CONSTRUCTION_CAPTURING_ALL;
  605. if(embedded)
  606. {
  607. box = NULL;
  608. bg = NULL;
  609. pos.w = QUEUE_SIZE * 37;
  610. pos.h = 32; //height of small creature img
  611. pos.x = screen->w/2 - pos.w/2;
  612. pos.y = (screen->h - 600)/2 + 10;
  613. }
  614. else
  615. {
  616. box = BitmapHandler::loadBitmap("CHRROP.pcx");
  617. bg = BitmapHandler::loadBitmap("DIBOXPI.pcx");
  618. pos.w = 600;
  619. pos.h = bg->h;
  620. }
  621. stackBoxes.resize(QUEUE_SIZE);
  622. for (int i = 0; i < QUEUE_SIZE; i++)
  623. {
  624. stackBoxes[i] = new StackBox(box);
  625. stackBoxes[i]->pos.x += 6 + (embedded ? 37 : 79)*i;
  626. }
  627. }
  628. CStackQueue::~CStackQueue()
  629. {
  630. SDL_FreeSurface(box);
  631. }
  632. void CStackQueue::showAll(SDL_Surface * to)
  633. {
  634. blitBg(to);
  635. CIntObject::showAll(to);
  636. }
  637. void CStackQueue::blitBg( SDL_Surface * to )
  638. {
  639. if(bg)
  640. {
  641. for (int w = 0; w < pos.w; w += bg->w)
  642. {
  643. blitAtLoc(bg, w, 0, to);
  644. }
  645. }
  646. }
  647. void CStackQueue::StackBox::showAll(SDL_Surface * to)
  648. {
  649. assert(my);
  650. if(bg)
  651. {
  652. graphics->blueToPlayersAdv(bg, my->owner);
  653. //SDL_UpdateRect(bg, 0, 0, 0, 0);
  654. SDL_Rect temp_rect = genRect(bg->h, bg->w, pos.x, pos.y);
  655. CSDL_Ext::blit8bppAlphaTo24bpp(bg, NULL, to, &temp_rect);
  656. //blitAt(bg, pos, to);
  657. blitAt(graphics->bigImgs[my->getCreature()->idNumber], pos.x +9, pos.y + 1, to);
  658. printAtMiddleLoc(makeNumberShort(my->count), pos.w/2, pos.h - 12, FONT_MEDIUM, Colors::Cornsilk, to);
  659. }
  660. else
  661. {
  662. blitAt(graphics->smallImgs[-2], pos, to);
  663. blitAt(graphics->smallImgs[my->getCreature()->idNumber], pos, to);
  664. const SDL_Color &ownerColor = (my->owner == 255 ? *graphics->neutralColor : graphics->playerColors[my->owner]);
  665. CSDL_Ext::drawBorder(to, pos, int3(ownerColor.r, ownerColor.g, ownerColor.b));
  666. printAtMiddleLoc(makeNumberShort(my->count), pos.w/2, pos.h - 8, FONT_TINY, Colors::Cornsilk, to);
  667. }
  668. }
  669. void CStackQueue::StackBox::setStack( const CStack *nStack )
  670. {
  671. my = nStack;
  672. }
  673. CStackQueue::StackBox::StackBox(SDL_Surface *BG)
  674. :my(NULL), bg(BG)
  675. {
  676. if(bg)
  677. {
  678. pos.w = bg->w;
  679. pos.h = bg->h;
  680. }
  681. else
  682. {
  683. pos.w = pos.h = 32;
  684. }
  685. pos.y += 2;
  686. }
  687. CStackQueue::StackBox::~StackBox()
  688. {
  689. }
  690. void CStackQueue::StackBox::hover( bool on )
  691. {
  692. }