1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

Refactor destructibleEnemyTurns

This commit is contained in:
MichalZr6
2024-12-02 13:06:58 +01:00
parent 8c3a417527
commit e3516120d8
5 changed files with 25 additions and 23 deletions

View File

@@ -22,9 +22,10 @@ bool AccessibilityInfo::tileAccessibleWithGate(BattleHex tile, BattleSide side)
if(accessibility == EAccessibility::ALIVE_STACK)
{
auto destructible = destructibleEnemyTurns.find(tile);
if(!destructibleEnemyTurns)
return false;
return destructible != destructibleEnemyTurns.end();
return destructibleEnemyTurns->at(tile.hex) >= 0;
}
if(accessibility != EAccessibility::ACCESSIBLE)

View File

@@ -27,7 +27,7 @@ enum class EAccessibility
DESTRUCTIBLE_WALL,
GATE, //sieges -> gate opens only for defender stacks
UNAVAILABLE, //indestructible wall parts, special battlefields (like boat-to-boat)
SIDE_COLUMN //used for first and last columns of hexes that are unavailable but wat machines can stand there
SIDE_COLUMN //used for first and last columns of hexes that are unavailable but war machines can stand there
};
@@ -35,7 +35,7 @@ using TAccessibilityArray = std::array<EAccessibility, GameConstants::BFIELD_SIZ
struct DLL_LINKAGE AccessibilityInfo : TAccessibilityArray
{
std::map<BattleHex, ui8> destructibleEnemyTurns;
const std::array<int8_t, GameConstants::BFIELD_SIZE> * destructibleEnemyTurns = nullptr;
public:
bool accessible(BattleHex tile, const battle::Unit * stack) const; //checks for both tiles if stack is double wide

View File

@@ -1083,28 +1083,25 @@ ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibi
for(BattleHex neighbour : BattleHexArray::neighbouringTilesCache[curHex.hex])
{
if(neighbour.isValid())
auto additionalCost = 0;
if(params.bypassEnemyStacks)
{
auto additionalCost = 0;
auto enemyToBypass = params.destructibleEnemyTurns.at(neighbour);
if(params.bypassEnemyStacks)
if(enemyToBypass >= 0)
{
auto enemyToBypass = params.destructibleEnemyTurns.find(neighbour);
if(enemyToBypass != params.destructibleEnemyTurns.end())
{
additionalCost = enemyToBypass->second;
}
additionalCost = enemyToBypass;
}
}
const int costFoundSoFar = ret.distances[neighbour.hex];
const int costFoundSoFar = ret.distances[neighbour.hex];
if(accessibleCache[neighbour.hex] && costToNeighbour + additionalCost < costFoundSoFar)
{
hexq.push(neighbour);
ret.distances[neighbour.hex] = costToNeighbour + additionalCost;
ret.predecessors[neighbour.hex] = curHex;
}
if(accessibleCache[neighbour.hex] && costToNeighbour + additionalCost < costFoundSoFar)
{
hexq.push(neighbour);
ret.distances[neighbour.hex] = costToNeighbour + additionalCost;
ret.predecessors[neighbour.hex] = curHex;
}
}
}
@@ -1280,7 +1277,7 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Pa
{
auto accessibility = getAccessibility(params.knownAccessible);
accessibility.destructibleEnemyTurns = params.destructibleEnemyTurns;
accessibility.destructibleEnemyTurns = & params.destructibleEnemyTurns;
return makeBFS(accessibility, params);
}

View File

@@ -22,6 +22,7 @@ ReachabilityInfo::Parameters::Parameters(const battle::Unit * Stack, BattleHex S
flying(Stack->hasBonusOfType(BonusType::FLYING))
{
knownAccessible = battle::Unit::getHexes(startPosition, doubleWide, side);
destructibleEnemyTurns.fill(-1);
}
ReachabilityInfo::ReachabilityInfo()

View File

@@ -33,12 +33,15 @@ struct DLL_LINKAGE ReachabilityInfo
bool ignoreKnownAccessible = false; //Ignore obstacles if it is in accessible hexes
bool bypassEnemyStacks = false; // in case of true will count amount of turns needed to kill enemy and thus move forward
BattleHexArray knownAccessible; //hexes that will be treated as accessible, even if they're occupied by stack (by default - tiles occupied by stack we do reachability for, so it doesn't block itself)
std::map<BattleHex, ui8> destructibleEnemyTurns; // hom many turns it is needed to kill enemy on specific hex
std::array<int8_t, GameConstants::BFIELD_SIZE> destructibleEnemyTurns; // how many turns it is needed to kill enemy on specific hex (index <=> hex)
BattleHex startPosition; //assumed position of stack
BattleSide perspective = BattleSide::ALL_KNOWING; //some obstacles (eg. quicksands) may be invisible for some side
Parameters() = default;
Parameters()
{
destructibleEnemyTurns.fill(-1);
}
Parameters(const battle::Unit * Stack, BattleHex StartPosition);
};