1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

Fixes for multiple new issues from Sonar

This commit is contained in:
Ivan Savenko
2025-02-20 16:57:52 +00:00
parent 0548f325e4
commit 2362c6da21
50 changed files with 190 additions and 223 deletions

View File

@@ -384,7 +384,7 @@ BattleAction BattleEvaluator::goTowardsNearest(const CStack * stack, const Battl
BattleHexArray targetHexes = hexes;
targetHexes.sort([&](const BattleHex & h1, const BattleHex & h2) -> bool
targetHexes.sort([&reachability](const BattleHex & h1, const BattleHex & h2) -> bool
{
return reachability.distances[h1.toInt()] < reachability.distances[h2.toInt()];
});
@@ -435,7 +435,7 @@ BattleAction BattleEvaluator::goTowardsNearest(const CStack * stack, const Battl
}
// Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
// We just check all available hexes and pick the one closest to the target.
auto nearestAvailableHex = vstd::minElementByFun(avHexes, [&](const BattleHex & hex) -> int
auto nearestAvailableHex = vstd::minElementByFun(avHexes, [this, &bestNeighbour, &stack, &obstacleHexes](const BattleHex & hex) -> int
{
const int NEGATIVE_OBSTACLE_PENALTY = 100; // avoid landing on negative obstacle (moat, fire wall, etc)
const int BLOCKED_STACK_PENALTY = 100; // avoid landing on moat

View File

@@ -506,7 +506,7 @@ ReachabilityData BattleExchangeEvaluator::getExchangeUnits(
uint8_t turn,
PotentialTargets & targets,
std::shared_ptr<HypotheticBattle> hb,
battle::Units additionalUnits) const
const battle::Units & additionalUnits) const
{
ReachabilityData result;
@@ -636,7 +636,7 @@ BattleScore BattleExchangeEvaluator::calculateExchange(
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
battle::Units additionalUnits) const
const battle::Units & additionalUnits) const
{
#if BATTLE_TRACE_LEVEL>=1
logAi->trace("Battle exchange at %d", ap.attack.shooting ? ap.dest.toInt() : ap.from.toInt());
@@ -1002,7 +1002,7 @@ const battle::Units & BattleExchangeEvaluator::getOneTurnReachableUnits(uint8_t
}
// avoid blocking path for stronger stack by weaker stack
bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * activeUnit, const BattleHex & position)
bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(const HypotheticBattle & hb, const battle::Unit * activeUnit, const BattleHex & position)
{
const int BLOCKING_THRESHOLD = 70;
const int BLOCKING_OWN_ATTACK_PENALTY = 100;

View File

@@ -133,7 +133,6 @@ class ReachabilityMapCache
std::map<uint32_t, ReachabilityInfo> unitReachabilityMap; // unit ID -> reachability
std::map<uint32_t, PerTurnData> hexReachabilityPerTurn;
//const ReachabilityInfo & update();
battle::Units computeOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, const BattleHex & hex);
public:
const battle::Units & getOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, const BattleHex & hex);
@@ -158,7 +157,7 @@ private:
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
battle::Units additionalUnits = {}) const;
const battle::Units & additionalUnits = {}) const;
bool canBeHitThisTurn(const AttackPossibility & ap);
@@ -193,9 +192,9 @@ public:
uint8_t turn,
PotentialTargets & targets,
std::shared_ptr<HypotheticBattle> hb,
battle::Units additionalUnits = {}) const;
const battle::Units & additionalUnits = {}) const;
bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, const BattleHex & position);
bool checkPositionBlocksOurStacks(const HypotheticBattle & hb, const battle::Unit * unit, const BattleHex & position);
MoveTarget findMoveTowardsUnreachable(
const battle::Unit * activeStack,

View File

@@ -209,7 +209,7 @@ BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
int creatureLevel = -1;
int creatureUpgrade = 0;
if(toBuild.IsDwelling())
if(toBuild.isDwelling())
{
creatureLevel = BuildingID::getLevelFromDwelling(toBuild);
creatureUpgrade = BuildingID::getUpgradedFromDwelling(toBuild);
@@ -267,7 +267,7 @@ BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
auto otherDwelling = [](const BuildingID & id) -> bool
{
return id.IsDwelling();
return id.isDwelling();
};
if(vstd::contains_if(missingBuildings, otherDwelling))
@@ -429,7 +429,7 @@ BuildingInfo::BuildingInfo(
}
else
{
if(id.IsDwelling())
if(id.isDwelling())
{
creatureGrows = creature->getGrowth();

View File

@@ -386,7 +386,7 @@ void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInsta
town->getObjectName());
#endif
ExecuteHeroChain heroChain = ExecuteHeroChain(path, town);
ExecuteHeroChain heroChain(path, town);
if (closestWay)
{

View File

@@ -895,7 +895,7 @@ public:
if(stayAtTown.town->mageGuildLevel() > 0)
evaluationContext.armyReward += evaluationContext.evaluator.getManaRecoveryArmyReward(stayAtTown.getHero());
if (evaluationContext.armyReward == 0)
if (vstd::isAlmostZero(evaluationContext.armyReward))
evaluationContext.isDefend = true;
else
{
@@ -1023,43 +1023,40 @@ public:
if(heroRole == HeroRole::MAIN)
evaluationContext.heroRole = heroRole;
if (hero)
// Assuming Slots() returns a collection of slots with slot.second->getCreatureID() and slot.second->getPower()
float heroPower = 0;
float totalPower = 0;
// Map to store the aggregated power of creatures by CreatureID
std::map<int, float> totalPowerByCreatureID;
// Calculate hero power and total power by CreatureID
for (auto slot : hero->Slots())
{
// Assuming Slots() returns a collection of slots with slot.second->getCreatureID() and slot.second->getPower()
float heroPower = 0;
float totalPower = 0;
int creatureID = slot.second->getCreatureID();
float slotPower = slot.second->getPower();
// Map to store the aggregated power of creatures by CreatureID
std::map<int, float> totalPowerByCreatureID;
// Add the power of this slot to the heroPower
heroPower += slotPower;
// Calculate hero power and total power by CreatureID
for (auto slot : hero->Slots())
// Accumulate the total power for the specific CreatureID
if (totalPowerByCreatureID.find(creatureID) == totalPowerByCreatureID.end())
{
int creatureID = slot.second->getCreatureID();
float slotPower = slot.second->getPower();
// Add the power of this slot to the heroPower
heroPower += slotPower;
// Accumulate the total power for the specific CreatureID
if (totalPowerByCreatureID.find(creatureID) == totalPowerByCreatureID.end())
{
// First time encountering this CreatureID, retrieve total creatures' power
totalPowerByCreatureID[creatureID] = ai->armyManager->getTotalCreaturesAvailable(creatureID).power;
}
// First time encountering this CreatureID, retrieve total creatures' power
totalPowerByCreatureID[creatureID] = ai->armyManager->getTotalCreaturesAvailable(creatureID).power;
}
}
// Calculate total power based on unique CreatureIDs
for (const auto& entry : totalPowerByCreatureID)
{
totalPower += entry.second;
}
// Calculate total power based on unique CreatureIDs
for (const auto& entry : totalPowerByCreatureID)
{
totalPower += entry.second;
}
// Compute the power ratio if total power is greater than zero
if (totalPower > 0)
{
evaluationContext.powerRatio = heroPower / totalPower;
}
// Compute the power ratio if total power is greater than zero
if (totalPower > 0)
{
evaluationContext.powerRatio = heroPower / totalPower;
}
if (target)
@@ -1249,7 +1246,7 @@ public:
auto potentialUpgradeValue = evaluationContext.evaluator.getUpgradeArmyReward(buildThis.town, bi);
evaluationContext.addNonCriticalStrategicalValue(potentialUpgradeValue / 10000.0f / (float)bi.prerequisitesCount);
if(bi.id.IsDwelling())
if(bi.id.isDwelling())
evaluationContext.armyReward += bi.armyStrength - evaluationContext.evaluator.ai->armyManager->evaluateStackPower(bi.baseCreatureID.toCreature(), bi.creatureGrows);
else //This is for prerequisite-buildings
evaluationContext.armyReward += evaluationContext.evaluator.ai->armyManager->evaluateStackPower(bi.baseCreatureID.toCreature(), bi.creatureGrows);

View File

@@ -41,8 +41,8 @@ struct AIPathNode : public CGPathNode
{
std::shared_ptr<const SpecialAction> specialAction;
const AIPathNode * chainOther;
const ChainActor * actor;
const AIPathNode * chainOther = nullptr;
const ChainActor * actor = nullptr;
uint64_t danger;
uint64_t armyLoss;
@@ -129,8 +129,8 @@ struct AIPath
struct ExchangeCandidate : public AIPathNode
{
AIPathNode * carrierParent;
AIPathNode * otherParent;
AIPathNode * carrierParent = nullptr;
AIPathNode * otherParent = nullptr;
};
enum EHeroChainPass

View File

@@ -50,8 +50,8 @@ namespace AIPathfinding
options.allowLayerTransitioningAfterBattle = true;
options.useTeleportWhirlpool = true;
options.forceUseTeleportWhirlpool = true;
options.useTeleportOneWay = ai->settings->isOneWayMonolithUsageAllowed();;
options.useTeleportOneWayRandom = ai->settings->isOneWayMonolithUsageAllowed();;
options.useTeleportOneWay = ai->settings->isOneWayMonolithUsageAllowed();
options.useTeleportOneWayRandom = ai->settings->isOneWayMonolithUsageAllowed();
}
AIPathfinderConfig::~AIPathfinderConfig() = default;

View File

@@ -222,7 +222,7 @@ bool BuildingManager::getBuildingOptions(const CGTownInstance * t)
std::vector<BuildingID> extraBuildings;
for (auto buildingInfo : t->getTown()->buildings)
{
if (buildingInfo.first.IsDwelling() && BuildingID::getUpgradedFromDwelling(buildingInfo.first) > 1)
if (buildingInfo.first.isDwelling() && BuildingID::getUpgradedFromDwelling(buildingInfo.first) > 1)
extraBuildings.push_back(buildingInfo.first);
}
return tryBuildAnyStructure(t, extraBuildings);