1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
/*
* BattleResultProcessor.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "BattleResultProcessor.h"
#include "../CGameHandler.h"
#include "../TurnTimerHandler.h"
#include "../processors/HeroPoolProcessor.h"
#include "../queries/QueriesProcessor.h"
#include "../queries/BattleQueries.h"
#include "../../lib/ArtifactUtils.h"
#include "../../lib/CStack.h"
#include "../../lib/CPlayerState.h"
#include "../../lib/IGameSettings.h"
#include "../../lib/battle/CBattleInfoCallback.h"
#include "../../lib/battle/IBattleState.h"
#include "../../lib/battle/SideInBattle.h"
#include "../../lib/gameState/CGameState.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/networkPacks/PacksForClientBattle.h"
#include "../../lib/networkPacks/PacksForClient.h"
#include "../../lib/spells/CSpellHandler.h"
#include <vstd/RNG.h>
BattleResultProcessor::BattleResultProcessor(BattleProcessor * owner, CGameHandler * newGameHandler)
// : owner(owner)
: gameHandler(newGameHandler)
{
}
CasualtiesAfterBattle::CasualtiesAfterBattle(const CBattleInfoCallback & battle, BattleSide sideInBattle):
army(battle.battleGetArmyObject(sideInBattle))
{
heroWithDeadCommander = ObjectInstanceID();
PlayerColor color = battle.sideToPlayer(sideInBattle);
auto allStacks = battle.battleGetStacksIf([color](const CStack * stack){
if (stack->summoned)//don't take into account temporary summoned stacks
return false;
if(stack->unitOwner() != color) //remove only our stacks
return false;
if (stack->isTurret())
return false;
return true;
});
for(const CStack * stConst : allStacks)
{
// Use const cast - in order to call non-const "takeResurrected" for proper calculation of casualties
// TODO: better solution
CStack * st = const_cast<CStack*>(stConst);
logGlobal->debug("Calculating casualties for %s", st->nodeName());
st->health.takeResurrected();
if(st->unitSlot() == SlotID::WAR_MACHINES_SLOT)
{
auto warMachine = st->unitType()->warMachine;
if(warMachine == ArtifactID::NONE)
{
logGlobal->error("Invalid creature in war machine virtual slot. Stack: %s", st->nodeName());
}
//catapult artifact remain even if "creature" killed in siege
else if(warMachine != ArtifactID::CATAPULT && st->getCount() <= 0)
{
logGlobal->debug("War machine has been destroyed");
auto hero = dynamic_cast<const CGHeroInstance*> (army);
if (hero)
removedWarMachines.push_back (ArtifactLocation(hero->id, hero->getArtPos(warMachine, true)));
else
logGlobal->error("War machine in army without hero");
}
}
else if(st->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
{
if(st->alive() && st->getCount() > 0)
{
logGlobal->debug("Permanently summoned %d units.", st->getCount());
const CreatureID summonedType = st->creatureId();
summoned[summonedType] += st->getCount();
}
}
else if(st->unitSlot() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
{
if (nullptr == st->base)
{
logGlobal->error("Stack with no base in commander slot. Stack: %s", st->nodeName());
}
else
{
auto c = dynamic_cast <const CCommanderInstance *>(st->base);
if(c)
{
auto h = dynamic_cast <const CGHeroInstance *>(army);
if(h && h->commander == c && (st->getCount() == 0 || !st->alive()))
{
logGlobal->debug("Commander is dead.");
heroWithDeadCommander = army->id; //TODO: unify commander handling
}
}
else
logGlobal->error("Stack with invalid instance in commander slot. Stack: %s", st->nodeName());
}
}
else if(st->base && !army->slotEmpty(st->unitSlot()))
{
logGlobal->debug("Count: %d; base count: %d", st->getCount(), army->getStackCount(st->unitSlot()));
if(st->getCount() == 0 || !st->alive())
{
logGlobal->debug("Stack has been destroyed.");
StackLocation sl(army, st->unitSlot());
newStackCounts.push_back(TStackAndItsNewCount(sl, 0));
}
else if(st->getCount() != army->getStackCount(st->unitSlot()))
{
logGlobal->debug("Stack size changed: %d -> %d units.", army->getStackCount(st->unitSlot()), st->getCount());
StackLocation sl(army, st->unitSlot());
newStackCounts.push_back(TStackAndItsNewCount(sl, st->getCount()));
}
}
else
{
logGlobal->warn("Unable to process stack: %s", st->nodeName());
}
}
}
void CasualtiesAfterBattle::updateArmy(CGameHandler *gh)
{
if (gh->getObjInstance(army->id) == nullptr)
throw std::runtime_error("Object " + army->getObjectName() + " is not on the map!");
for (TStackAndItsNewCount &ncount : newStackCounts)
{
if (ncount.second > 0)
gh->changeStackCount(ncount.first, ncount.second, true);
else
gh->eraseStack(ncount.first, true);
}
for (auto summoned_iter : summoned)
{
SlotID slot = army->getSlotFor(summoned_iter.first);
if (slot.validSlot())
{
StackLocation location(army, slot);
gh->addToSlot(location, summoned_iter.first.toCreature(), summoned_iter.second);
}
else
{
//even if it will be possible to summon anything permanently it should be checked for free slot
//necromancy is handled separately
gh->complain("No free slot to put summoned creature");
}
}
for (auto al : removedWarMachines)
{
gh->removeArtifact(al);
}
if (heroWithDeadCommander != ObjectInstanceID())
{
SetCommanderProperty scp;
scp.heroid = heroWithDeadCommander;
scp.which = SetCommanderProperty::ALIVE;
scp.amount = 0;
gh->sendAndApply(scp);
}
}
FinishingBattleHelper::FinishingBattleHelper(const CBattleInfoCallback & info, const BattleResult & result, int remainingBattleQueriesCount)
{
if (result.winner == BattleSide::ATTACKER)
{
winnerHero = info.getBattle()->getSideHero(BattleSide::ATTACKER);
loserHero = info.getBattle()->getSideHero(BattleSide::DEFENDER);
victor = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
loser = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
}
else
{
winnerHero = info.getBattle()->getSideHero(BattleSide::DEFENDER);
loserHero = info.getBattle()->getSideHero(BattleSide::ATTACKER);
victor = info.getBattle()->getSidePlayer(BattleSide::DEFENDER);
loser = info.getBattle()->getSidePlayer(BattleSide::ATTACKER);
}
winnerSide = result.winner;
this->remainingBattleQueriesCount = remainingBattleQueriesCount;
}
void BattleResultProcessor::endBattle(const CBattleInfoCallback & battle)
{
auto const & giveExp = [&battle](BattleResult &r)
{
if (r.winner == BattleSide::NONE)
{
// draw
return;
}
r.exp[BattleSide::ATTACKER] = 0;
r.exp[BattleSide::DEFENDER] = 0;
for (auto i = r.casualties[battle.otherSide(r.winner)].begin(); i!=r.casualties[battle.otherSide(r.winner)].end(); i++)
{
r.exp[r.winner] += VLC->creh->objects.at(i->first)->valOfBonuses(BonusType::STACK_HEALTH) * i->second;
}
};
LOG_TRACE(logGlobal);
auto * battleResult = battleResults.at(battle.getBattle()->getBattleID()).get();
const auto * heroAttacker = battle.battleGetFightingHero(BattleSide::ATTACKER);
const auto * heroDefender = battle.battleGetFightingHero(BattleSide::DEFENDER);
//Fill BattleResult structure with exp info
giveExp(*battleResult);
if (battleResult->result == EBattleResult::NORMAL) // give 500 exp for defeating hero, unless he escaped
{
if(heroAttacker)
battleResult->exp[BattleSide::DEFENDER] += 500;
if(heroDefender)
battleResult->exp[BattleSide::ATTACKER] += 500;
}
// Give 500 exp to winner if a town was conquered during the battle
const auto * defendedTown = battle.battleGetDefendedTown();
if (defendedTown && battleResult->winner == BattleSide::ATTACKER)
battleResult->exp[BattleSide::ATTACKER] += 500;
if(heroAttacker)
battleResult->exp[BattleSide::ATTACKER] = heroAttacker->calculateXp(battleResult->exp[BattleSide::ATTACKER]);//scholar skill
if(heroDefender)
battleResult->exp[BattleSide::DEFENDER] = heroDefender->calculateXp(battleResult->exp[BattleSide::DEFENDER]);
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle.sideToPlayer(BattleSide::ATTACKER)));
if(!battleQuery)
battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle.sideToPlayer(BattleSide::DEFENDER)));
if (!battleQuery)
{
logGlobal->error("Cannot find battle query!");
gameHandler->complain("Player " + boost::lexical_cast<std::string>(battle.sideToPlayer(BattleSide::ATTACKER)) + " has no battle query at the top!");
return;
}
battleQuery->result = std::make_optional(*battleResult);
//Check how many battle gameHandler->queries were created (number of players blocked by battle)
const int queriedPlayers = battleQuery ? (int)boost::count(gameHandler->queries->allQueries(), battleQuery) : 0;
assert(finishingBattles.count(battle.getBattle()->getBattleID()) == 0);
finishingBattles[battle.getBattle()->getBattleID()] = std::make_unique<FinishingBattleHelper>(battle, *battleResult, queriedPlayers);
// in battles against neutrals, 1st player can ask to replay battle manually
const auto * attackerPlayer = gameHandler->getPlayerState(battle.getBattle()->getSidePlayer(BattleSide::ATTACKER));
const auto * defenderPlayer = gameHandler->getPlayerState(battle.getBattle()->getSidePlayer(BattleSide::DEFENDER));
bool isAttackerHuman = attackerPlayer && attackerPlayer->isHuman();
bool isDefenderHuman = defenderPlayer && defenderPlayer->isHuman();
bool onlyOnePlayerHuman = isAttackerHuman != isDefenderHuman;
// in battles against neutrals attacker can ask to replay battle manually, additionally in battles against AI player human side can also ask for replay
if(onlyOnePlayerHuman)
{
auto battleDialogQuery = std::make_shared<CBattleDialogQuery>(gameHandler, battle.getBattle(), battleQuery->result);
battleResult->queryID = battleDialogQuery->queryID;
gameHandler->queries->addQuery(battleDialogQuery);
}
else
battleResult->queryID = QueryID::NONE;
//set same battle result for all gameHandler->queries
for(auto q : gameHandler->queries->allQueries())
{
auto otherBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(q);
if(otherBattleQuery && otherBattleQuery->battleID == battle.getBattle()->getBattleID())
otherBattleQuery->result = battleQuery->result;
}
gameHandler->turnTimerHandler->onBattleEnd(battle.getBattle()->getBattleID());
gameHandler->sendAndApply(*battleResult);
if (battleResult->queryID == QueryID::NONE)
endBattleConfirm(battle);
}
void BattleResultProcessor::endBattleConfirm(const CBattleInfoCallback & battle)
{
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle.sideToPlayer(BattleSide::ATTACKER)));
if(!battleQuery)
battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle.sideToPlayer(BattleSide::DEFENDER)));
if(!battleQuery)
{
logGlobal->trace("No battle query, battle end was confirmed by another player");
return;
}
auto * battleResult = battleResults.at(battle.getBattle()->getBattleID()).get();
auto * finishingBattle = finishingBattles.at(battle.getBattle()->getBattleID()).get();
const EBattleResult result = battleResult->result;
//calculate casualties before deleting battle
CasualtiesAfterBattle cab1(battle, BattleSide::ATTACKER);
CasualtiesAfterBattle cab2(battle, BattleSide::DEFENDER);
cab1.updateArmy(gameHandler);
cab2.updateArmy(gameHandler); //take casualties after battle is deleted
if(battleResult->winner == BattleSide::DEFENDER
&& finishingBattle->winnerHero
&& finishingBattle->winnerHero->visitedTown
&& !finishingBattle->winnerHero->inTownGarrison
&& finishingBattle->winnerHero->visitedTown->garrisonHero == finishingBattle->winnerHero)
{
gameHandler->swapGarrisonOnSiege(finishingBattle->winnerHero->visitedTown->id); //return defending visitor from garrison to its rightful place
}
//give exp
if(!finishingBattle->isDraw() && battleResult->exp[finishingBattle->winnerSide] && finishingBattle->winnerHero)
gameHandler->giveExperience(finishingBattle->winnerHero, battleResult->exp[finishingBattle->winnerSide]);
// Eagle Eye handling
if(!finishingBattle->isDraw() && finishingBattle->winnerHero)
{
ChangeSpells spells;
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(VLC->spells());
if(spell
&& spell->getLevel() <= eagleEyeLevel
&& !finishingBattle->winnerHero->spellbookContainsSpell(spell->getId())
&& gameHandler->getRandomGenerator().nextInt(99) < eagleEyeChance)
{
spells.spells.insert(spell->getId());
}
}
}
if(!spells.spells.empty())
{
spells.learn = 1;
spells.hid = finishingBattle->winnerHero->id;
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());
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 << ".";
iw.text.replaceRawString(names.str());
auto it = spells.spells.begin();
for(int i = 0; i < spells.spells.size(); i++, it++)
{
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);
}
gameHandler->sendAndApply(iw);
gameHandler->sendAndApply(spells);
}
}
// Artifacts handling
if(result == EBattleResult::NORMAL && !finishingBattle->isDraw() && finishingBattle->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)
{
assert(art);
const auto dstSlot = ArtifactUtils::getArtAnyPosition(&artFittingSet, art->getTypeId());
if(dstSlot != ArtifactPosition::PRE_FIRST)
{
pack.artsPack0.emplace_back(BulkMoveArtifacts::LinkedSlots(srcSlot, dstSlot));
if(ArtifactUtils::isSlotEquipment(dstSlot))
pack.artsPack0.back().askAssemble = true;
arts.emplace_back(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)
{
packHero.srcArtHolder = finishingBattle->loserHero->id;
for(const auto & slot : ArtifactUtils::commonWornSlots())
{
if(const auto artSlot = finishingBattle->loserHero->artifactsWorn.find(slot);
artSlot != finishingBattle->loserHero->artifactsWorn.end() && ArtifactUtils::isArtRemovable(*artSlot))
{
addArtifactToTransfer(packHero, artSlot->first, artSlot->second.getArt());
}
}
for(const auto & artSlot : finishingBattle->loserHero->artifactsInBackpack)
{
if(const auto art = artSlot.getArt(); art->getTypeId() != ArtifactID::GRAIL)
addArtifactToTransfer(packHero, finishingBattle->loserHero->getArtPos(art), art);
}
if(finishingBattle->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)
addArtifactToTransfer(packCommander, artSlot.first, artSlot.second.getArt());
sendArtifacts(packCommander);
}
auto armyObj = battle.battleGetArmyObject(battle.otherSide(battleResult->winner));
for(const auto & armySlot : armyObj->stacks)
{
BulkMoveArtifacts packsArmy(finishingBattle->winnerHero->getOwner(), finishingBattle->loserHero->id, finishingBattle->winnerHero->id, false);
packsArmy.srcArtHolder = armyObj->id;
packsArmy.srcCreature = armySlot.first;
for(const auto & artSlot : armySlot.second->artifactsWorn)
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
{
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++;
}
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, necroSlot), raisedStack.getCreature(), raisedStack.count);
}
BattleResultsApplied resultsApplied;
resultsApplied.battleID = battleID;
resultsApplied.player1 = finishingBattle->victor;
resultsApplied.player2 = finishingBattle->loser;
gameHandler->sendAndApply(resultsApplied);
//handle victory/loss of engaged players
std::set<PlayerColor> playerColors = {finishingBattle->loser, finishingBattle->victor};
gameHandler->checkVictoryLossConditions(playerColors);
if (result.result == EBattleResult::SURRENDER)
{
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroSurrendered++;
gameHandler->heroPool->onHeroSurrendered(finishingBattle->loser, finishingBattle->loserHero);
}
if (result.result == EBattleResult::ESCAPE)
{
gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroEscaped++;
gameHandler->heroPool->onHeroEscaped(finishingBattle->loser, finishingBattle->loserHero);
}
if (result.winner != BattleSide::NONE && finishingBattle->winnerHero && finishingBattle->winnerHero->stacks.empty()
&& (!finishingBattle->winnerHero->commander || !finishingBattle->winnerHero->commander->alive))
{
RemoveObject ro(finishingBattle->winnerHero->id, finishingBattle->winnerHero->getOwner());
gameHandler->sendAndApply(ro);
if (gameHandler->getSettings().getBoolean(EGameSettings::HEROES_RETREAT_ON_WIN_WITHOUT_TROOPS))
gameHandler->heroPool->onHeroEscaped(finishingBattle->victor, finishingBattle->winnerHero);
}
finishingBattles.erase(battleID);
battleResults.erase(battleID);
}
void BattleResultProcessor::setBattleResult(const CBattleInfoCallback & battle, EBattleResult resultType, BattleSide victoriusSide)
{
assert(battleResults.count(battle.getBattle()->getBattleID()) == 0);
battleResults[battle.getBattle()->getBattleID()] = std::make_unique<BattleResult>();
auto & battleResult = battleResults[battle.getBattle()->getBattleID()];
battleResult->battleID = battle.getBattle()->getBattleID();
battleResult->result = resultType;
battleResult->winner = victoriusSide; //surrendering side loses
auto allStacks = battle.battleGetStacksIf([](const CStack * stack){
if (stack->summoned)//don't take into account temporary summoned stacks
return false;
if (stack->isTurret())
return false;
return true;
});
for(const auto & st : allStacks) //setting casualties
{
si32 killed = st->getKilled();
if(killed > 0)
battleResult->casualties[st->unitSide()][st->creatureId()] += killed;
}
}
bool BattleResultProcessor::battleIsEnding(const CBattleInfoCallback & battle) const
{
return battleResults.count(battle.getBattle()->getBattleID()) != 0;
}
|