1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Add proper handling for 2-hex units + extract range calculation method

This commit is contained in:
Dydzio 2023-01-12 17:07:40 +01:00 committed by Nordsoft91
parent cf1674d0ca
commit 01ce01d8f9
2 changed files with 14 additions and 14 deletions

View File

@ -709,16 +709,7 @@ bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker, BattleHe
}
int shootingRange = limitedRangeBonus->val;
int distanceBetweenHexes = BattleHex::getDistance(attacker->getPosition(), dest);
if(distanceBetweenHexes <= shootingRange)
{
return true;
}
else
{
return false;
}
return isEnemyUnitWithinSpecifiedRange(attacker->getPosition(), defender, shootingRange);
}
}
@ -1618,10 +1609,8 @@ bool CBattleInfoCallback::battleHasDistancePenalty(const IBonusBearer * shooter,
if(auto target = battleGetUnitByPos(destHex, true))
{
//If any hex of target creature is within range, there is no penalty
for(auto hex : target->getHexes())
if(BattleHex::getDistance(shooterPosition, hex) <= GameConstants::BATTLE_PENALTY_DISTANCE)
return false;
//TODO what about two-hex shooters?
if(isEnemyUnitWithinSpecifiedRange(shooterPosition, target, GameConstants::BATTLE_PENALTY_DISTANCE))
return false;
}
else
{
@ -1632,6 +1621,16 @@ bool CBattleInfoCallback::battleHasDistancePenalty(const IBonusBearer * shooter,
return true;
}
bool CBattleInfoCallback::isEnemyUnitWithinSpecifiedRange(BattleHex attackerPosition, const battle::Unit * defenderUnit, unsigned int range) const
{
for(auto hex : defenderUnit->getHexes())
if(BattleHex::getDistance(attackerPosition, hex) <= range)
return true;
//TODO what about shooting distance calculation for two-hex shooters? Is it correct?
return false;
}
BattleHex CBattleInfoCallback::wallPartToBattleHex(EWallPart::EWallPart part) const
{
RETURN_IF_NOT_BATTLE(BattleHex::INVALID);

View File

@ -94,6 +94,7 @@ public:
int battleGetSurrenderCost(PlayerColor Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
ReachabilityInfo::TDistances battleGetDistances(const battle::Unit * unit, BattleHex assumedPosition) const;
std::set<BattleHex> battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const;
bool isEnemyUnitWithinSpecifiedRange(BattleHex attackerPosition, const battle::Unit * defenderUnit, unsigned int range) const;
bool battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const; //determines if stack with given ID can attack target at the selected destination
bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination