From 6f67602d5770707e2b3d0142ac6f97ea77a2343f Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 6 Jul 2013 15:25:13 +1200 Subject: [PATCH] Use Target range checks in attack activities. --- OpenRA.FileFormats/WRange.cs | 1 + OpenRA.Mods.RA/Activities/Attack.cs | 27 ++++++++++--------- OpenRA.Mods.RA/Activities/Heal.cs | 4 +-- OpenRA.Mods.RA/Attack/AttackFrontal.cs | 15 ++++++----- OpenRA.Mods.RA/Attack/AttackLeap.cs | 8 ------ OpenRA.Mods.RA/Attack/AttackMedic.cs | 9 ++++--- OpenRA.Mods.RA/Missions/Allies03Script.cs | 2 +- OpenRA.Mods.RA/Missions/Allies04Script.cs | 4 +-- OpenRA.Mods.RA/Missions/FortLonestarScript.cs | 2 +- OpenRA.Mods.RA/Missions/MissionUtils.cs | 2 +- OpenRA.Mods.RA/Missions/Survival01Script.cs | 2 +- OpenRA.Mods.RA/Missions/Survival02Script.cs | 2 +- 12 files changed, 39 insertions(+), 39 deletions(-) diff --git a/OpenRA.FileFormats/WRange.cs b/OpenRA.FileFormats/WRange.cs index 1f4c45ead2..216f8bd77e 100644 --- a/OpenRA.FileFormats/WRange.cs +++ b/OpenRA.FileFormats/WRange.cs @@ -22,6 +22,7 @@ namespace OpenRA public WRange(int r) { Range = r; } public static readonly WRange Zero = new WRange(0); + public static WRange FromCells(int cells) { return new WRange(1024*cells); } public static WRange operator +(WRange a, WRange b) { return new WRange(a.Range + b.Range); } public static WRange operator -(WRange a, WRange b) { return new WRange(a.Range - b.Range); } diff --git a/OpenRA.Mods.RA/Activities/Attack.cs b/OpenRA.Mods.RA/Activities/Attack.cs index cae2b93656..91b52e241a 100755 --- a/OpenRA.Mods.RA/Activities/Attack.cs +++ b/OpenRA.Mods.RA/Activities/Attack.cs @@ -19,7 +19,7 @@ namespace OpenRA.Mods.RA.Activities { protected Target Target; ITargetable targetable; - int Range; + WRange Range; bool AllowMovement; int nextPathTime; @@ -27,7 +27,10 @@ namespace OpenRA.Mods.RA.Activities const int delayBetweenPathingAttempts = 20; const int delaySpread = 5; - public Attack(Target target, int range, bool allowMovement) + public Attack(Target target, WRange range) + : this(target, range, true) {} + + public Attack(Target target, WRange range, bool allowMovement) { Target = target; if (target.IsActor) @@ -37,20 +40,18 @@ namespace OpenRA.Mods.RA.Activities AllowMovement = allowMovement; } - public Attack(Target target, int range) : this(target, range, true) {} - - public override Activity Tick( Actor self ) + public override Activity Tick(Actor self) { var attack = self.Trait(); - - var ret = InnerTick( self, attack ); - attack.IsAttacking = ( ret == this ); + var ret = InnerTick(self, attack); + attack.IsAttacking = (ret == this); return ret; } - protected virtual Activity InnerTick( Actor self, AttackBase attack ) + protected virtual Activity InnerTick(Actor self, AttackBase attack) { - if (IsCanceled) return NextActivity; + if (IsCanceled) + return NextActivity; if (!Target.IsValid) return NextActivity; @@ -61,7 +62,7 @@ namespace OpenRA.Mods.RA.Activities if (targetable != null && !targetable.TargetableBy(Target.Actor, self)) return NextActivity; - if (!Combat.IsInRange(self.CenterLocation, Range, Target)) + if (!Target.IsInRange(self.CenterPosition, Range)) { if (--nextPathTime > 0) return this; @@ -69,13 +70,13 @@ namespace OpenRA.Mods.RA.Activities nextPathTime = self.World.SharedRandom.Next(delayBetweenPathingAttempts - delaySpread, delayBetweenPathingAttempts + delaySpread); - return (AllowMovement) ? Util.SequenceActivities(self.Trait().MoveWithinRange(Target, Range), this) : NextActivity; + return (AllowMovement) ? Util.SequenceActivities(self.Trait().MoveWithinRange(Target, Range.Range / 1024), this) : NextActivity; } var desiredFacing = Util.GetFacing(Target.CenterLocation - self.CenterLocation, 0); var facing = self.Trait(); if (facing.Facing != desiredFacing) - return Util.SequenceActivities( new Turn( desiredFacing ), this ); + return Util.SequenceActivities(new Turn(desiredFacing), this); attack.DoAttack(self, Target); return this; diff --git a/OpenRA.Mods.RA/Activities/Heal.cs b/OpenRA.Mods.RA/Activities/Heal.cs index 0861e56c54..199f89d041 100755 --- a/OpenRA.Mods.RA/Activities/Heal.cs +++ b/OpenRA.Mods.RA/Activities/Heal.cs @@ -17,10 +17,10 @@ namespace OpenRA.Mods.RA.Activities /* non-turreted attack */ public class Heal : Attack { - public Heal(Target target, int range, bool allowMovement) + public Heal(Target target, WRange range, bool allowMovement) : base(target, range, allowMovement) {} - protected override Activity InnerTick( Actor self, AttackBase attack ) + protected override Activity InnerTick(Actor self, AttackBase attack) { if (Target.IsActor && Target.Actor.GetDamageState() == DamageState.Undamaged) return NextActivity; diff --git a/OpenRA.Mods.RA/Attack/AttackFrontal.cs b/OpenRA.Mods.RA/Attack/AttackFrontal.cs index 2f52e23625..c93cffbd2a 100644 --- a/OpenRA.Mods.RA/Attack/AttackFrontal.cs +++ b/OpenRA.Mods.RA/Attack/AttackFrontal.cs @@ -28,15 +28,15 @@ namespace OpenRA.Mods.RA public AttackFrontal(Actor self, AttackFrontalInfo info) : base( self ) { this.info = info; } - protected override bool CanAttack( Actor self, Target target ) + protected override bool CanAttack(Actor self, Target target) { - if( !base.CanAttack( self, target ) ) + if (!base.CanAttack(self, target)) return false; var facing = self.Trait().Facing; var facingToTarget = Util.GetFacing(target.CenterLocation - self.CenterLocation, facing); - if( Math.Abs( facingToTarget - facing ) % 256 > info.FacingTolerance ) + if (Math.Abs(facingToTarget - facing) % 256 > info.FacingTolerance) return false; return true; @@ -44,10 +44,13 @@ namespace OpenRA.Mods.RA public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove) { - var weapon = ChooseArmamentForTarget(newTarget); - if (weapon == null) + var a = ChooseArmamentForTarget(newTarget); + if (a == null) return null; - return new Activities.Attack(newTarget, Math.Max(0, (int)weapon.Weapon.Range), allowMove); + + // TODO: Define weapon ranges as WRange + var range = new WRange(Math.Max(0,(int)(1024*a.Weapon.Range))); + return new Activities.Attack(newTarget, range, allowMove); } } } diff --git a/OpenRA.Mods.RA/Attack/AttackLeap.cs b/OpenRA.Mods.RA/Attack/AttackLeap.cs index b9acc3a097..1021bd7514 100644 --- a/OpenRA.Mods.RA/Attack/AttackLeap.cs +++ b/OpenRA.Mods.RA/Attack/AttackLeap.cs @@ -42,13 +42,5 @@ namespace OpenRA.Mods.RA self.CancelActivity(); self.QueueActivity(new Leap(self, target)); } - - public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove) - { - var a = ChooseArmamentForTarget(newTarget); - if (a == null) - return null; - return new Activities.Attack(newTarget, Math.Max(0, (int)a.Weapon.Range), allowMove); - } } } diff --git a/OpenRA.Mods.RA/Attack/AttackMedic.cs b/OpenRA.Mods.RA/Attack/AttackMedic.cs index ec705f25ba..699b2d9cbd 100644 --- a/OpenRA.Mods.RA/Attack/AttackMedic.cs +++ b/OpenRA.Mods.RA/Attack/AttackMedic.cs @@ -29,10 +29,13 @@ namespace OpenRA.Mods.RA public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove) { - var weapon = ChooseArmamentForTarget(newTarget); - if (weapon == null) + var a = ChooseArmamentForTarget(newTarget); + if (a == null) return null; - return new Activities.Heal(newTarget, Math.Max(0, (int)weapon.Weapon.Range), allowMove); + + // TODO: Define weapon ranges as WRange + var range = new WRange(Math.Max(0,(int)(1024*a.Weapon.Range))); + return new Activities.Heal(newTarget, range, allowMove); } } } diff --git a/OpenRA.Mods.RA/Missions/Allies03Script.cs b/OpenRA.Mods.RA/Missions/Allies03Script.cs index 28ee1db87a..1adde71cf4 100644 --- a/OpenRA.Mods.RA/Missions/Allies03Script.cs +++ b/OpenRA.Mods.RA/Missions/Allies03Script.cs @@ -297,7 +297,7 @@ namespace OpenRA.Mods.RA.Missions var enemy = FirstUnshroudedOrDefault(enemies.OrderBy(u => (self.CenterLocation - u.CenterLocation).LengthSquared), world, 10); if (enemy != null) - self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), 3))); + self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), WRange.FromCells(3)))); } void ManageSovietUnits() diff --git a/OpenRA.Mods.RA/Missions/Allies04Script.cs b/OpenRA.Mods.RA/Missions/Allies04Script.cs index a9c32e015a..d89c7085e2 100644 --- a/OpenRA.Mods.RA/Missions/Allies04Script.cs +++ b/OpenRA.Mods.RA/Missions/Allies04Script.cs @@ -125,7 +125,7 @@ namespace OpenRA.Mods.RA.Missions if (world.FrameNumber == frameInfiltrated + 1500 * 12 && !bridgeTank.IsDead() && bridgeTank.IsInWorld && !bridge.IsDead()) { - bridgeTank.QueueActivity(new Attack(Target.FromPos(bridge.CenterLocation), 4)); + bridgeTank.QueueActivity(new Attack(Target.FromPos(bridge.CenterLocation), WRange.FromCells(4))); attackingBridge = true; } if (attackingBridge && bridge.IsDead()) @@ -152,7 +152,7 @@ namespace OpenRA.Mods.RA.Missions var enemy = enemies.OrderBy(u => (attacker.CenterLocation - u.CenterLocation).LengthSquared).FirstOrDefault(); if (enemy != null) - attacker.QueueActivity(new AttackMove.AttackMoveActivity(attacker, new Attack(Target.FromActor(enemy), 3))); + attacker.QueueActivity(new AttackMove.AttackMoveActivity(attacker, new Attack(Target.FromActor(enemy), WRange.FromCells(3)))); else { attackingTown = false; diff --git a/OpenRA.Mods.RA/Missions/FortLonestarScript.cs b/OpenRA.Mods.RA/Missions/FortLonestarScript.cs index 9e61f389fc..9aebdad818 100644 --- a/OpenRA.Mods.RA/Missions/FortLonestarScript.cs +++ b/OpenRA.Mods.RA/Missions/FortLonestarScript.cs @@ -82,7 +82,7 @@ namespace OpenRA.Mods.RA.Missions var targetEnemy = enemies.OrderBy(u => (self.CenterLocation - u.CenterLocation).LengthSquared).FirstOrDefault(); if (targetEnemy != null) { - self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(targetEnemy), 6))); + self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(targetEnemy), WRange.FromCells(6)))); } } diff --git a/OpenRA.Mods.RA/Missions/MissionUtils.cs b/OpenRA.Mods.RA/Missions/MissionUtils.cs index 93d9b943af..2d0537c547 100644 --- a/OpenRA.Mods.RA/Missions/MissionUtils.cs +++ b/OpenRA.Mods.RA/Missions/MissionUtils.cs @@ -213,7 +213,7 @@ namespace OpenRA.Mods.RA.Missions var enemy = enemies.OrderBy(u => (self.CenterLocation - u.CenterLocation).LengthSquared).FirstOrDefault(); if (enemy != null) - self.QueueActivity(queued, new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), 3))); + self.QueueActivity(queued, new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), WRange.FromCells(3)))); } } diff --git a/OpenRA.Mods.RA/Missions/Survival01Script.cs b/OpenRA.Mods.RA/Missions/Survival01Script.cs index 3f976702bf..b5b62aa75b 100644 --- a/OpenRA.Mods.RA/Missions/Survival01Script.cs +++ b/OpenRA.Mods.RA/Missions/Survival01Script.cs @@ -214,7 +214,7 @@ namespace OpenRA.Mods.RA.Missions var targetEnemy = enemies.OrderBy(u => (self.CenterLocation - u.CenterLocation).LengthSquared).FirstOrDefault(); if (targetEnemy != null) { - self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(targetEnemy), 3))); + self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(targetEnemy), WRange.FromCells(3)))); } } diff --git a/OpenRA.Mods.RA/Missions/Survival02Script.cs b/OpenRA.Mods.RA/Missions/Survival02Script.cs index 9036003048..bfb8861f6e 100644 --- a/OpenRA.Mods.RA/Missions/Survival02Script.cs +++ b/OpenRA.Mods.RA/Missions/Survival02Script.cs @@ -139,7 +139,7 @@ namespace OpenRA.Mods.RA.Missions var enemy = FirstUnshroudedOrDefault(enemies.OrderBy(u => (self.CenterLocation - u.CenterLocation).LengthSquared), world, 20); if (enemy != null) - self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), 3))); + self.QueueActivity(new AttackMove.AttackMoveActivity(self, new Attack(Target.FromActor(enemy), WRange.FromCells(3)))); } void SpawnAndAttack(string[] squad, Player owner, CPos location)