|
@@ -9,6 +9,7 @@
|
|
|
*/
|
|
*/
|
|
|
#include "StdInc.h"
|
|
#include "StdInc.h"
|
|
|
#include "BattleResultProcessor.h"
|
|
#include "BattleResultProcessor.h"
|
|
|
|
|
+#include "battle/BattleInfo.h"
|
|
|
|
|
|
|
|
#include "../CGameHandler.h"
|
|
#include "../CGameHandler.h"
|
|
|
#include "../TurnTimerHandler.h"
|
|
#include "../TurnTimerHandler.h"
|
|
@@ -20,17 +21,12 @@
|
|
|
#include "../../lib/CStack.h"
|
|
#include "../../lib/CStack.h"
|
|
|
#include "../../lib/CPlayerState.h"
|
|
#include "../../lib/CPlayerState.h"
|
|
|
#include "../../lib/IGameSettings.h"
|
|
#include "../../lib/IGameSettings.h"
|
|
|
-#include "../../lib/battle/CBattleInfoCallback.h"
|
|
|
|
|
-#include "../../lib/battle/IBattleState.h"
|
|
|
|
|
#include "../../lib/battle/SideInBattle.h"
|
|
#include "../../lib/battle/SideInBattle.h"
|
|
|
#include "../../lib/gameState/CGameState.h"
|
|
#include "../../lib/gameState/CGameState.h"
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
|
|
#include "../../lib/networkPacks/PacksForClientBattle.h"
|
|
#include "../../lib/networkPacks/PacksForClientBattle.h"
|
|
|
-#include "../../lib/networkPacks/PacksForClient.h"
|
|
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
|
#include "../../lib/spells/CSpellHandler.h"
|
|
|
|
|
|
|
|
-#include <vstd/RNG.h>
|
|
|
|
|
-
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
BattleResultProcessor::BattleResultProcessor(BattleProcessor * owner, CGameHandler * newGameHandler)
|
|
BattleResultProcessor::BattleResultProcessor(BattleProcessor * owner, CGameHandler * newGameHandler)
|
|
@@ -186,17 +182,19 @@ void CasualtiesAfterBattle::updateArmy(CGameHandler *gh)
|
|
|
|
|
|
|
|
FinishingBattleHelper::FinishingBattleHelper(const CBattleInfoCallback & info, const BattleResult & result, int remainingBattleQueriesCount)
|
|
FinishingBattleHelper::FinishingBattleHelper(const CBattleInfoCallback & info, const BattleResult & result, int remainingBattleQueriesCount)
|
|
|
{
|
|
{
|
|
|
|
|
+ const auto attackerHero = info.getBattle()->getSideHero(BattleSide::ATTACKER);
|
|
|
|
|
+ const auto defenderHero = info.getBattle()->getSideHero(BattleSide::DEFENDER);
|
|
|
if (result.winner == BattleSide::ATTACKER)
|
|
if (result.winner == BattleSide::ATTACKER)
|
|
|
{
|
|
{
|
|
|
- winnerHero = info.getBattle()->getSideHero(BattleSide::ATTACKER);
|
|
|
|
|
- loserHero = info.getBattle()->getSideHero(BattleSide::DEFENDER);
|
|
|
|
|
|
|
+ winnerId = attackerHero ? attackerHero->id : ObjectInstanceID::NONE;
|
|
|
|
|
+ loserId = defenderHero ? defenderHero->id : ObjectInstanceID::NONE;
|
|
|
victor = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
|
|
victor = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
|
|
|
loser = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
|
|
loser = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- winnerHero = info.getBattle()->getSideHero(BattleSide::DEFENDER);
|
|
|
|
|
- loserHero = info.getBattle()->getSideHero(BattleSide::ATTACKER);
|
|
|
|
|
|
|
+ winnerId = defenderHero ? defenderHero->id : ObjectInstanceID::NONE;
|
|
|
|
|
+ loserId = attackerHero ? attackerHero->id : ObjectInstanceID::NONE;
|
|
|
victor = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
|
|
victor = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
|
|
|
loser = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
|
|
loser = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
|
|
|
}
|
|
}
|
|
@@ -313,8 +311,6 @@ void BattleResultProcessor::endBattleConfirm(const CBattleInfoCallback & battle)
|
|
|
auto * battleResult = battleResults.at(battle.getBattle()->getBattleID()).get();
|
|
auto * battleResult = battleResults.at(battle.getBattle()->getBattleID()).get();
|
|
|
auto * finishingBattle = finishingBattles.at(battle.getBattle()->getBattleID()).get();
|
|
auto * finishingBattle = finishingBattles.at(battle.getBattle()->getBattleID()).get();
|
|
|
|
|
|
|
|
- const EBattleResult result = battleResult->result;
|
|
|
|
|
-
|
|
|
|
|
//calculate casualties before deleting battle
|
|
//calculate casualties before deleting battle
|
|
|
CasualtiesAfterBattle cab1(battle, BattleSide::ATTACKER);
|
|
CasualtiesAfterBattle cab1(battle, BattleSide::ATTACKER);
|
|
|
CasualtiesAfterBattle cab2(battle, BattleSide::DEFENDER);
|
|
CasualtiesAfterBattle cab2(battle, BattleSide::DEFENDER);
|
|
@@ -322,280 +318,209 @@ void BattleResultProcessor::endBattleConfirm(const CBattleInfoCallback & battle)
|
|
|
cab1.updateArmy(gameHandler);
|
|
cab1.updateArmy(gameHandler);
|
|
|
cab2.updateArmy(gameHandler); //take casualties after battle is deleted
|
|
cab2.updateArmy(gameHandler); //take casualties after battle is deleted
|
|
|
|
|
|
|
|
|
|
+ const auto winnerHero = battle.battleGetFightingHero(finishingBattle->winnerSide);
|
|
|
|
|
+ const auto loserHero = battle.battleGetFightingHero(CBattleInfoEssentials::otherSide(finishingBattle->winnerSide));
|
|
|
|
|
+
|
|
|
if(battleResult->winner == BattleSide::DEFENDER
|
|
if(battleResult->winner == BattleSide::DEFENDER
|
|
|
- && finishingBattle->winnerHero
|
|
|
|
|
- && finishingBattle->winnerHero->visitedTown
|
|
|
|
|
- && !finishingBattle->winnerHero->inTownGarrison
|
|
|
|
|
- && finishingBattle->winnerHero->visitedTown->garrisonHero == finishingBattle->winnerHero)
|
|
|
|
|
|
|
+ && winnerHero
|
|
|
|
|
+ && winnerHero->visitedTown
|
|
|
|
|
+ && !winnerHero->inTownGarrison
|
|
|
|
|
+ && winnerHero->visitedTown->garrisonHero == winnerHero)
|
|
|
{
|
|
{
|
|
|
- gameHandler->swapGarrisonOnSiege(finishingBattle->winnerHero->visitedTown->id); //return defending visitor from garrison to its rightful place
|
|
|
|
|
|
|
+ gameHandler->swapGarrisonOnSiege(winnerHero->visitedTown->id); //return defending visitor from garrison to its rightful place
|
|
|
}
|
|
}
|
|
|
//give exp
|
|
//give exp
|
|
|
- if(!finishingBattle->isDraw() && battleResult->exp[finishingBattle->winnerSide] && finishingBattle->winnerHero)
|
|
|
|
|
- gameHandler->giveExperience(finishingBattle->winnerHero, battleResult->exp[finishingBattle->winnerSide]);
|
|
|
|
|
|
|
+ if(!finishingBattle->isDraw() && battleResult->exp[finishingBattle->winnerSide] && winnerHero)
|
|
|
|
|
+ gameHandler->giveExperience(winnerHero, battleResult->exp[finishingBattle->winnerSide]);
|
|
|
|
|
|
|
|
- // Eagle Eye handling
|
|
|
|
|
- if(!finishingBattle->isDraw() && finishingBattle->winnerHero)
|
|
|
|
|
|
|
+ // Add statistics
|
|
|
|
|
+ if(loserHero && !finishingBattle->isDraw())
|
|
|
|
|
+ {
|
|
|
|
|
+ ConstTransitivePtr<CGHeroInstance> strongestHero = nullptr;
|
|
|
|
|
+ for(auto & hero : gameHandler->gameState()->getPlayerState(finishingBattle->loser)->getHeroes())
|
|
|
|
|
+ if(!strongestHero || hero->exp > strongestHero->exp)
|
|
|
|
|
+ strongestHero = hero;
|
|
|
|
|
+ if(strongestHero->id == finishingBattle->loserId && strongestHero->level > 5)
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->victor].lastDefeatedStrongestHeroDay = gameHandler->gameState()->getDate(Date::DAY);
|
|
|
|
|
+ }
|
|
|
|
|
+ if(battle.sideToPlayer(BattleSide::ATTACKER) == PlayerColor::NEUTRAL || battle.sideToPlayer(BattleSide::DEFENDER) == PlayerColor::NEUTRAL)
|
|
|
|
|
+ {
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::ATTACKER)].numBattlesNeutral++;
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::DEFENDER)].numBattlesNeutral++;
|
|
|
|
|
+ if(!finishingBattle->isDraw())
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesNeutral++;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
{
|
|
{
|
|
|
- ChangeSpells spells;
|
|
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::ATTACKER)].numBattlesPlayer++;
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::DEFENDER)].numBattlesPlayer++;
|
|
|
|
|
+ if(!finishingBattle->isDraw())
|
|
|
|
|
+ gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesPlayer++;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if(auto eagleEyeLevel = finishingBattle->winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_LEVEL_LIMIT))
|
|
|
|
|
- {
|
|
|
|
|
- auto eagleEyeChance = finishingBattle->winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_CHANCE);
|
|
|
|
|
- for(auto & spellId : battle.getBattle()->getUsedSpells(battle.otherSide(battleResult->winner)))
|
|
|
|
|
- {
|
|
|
|
|
- auto spell = spellId.toEntity(LIBRARY->spells());
|
|
|
|
|
- if(spell
|
|
|
|
|
- && spell->getLevel() <= eagleEyeLevel
|
|
|
|
|
- && !finishingBattle->winnerHero->spellbookContainsSpell(spell->getId())
|
|
|
|
|
- && gameHandler->getRandomGenerator().nextInt(99) < eagleEyeChance)
|
|
|
|
|
- {
|
|
|
|
|
- spells.spells.insert(spell->getId());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ BattleResultAccepted raccepted;
|
|
|
|
|
+ raccepted.battleID = battle.getBattle()->getBattleID();
|
|
|
|
|
+ raccepted.heroResult[finishingBattle->winnerSide].heroId = winnerHero ? winnerHero->id : ObjectInstanceID::NONE;
|
|
|
|
|
+ raccepted.heroResult[CBattleInfoEssentials::otherSide(finishingBattle->winnerSide)].heroId = loserHero ? loserHero->id : ObjectInstanceID::NONE;
|
|
|
|
|
+ raccepted.heroResult[BattleSide::ATTACKER].armyId = battle.battleGetArmyObject(BattleSide::ATTACKER)->id;
|
|
|
|
|
+ raccepted.heroResult[BattleSide::DEFENDER].armyId = battle.battleGetArmyObject(BattleSide::DEFENDER)->id;
|
|
|
|
|
+ raccepted.heroResult[BattleSide::ATTACKER].exp = battleResult->exp[BattleSide::ATTACKER];
|
|
|
|
|
+ raccepted.heroResult[BattleSide::DEFENDER].exp = battleResult->exp[BattleSide::DEFENDER];
|
|
|
|
|
+ raccepted.winnerSide = finishingBattle->winnerSide;
|
|
|
|
|
+ gameHandler->sendAndApply(raccepted);
|
|
|
|
|
|
|
|
- if(!spells.spells.empty())
|
|
|
|
|
- {
|
|
|
|
|
- spells.learn = 1;
|
|
|
|
|
- spells.hid = finishingBattle->winnerHero->id;
|
|
|
|
|
|
|
+ //--> continuation (battleFinalize) occurs after level-up gameHandler->queries are handled or on removing query
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void BattleResultProcessor::battleFinalize(const BattleID & battleID, const BattleResult & result)
|
|
|
|
|
+{
|
|
|
|
|
+ LOG_TRACE(logGlobal);
|
|
|
|
|
|
|
|
- InfoWindow iw;
|
|
|
|
|
- iw.player = finishingBattle->winnerHero->tempOwner;
|
|
|
|
|
- iw.text.appendLocalString(EMetaText::GENERAL_TXT, 221); //Through eagle-eyed observation, %s is able to learn %s
|
|
|
|
|
- iw.text.replaceRawString(finishingBattle->winnerHero->getNameTranslated());
|
|
|
|
|
|
|
+ assert(finishingBattles.count(battleID) != 0);
|
|
|
|
|
+ if(finishingBattles.count(battleID) == 0)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
- std::ostringstream names;
|
|
|
|
|
- for(int i = 0; i < spells.spells.size(); i++)
|
|
|
|
|
- {
|
|
|
|
|
- names << "%s";
|
|
|
|
|
- if(i < spells.spells.size() - 2)
|
|
|
|
|
- names << ", ";
|
|
|
|
|
- else if(i < spells.spells.size() - 1)
|
|
|
|
|
- names << "%s";
|
|
|
|
|
- }
|
|
|
|
|
- names << ".";
|
|
|
|
|
|
|
+ auto & finishingBattle = finishingBattles[battleID];
|
|
|
|
|
+
|
|
|
|
|
+ finishingBattle->remainingBattleQueriesCount--;
|
|
|
|
|
+ logGlobal->trace("Decremented gameHandler->queries count to %d", finishingBattle->remainingBattleQueriesCount);
|
|
|
|
|
+
|
|
|
|
|
+ if (finishingBattle->remainingBattleQueriesCount > 0)
|
|
|
|
|
+ //Battle results will be handled when all battle gameHandler->queries are closed
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ //TODO consider if we really want it to work like above. ATM each player as unblocked as soon as possible
|
|
|
|
|
+ // but the battle consequences are applied after final player is unblocked. Hard to abuse...
|
|
|
|
|
+ // Still, it looks like a hole.
|
|
|
|
|
|
|
|
- iw.text.replaceRawString(names.str());
|
|
|
|
|
|
|
+ const auto battle = std::find_if(gameHandler->gameState()->currentBattles.begin(), gameHandler->gameState()->currentBattles.end(),
|
|
|
|
|
+ [battleID](const auto & desiredBattle)
|
|
|
|
|
+ {
|
|
|
|
|
+ return desiredBattle->battleID == battleID;
|
|
|
|
|
+ });
|
|
|
|
|
+ assert(battle != gameHandler->gameState()->currentBattles.end());
|
|
|
|
|
|
|
|
- auto it = spells.spells.begin();
|
|
|
|
|
- for(int i = 0; i < spells.spells.size(); i++, it++)
|
|
|
|
|
|
|
+ const auto winnerHero = (*battle)->battleGetFightingHero(finishingBattle->winnerSide);
|
|
|
|
|
+ const auto loserHero = (*battle)->battleGetFightingHero(CBattleInfoEssentials::otherSide(finishingBattle->winnerSide));
|
|
|
|
|
+ BattleResultsApplied resultsApplied;
|
|
|
|
|
+
|
|
|
|
|
+ // Eagle Eye handling
|
|
|
|
|
+ if(!finishingBattle->isDraw() && winnerHero)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(auto eagleEyeLevel = winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_LEVEL_LIMIT))
|
|
|
|
|
+ {
|
|
|
|
|
+ resultsApplied.learnedSpells.learn = 1;
|
|
|
|
|
+ resultsApplied.learnedSpells.hid = finishingBattle->winnerId;
|
|
|
|
|
+ for(const auto & spellId : (*battle)->getUsedSpells(CBattleInfoEssentials::otherSide(result.winner)))
|
|
|
{
|
|
{
|
|
|
- iw.text.replaceName(*it);
|
|
|
|
|
- if(i == spells.spells.size() - 2) //we just added pre-last name
|
|
|
|
|
- iw.text.replaceLocalString(EMetaText::GENERAL_TXT, 141); // " and "
|
|
|
|
|
- iw.components.emplace_back(ComponentType::SPELL, *it);
|
|
|
|
|
|
|
+ const auto spell = spellId.toEntity(LIBRARY->spells());
|
|
|
|
|
+ if(spell
|
|
|
|
|
+ && spell->getLevel() <= eagleEyeLevel
|
|
|
|
|
+ && !winnerHero->spellbookContainsSpell(spell->getId())
|
|
|
|
|
+ && gameHandler->getRandomGenerator().nextInt(99) < winnerHero->valOfBonuses(BonusType::LEARN_BATTLE_SPELL_CHANCE))
|
|
|
|
|
+ {
|
|
|
|
|
+ resultsApplied.learnedSpells.spells.insert(spell->getId());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- gameHandler->sendAndApply(iw);
|
|
|
|
|
- gameHandler->sendAndApply(spells);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
// Artifacts handling
|
|
// Artifacts handling
|
|
|
- if(result == EBattleResult::NORMAL && !finishingBattle->isDraw() && finishingBattle->winnerHero)
|
|
|
|
|
|
|
+ if(result.result == EBattleResult::NORMAL && !finishingBattle->isDraw() && winnerHero)
|
|
|
{
|
|
{
|
|
|
- std::vector<const CArtifactInstance*> arts; // display them in window
|
|
|
|
|
- CArtifactFittingSet artFittingSet(*finishingBattle->winnerHero);
|
|
|
|
|
-
|
|
|
|
|
- const auto addArtifactToTransfer = [&artFittingSet, &arts](BulkMoveArtifacts & pack, const ArtifactPosition & srcSlot, const CArtifactInstance * art)
|
|
|
|
|
|
|
+ CArtifactFittingSet artFittingSet(*winnerHero);
|
|
|
|
|
+ const auto addArtifactToTransfer = [&artFittingSet](BulkMoveArtifacts & pack, const ArtifactPosition & srcSlot, const CArtifactInstance * art)
|
|
|
{
|
|
{
|
|
|
assert(art);
|
|
assert(art);
|
|
|
const auto dstSlot = ArtifactUtils::getArtAnyPosition(&artFittingSet, art->getTypeId());
|
|
const auto dstSlot = ArtifactUtils::getArtAnyPosition(&artFittingSet, art->getTypeId());
|
|
|
if(dstSlot != ArtifactPosition::PRE_FIRST)
|
|
if(dstSlot != ArtifactPosition::PRE_FIRST)
|
|
|
{
|
|
{
|
|
|
- pack.artsPack0.emplace_back(BulkMoveArtifacts::LinkedSlots(srcSlot, dstSlot));
|
|
|
|
|
|
|
+ pack.artsPack0.emplace_back(MoveArtifactInfo(srcSlot, dstSlot));
|
|
|
if(ArtifactUtils::isSlotEquipment(dstSlot))
|
|
if(ArtifactUtils::isSlotEquipment(dstSlot))
|
|
|
pack.artsPack0.back().askAssemble = true;
|
|
pack.artsPack0.back().askAssemble = true;
|
|
|
- arts.emplace_back(art);
|
|
|
|
|
artFittingSet.putArtifact(dstSlot, const_cast<CArtifactInstance*>(art));
|
|
artFittingSet.putArtifact(dstSlot, const_cast<CArtifactInstance*>(art));
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
- const auto sendArtifacts = [this](BulkMoveArtifacts & bma)
|
|
|
|
|
- {
|
|
|
|
|
- if(!bma.artsPack0.empty())
|
|
|
|
|
- gameHandler->sendAndApply(bma);
|
|
|
|
|
- };
|
|
|
|
|
|
|
|
|
|
- BulkMoveArtifacts packHero(finishingBattle->winnerHero->getOwner(), ObjectInstanceID::NONE, finishingBattle->winnerHero->id, false);
|
|
|
|
|
- if(finishingBattle->loserHero)
|
|
|
|
|
|
|
+ if(loserHero)
|
|
|
{
|
|
{
|
|
|
- packHero.srcArtHolder = finishingBattle->loserHero->id;
|
|
|
|
|
|
|
+ auto & packHero = resultsApplied.artifacts.emplace_back(finishingBattle->victor, finishingBattle->loserId, finishingBattle->winnerId, false);
|
|
|
|
|
+ packHero.srcArtHolder = finishingBattle->loserId;
|
|
|
for(const auto & slot : ArtifactUtils::commonWornSlots())
|
|
for(const auto & slot : ArtifactUtils::commonWornSlots())
|
|
|
{
|
|
{
|
|
|
- if(const auto artSlot = finishingBattle->loserHero->artifactsWorn.find(slot);
|
|
|
|
|
- artSlot != finishingBattle->loserHero->artifactsWorn.end() && ArtifactUtils::isArtRemovable(*artSlot))
|
|
|
|
|
|
|
+ if(const auto artSlot = loserHero->artifactsWorn.find(slot); artSlot != loserHero->artifactsWorn.end() && ArtifactUtils::isArtRemovable(*artSlot))
|
|
|
{
|
|
{
|
|
|
addArtifactToTransfer(packHero, artSlot->first, artSlot->second.getArt());
|
|
addArtifactToTransfer(packHero, artSlot->first, artSlot->second.getArt());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- for(const auto & artSlot : finishingBattle->loserHero->artifactsInBackpack)
|
|
|
|
|
|
|
+ for(const auto & artSlot : loserHero->artifactsInBackpack)
|
|
|
{
|
|
{
|
|
|
if(const auto art = artSlot.getArt(); art->getTypeId() != ArtifactID::GRAIL)
|
|
if(const auto art = artSlot.getArt(); art->getTypeId() != ArtifactID::GRAIL)
|
|
|
- addArtifactToTransfer(packHero, finishingBattle->loserHero->getArtPos(art), art);
|
|
|
|
|
|
|
+ addArtifactToTransfer(packHero, loserHero->getArtPos(art), art);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(finishingBattle->loserHero->commander)
|
|
|
|
|
|
|
+ if(loserHero->commander)
|
|
|
{
|
|
{
|
|
|
- BulkMoveArtifacts packCommander(finishingBattle->winnerHero->getOwner(), finishingBattle->loserHero->id, finishingBattle->winnerHero->id, false);
|
|
|
|
|
- packCommander.srcCreature = finishingBattle->loserHero->findStack(finishingBattle->loserHero->commander);
|
|
|
|
|
- for(const auto & artSlot : finishingBattle->loserHero->commander->artifactsWorn)
|
|
|
|
|
|
|
+ auto & packCommander = resultsApplied.artifacts.emplace_back(finishingBattle->victor, finishingBattle->loserId, finishingBattle->winnerId, false);
|
|
|
|
|
+ packCommander.srcCreature = loserHero->findStack(loserHero->commander);
|
|
|
|
|
+ for(const auto & artSlot : loserHero->commander->artifactsWorn)
|
|
|
addArtifactToTransfer(packCommander, artSlot.first, artSlot.second.getArt());
|
|
addArtifactToTransfer(packCommander, artSlot.first, artSlot.second.getArt());
|
|
|
- sendArtifacts(packCommander);
|
|
|
|
|
}
|
|
}
|
|
|
- auto armyObj = battle.battleGetArmyObject(battle.otherSide(battleResult->winner));
|
|
|
|
|
|
|
+ auto armyObj = dynamic_cast<const CArmedInstance*>(gameHandler->getObj(finishingBattle->loserId));
|
|
|
for(const auto & armySlot : armyObj->stacks)
|
|
for(const auto & armySlot : armyObj->stacks)
|
|
|
{
|
|
{
|
|
|
- BulkMoveArtifacts packsArmy(finishingBattle->winnerHero->getOwner(), finishingBattle->loserHero->id, finishingBattle->winnerHero->id, false);
|
|
|
|
|
|
|
+ auto & packsArmy = resultsApplied.artifacts.emplace_back(finishingBattle->victor, finishingBattle->loserId, finishingBattle->winnerId, false);
|
|
|
packsArmy.srcArtHolder = armyObj->id;
|
|
packsArmy.srcArtHolder = armyObj->id;
|
|
|
packsArmy.srcCreature = armySlot.first;
|
|
packsArmy.srcCreature = armySlot.first;
|
|
|
for(const auto & artSlot : armySlot.second->artifactsWorn)
|
|
for(const auto & artSlot : armySlot.second->artifactsWorn)
|
|
|
addArtifactToTransfer(packsArmy, artSlot.first, armySlot.second->getArt(artSlot.first));
|
|
addArtifactToTransfer(packsArmy, artSlot.first, armySlot.second->getArt(artSlot.first));
|
|
|
- sendArtifacts(packsArmy);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // Display loot
|
|
|
|
|
- if(!arts.empty())
|
|
|
|
|
- {
|
|
|
|
|
- InfoWindow iw;
|
|
|
|
|
- iw.player = finishingBattle->winnerHero->tempOwner;
|
|
|
|
|
- iw.text.appendLocalString(EMetaText::GENERAL_TXT, 30); //You have captured enemy artifact
|
|
|
|
|
-
|
|
|
|
|
- for(const auto art : arts) //TODO; separate function to display loot for various objects?
|
|
|
|
|
- {
|
|
|
|
|
- if(art->isScroll())
|
|
|
|
|
- iw.components.emplace_back(ComponentType::SPELL_SCROLL, art->getScrollSpellID());
|
|
|
|
|
- else
|
|
|
|
|
- iw.components.emplace_back(ComponentType::ARTIFACT, art->getTypeId());
|
|
|
|
|
-
|
|
|
|
|
- if(iw.components.size() >= GameConstants::INFO_WINDOW_ARTIFACTS_MAX_ITEMS)
|
|
|
|
|
- {
|
|
|
|
|
- gameHandler->sendAndApply(iw);
|
|
|
|
|
- iw.components.clear();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- gameHandler->sendAndApply(iw);
|
|
|
|
|
- }
|
|
|
|
|
- if(!packHero.artsPack0.empty())
|
|
|
|
|
- sendArtifacts(packHero);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Remove beaten hero
|
|
|
|
|
- if(finishingBattle->loserHero)
|
|
|
|
|
- {
|
|
|
|
|
- //add statistics
|
|
|
|
|
- if(!finishingBattle->isDraw())
|
|
|
|
|
- {
|
|
|
|
|
- ConstTransitivePtr<CGHeroInstance> strongestHero = nullptr;
|
|
|
|
|
- for(auto & hero : gameHandler->gameState()->getPlayerState(finishingBattle->loser)->getHeroes())
|
|
|
|
|
- if(!strongestHero || hero->exp > strongestHero->exp)
|
|
|
|
|
- strongestHero = hero;
|
|
|
|
|
- if(strongestHero->id == finishingBattle->loserHero->id && strongestHero->level > 5)
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->victor].lastDefeatedStrongestHeroDay = gameHandler->gameState()->getDate(Date::DAY);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- RemoveObject ro(finishingBattle->loserHero->id, finishingBattle->victor);
|
|
|
|
|
- gameHandler->sendAndApply(ro);
|
|
|
|
|
- }
|
|
|
|
|
- // For draw case both heroes should be removed
|
|
|
|
|
- if(finishingBattle->isDraw() && finishingBattle->winnerHero)
|
|
|
|
|
- {
|
|
|
|
|
- RemoveObject ro(finishingBattle->winnerHero->id, finishingBattle->loser);
|
|
|
|
|
- gameHandler->sendAndApply(ro);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // add statistic
|
|
|
|
|
- if(battle.sideToPlayer(BattleSide::ATTACKER) == PlayerColor::NEUTRAL || battle.sideToPlayer(BattleSide::DEFENDER) == PlayerColor::NEUTRAL)
|
|
|
|
|
- {
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::ATTACKER)].numBattlesNeutral++;
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::DEFENDER)].numBattlesNeutral++;
|
|
|
|
|
- if(!finishingBattle->isDraw())
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesNeutral++;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+ // Necromancy handling
|
|
|
|
|
+ // Give raised units to winner, if any were raised, units will be given after casualties are taken
|
|
|
|
|
+ if(winnerHero)
|
|
|
{
|
|
{
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::ATTACKER)].numBattlesPlayer++;
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(BattleSide::DEFENDER)].numBattlesPlayer++;
|
|
|
|
|
- if(!finishingBattle->isDraw())
|
|
|
|
|
- gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesPlayer++;
|
|
|
|
|
|
|
+ resultsApplied.raisedStack = winnerHero->calculateNecromancy(result);
|
|
|
|
|
+ const SlotID necroSlot = resultsApplied.raisedStack.getCreature() ? winnerHero->getSlotFor(resultsApplied.raisedStack.getCreature()) : SlotID();
|
|
|
|
|
+ if(necroSlot != SlotID() && !finishingBattle->isDraw())
|
|
|
|
|
+ gameHandler->addToSlot(StackLocation(finishingBattle->winnerId, necroSlot), resultsApplied.raisedStack.getCreature(), resultsApplied.raisedStack.count);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- BattleResultAccepted raccepted;
|
|
|
|
|
- raccepted.battleID = battle.getBattle()->getBattleID();
|
|
|
|
|
- raccepted.heroResult[BattleSide::ATTACKER].army = const_cast<CArmedInstance*>(battle.battleGetArmyObject(BattleSide::ATTACKER));
|
|
|
|
|
- raccepted.heroResult[BattleSide::DEFENDER].army = const_cast<CArmedInstance*>(battle.battleGetArmyObject(BattleSide::DEFENDER));
|
|
|
|
|
- raccepted.heroResult[BattleSide::ATTACKER].hero = const_cast<CGHeroInstance*>(battle.battleGetFightingHero(BattleSide::ATTACKER));
|
|
|
|
|
- raccepted.heroResult[BattleSide::DEFENDER].hero = const_cast<CGHeroInstance*>(battle.battleGetFightingHero(BattleSide::DEFENDER));
|
|
|
|
|
- raccepted.heroResult[BattleSide::ATTACKER].exp = battleResult->exp[BattleSide::ATTACKER];
|
|
|
|
|
- raccepted.heroResult[BattleSide::DEFENDER].exp = battleResult->exp[BattleSide::DEFENDER];
|
|
|
|
|
- raccepted.winnerSide = finishingBattle->winnerSide;
|
|
|
|
|
- gameHandler->sendAndApply(raccepted);
|
|
|
|
|
-
|
|
|
|
|
- gameHandler->queries->popIfTop(battleQuery);
|
|
|
|
|
- //--> continuation (battleAfterLevelUp) occurs after level-up gameHandler->queries are handled or on removing query
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void BattleResultProcessor::battleAfterLevelUp(const BattleID & battleID, const BattleResult & result)
|
|
|
|
|
-{
|
|
|
|
|
- LOG_TRACE(logGlobal);
|
|
|
|
|
-
|
|
|
|
|
- assert(finishingBattles.count(battleID) != 0);
|
|
|
|
|
- if(finishingBattles.count(battleID) == 0)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- auto & finishingBattle = finishingBattles[battleID];
|
|
|
|
|
-
|
|
|
|
|
- finishingBattle->remainingBattleQueriesCount--;
|
|
|
|
|
- logGlobal->trace("Decremented gameHandler->queries count to %d", finishingBattle->remainingBattleQueriesCount);
|
|
|
|
|
-
|
|
|
|
|
- if (finishingBattle->remainingBattleQueriesCount > 0)
|
|
|
|
|
- //Battle results will be handled when all battle gameHandler->queries are closed
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- //TODO consider if we really want it to work like above. ATM each player as unblocked as soon as possible
|
|
|
|
|
- // but the battle consequences are applied after final player is unblocked. Hard to abuse...
|
|
|
|
|
- // Still, it looks like a hole.
|
|
|
|
|
-
|
|
|
|
|
- // Necromancy if applicable.
|
|
|
|
|
- const CStackBasicDescriptor raisedStack = finishingBattle->winnerHero ? finishingBattle->winnerHero->calculateNecromancy(result) : CStackBasicDescriptor();
|
|
|
|
|
- // Give raised units to winner and show dialog, if any were raised,
|
|
|
|
|
- // units will be given after casualties are taken
|
|
|
|
|
- const SlotID necroSlot = raisedStack.getCreature() ? finishingBattle->winnerHero->getSlotFor(raisedStack.getCreature()) : SlotID();
|
|
|
|
|
-
|
|
|
|
|
- if (necroSlot != SlotID() && !finishingBattle->isDraw())
|
|
|
|
|
- {
|
|
|
|
|
- finishingBattle->winnerHero->showNecromancyDialog(raisedStack, gameHandler->getRandomGenerator());
|
|
|
|
|
- gameHandler->addToSlot(StackLocation(finishingBattle->winnerHero->id, necroSlot), raisedStack.getCreature(), raisedStack.count);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- BattleResultsApplied resultsApplied;
|
|
|
|
|
resultsApplied.battleID = battleID;
|
|
resultsApplied.battleID = battleID;
|
|
|
- resultsApplied.player1 = finishingBattle->victor;
|
|
|
|
|
- resultsApplied.player2 = finishingBattle->loser;
|
|
|
|
|
|
|
+ resultsApplied.victor = finishingBattle->victor;
|
|
|
|
|
+ resultsApplied.loser = finishingBattle->loser;
|
|
|
gameHandler->sendAndApply(resultsApplied);
|
|
gameHandler->sendAndApply(resultsApplied);
|
|
|
|
|
|
|
|
//handle victory/loss of engaged players
|
|
//handle victory/loss of engaged players
|
|
|
- std::set<PlayerColor> playerColors = {finishingBattle->loser, finishingBattle->victor};
|
|
|
|
|
- gameHandler->checkVictoryLossConditions(playerColors);
|
|
|
|
|
|
|
+ gameHandler->checkVictoryLossConditions({finishingBattle->loser, finishingBattle->victor});
|
|
|
|
|
|
|
|
if (result.result == EBattleResult::SURRENDER)
|
|
if (result.result == EBattleResult::SURRENDER)
|
|
|
{
|
|
{
|
|
|
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroSurrendered++;
|
|
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroSurrendered++;
|
|
|
- gameHandler->heroPool->onHeroSurrendered(finishingBattle->loser, finishingBattle->loserHero);
|
|
|
|
|
|
|
+ gameHandler->heroPool->onHeroSurrendered(finishingBattle->loser, loserHero);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (result.result == EBattleResult::ESCAPE)
|
|
if (result.result == EBattleResult::ESCAPE)
|
|
|
{
|
|
{
|
|
|
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroEscaped++;
|
|
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroEscaped++;
|
|
|
- gameHandler->heroPool->onHeroEscaped(finishingBattle->loser, finishingBattle->loserHero);
|
|
|
|
|
|
|
+ gameHandler->heroPool->onHeroEscaped(finishingBattle->loser, loserHero);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (result.winner != BattleSide::NONE && finishingBattle->winnerHero && finishingBattle->winnerHero->stacks.empty()
|
|
|
|
|
- && (!finishingBattle->winnerHero->commander || !finishingBattle->winnerHero->commander->alive))
|
|
|
|
|
|
|
+ // Remove beaten hero
|
|
|
|
|
+ if(loserHero)
|
|
|
{
|
|
{
|
|
|
- RemoveObject ro(finishingBattle->winnerHero->id, finishingBattle->winnerHero->getOwner());
|
|
|
|
|
|
|
+ RemoveObject ro(loserHero->id, finishingBattle->victor);
|
|
|
gameHandler->sendAndApply(ro);
|
|
gameHandler->sendAndApply(ro);
|
|
|
-
|
|
|
|
|
- if (gameHandler->getSettings().getBoolean(EGameSettings::HEROES_RETREAT_ON_WIN_WITHOUT_TROOPS))
|
|
|
|
|
- gameHandler->heroPool->onHeroEscaped(finishingBattle->victor, finishingBattle->winnerHero);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ // For draw case both heroes should be removed
|
|
|
|
|
+ if(finishingBattle->isDraw() && winnerHero)
|
|
|
|
|
+ {
|
|
|
|
|
+ RemoveObject ro(winnerHero->id, finishingBattle->loser);
|
|
|
|
|
+ gameHandler->sendAndApply(ro);
|
|
|
|
|
+ if(gameHandler->getSettings().getBoolean(EGameSettings::HEROES_RETREAT_ON_WIN_WITHOUT_TROOPS))
|
|
|
|
|
+ gameHandler->heroPool->onHeroEscaped(finishingBattle->victor, winnerHero);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
finishingBattles.erase(battleID);
|
|
finishingBattles.erase(battleID);
|