BattleResultProcessor.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * BattleResultProcessor.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 "BattleResultProcessor.h"
  12. #include "../CGameHandler.h"
  13. #include "../processors/HeroPoolProcessor.h"
  14. #include "../queries/QueriesProcessor.h"
  15. #include "../queries/BattleQueries.h"
  16. #include "../../lib/ArtifactUtils.h"
  17. #include "../../lib/CStack.h"
  18. #include "../../lib/GameSettings.h"
  19. #include "../../lib/battle/BattleInfo.h"
  20. #include "../../lib/gameState/CGameState.h"
  21. #include "../../lib/mapObjects/CGTownInstance.h"
  22. #include "../../lib/serializer/Cast.h"
  23. #include "../../lib/spells/CSpellHandler.h"
  24. BattleResultProcessor::BattleResultProcessor(BattleProcessor * owner)
  25. // : owner(owner)
  26. : gameHandler(nullptr)
  27. {
  28. }
  29. void BattleResultProcessor::setGameHandler(CGameHandler * newGameHandler)
  30. {
  31. gameHandler = newGameHandler;
  32. }
  33. CasualtiesAfterBattle::CasualtiesAfterBattle(const SideInBattle & battleSide, const BattleInfo * bat):
  34. army(battleSide.armyObject)
  35. {
  36. heroWithDeadCommander = ObjectInstanceID();
  37. PlayerColor color = battleSide.color;
  38. for(CStack * st : bat->stacks)
  39. {
  40. if(st->summoned) //don't take into account temporary summoned stacks
  41. continue;
  42. if(st->unitOwner() != color) //remove only our stacks
  43. continue;
  44. logGlobal->debug("Calculating casualties for %s", st->nodeName());
  45. st->health.takeResurrected();
  46. if(st->unitSlot() == SlotID::ARROW_TOWERS_SLOT)
  47. {
  48. logGlobal->debug("Ignored arrow towers stack.");
  49. }
  50. else if(st->unitSlot() == SlotID::WAR_MACHINES_SLOT)
  51. {
  52. auto warMachine = st->unitType()->warMachine;
  53. if(warMachine == ArtifactID::NONE)
  54. {
  55. logGlobal->error("Invalid creature in war machine virtual slot. Stack: %s", st->nodeName());
  56. }
  57. //catapult artifact remain even if "creature" killed in siege
  58. else if(warMachine != ArtifactID::CATAPULT && st->getCount() <= 0)
  59. {
  60. logGlobal->debug("War machine has been destroyed");
  61. auto hero = dynamic_ptr_cast<CGHeroInstance> (army);
  62. if (hero)
  63. removedWarMachines.push_back (ArtifactLocation(hero, hero->getArtPos(warMachine, true)));
  64. else
  65. logGlobal->error("War machine in army without hero");
  66. }
  67. }
  68. else if(st->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
  69. {
  70. if(st->alive() && st->getCount() > 0)
  71. {
  72. logGlobal->debug("Permanently summoned %d units.", st->getCount());
  73. const CreatureID summonedType = st->creatureId();
  74. summoned[summonedType] += st->getCount();
  75. }
  76. }
  77. else if(st->unitSlot() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  78. {
  79. if (nullptr == st->base)
  80. {
  81. logGlobal->error("Stack with no base in commander slot. Stack: %s", st->nodeName());
  82. }
  83. else
  84. {
  85. auto c = dynamic_cast <const CCommanderInstance *>(st->base);
  86. if(c)
  87. {
  88. auto h = dynamic_cast <const CGHeroInstance *>(army);
  89. if(h && h->commander == c && (st->getCount() == 0 || !st->alive()))
  90. {
  91. logGlobal->debug("Commander is dead.");
  92. heroWithDeadCommander = army->id; //TODO: unify commander handling
  93. }
  94. }
  95. else
  96. logGlobal->error("Stack with invalid instance in commander slot. Stack: %s", st->nodeName());
  97. }
  98. }
  99. else if(st->base && !army->slotEmpty(st->unitSlot()))
  100. {
  101. logGlobal->debug("Count: %d; base count: %d", st->getCount(), army->getStackCount(st->unitSlot()));
  102. if(st->getCount() == 0 || !st->alive())
  103. {
  104. logGlobal->debug("Stack has been destroyed.");
  105. StackLocation sl(army, st->unitSlot());
  106. newStackCounts.push_back(TStackAndItsNewCount(sl, 0));
  107. }
  108. else if(st->getCount() < army->getStackCount(st->unitSlot()))
  109. {
  110. logGlobal->debug("Stack lost %d units.", army->getStackCount(st->unitSlot()) - st->getCount());
  111. StackLocation sl(army, st->unitSlot());
  112. newStackCounts.push_back(TStackAndItsNewCount(sl, st->getCount()));
  113. }
  114. else if(st->getCount() > army->getStackCount(st->unitSlot()))
  115. {
  116. logGlobal->debug("Stack gained %d units.", st->getCount() - army->getStackCount(st->unitSlot()));
  117. StackLocation sl(army, st->unitSlot());
  118. newStackCounts.push_back(TStackAndItsNewCount(sl, st->getCount()));
  119. }
  120. }
  121. else
  122. {
  123. logGlobal->warn("Unable to process stack: %s", st->nodeName());
  124. }
  125. }
  126. }
  127. void CasualtiesAfterBattle::updateArmy(CGameHandler *gh)
  128. {
  129. for (TStackAndItsNewCount &ncount : newStackCounts)
  130. {
  131. if (ncount.second > 0)
  132. gh->changeStackCount(ncount.first, ncount.second, true);
  133. else
  134. gh->eraseStack(ncount.first, true);
  135. }
  136. for (auto summoned_iter : summoned)
  137. {
  138. SlotID slot = army->getSlotFor(summoned_iter.first);
  139. if (slot.validSlot())
  140. {
  141. StackLocation location(army, slot);
  142. gh->addToSlot(location, summoned_iter.first.toCreature(), summoned_iter.second);
  143. }
  144. else
  145. {
  146. //even if it will be possible to summon anything permanently it should be checked for free slot
  147. //necromancy is handled separately
  148. gh->complain("No free slot to put summoned creature");
  149. }
  150. }
  151. for (auto al : removedWarMachines)
  152. {
  153. gh->removeArtifact(al);
  154. }
  155. if (heroWithDeadCommander != ObjectInstanceID())
  156. {
  157. SetCommanderProperty scp;
  158. scp.heroid = heroWithDeadCommander;
  159. scp.which = SetCommanderProperty::ALIVE;
  160. scp.amount = 0;
  161. gh->sendAndApply(&scp);
  162. }
  163. }
  164. FinishingBattleHelper::FinishingBattleHelper(std::shared_ptr<const CBattleQuery> Query, int remainingBattleQueriesCount)
  165. {
  166. assert(Query->result);
  167. assert(Query->bi);
  168. auto &result = *Query->result;
  169. auto &info = *Query->bi;
  170. winnerHero = result.winner != 0 ? info.sides[1].hero : info.sides[0].hero;
  171. loserHero = result.winner != 0 ? info.sides[0].hero : info.sides[1].hero;
  172. victor = info.sides[result.winner].color;
  173. loser = info.sides[!result.winner].color;
  174. winnerSide = result.winner;
  175. this->remainingBattleQueriesCount = remainingBattleQueriesCount;
  176. }
  177. FinishingBattleHelper::FinishingBattleHelper()
  178. {
  179. winnerHero = loserHero = nullptr;
  180. winnerSide = 0;
  181. remainingBattleQueriesCount = 0;
  182. }
  183. void BattleResultProcessor::endBattle(int3 tile, const CGHeroInstance * heroAttacker, const CGHeroInstance * heroDefender)
  184. {
  185. auto const & giveExp = [](BattleResult &r)
  186. {
  187. if (r.winner > 1)
  188. {
  189. // draw
  190. return;
  191. }
  192. r.exp[0] = 0;
  193. r.exp[1] = 0;
  194. for (auto i = r.casualties[!r.winner].begin(); i!=r.casualties[!r.winner].end(); i++)
  195. {
  196. r.exp[r.winner] += VLC->creh->objects.at(i->first)->valOfBonuses(BonusType::STACK_HEALTH) * i->second;
  197. }
  198. };
  199. LOG_TRACE(logGlobal);
  200. //Fill BattleResult structure with exp info
  201. giveExp(*battleResult);
  202. if (battleResult->result == EBattleResult::NORMAL) // give 500 exp for defeating hero, unless he escaped
  203. {
  204. if(heroAttacker)
  205. battleResult->exp[1] += 500;
  206. if(heroDefender)
  207. battleResult->exp[0] += 500;
  208. }
  209. if(heroAttacker)
  210. battleResult->exp[0] = heroAttacker->calculateXp(battleResult->exp[0]);//scholar skill
  211. if(heroDefender)
  212. battleResult->exp[1] = heroDefender->calculateXp(battleResult->exp[1]);
  213. auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(gameHandler->gameState()->curB->sides[0].color));
  214. if (!battleQuery)
  215. {
  216. logGlobal->error("Cannot find battle query!");
  217. gameHandler->complain("Player " + boost::lexical_cast<std::string>(gameHandler->gameState()->curB->sides[0].color) + " has no battle query at the top!");
  218. return;
  219. }
  220. battleQuery->result = std::make_optional(*battleResult);
  221. //Check how many battle gameHandler->queries were created (number of players blocked by battle)
  222. const int queriedPlayers = battleQuery ? (int)boost::count(gameHandler->queries->allQueries(), battleQuery) : 0;
  223. finishingBattle = std::make_unique<FinishingBattleHelper>(battleQuery, queriedPlayers);
  224. // in battles against neutrals, 1st player can ask to replay battle manually
  225. if (!gameHandler->gameState()->curB->sides[1].color.isValidPlayer())
  226. {
  227. auto battleDialogQuery = std::make_shared<CBattleDialogQuery>(gameHandler, gameHandler->gameState()->curB);
  228. battleResult->queryID = battleDialogQuery->queryID;
  229. gameHandler->queries->addQuery(battleDialogQuery);
  230. }
  231. else
  232. battleResult->queryID = -1;
  233. //set same battle result for all gameHandler->queries
  234. for(auto q : gameHandler->queries->allQueries())
  235. {
  236. auto otherBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(q);
  237. if(otherBattleQuery)
  238. otherBattleQuery->result = battleQuery->result;
  239. }
  240. gameHandler->turnTimerHandler.onBattleEnd();
  241. gameHandler->sendAndApply(battleResult.get()); //after this point casualties objects are destroyed
  242. if (battleResult->queryID == QueryID::NONE)
  243. endBattleConfirm(gameHandler->gameState()->curB);
  244. }
  245. void BattleResultProcessor::endBattleConfirm(const BattleInfo * battleInfo)
  246. {
  247. auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battleInfo->sides.at(0).color));
  248. if(!battleQuery)
  249. {
  250. logGlobal->trace("No battle query, battle end was confirmed by another player");
  251. return;
  252. }
  253. const EBattleResult result = battleResult.get()->result;
  254. CasualtiesAfterBattle cab1(battleInfo->sides.at(0), battleInfo), cab2(battleInfo->sides.at(1), battleInfo); //calculate casualties before deleting battle
  255. ChangeSpells cs; //for Eagle Eye
  256. if(!finishingBattle->isDraw() && finishingBattle->winnerHero)
  257. {
  258. if (int eagleEyeLevel = finishingBattle->winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_LEVEL_LIMIT, -1))
  259. {
  260. double eagleEyeChance = finishingBattle->winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_CHANCE, 0);
  261. for(auto & spellId : battleInfo->sides.at(!battleResult->winner).usedSpellsHistory)
  262. {
  263. auto spell = spellId.toSpell(VLC->spells());
  264. if(spell && spell->getLevel() <= eagleEyeLevel && !finishingBattle->winnerHero->spellbookContainsSpell(spell->getId()) && gameHandler->getRandomGenerator().nextInt(99) < eagleEyeChance)
  265. cs.spells.insert(spell->getId());
  266. }
  267. }
  268. }
  269. std::vector<const CArtifactInstance *> arts; //display them in window
  270. if(result == EBattleResult::NORMAL && !finishingBattle->isDraw() && finishingBattle->winnerHero)
  271. {
  272. auto sendMoveArtifact = [&](const CArtifactInstance *art, MoveArtifact *ma)
  273. {
  274. const auto slot = ArtifactUtils::getArtAnyPosition(finishingBattle->winnerHero, art->getTypeId());
  275. if(slot != ArtifactPosition::PRE_FIRST)
  276. {
  277. arts.push_back(art);
  278. ma->dst = ArtifactLocation(finishingBattle->winnerHero, slot);
  279. if(ArtifactUtils::isSlotBackpack(slot))
  280. ma->askAssemble = false;
  281. gameHandler->sendAndApply(ma);
  282. }
  283. };
  284. if (finishingBattle->loserHero)
  285. {
  286. //TODO: wrap it into a function, somehow (std::variant -_-)
  287. auto artifactsWorn = finishingBattle->loserHero->artifactsWorn;
  288. for (auto artSlot : artifactsWorn)
  289. {
  290. MoveArtifact ma;
  291. ma.src = ArtifactLocation(finishingBattle->loserHero, artSlot.first);
  292. const CArtifactInstance * art = ma.src.getArt();
  293. if (art && !art->artType->isBig() &&
  294. art->artType->getId() != ArtifactID::SPELLBOOK)
  295. // don't move war machines or locked arts (spellbook)
  296. {
  297. sendMoveArtifact(art, &ma);
  298. }
  299. }
  300. for(int slotNumber = finishingBattle->loserHero->artifactsInBackpack.size() - 1; slotNumber >= 0; slotNumber--)
  301. {
  302. //we assume that no big artifacts can be found
  303. MoveArtifact ma;
  304. ma.src = ArtifactLocation(finishingBattle->loserHero,
  305. ArtifactPosition(ArtifactPosition::BACKPACK_START + slotNumber)); //backpack automatically shifts arts to beginning
  306. const CArtifactInstance * art = ma.src.getArt();
  307. if (art->artType->getId() != ArtifactID::GRAIL) //grail may not be won
  308. {
  309. sendMoveArtifact(art, &ma);
  310. }
  311. }
  312. if (finishingBattle->loserHero->commander) //TODO: what if commanders belong to no hero?
  313. {
  314. artifactsWorn = finishingBattle->loserHero->commander->artifactsWorn;
  315. for (auto artSlot : artifactsWorn)
  316. {
  317. MoveArtifact ma;
  318. ma.src = ArtifactLocation(finishingBattle->loserHero->commander.get(), artSlot.first);
  319. const CArtifactInstance * art = ma.src.getArt();
  320. if (art && !art->artType->isBig())
  321. {
  322. sendMoveArtifact(art, &ma);
  323. }
  324. }
  325. }
  326. }
  327. for (auto armySlot : battleInfo->sides.at(!battleResult->winner).armyObject->stacks)
  328. {
  329. auto artifactsWorn = armySlot.second->artifactsWorn;
  330. for (auto artSlot : artifactsWorn)
  331. {
  332. MoveArtifact ma;
  333. ma.src = ArtifactLocation(armySlot.second, artSlot.first);
  334. const CArtifactInstance * art = ma.src.getArt();
  335. if (art && !art->artType->isBig())
  336. {
  337. sendMoveArtifact(art, &ma);
  338. }
  339. }
  340. }
  341. }
  342. if (arts.size()) //display loot
  343. {
  344. InfoWindow iw;
  345. iw.player = finishingBattle->winnerHero->tempOwner;
  346. iw.text.appendLocalString (EMetaText::GENERAL_TXT, 30); //You have captured enemy artifact
  347. for (auto art : arts) //TODO; separate function to display loot for various ojects?
  348. {
  349. iw.components.emplace_back(
  350. Component::EComponentType::ARTIFACT, art->artType->getId(),
  351. art->artType->getId() == ArtifactID::SPELL_SCROLL? art->getScrollSpellID() : SpellID(0), 0);
  352. if (iw.components.size() >= 14)
  353. {
  354. gameHandler->sendAndApply(&iw);
  355. iw.components.clear();
  356. }
  357. }
  358. if (iw.components.size())
  359. {
  360. gameHandler->sendAndApply(&iw);
  361. }
  362. }
  363. //Eagle Eye secondary skill handling
  364. if (!cs.spells.empty())
  365. {
  366. cs.learn = 1;
  367. cs.hid = finishingBattle->winnerHero->id;
  368. InfoWindow iw;
  369. iw.player = finishingBattle->winnerHero->tempOwner;
  370. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 221); //Through eagle-eyed observation, %s is able to learn %s
  371. iw.text.replaceRawString(finishingBattle->winnerHero->getNameTranslated());
  372. std::ostringstream names;
  373. for (int i = 0; i < cs.spells.size(); i++)
  374. {
  375. names << "%s";
  376. if (i < cs.spells.size() - 2)
  377. names << ", ";
  378. else if (i < cs.spells.size() - 1)
  379. names << "%s";
  380. }
  381. names << ".";
  382. iw.text.replaceRawString(names.str());
  383. auto it = cs.spells.begin();
  384. for (int i = 0; i < cs.spells.size(); i++, it++)
  385. {
  386. iw.text.replaceLocalString(EMetaText::SPELL_NAME, it->toEnum());
  387. if (i == cs.spells.size() - 2) //we just added pre-last name
  388. iw.text.replaceLocalString(EMetaText::GENERAL_TXT, 141); // " and "
  389. iw.components.emplace_back(Component::EComponentType::SPELL, *it, 0, 0);
  390. }
  391. gameHandler->sendAndApply(&iw);
  392. gameHandler->sendAndApply(&cs);
  393. }
  394. cab1.updateArmy(gameHandler);
  395. cab2.updateArmy(gameHandler); //take casualties after battle is deleted
  396. if(finishingBattle->loserHero) //remove beaten hero
  397. {
  398. RemoveObject ro(finishingBattle->loserHero->id);
  399. gameHandler->sendAndApply(&ro);
  400. }
  401. if(finishingBattle->isDraw() && finishingBattle->winnerHero) //for draw case both heroes should be removed
  402. {
  403. RemoveObject ro(finishingBattle->winnerHero->id);
  404. gameHandler->sendAndApply(&ro);
  405. }
  406. if(battleResult->winner == BattleSide::DEFENDER
  407. && finishingBattle->winnerHero
  408. && finishingBattle->winnerHero->visitedTown
  409. && !finishingBattle->winnerHero->inTownGarrison
  410. && finishingBattle->winnerHero->visitedTown->garrisonHero == finishingBattle->winnerHero)
  411. {
  412. gameHandler->swapGarrisonOnSiege(finishingBattle->winnerHero->visitedTown->id); //return defending visitor from garrison to its rightful place
  413. }
  414. //give exp
  415. if(!finishingBattle->isDraw() && battleResult->exp[finishingBattle->winnerSide] && finishingBattle->winnerHero)
  416. gameHandler->changePrimSkill(finishingBattle->winnerHero, PrimarySkill::EXPERIENCE, battleResult->exp[finishingBattle->winnerSide]);
  417. BattleResultAccepted raccepted;
  418. raccepted.heroResult[0].army = const_cast<CArmedInstance*>(battleInfo->sides.at(0).armyObject);
  419. raccepted.heroResult[1].army = const_cast<CArmedInstance*>(battleInfo->sides.at(1).armyObject);
  420. raccepted.heroResult[0].hero = const_cast<CGHeroInstance*>(battleInfo->sides.at(0).hero);
  421. raccepted.heroResult[1].hero = const_cast<CGHeroInstance*>(battleInfo->sides.at(1).hero);
  422. raccepted.heroResult[0].exp = battleResult->exp[0];
  423. raccepted.heroResult[1].exp = battleResult->exp[1];
  424. raccepted.winnerSide = finishingBattle->winnerSide;
  425. gameHandler->sendAndApply(&raccepted);
  426. gameHandler->queries->popIfTop(battleQuery);
  427. //--> continuation (battleAfterLevelUp) occurs after level-up gameHandler->queries are handled or on removing query
  428. }
  429. void BattleResultProcessor::battleAfterLevelUp(const BattleResult &result)
  430. {
  431. LOG_TRACE(logGlobal);
  432. if(!finishingBattle)
  433. return;
  434. finishingBattle->remainingBattleQueriesCount--;
  435. logGlobal->trace("Decremented gameHandler->queries count to %d", finishingBattle->remainingBattleQueriesCount);
  436. if (finishingBattle->remainingBattleQueriesCount > 0)
  437. //Battle results will be handled when all battle gameHandler->queries are closed
  438. return;
  439. //TODO consider if we really want it to work like above. ATM each player as unblocked as soon as possible
  440. // but the battle consequences are applied after final player is unblocked. Hard to abuse...
  441. // Still, it looks like a hole.
  442. // Necromancy if applicable.
  443. const CStackBasicDescriptor raisedStack = finishingBattle->winnerHero ? finishingBattle->winnerHero->calculateNecromancy(*battleResult) : CStackBasicDescriptor();
  444. // Give raised units to winner and show dialog, if any were raised,
  445. // units will be given after casualties are taken
  446. const SlotID necroSlot = raisedStack.type ? finishingBattle->winnerHero->getSlotFor(raisedStack.type) : SlotID();
  447. if (necroSlot != SlotID())
  448. {
  449. finishingBattle->winnerHero->showNecromancyDialog(raisedStack, gameHandler->getRandomGenerator());
  450. gameHandler->addToSlot(StackLocation(finishingBattle->winnerHero, necroSlot), raisedStack.type, raisedStack.count);
  451. }
  452. BattleResultsApplied resultsApplied;
  453. resultsApplied.player1 = finishingBattle->victor;
  454. resultsApplied.player2 = finishingBattle->loser;
  455. gameHandler->sendAndApply(&resultsApplied);
  456. gameHandler->setBattle(nullptr);
  457. //handle victory/loss of engaged players
  458. std::set<PlayerColor> playerColors = {finishingBattle->loser, finishingBattle->victor};
  459. gameHandler->checkVictoryLossConditions(playerColors);
  460. if (result.result == EBattleResult::SURRENDER)
  461. gameHandler->heroPool->onHeroSurrendered(finishingBattle->loser, finishingBattle->loserHero);
  462. if (result.result == EBattleResult::ESCAPE)
  463. gameHandler->heroPool->onHeroEscaped(finishingBattle->loser, finishingBattle->loserHero);
  464. if (result.winner != 2 && finishingBattle->winnerHero && finishingBattle->winnerHero->stacks.empty()
  465. && (!finishingBattle->winnerHero->commander || !finishingBattle->winnerHero->commander->alive))
  466. {
  467. RemoveObject ro(finishingBattle->winnerHero->id);
  468. gameHandler->sendAndApply(&ro);
  469. if (VLC->settings()->getBoolean(EGameSettings::HEROES_RETREAT_ON_WIN_WITHOUT_TROOPS))
  470. gameHandler->heroPool->onHeroEscaped(finishingBattle->victor, finishingBattle->winnerHero);
  471. }
  472. finishingBattle.reset();
  473. battleResult.reset();
  474. }
  475. void BattleResultProcessor::setBattleResult(EBattleResult resultType, int victoriusSide)
  476. {
  477. battleResult = std::make_unique<BattleResult>();
  478. battleResult->result = resultType;
  479. battleResult->winner = victoriusSide; //surrendering side loses
  480. gameHandler->gameState()->curB->calculateCasualties(battleResult->casualties);
  481. }
  482. void BattleResultProcessor::setupBattle()
  483. {
  484. finishingBattle.reset();
  485. battleResult.reset();
  486. }
  487. bool BattleResultProcessor::battleIsEnding() const
  488. {
  489. return battleResult != nullptr;
  490. }