BattleResultWindow.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * BattleResultWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleResultWindow.h"
  12. #include "BattleWindow.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../media/IMusicPlayer.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../widgets/VideoWidget.h"
  22. #include "../../lib/CStack.h"
  23. #include "../../lib/ConditionalWait.h"
  24. #include "../../lib/GameLibrary.h"
  25. #include "../../lib/StartInfo.h"
  26. #include "../../lib/battle/CPlayerBattleCallback.h"
  27. #include "../../lib/callback/CCallback.h"
  28. #include "../../lib/gameState/InfoAboutArmy.h"
  29. #include "../../lib/mapObjects/CGHeroInstance.h"
  30. #include "../../lib/networkPacks/PacksForClientBattle.h"
  31. #include "../../lib/texts/CGeneralTextHandler.h"
  32. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay)
  33. : owner(_owner)
  34. {
  35. OBJECT_CONSTRUCTION;
  36. background = std::make_shared<CPicture>(ImagePath::builtin("CPRESULT"));
  37. background->setPlayerColor(owner.playerID);
  38. pos = center(background->pos);
  39. exit = std::make_shared<CButton>(Point(384, 505), AnimationPath::builtin("iok6432.def"), std::make_pair("", ""), [&](){ bExitf();}, EShortcut::GLOBAL_ACCEPT);
  40. exit->setBorderColor(Colors::METALLIC_GOLD);
  41. if(allowReplay || owner.cb->getStartInfo()->extraOptionsInfo.unlimitedReplay)
  42. {
  43. repeat = std::make_shared<CButton>(Point(24, 505), AnimationPath::builtin("icn6432.def"), std::make_pair("", ""), [&](){ bRepeatf();}, EShortcut::GLOBAL_CANCEL);
  44. repeat->setBorderColor(Colors::METALLIC_GOLD);
  45. labels.push_back(std::make_shared<CLabel>(232, 520, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->translate("vcmi.battleResultsWindow.applyResultsLabel")));
  46. }
  47. if(br.winner == BattleSide::ATTACKER)
  48. {
  49. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[410]));
  50. }
  51. else
  52. {
  53. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[411]));
  54. }
  55. if(br.winner == BattleSide::DEFENDER)
  56. {
  57. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[410]));
  58. }
  59. else
  60. {
  61. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[411]));
  62. }
  63. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->allTexts[407]));
  64. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[408]));
  65. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[409]));
  66. std::string sideNames[2] = {"N/A", "N/A"};
  67. for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
  68. {
  69. auto heroInfo = owner.cb->getBattle(br.battleID)->battleGetHeroInfo(i);
  70. const int xs[] = {21, 392};
  71. if(heroInfo.portraitSource.isValid()) //attacking hero
  72. {
  73. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), heroInfo.getIconIndex(), 0, xs[static_cast<int>(i)], 38));
  74. sideNames[static_cast<int>(i)] = heroInfo.name;
  75. }
  76. else
  77. {
  78. auto stacks = owner.cb->getBattle(br.battleID)->battleGetAllStacks();
  79. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  80. {
  81. return stack->unitSide() != i || !stack->base;
  82. });
  83. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  84. {
  85. return stack->unitType()->getAIValue();
  86. });
  87. if(best != stacks.end()) //should be always but to be safe...
  88. {
  89. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), (*best)->unitType()->getIconIndex(), 0, xs[static_cast<int>(i)], 38));
  90. sideNames[static_cast<int>(i)] = (*best)->unitType()->getNamePluralTranslated();
  91. }
  92. }
  93. }
  94. //printing attacker and defender's names
  95. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  96. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  97. //printing casualties
  98. for(auto step : {BattleSide::ATTACKER, BattleSide::DEFENDER})
  99. {
  100. if(br.casualties[step].size()==0)
  101. {
  102. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * static_cast<int>(step), FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[523]));
  103. }
  104. else
  105. {
  106. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  107. int yPos = 344 + static_cast<int>(step) * 97;
  108. for(auto & elem : br.casualties[step])
  109. {
  110. auto creature = elem.first.toEntity(LIBRARY);
  111. if (creature->getId() == CreatureID::ARROW_TOWERS )
  112. continue; // do not show destroyed towers in battle results
  113. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), creature->getIconIndex(), 0, xPos, yPos));
  114. std::ostringstream amount;
  115. amount<<elem.second;
  116. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  117. xPos += 42;
  118. }
  119. }
  120. }
  121. auto resources = getResources(br);
  122. description = std::make_shared<CTextBox>(resources.resultText.toString(), Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  123. videoPlayer = std::make_shared<VideoWidget>(Point(107, 70), resources.prologueVideo, resources.loopedVideo, false);
  124. ENGINE->music().playMusic(resources.musicName, false, true);
  125. }
  126. BattleResultResources BattleResultWindow::getResources(const BattleResult & br)
  127. {
  128. //printing result description
  129. bool weAreAttacker = owner.cb->getBattle(br.battleID)->battleGetMySide() == BattleSide::ATTACKER;
  130. bool weAreDefender = !weAreAttacker;
  131. bool weWon = (br.winner == BattleSide::ATTACKER && weAreAttacker) || (br.winner == BattleSide::DEFENDER && !weAreAttacker);
  132. bool isSiege = owner.cb->getBattle(br.battleID)->battleGetDefendedTown() != nullptr;
  133. BattleResultResources resources;
  134. if(weWon)
  135. {
  136. if(isSiege && weAreDefender)
  137. {
  138. resources.musicName = AudioPath::builtin("Music/Defend Castle");
  139. resources.prologueVideo = VideoPath::builtin("DEFENDALL.BIK");
  140. resources.loopedVideo = VideoPath::builtin("defendloop.bik");
  141. }
  142. else
  143. {
  144. resources.musicName = AudioPath::builtin("Music/Win Battle");
  145. resources.prologueVideo = VideoPath::builtin("WIN3.BIK");
  146. resources.loopedVideo = VideoPath::builtin("WIN3.BIK");
  147. }
  148. switch(br.result)
  149. {
  150. case EBattleResult::NORMAL:
  151. resources.resultText.appendTextID("core.genrltxt.304");
  152. break;
  153. case EBattleResult::ESCAPE:
  154. resources.resultText.appendTextID("core.genrltxt.303");
  155. break;
  156. case EBattleResult::SURRENDER:
  157. resources.resultText.appendTextID("core.genrltxt.302");
  158. break;
  159. default:
  160. throw std::runtime_error("Invalid battle result!");
  161. }
  162. const CGHeroInstance * ourHero = owner.cb->getBattle(br.battleID)->battleGetMyHero();
  163. if(ourHero)
  164. {
  165. resources.resultText.appendTextID("core.genrltxt.305");
  166. resources.resultText.replaceTextID(ourHero->getNameTextID());
  167. resources.resultText.replaceNumber(br.exp[weAreAttacker ? BattleSide::ATTACKER : BattleSide::DEFENDER]);
  168. }
  169. }
  170. else // we lose
  171. {
  172. switch(br.result)
  173. {
  174. case EBattleResult::NORMAL:
  175. resources.resultText.appendTextID("core.genrltxt.311");
  176. resources.musicName = AudioPath::builtin("Music/LoseCombat");
  177. resources.prologueVideo = VideoPath::builtin("LBSTART.BIK");
  178. resources.loopedVideo = VideoPath::builtin("LBLOOP.BIK");
  179. break;
  180. case EBattleResult::ESCAPE:
  181. resources.resultText.appendTextID("core.genrltxt.310");
  182. resources.musicName = AudioPath::builtin("Music/Retreat Battle");
  183. resources.prologueVideo = VideoPath::builtin("RTSTART.BIK");
  184. resources.loopedVideo = VideoPath::builtin("RTLOOP.BIK");
  185. break;
  186. case EBattleResult::SURRENDER:
  187. resources.resultText.appendTextID("core.genrltxt.309");
  188. resources.musicName = AudioPath::builtin("Music/Surrender Battle");
  189. resources.prologueVideo = VideoPath::builtin("SURRENDER.BIK");
  190. resources.loopedVideo = VideoPath::builtin("SURRENDER.BIK");
  191. break;
  192. default:
  193. throw std::runtime_error("Invalid battle result!");
  194. }
  195. if(isSiege && weAreDefender)
  196. {
  197. resources.musicName = AudioPath::builtin("Music/LoseCastle");
  198. resources.prologueVideo = VideoPath::builtin("LOSECSTL.BIK");
  199. resources.loopedVideo = VideoPath::builtin("LOSECSLP.BIK");
  200. }
  201. }
  202. return resources;
  203. }
  204. void BattleResultWindow::activate()
  205. {
  206. owner.showingDialog->setBusy();
  207. CIntObject::activate();
  208. }
  209. void BattleResultWindow::buttonPressed(int button)
  210. {
  211. if(resultCallback)
  212. resultCallback(button);
  213. CPlayerInterface & intTmp = owner; //copy reference because "this" will be destructed soon
  214. close();
  215. if(ENGINE->windows().topWindow<BattleWindow>())
  216. ENGINE->windows().popWindows(1); //pop battle interface if present
  217. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  218. //so we can be sure that there is no dialogs left on GUI stack.
  219. intTmp.showingDialog->setFree();
  220. }
  221. void BattleResultWindow::bExitf()
  222. {
  223. buttonPressed(0);
  224. }
  225. void BattleResultWindow::bRepeatf()
  226. {
  227. buttonPressed(1);
  228. }