2023-07-23 23:00:37 +02:00
|
|
|
/*
|
|
|
|
* BattleProcessor.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 "BattleProcessor.h"
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
#include "BattleActionProcessor.h"
|
|
|
|
#include "BattleFlowProcessor.h"
|
|
|
|
#include "BattleResultProcessor.h"
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
#include "../CGameHandler.h"
|
|
|
|
#include "../CVCMIServer.h"
|
|
|
|
#include "../processors/HeroPoolProcessor.h"
|
|
|
|
#include "../queries/QueriesProcessor.h"
|
|
|
|
#include "../queries/BattleQueries.h"
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
#include "../../lib/ArtifactUtils.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/CStack.h"
|
|
|
|
#include "../../lib/CondSh.h"
|
|
|
|
#include "../../lib/GameSettings.h"
|
|
|
|
#include "../../lib/ScopeGuard.h"
|
|
|
|
#include "../../lib/TerrainHandler.h"
|
|
|
|
#include "../../lib/UnlockGuard.h"
|
|
|
|
#include "../../lib/battle/BattleInfo.h"
|
|
|
|
#include "../../lib/battle/CUnitState.h"
|
|
|
|
#include "../../lib/gameState/CGameState.h"
|
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
|
|
|
#include "../../lib/mapping/CMap.h"
|
|
|
|
#include "../../lib/modding/IdentifierStorage.h"
|
|
|
|
#include "../../lib/serializer/Cast.h"
|
|
|
|
#include "../../lib/spells/AbilityCaster.h"
|
|
|
|
#include "../../lib/spells/BonusCaster.h"
|
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
|
|
|
#include "../../lib/spells/ISpellMechanics.h"
|
|
|
|
#include "../../lib/spells/ObstacleCasterProxy.h"
|
|
|
|
#include "../../lib/spells/Problem.h"
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
#define COMPLAIN_RET_IF(cond, txt) do {if (cond){gameHandler->complain(txt); return;}} while(0)
|
|
|
|
#define COMPLAIN_RET_FALSE_IF(cond, txt) do {if (cond){gameHandler->complain(txt); return false;}} while(0)
|
|
|
|
#define COMPLAIN_RET(txt) {gameHandler->complain(txt); return false;}
|
|
|
|
#define COMPLAIN_RETF(txt, FORMAT) {gameHandler->complain(boost::str(boost::format(txt) % FORMAT)); return false;}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
BattleProcessor::BattleProcessor(CGameHandler * gameHandler)
|
|
|
|
: gameHandler(gameHandler)
|
|
|
|
, flowProcessor(std::make_unique<BattleFlowProcessor>(this))
|
|
|
|
, actionsProcessor(std::make_unique<BattleActionProcessor>(this))
|
|
|
|
, resultProcessor(std::make_unique<BattleResultProcessor>(this))
|
|
|
|
{
|
|
|
|
setGameHandler(gameHandler);
|
|
|
|
}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
BattleProcessor::BattleProcessor():
|
|
|
|
BattleProcessor(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
BattleProcessor::~BattleProcessor() = default;
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
void BattleProcessor::engageIntoBattle(PlayerColor player)
|
|
|
|
{
|
|
|
|
//notify interfaces
|
|
|
|
PlayerBlocked pb;
|
|
|
|
pb.player = player;
|
|
|
|
pb.reason = PlayerBlocked::UPCOMING_BATTLE;
|
|
|
|
pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
|
|
|
|
gameHandler->sendAndApply(&pb);
|
|
|
|
}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
void BattleProcessor::startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
|
|
|
|
const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
|
|
|
|
const CGTownInstance *town) //use hero=nullptr for no hero
|
|
|
|
{
|
|
|
|
if(gameHandler->gameState()->curB)
|
|
|
|
gameHandler->gameState()->curB.dellNull();
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
engageIntoBattle(army1->tempOwner);
|
|
|
|
engageIntoBattle(army2->tempOwner);
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
static const CArmedInstance *armies[2];
|
|
|
|
armies[0] = army1;
|
|
|
|
armies[1] = army2;
|
|
|
|
static const CGHeroInstance*heroes[2];
|
|
|
|
heroes[0] = hero1;
|
|
|
|
heroes[1] = hero2;
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
setupBattle(tile, armies, heroes, creatureBank, town); //initializes stacks, places creatures on battlefield, blocks and informs player interfaces
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(gameHandler->gameState()->curB->sides[0].color));
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
//existing battle query for retying auto-combat
|
|
|
|
if(lastBattleQuery)
|
|
|
|
{
|
|
|
|
for(int i : {0, 1})
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
if(heroes[i])
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
SetMana restoreInitialMana;
|
|
|
|
restoreInitialMana.val = lastBattleQuery->initialHeroMana[i];
|
|
|
|
restoreInitialMana.hid = heroes[i]->id;
|
|
|
|
gameHandler->sendAndApply(&restoreInitialMana);
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
lastBattleQuery->bi = gameHandler->gameState()->curB;
|
|
|
|
lastBattleQuery->result = std::nullopt;
|
|
|
|
lastBattleQuery->belligerents[0] = gameHandler->gameState()->curB->sides[0].armyObject;
|
|
|
|
lastBattleQuery->belligerents[1] = gameHandler->gameState()->curB->sides[1].armyObject;
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
auto nextBattleQuery = std::make_shared<CBattleQuery>(gameHandler, gameHandler->gameState()->curB);
|
|
|
|
for(int i : {0, 1})
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
if(heroes[i])
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
nextBattleQuery->initialHeroMana[i] = heroes[i]->mana;
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-14 18:46:42 +02:00
|
|
|
gameHandler->queries->addQuery(nextBattleQuery);
|
|
|
|
|
|
|
|
flowProcessor->onBattleStarted();
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank)
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
startBattlePrimary(army1, army2, tile,
|
|
|
|
army1->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army1) : nullptr,
|
|
|
|
army2->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army2) : nullptr,
|
|
|
|
creatureBank);
|
|
|
|
}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank)
|
|
|
|
{
|
|
|
|
startBattleI(army1, army2, army2->visitablePos(), creatureBank);
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
void BattleProcessor::setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town)
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
const auto & t = *gameHandler->getTile(tile);
|
|
|
|
TerrainId terrain = t.terType->getId();
|
|
|
|
if (gameHandler->gameState()->map->isCoastalTile(tile)) //coastal tile is always ground
|
|
|
|
terrain = ETerrainId::SAND;
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
BattleField terType = gameHandler->gameState()->battleGetBattlefieldType(tile, gameHandler->getRandomGenerator());
|
|
|
|
if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat)
|
|
|
|
terType = BattleField(*VLC->identifiers()->getIdentifier("core", "battlefield.ship_to_ship"));
|
2023-07-23 23:00:37 +02:00
|
|
|
|
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
//send info about battles
|
|
|
|
BattleStart bs;
|
|
|
|
bs.info = BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, creatureBank, town);
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
engageIntoBattle(bs.info->sides[0].color);
|
|
|
|
engageIntoBattle(bs.info->sides[1].color);
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->sides[0].color));
|
|
|
|
bs.info->replayAllowed = lastBattleQuery == nullptr && !bs.info->sides[1].color.isValidPlayer();
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-14 18:46:42 +02:00
|
|
|
gameHandler->sendAndApply(&bs);
|
|
|
|
}
|
2023-07-23 23:00:37 +02:00
|
|
|
|
2023-08-17 14:18:16 +02:00
|
|
|
bool BattleProcessor::checkBattleStateChanges()
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
//check if drawbridge state need to be changes
|
|
|
|
if (gameHandler->battleGetSiegeLevel() > 0)
|
|
|
|
updateGateState();
|
|
|
|
|
|
|
|
//check if battle ended
|
|
|
|
if (auto result = gameHandler->battleIsFinished())
|
2023-07-23 23:00:37 +02:00
|
|
|
{
|
2023-08-14 18:46:42 +02:00
|
|
|
setBattleResult(EBattleResult::NORMAL, *result);
|
2023-08-17 14:18:16 +02:00
|
|
|
return true;
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
2023-08-17 14:18:16 +02:00
|
|
|
|
|
|
|
return false;
|
2023-07-23 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleProcessor::updateGateState()
|
|
|
|
{
|
|
|
|
// GATE_BRIDGE - leftmost tile, located over moat
|
|
|
|
// GATE_OUTER - central tile, mostly covered by gate image
|
|
|
|
// GATE_INNER - rightmost tile, inside the walls
|
|
|
|
|
|
|
|
// GATE_OUTER or GATE_INNER:
|
|
|
|
// - if defender moves unit on these tiles, bridge will open
|
|
|
|
// - if there is a creature (dead or alive) on these tiles, bridge will always remain open
|
|
|
|
// - blocked to attacker if bridge is closed
|
|
|
|
|
|
|
|
// GATE_BRIDGE
|
|
|
|
// - if there is a unit or corpse here, bridge can't open (and can't close in fortress)
|
|
|
|
// - if Force Field is cast here, bridge can't open (but can close, in any town)
|
|
|
|
// - deals moat damage to attacker if bridge is closed (fortress only)
|
|
|
|
|
|
|
|
bool hasForceFieldOnBridge = !gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), true).empty();
|
|
|
|
bool hasStackAtGateInner = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_INNER), false) != nullptr;
|
|
|
|
bool hasStackAtGateOuter = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_OUTER), false) != nullptr;
|
|
|
|
bool hasStackAtGateBridge = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_BRIDGE), false) != nullptr;
|
|
|
|
bool hasWideMoat = vstd::contains_if(gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), false), [](const std::shared_ptr<const CObstacleInstance> & obst)
|
|
|
|
{
|
|
|
|
return obst->obstacleType == CObstacleInstance::MOAT;
|
|
|
|
});
|
|
|
|
|
|
|
|
BattleUpdateGateState db;
|
|
|
|
db.state = gameHandler->gameState()->curB->si.gateState;
|
|
|
|
if (gameHandler->gameState()->curB->si.wallState[EWallPart::GATE] == EWallState::DESTROYED)
|
|
|
|
{
|
|
|
|
db.state = EGateState::DESTROYED;
|
|
|
|
}
|
|
|
|
else if (db.state == EGateState::OPENED)
|
|
|
|
{
|
|
|
|
bool hasStackOnLongBridge = hasStackAtGateBridge && hasWideMoat;
|
|
|
|
bool gateCanClose = !hasStackAtGateInner && !hasStackAtGateOuter && !hasStackOnLongBridge;
|
|
|
|
|
|
|
|
if (gateCanClose)
|
|
|
|
db.state = EGateState::CLOSED;
|
|
|
|
else
|
|
|
|
db.state = EGateState::OPENED;
|
|
|
|
}
|
|
|
|
else // CLOSED or BLOCKED
|
|
|
|
{
|
|
|
|
bool gateBlocked = hasForceFieldOnBridge || hasStackAtGateBridge;
|
|
|
|
|
|
|
|
if (gateBlocked)
|
|
|
|
db.state = EGateState::BLOCKED;
|
|
|
|
else
|
|
|
|
db.state = EGateState::CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db.state != gameHandler->gameState()->curB->si.gateState)
|
|
|
|
gameHandler->sendAndApply(&db);
|
|
|
|
}
|
|
|
|
|
2023-08-13 22:08:53 +02:00
|
|
|
bool BattleProcessor::makeBattleAction(PlayerColor player, BattleAction &ba)
|
|
|
|
{
|
|
|
|
const BattleInfo * b = gameHandler->gameState()->curB;
|
|
|
|
|
|
|
|
if(!b && gameHandler->complain("Can not make action - there is no battle ongoing!"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ba.side != 0 && ba.side != 1 && gameHandler->complain("Can not make action - invalid battle side!"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(b->tacticDistance)
|
|
|
|
{
|
|
|
|
if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
|
|
|
|
&& ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
|
|
|
|
{
|
|
|
|
gameHandler->complain("Can not make actions while in tactics mode!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(player != b->sides[ba.side].color)
|
|
|
|
{
|
|
|
|
gameHandler->complain("Can not make actions in battles you are not part of!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto active = b->battleActiveUnit();
|
|
|
|
if(!active && gameHandler->complain("No active unit in battle!"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto unitOwner = b->battleGetOwner(active);
|
|
|
|
|
|
|
|
if(player != unitOwner && gameHandler->complain("Can not make actions in battles you are not part of!"))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:51:50 +02:00
|
|
|
return makeBattleAction(ba);
|
2023-08-14 18:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleProcessor::setBattleResult(EBattleResult resultType, int victoriusSide)
|
|
|
|
{
|
|
|
|
resultProcessor->setBattleResult(resultType, victoriusSide);
|
2023-08-16 23:51:50 +02:00
|
|
|
resultProcessor->endBattle(gameHandler->gameState()->curB->tile, gameHandler->gameState()->curB->battleGetFightingHero(0), gameHandler->gameState()->curB->battleGetFightingHero(1));
|
2023-08-14 18:46:42 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 23:51:50 +02:00
|
|
|
bool BattleProcessor::makeBattleAction(const BattleAction &ba)
|
2023-08-14 18:46:42 +02:00
|
|
|
{
|
2023-08-16 23:51:50 +02:00
|
|
|
bool result = actionsProcessor->makeBattleAction(ba);
|
|
|
|
flowProcessor->onActionMade(ba);
|
|
|
|
return result;
|
2023-08-14 18:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BattleProcessor::endBattleConfirm(const BattleInfo * battleInfo)
|
|
|
|
{
|
|
|
|
resultProcessor->endBattleConfirm(battleInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleProcessor::battleAfterLevelUp(const BattleResult &result)
|
|
|
|
{
|
|
|
|
resultProcessor->battleAfterLevelUp(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleProcessor::setGameHandler(CGameHandler * newGameHandler)
|
|
|
|
{
|
|
|
|
gameHandler = newGameHandler;
|
|
|
|
|
|
|
|
actionsProcessor->setGameHandler(newGameHandler);
|
|
|
|
flowProcessor->setGameHandler(newGameHandler);
|
|
|
|
resultProcessor->setGameHandler(newGameHandler);
|
2023-08-13 22:08:53 +02:00
|
|
|
}
|