BattleResultProcessor.cpp 19 KB

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