mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-29 23:07:48 +02:00
Unified handling of battle sides ID's
- Replaced BattleSide namespace-enum with enum class - Merged two different BattleSide enum's into one - Merged BattlePerspective enum into BattleSide enum - Changed all places that use integers to represent battle side to use BattleSide enum - Added BattleSideArray convenience wrapper for std::array that is always 2-elements in size and allows access to its elements using BattleSide enum
This commit is contained in:
@@ -27,10 +27,20 @@
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
///BattleInfo
|
||||
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackInstance & base, ui8 side, const SlotID & slot, BattleHex position)
|
||||
const SideInBattle & BattleInfo::getSide(BattleSide side) const
|
||||
{
|
||||
PlayerColor owner = sides[side].color;
|
||||
return sides.at(side);
|
||||
}
|
||||
|
||||
SideInBattle & BattleInfo::getSide(BattleSide side)
|
||||
{
|
||||
return sides.at(side);
|
||||
}
|
||||
|
||||
///BattleInfo
|
||||
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackInstance & base, BattleSide side, const SlotID & slot, BattleHex position)
|
||||
{
|
||||
PlayerColor owner = getSide(side).color;
|
||||
assert(!owner.isValidPlayer() || (base.armyObj && base.armyObj->tempOwner == owner));
|
||||
|
||||
auto * ret = new CStack(&base, owner, id, side, slot);
|
||||
@@ -39,9 +49,9 @@ CStack * BattleInfo::generateNewStack(uint32_t id, const CStackInstance & base,
|
||||
return ret;
|
||||
}
|
||||
|
||||
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackBasicDescriptor & base, ui8 side, const SlotID & slot, BattleHex position)
|
||||
CStack * BattleInfo::generateNewStack(uint32_t id, const CStackBasicDescriptor & base, BattleSide side, const SlotID & slot, BattleHex position)
|
||||
{
|
||||
PlayerColor owner = sides[side].color;
|
||||
PlayerColor owner = getSide(side).color;
|
||||
auto * ret = new CStack(&base, owner, id, side, slot);
|
||||
ret->initialPosition = position;
|
||||
stacks.push_back(ret);
|
||||
@@ -50,7 +60,7 @@ CStack * BattleInfo::generateNewStack(uint32_t id, const CStackBasicDescriptor &
|
||||
|
||||
void BattleInfo::localInit()
|
||||
{
|
||||
for(int i = 0; i < 2; i++)
|
||||
for(BattleSide i : { BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
{
|
||||
auto * armyObj = battleGetArmyObject(i);
|
||||
armyObj->battle = this;
|
||||
@@ -162,12 +172,12 @@ struct RangeGenerator
|
||||
std::function<int()> myRand;
|
||||
};
|
||||
|
||||
BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const BattleField & battlefieldType, const CArmedInstance * armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance * town)
|
||||
BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const BattleField & battlefieldType, BattleSideArray<const CArmedInstance *> armies, BattleSideArray<const CGHeroInstance *> heroes, bool creatureBank, const CGTownInstance * town)
|
||||
{
|
||||
CMP_stack cmpst;
|
||||
auto * curB = new BattleInfo();
|
||||
|
||||
for(auto i = 0u; i < curB->sides.size(); i++)
|
||||
for(auto i : { BattleSide::LEFT_SIDE, BattleSide::RIGHT_SIDE})
|
||||
curB->sides[i].init(heroes[i], armies[i]);
|
||||
|
||||
|
||||
@@ -315,36 +325,32 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
|
||||
|
||||
//reading battleStartpos - add creatures AFTER random obstacles are generated
|
||||
//TODO: parse once to some structure
|
||||
std::vector<std::vector<int>> looseFormations[2];
|
||||
std::vector<std::vector<int>> tightFormations[2];
|
||||
std::vector<std::vector<int>> creBankFormations[2];
|
||||
std::vector<int> commanderField;
|
||||
std::vector<int> commanderBank;
|
||||
BattleSideArray<std::vector<std::vector<int>>> looseFormations;
|
||||
BattleSideArray<std::vector<std::vector<int>>> tightFormations;
|
||||
BattleSideArray<std::vector<std::vector<int>>> creBankFormations;
|
||||
BattleSideArray<int> commanderField;
|
||||
BattleSideArray<int> commanderBank;
|
||||
const JsonNode config(JsonPath::builtin("config/battleStartpos.json"));
|
||||
const JsonVector &positions = config["battle_positions"].Vector();
|
||||
|
||||
CGH::readBattlePositions(positions[0]["levels"], looseFormations[0]);
|
||||
CGH::readBattlePositions(positions[1]["levels"], looseFormations[1]);
|
||||
CGH::readBattlePositions(positions[2]["levels"], tightFormations[0]);
|
||||
CGH::readBattlePositions(positions[3]["levels"], tightFormations[1]);
|
||||
CGH::readBattlePositions(positions[4]["levels"], creBankFormations[0]);
|
||||
CGH::readBattlePositions(positions[5]["levels"], creBankFormations[1]);
|
||||
CGH::readBattlePositions(positions[0]["levels"], looseFormations[BattleSide::ATTACKER]);
|
||||
CGH::readBattlePositions(positions[1]["levels"], looseFormations[BattleSide::DEFENDER]);
|
||||
CGH::readBattlePositions(positions[2]["levels"], tightFormations[BattleSide::ATTACKER]);
|
||||
CGH::readBattlePositions(positions[3]["levels"], tightFormations[BattleSide::DEFENDER]);
|
||||
CGH::readBattlePositions(positions[4]["levels"], creBankFormations[BattleSide::ATTACKER]);
|
||||
CGH::readBattlePositions(positions[5]["levels"], creBankFormations[BattleSide::DEFENDER]);
|
||||
|
||||
for (auto position : config["commanderPositions"]["field"].Vector())
|
||||
{
|
||||
commanderField.push_back(static_cast<int>(position.Float()));
|
||||
}
|
||||
for (auto position : config["commanderPositions"]["creBank"].Vector())
|
||||
{
|
||||
commanderBank.push_back(static_cast<int>(position.Float()));
|
||||
}
|
||||
commanderField[BattleSide::ATTACKER] = config["commanderPositions"]["field"][0].Integer();
|
||||
commanderField[BattleSide::DEFENDER] = config["commanderPositions"]["field"][1].Integer();
|
||||
|
||||
commanderBank[BattleSide::ATTACKER] = config["commanderPositions"]["creBank"][0].Integer();
|
||||
commanderBank[BattleSide::DEFENDER] = config["commanderPositions"]["creBank"][1].Integer();
|
||||
|
||||
//adding war machines
|
||||
if(!creatureBank)
|
||||
{
|
||||
//Checks if hero has artifact and create appropriate stack
|
||||
auto handleWarMachine = [&](int side, const ArtifactPosition & artslot, BattleHex hex)
|
||||
auto handleWarMachine = [&](BattleSide side, const ArtifactPosition & artslot, BattleHex hex)
|
||||
{
|
||||
const CArtifactInstance * warMachineArt = heroes[side]->getArt(artslot);
|
||||
|
||||
@@ -357,28 +363,28 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
|
||||
}
|
||||
};
|
||||
|
||||
if(heroes[0])
|
||||
if(heroes[BattleSide::ATTACKER])
|
||||
{
|
||||
|
||||
handleWarMachine(0, ArtifactPosition::MACH1, 52);
|
||||
handleWarMachine(0, ArtifactPosition::MACH2, 18);
|
||||
handleWarMachine(0, ArtifactPosition::MACH3, 154);
|
||||
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH1, 52);
|
||||
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH2, 18);
|
||||
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH3, 154);
|
||||
if(town && town->hasFort())
|
||||
handleWarMachine(0, ArtifactPosition::MACH4, 120);
|
||||
handleWarMachine(BattleSide::ATTACKER, ArtifactPosition::MACH4, 120);
|
||||
}
|
||||
|
||||
if(heroes[1])
|
||||
if(heroes[BattleSide::DEFENDER])
|
||||
{
|
||||
if(!town) //defending hero shouldn't receive ballista (bug #551)
|
||||
handleWarMachine(1, ArtifactPosition::MACH1, 66);
|
||||
handleWarMachine(1, ArtifactPosition::MACH2, 32);
|
||||
handleWarMachine(1, ArtifactPosition::MACH3, 168);
|
||||
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH1, 66);
|
||||
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH2, 32);
|
||||
handleWarMachine(BattleSide::DEFENDER, ArtifactPosition::MACH3, 168);
|
||||
}
|
||||
}
|
||||
//war machines added
|
||||
|
||||
//battleStartpos read
|
||||
for(int side = 0; side < 2; side++)
|
||||
for(BattleSide side : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
{
|
||||
int formationNo = armies[side]->stacksCount() - 1;
|
||||
vstd::abetween(formationNo, 0, GameConstants::ARMY_SIZE - 1);
|
||||
@@ -397,14 +403,14 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
|
||||
|
||||
BattleHex pos = (k < formationVector->size() ? formationVector->at(k) : 0);
|
||||
if(creatureBank && i->second->type->isDoubleWide())
|
||||
pos += side ? BattleHex::LEFT : BattleHex::RIGHT;
|
||||
pos += side == BattleSide::RIGHT_SIDE ? BattleHex::LEFT : BattleHex::RIGHT;
|
||||
|
||||
curB->generateNewStack(curB->nextUnitId(), *i->second, side, i->first, pos);
|
||||
}
|
||||
}
|
||||
|
||||
//adding commanders
|
||||
for (int i = 0; i < 2; ++i)
|
||||
for(BattleSide i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
{
|
||||
if (heroes[i] && heroes[i]->commander && heroes[i]->commander->alive)
|
||||
{
|
||||
@@ -416,14 +422,14 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
|
||||
if (curB->town && curB->town->fortLevel() >= CGTownInstance::CITADEL)
|
||||
{
|
||||
// keep tower
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_CENTRAL_TOWER);
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_CENTRAL_TOWER);
|
||||
|
||||
if (curB->town->fortLevel() >= CGTownInstance::CASTLE)
|
||||
{
|
||||
// lower tower + upper tower
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_UPPER_TOWER);
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_UPPER_TOWER);
|
||||
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), 1, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_BOTTOM_TOWER);
|
||||
curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(CreatureID::ARROW_TOWERS, 1), BattleSide::DEFENDER, SlotID::ARROW_TOWERS_SLOT, BattleHex::CASTLE_BOTTOM_TOWER);
|
||||
}
|
||||
|
||||
//Moat generating is done on server
|
||||
@@ -453,11 +459,9 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
|
||||
//tactics
|
||||
bool isTacticsAllowed = !creatureBank; //no tactics in creature banks
|
||||
|
||||
constexpr int sideSize = 2;
|
||||
|
||||
std::array<int, sideSize> battleRepositionHex = {};
|
||||
std::array<int, sideSize> battleRepositionHexBlock = {};
|
||||
for(int i = 0; i < sideSize; i++)
|
||||
BattleSideArray<int> battleRepositionHex = {};
|
||||
BattleSideArray<int> battleRepositionHexBlock = {};
|
||||
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
{
|
||||
if(heroes[i])
|
||||
{
|
||||
@@ -508,14 +512,14 @@ const CGHeroInstance * BattleInfo::getHero(const PlayerColor & player) const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ui8 BattleInfo::whatSide(const PlayerColor & player) const
|
||||
BattleSide BattleInfo::whatSide(const PlayerColor & player) const
|
||||
{
|
||||
for(int i = 0; i < sides.size(); i++)
|
||||
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
if(sides[i].color == player)
|
||||
return i;
|
||||
|
||||
logGlobal->warn("BattleInfo::whatSide: Player %s is not in battle!", player.toString());
|
||||
return -1;
|
||||
return BattleSide::NONE;
|
||||
}
|
||||
|
||||
CStack * BattleInfo::getStack(int stackID, bool onlyAlive)
|
||||
@@ -529,7 +533,7 @@ BattleInfo::BattleInfo():
|
||||
town(nullptr),
|
||||
tile(-1,-1,-1),
|
||||
battlefieldType(BattleField::NONE),
|
||||
tacticsSide(0),
|
||||
tacticsSide(BattleSide::NONE),
|
||||
tacticDistance(0)
|
||||
{
|
||||
setNodeType(BATTLE);
|
||||
@@ -555,7 +559,7 @@ BattleInfo::~BattleInfo()
|
||||
for (auto & elem : stacks)
|
||||
delete elem;
|
||||
|
||||
for(int i = 0; i < 2; i++)
|
||||
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
if(auto * _armyObj = battleGetArmyObject(i))
|
||||
_armyObj->battle = nullptr;
|
||||
}
|
||||
@@ -600,27 +604,27 @@ IBattleInfo::ObstacleCList BattleInfo::getAllObstacles() const
|
||||
return ret;
|
||||
}
|
||||
|
||||
PlayerColor BattleInfo::getSidePlayer(ui8 side) const
|
||||
PlayerColor BattleInfo::getSidePlayer(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).color;
|
||||
return getSide(side).color;
|
||||
}
|
||||
|
||||
const CArmedInstance * BattleInfo::getSideArmy(ui8 side) const
|
||||
const CArmedInstance * BattleInfo::getSideArmy(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).armyObject;
|
||||
return getSide(side).armyObject;
|
||||
}
|
||||
|
||||
const CGHeroInstance * BattleInfo::getSideHero(ui8 side) const
|
||||
const CGHeroInstance * BattleInfo::getSideHero(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).hero;
|
||||
return getSide(side).hero;
|
||||
}
|
||||
|
||||
ui8 BattleInfo::getTacticDist() const
|
||||
uint8_t BattleInfo::getTacticDist() const
|
||||
{
|
||||
return tacticDistance;
|
||||
}
|
||||
|
||||
ui8 BattleInfo::getTacticsSide() const
|
||||
BattleSide BattleInfo::getTacticsSide() const
|
||||
{
|
||||
return tacticsSide;
|
||||
}
|
||||
@@ -640,14 +644,14 @@ EGateState BattleInfo::getGateState() const
|
||||
return si.gateState;
|
||||
}
|
||||
|
||||
uint32_t BattleInfo::getCastSpells(ui8 side) const
|
||||
uint32_t BattleInfo::getCastSpells(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).castSpellsCount;
|
||||
return getSide(side).castSpellsCount;
|
||||
}
|
||||
|
||||
int32_t BattleInfo::getEnchanterCounter(ui8 side) const
|
||||
int32_t BattleInfo::getEnchanterCounter(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).enchanterCounter;
|
||||
return getSide(side).enchanterCounter;
|
||||
}
|
||||
|
||||
const IBonusBearer * BattleInfo::getBonusBearer() const
|
||||
@@ -685,14 +689,14 @@ bool BattleInfo::isCreatureBank() const
|
||||
}
|
||||
|
||||
|
||||
std::vector<SpellID> BattleInfo::getUsedSpells(ui8 side) const
|
||||
std::vector<SpellID> BattleInfo::getUsedSpells(BattleSide side) const
|
||||
{
|
||||
return sides.at(side).usedSpellsHistory;
|
||||
return getSide(side).usedSpellsHistory;
|
||||
}
|
||||
|
||||
void BattleInfo::nextRound()
|
||||
{
|
||||
for(int i = 0; i < 2; ++i)
|
||||
for(auto i : {BattleSide::ATTACKER, BattleSide::DEFENDER})
|
||||
{
|
||||
sides.at(i).castSpellsCount = 0;
|
||||
vstd::amax(--sides.at(i).enchanterCounter, 0);
|
||||
@@ -994,12 +998,12 @@ void BattleInfo::removeObstacle(uint32_t id)
|
||||
}
|
||||
}
|
||||
|
||||
CArmedInstance * BattleInfo::battleGetArmyObject(ui8 side) const
|
||||
CArmedInstance * BattleInfo::battleGetArmyObject(BattleSide side) const
|
||||
{
|
||||
return const_cast<CArmedInstance*>(CBattleInfoEssentials::battleGetArmyObject(side));
|
||||
}
|
||||
|
||||
CGHeroInstance * BattleInfo::battleGetFightingHero(ui8 side) const
|
||||
CGHeroInstance * BattleInfo::battleGetFightingHero(BattleSide side) const
|
||||
{
|
||||
return const_cast<CGHeroInstance*>(CBattleInfoEssentials::battleGetFightingHero(side));
|
||||
}
|
||||
@@ -1009,7 +1013,7 @@ scripting::Pool * BattleInfo::getContextPool() const
|
||||
{
|
||||
//this is real battle, use global scripting context pool
|
||||
//TODO: make this line not ugly
|
||||
return battleGetFightingHero(0)->cb->getGlobalContextPool();
|
||||
return battleGetFightingHero(BattleSide::ATTACKER)->cb->getGlobalContextPool();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1045,7 +1049,7 @@ bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b) const
|
||||
return false;
|
||||
}
|
||||
|
||||
CMP_stack::CMP_stack(int Phase, int Turn, uint8_t Side):
|
||||
CMP_stack::CMP_stack(int Phase, int Turn, BattleSide Side):
|
||||
phase(Phase),
|
||||
turn(Turn),
|
||||
side(Side)
|
||||
|
||||
Reference in New Issue
Block a user