Filter out hidden targets in CanAttack

Units should not be able to attack entities with "hidden" visibility.
Visible and fogged (mirage/retainInFog) targets remain attackable.
This commit is contained in:
Atrik 2026-05-18 14:54:26 +02:00
parent 2447d8d01c
commit 274e3fd594
3 changed files with 13 additions and 1 deletions

View file

@ -271,6 +271,15 @@ Attack.prototype.CanAttack = function(target, wantedTypes)
if (!cmpTargetPlayer || !cmpEntityPlayer)
return false;
// Must be visible or miraged / with retainInFog flag, not completely hidden
const cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (cmpRangeManager)
{
const visibility = cmpRangeManager.GetLosVisibility(target, cmpEntityPlayer.GetPlayerID());
if (visibility == "hidden")
return false;
}
const types = this.GetAttackTypes(wantedTypes);
const entityOwner = cmpEntityPlayer.GetPlayerID();
const targetOwner = cmpTargetPlayer.GetPlayerID();

View file

@ -6399,6 +6399,8 @@ UnitAI.prototype.GetQueryRange = function(iid)
}
else if (this.GetStance().respondChase)
// Chase stances: use full vision range (unit will chase anything it sees)
// Range being sometimes parabolic, it could extend beyond vision range,
// which can cause vision range to limit the effective attack range.
ret.max = visionRange;
else if (this.GetStance().respondHoldGround)
{

View file

@ -60,7 +60,8 @@ function attackComponentTest(defenderClass, isEnemy, test_function)
AddMock(SYSTEM_ENTITY, IID_RangeManager, {
"GetEffectiveParabolicRange": () => 25,
"GetMaxParabolicHeightDiff": () => 15
"GetMaxParabolicHeightDiff": () => 15,
"GetLosVisibility": (target, owner) => "visible"
});
const attacker = entityID;