From 85500c0ec78f9506ff8b95df9030fcd614a64892 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 19 Jan 2014 10:25:46 +1300 Subject: [PATCH] Tidy AutoTarget code. --- OpenRA.Mods.RA/AutoTarget.cs | 63 +++++++++++-------- OpenRA.Mods.RA/Missions/Allies01Script.cs | 2 +- OpenRA.Mods.RA/Missions/Allies04Script.cs | 2 +- .../Missions/DesertShellmapScript.cs | 4 +- OpenRA.Mods.RA/Missions/Survival02Script.cs | 2 +- .../Scripting/LuaScriptInterface.cs | 4 +- OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs | 6 +- 7 files changed, 46 insertions(+), 37 deletions(-) diff --git a/OpenRA.Mods.RA/AutoTarget.cs b/OpenRA.Mods.RA/AutoTarget.cs index 5f8d0d54f0..e29d20199e 100644 --- a/OpenRA.Mods.RA/AutoTarget.cs +++ b/OpenRA.Mods.RA/AutoTarget.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -8,10 +8,10 @@ */ #endregion -using OpenRA.FileFormats; -using OpenRA.Traits; using System.Drawing; using System.Linq; +using OpenRA.FileFormats; +using OpenRA.Traits; namespace OpenRA.Mods.RA { @@ -32,62 +32,72 @@ namespace OpenRA.Mods.RA public object Create(ActorInitializer init) { return new AutoTarget(init.self, this); } } - public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything }; + public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything } public class AutoTarget : INotifyIdle, INotifyDamage, ITick, IResolveOrder, ISync { - readonly AutoTargetInfo Info; + readonly AutoTargetInfo info; readonly AttackBase attack; readonly AttackTurreted at; + [Sync] int nextScanTime = 0; - [Sync] public int nextScanTime = 0; - public UnitStance stance; - [Sync] public int stanceNumber { get { return (int)stance; } } - public UnitStance predictedStance; /* NOT SYNCED: do not refer to this anywhere other than UI code */ + public UnitStance Stance; [Sync] public Actor Aggressor; [Sync] public Actor TargetedActor; + // NOT SYNCED: do not refer to this anywhere other than UI code + public UnitStance PredictedStance; + public AutoTarget(Actor self, AutoTargetInfo info) { - Info = info; + this.info = info; attack = self.Trait(); - stance = Info.InitialStance; - predictedStance = stance; + Stance = info.InitialStance; + PredictedStance = Stance; at = self.TraitOrDefault(); } public void ResolveOrder(Actor self, Order order) { if (order.OrderString == "SetUnitStance") - stance = (UnitStance)order.TargetLocation.X; + Stance = (UnitStance)order.TargetLocation.X; } public void Damaged(Actor self, AttackInfo e) { - if (!self.IsIdle) return; - if (e.Attacker.Destroyed) return; + if (!self.IsIdle) + return; - if (stance < UnitStance.ReturnFire) return; + if (e.Attacker.Destroyed) + return; + + if (Stance < UnitStance.ReturnFire) return; // not a lot we can do about things we can't hurt... although maybe we should automatically run away? - if (!attack.HasAnyValidWeapons(Target.FromActor(e.Attacker))) return; + if (!attack.HasAnyValidWeapons(Target.FromActor(e.Attacker))) + return; - // don't retaliate against own units force-firing on us. it's usually not what the player wanted. - if (e.Attacker.AppearsFriendlyTo(self)) return; + // don't retaliate against own units force-firing on us. It's usually not what the player wanted. + if (e.Attacker.AppearsFriendlyTo(self)) + return; - if (e.Damage < 0) return; // don't retaliate against healers + // don't retaliate against healers + if (e.Damage < 0) + return; Aggressor = e.Attacker; - if (at == null || !at.IsReachableTarget(at.Target, Info.AllowMovement && stance != UnitStance.Defend)) + if (at == null || !at.IsReachableTarget(at.Target, info.AllowMovement && Stance != UnitStance.Defend)) Attack(self, e.Attacker); } public void TickIdle(Actor self) { - if (stance < UnitStance.Defend) return; + if (Stance < UnitStance.Defend) + return; - if (at == null || !at.IsReachableTarget(at.Target, Info.AllowMovement && stance != UnitStance.Defend)) + var allowMovement = info.AllowMovement && Stance != UnitStance.Defend; + if (at == null || !at.IsReachableTarget(at.Target, allowMovement)) ScanAndAttack(self); } @@ -99,7 +109,7 @@ namespace OpenRA.Mods.RA public Actor ScanForTarget(Actor self, Actor currentTarget) { - var range = Info.ScanRadius > 0 ? WRange.FromCells(Info.ScanRadius) : attack.GetMaximumRange(); + var range = info.ScanRadius > 0 ? WRange.FromCells(info.ScanRadius) : attack.GetMaximumRange(); if (self.IsIdle || currentTarget == null || !Target.FromActor(currentTarget).IsInRange(self.CenterPosition, range)) if (nextScanTime <= 0) return ChooseTarget(self, range); @@ -119,12 +129,12 @@ namespace OpenRA.Mods.RA TargetedActor = targetActor; var target = Target.FromActor(targetActor); self.SetTargetLine(target, Color.Red, false); - attack.AttackTarget(target, false, Info.AllowMovement && stance != UnitStance.Defend); + attack.AttackTarget(target, false, info.AllowMovement && Stance != UnitStance.Defend); } Actor ChooseTarget(Actor self, WRange range) { - nextScanTime = self.World.SharedRandom.Next(Info.MinimumScanTimeInterval, Info.MaximumScanTimeInterval); + nextScanTime = self.World.SharedRandom.Next(info.MinimumScanTimeInterval, info.MaximumScanTimeInterval); var inRange = self.World.FindActorsInCircle(self.CenterPosition, range); if (self.Owner.HasFogVisibility()) @@ -150,5 +160,4 @@ namespace OpenRA.Mods.RA [Desc("Will not get automatically targeted by enemy (like walls)")] class AutoTargetIgnoreInfo : TraitInfo { } class AutoTargetIgnore { } - } diff --git a/OpenRA.Mods.RA/Missions/Allies01Script.cs b/OpenRA.Mods.RA/Missions/Allies01Script.cs index 010cdc8a0d..868e46c3e5 100644 --- a/OpenRA.Mods.RA/Missions/Allies01Script.cs +++ b/OpenRA.Mods.RA/Missions/Allies01Script.cs @@ -272,7 +272,7 @@ namespace OpenRA.Mods.RA.Missions void SetAlliedUnitsToDefensiveStance() { foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == allies && !a.IsDead() && a.HasTrait())) - actor.Trait().stance = UnitStance.Defend; + actor.Trait().Stance = UnitStance.Defend; } public void WorldLoaded(World w, WorldRenderer wr) diff --git a/OpenRA.Mods.RA/Missions/Allies04Script.cs b/OpenRA.Mods.RA/Missions/Allies04Script.cs index a52787084d..5b1a3e41f8 100644 --- a/OpenRA.Mods.RA/Missions/Allies04Script.cs +++ b/OpenRA.Mods.RA/Missions/Allies04Script.cs @@ -349,7 +349,7 @@ namespace OpenRA.Mods.RA.Missions void SetupSubStances() { foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait())) - actor.Trait().stance = UnitStance.Defend; + actor.Trait().Stance = UnitStance.Defend; } public void WorldLoaded(World w, WorldRenderer wr) diff --git a/OpenRA.Mods.RA/Missions/DesertShellmapScript.cs b/OpenRA.Mods.RA/Missions/DesertShellmapScript.cs index b578c89729..c9d7b0dc6e 100644 --- a/OpenRA.Mods.RA/Missions/DesertShellmapScript.cs +++ b/OpenRA.Mods.RA/Missions/DesertShellmapScript.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#region Copyright & License Information /* * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made @@ -290,7 +290,7 @@ namespace OpenRA.Mods.RA.Missions foreach (var actor in actors.Values) { if (actor.Owner == allies && actor.HasTrait()) - actor.Trait().stance = UnitStance.Defend; + actor.Trait().Stance = UnitStance.Defend; if (actor.IsInWorld && (actor.HasTrait() || actor.Owner == allies || (actor.Owner == soviets && actor.HasTrait()))) actor.AddTrait(new Invulnerable()); diff --git a/OpenRA.Mods.RA/Missions/Survival02Script.cs b/OpenRA.Mods.RA/Missions/Survival02Script.cs index dcace57143..7139793f37 100644 --- a/OpenRA.Mods.RA/Missions/Survival02Script.cs +++ b/OpenRA.Mods.RA/Missions/Survival02Script.cs @@ -123,7 +123,7 @@ namespace OpenRA.Mods.RA.Missions void SetSovietUnitsToDefensiveStance() { foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait())) - actor.Trait().stance = UnitStance.Defend; + actor.Trait().Stance = UnitStance.Defend; } Actor FirstUnshroudedOrDefault(IEnumerable actors, World world, int shroudRange) diff --git a/OpenRA.Mods.RA/Scripting/LuaScriptInterface.cs b/OpenRA.Mods.RA/Scripting/LuaScriptInterface.cs index 61979cc596..9c52a48dde 100644 --- a/OpenRA.Mods.RA/Scripting/LuaScriptInterface.cs +++ b/OpenRA.Mods.RA/Scripting/LuaScriptInterface.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#region Copyright & License Information /* * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made @@ -283,7 +283,7 @@ namespace OpenRA.Mods.RA.Scripting { var at = actor.TraitOrDefault(); if (at != null) - at.stance = Enum.Parse(stance); + at.Stance = Enum.Parse(stance); } [LuaGlobal] diff --git a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs index c86534ea1c..ca05bf27ed 100644 --- a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs +++ b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#region Copyright & License Information /* * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made @@ -150,7 +150,7 @@ namespace OpenRA.Mods.RA.Widgets var stances = Enum.GetValues(); var nextStance = stances.Concat(stances) - .SkipWhile(s => s != actor.Second.predictedStance) + .SkipWhile(s => s != actor.Second.PredictedStance) .Skip(1) .First(); @@ -158,7 +158,7 @@ namespace OpenRA.Mods.RA.Widgets { var at = a.TraitOrDefault(); if (at != null) - at.predictedStance = nextStance; + at.PredictedStance = nextStance; // FIXME: Abuse of the type system here with `CPos` return new Order("SetUnitStance", a, false) { TargetLocation = new CPos((int)nextStance, 0) };