Skip to content

Commit

Permalink
fix off-by-one in IsInRange. use CenterLocation in combat code where …
Browse files Browse the repository at this point in the history
…appropriate
  • Loading branch information
ytinasni authored and chrisforbes committed Nov 5, 2010
1 parent 5c0cd50 commit 80caf18
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions OpenRA.Mods.RA/AttackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public void ScanAndAttack(Actor self, bool allowMovement)
var attack = self.Trait<AttackBase>();
var range = attack.GetMaximumRange();

if (!attack.target.IsValid ||
(Util.CellContaining(attack.target.CenterLocation) - self.Location).LengthSquared > range * range)
if (!attack.target.IsValid || !Combat.IsInRange( self.CenterLocation, range, attack.target ))
AttackTarget(self, ChooseTarget(self, range), allowMovement);

var info = self.Info.Traits.Get<AttackBaseInfo>();
Expand All @@ -235,7 +234,7 @@ Actor ChooseTarget(Actor self, float range)
.Where(a => a.Owner != null && self.Owner.Stances[a.Owner] == Stance.Enemy)
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.Where(a => !a.HasTrait<Cloak>() || a.Trait<Cloak>().IsVisible(a, self.Owner))
.OrderBy(a => (a.Location - self.Location).LengthSquared)
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
}

Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.RA/AutoHeal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool NeedsNewTarget(Actor self)

if (!attack.target.IsValid)
return true; // he's dead.
if ((attack.target.CenterLocation - self.Location).LengthSquared > range * range + 2)
if( !Combat.IsInRange( self.CenterLocation, range, attack.target ) )
return true; // wandered off faster than we could follow

if (attack.target.IsActor
Expand Down Expand Up @@ -64,7 +64,7 @@ Actor ChooseTarget(Actor self, float range)
.Where(a => a.IsInWorld && !a.IsDead())
.Where(a => a.HasTrait<Health>() && a.GetDamageState() > DamageState.Undamaged)
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.OrderBy(a => (a.Location - self.Location).LengthSquared)
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.RA/CarpetBomb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Tick(Actor self)
{
var info = self.Info.Traits.Get<CarpetBombInfo>();

if ((self.Location - Target).LengthSquared > info.Range * info.Range)
if( !Combat.IsInRange( self.CenterLocation, info.Range, Target ) )
return;

var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.RA/Combat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ public static bool IsInRange( float2 attackOrigin, float range, Actor target )
{
var rsq = range * range * Game.CellSize * Game.CellSize;
foreach( var cell in target.Trait<ITargetable>().TargetableCells( target ) )
if( ( attackOrigin - cell * Game.CellSize ).LengthSquared < rsq )
if( ( attackOrigin - cell * Game.CellSize ).LengthSquared <= rsq )
return true;
return false;
}

public static bool IsInRange( float2 attackOrigin, float range, float2 targetLocation )
{
var rsq = range * range * Game.CellSize * Game.CellSize;
return ( attackOrigin - targetLocation ).LengthSquared < rsq;
return ( attackOrigin - targetLocation ).LengthSquared <= rsq;
}

public static bool IsInRange( float2 attackOrigin, float range, Target target )
Expand Down

0 comments on commit 80caf18

Please sign in to comment.