1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-29 23:07:48 +02:00

Unit.cpp refactor and some other minor changes

This commit is contained in:
MichalZr6
2024-12-04 21:37:31 +01:00
parent e3516120d8
commit fb9a3da651
12 changed files with 142 additions and 179 deletions

View File

@@ -651,7 +651,7 @@ BattleHexArray CBattleInfoCallback::battleGetAvailableHexes(const battle::Unit *
if(!otherSt->isValidTarget(false))
continue;
BattleHexArray occupied = otherSt->getHexes();
const BattleHexArray & occupied = otherSt->getHexes();
if(battleCanShoot(unit, otherSt->getPosition()))
{
@@ -964,9 +964,7 @@ AccessibilityInfo CBattleInfoCallback::getAccessibility() const
if(bFieldType != BattleField::NONE)
{
BattleHexArray impassableHexes = bFieldType.getInfo()->impassableHexes;
for(auto hex : impassableHexes)
for(auto hex : bFieldType.getInfo()->impassableHexes)
ret[hex] = EAccessibility::UNAVAILABLE;
}
@@ -1031,20 +1029,20 @@ AccessibilityInfo CBattleInfoCallback::getAccessibility() const
AccessibilityInfo CBattleInfoCallback::getAccessibility(const battle::Unit * stack) const
{
return getAccessibility(battle::Unit::getHexes(stack->getPosition(), stack->doubleWide(), stack->unitSide()));
return getAccessibility(& battle::Unit::getHexes(stack->getPosition(), stack->doubleWide(), stack->unitSide()));
}
AccessibilityInfo CBattleInfoCallback::getAccessibility(const BattleHexArray & accessibleHexes) const
AccessibilityInfo CBattleInfoCallback::getAccessibility(const BattleHexArray * accessibleHexes) const
{
auto ret = getAccessibility();
for(auto hex : accessibleHexes)
for(auto hex : *accessibleHexes)
if(hex.isValid())
ret[hex] = EAccessibility::ACCESSIBLE;
return ret;
}
ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibility, const ReachabilityInfo::Parameters & params) const
ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo & accessibility, const ReachabilityInfo::Parameters & params) const
{
ReachabilityInfo ret;
ret.accessibility = accessibility;
@@ -1114,11 +1112,11 @@ bool CBattleInfoCallback::isInObstacle(
const BattleHexArray & obstacleHexes,
const ReachabilityInfo::Parameters & params) const
{
auto occupiedHexes = battle::Unit::getHexes(hex, params.doubleWide, params.side);
const BattleHexArray & occupiedHexes = battle::Unit::getHexes(hex, params.doubleWide, params.side);
for(auto occupiedHex : occupiedHexes)
{
if(params.ignoreKnownAccessible && params.knownAccessible.contains(occupiedHex))
if(params.ignoreKnownAccessible && params.knownAccessible->contains(occupiedHex))
continue;
if(obstacleHexes.contains(occupiedHex))
@@ -1146,7 +1144,7 @@ BattleHexArray CBattleInfoCallback::getStoppers(BattleSide whichSidePerspective)
if(!battleIsObstacleVisibleForSide(*oi, whichSidePerspective))
continue;
for(const auto & hex : oi->getStoppingTile())
for(auto hex : oi->getStoppingTile())
{
if(hex == BattleHex::GATE_BRIDGE && oi->obstacleType == CObstacleInstance::MOAT)
{
@@ -1162,7 +1160,6 @@ BattleHexArray CBattleInfoCallback::getStoppers(BattleSide whichSidePerspective)
std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(const battle::Unit * closest) const
{
auto reachability = getReachability(closest);
auto avHexes = battleGetAvailableHexes(reachability, closest, false);
// I hate std::pairs with their undescriptive member names first / second
struct DistStack
@@ -1181,7 +1178,7 @@ std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(
for(const battle::Unit * st : possible)
{
for(BattleHex hex : avHexes)
for(BattleHex hex : battleGetAvailableHexes(reachability, closest, false))
if(CStack::isMeleeAttackPossible(closest, st, hex))
{
DistStack hlp = {reachability.distances[hex], hex, st};
@@ -1269,7 +1266,7 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const battle::Unit * unit)
return getReachability(params);
}
ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Parameters &params) const
ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Parameters & params) const
{
if(params.flying)
return getFlyingReachability(params);
@@ -1283,7 +1280,7 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Pa
}
}
ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityInfo::Parameters &params) const
ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityInfo::Parameters & params) const
{
ReachabilityInfo ret;
ret.accessibility = getAccessibility(params.knownAccessible);
@@ -1340,11 +1337,11 @@ AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes(
}
if(attacker->hasBonusOfType(BonusType::ATTACKS_ALL_ADJACENT))
{
boost::copy(attacker->getSurroundingHexes(attackerPos), vstd::set_inserter(at.hostileCreaturePositions));
at.hostileCreaturePositions.merge(attacker->getSurroundingHexes(attackerPos));
}
if(attacker->hasBonusOfType(BonusType::THREE_HEADED_ATTACK))
{
BattleHexArray hexes = attacker->getSurroundingHexes(attackerPos);
const BattleHexArray & hexes = attacker->getSurroundingHexes(attackerPos);
for(BattleHex tile : hexes)
{
if((BattleHex::mutualPosition(tile, destinationTile) > -1 && BattleHex::mutualPosition(tile, attackOriginHex) > -1)) //adjacent both to attacker's head and attacked tile
@@ -1432,9 +1429,8 @@ AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const battle::
if(attacker->hasBonusOfType(BonusType::SHOOTS_ALL_ADJACENT) && !BattleHexArray::neighbouringTilesCache[attackerPos].contains(destinationTile))
{
auto targetHexes = BattleHexArray::neighbouringTilesCache[destinationTile];
targetHexes.insert(destinationTile);
boost::copy(targetHexes, vstd::set_inserter(at.hostileCreaturePositions));
at.hostileCreaturePositions.merge(BattleHexArray::neighbouringTilesCache[destinationTile]);
at.hostileCreaturePositions.insert(destinationTile);
}
return at;