From 01ce01d8f95f5c617669dc53e2501e056474ea75 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Thu, 12 Jan 2023 17:07:40 +0100 Subject: [PATCH] Add proper handling for 2-hex units + extract range calculation method --- lib/battle/CBattleInfoCallback.cpp | 27 +++++++++++++-------------- lib/battle/CBattleInfoCallback.h | 1 + 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/battle/CBattleInfoCallback.cpp b/lib/battle/CBattleInfoCallback.cpp index a4caac6a2..15bf09923 100644 --- a/lib/battle/CBattleInfoCallback.cpp +++ b/lib/battle/CBattleInfoCallback.cpp @@ -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); diff --git a/lib/battle/CBattleInfoCallback.h b/lib/battle/CBattleInfoCallback.h index 75fc6bf81..e613e906f 100644 --- a/lib/battle/CBattleInfoCallback.h +++ b/lib/battle/CBattleInfoCallback.h @@ -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 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