CBattleInterfaceClasses.cpp 23 KB

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