From 05f69582862ab127a8959471a33661df19ec9a22 Mon Sep 17 00:00:00 2001 From: geckosoft Date: Thu, 11 Nov 2010 06:49:26 +0100 Subject: [PATCH] Changed: From running on the local player, to running on all players --- OpenRA.Mods.RA/AttackBase.cs | 7 - OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 1 - OpenRA.Mods.RA/UnitStances/UnitStance.cs | 121 +++++++++--------- .../UnitStances/UnitStanceAggressive.cs | 15 ++- OpenRA.Mods.RA/UnitStances/UnitStanceGuard.cs | 16 ++- .../UnitStances/UnitStanceHoldFire.cs | 17 ++- OpenRA.Mods.RA/UnitStances/UnitStanceNone.cs | 21 --- .../UnitStances/UnitStanceReturnFire.cs | 13 +- OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs | 2 +- 9 files changed, 102 insertions(+), 111 deletions(-) delete mode 100644 OpenRA.Mods.RA/UnitStances/UnitStanceNone.cs diff --git a/OpenRA.Mods.RA/AttackBase.cs b/OpenRA.Mods.RA/AttackBase.cs index d9eb4e42a6..eb4cdc990f 100644 --- a/OpenRA.Mods.RA/AttackBase.cs +++ b/OpenRA.Mods.RA/AttackBase.cs @@ -167,13 +167,6 @@ namespace OpenRA.Mods.RA return; } // else not an attack order - // StopAttack order cancels the current activity IF it is an attack one - if (order.OrderString == "StopAttack") - { - if (self.GetCurrentActivity() is Activities.Attack) - self.GetCurrentActivity().Cancel(self); - } - target = Target.None; /* hack */ diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 8de12b9924..8e4864abba 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -98,7 +98,6 @@ - diff --git a/OpenRA.Mods.RA/UnitStances/UnitStance.cs b/OpenRA.Mods.RA/UnitStances/UnitStance.cs index ab539a21d5..fc05a57e67 100644 --- a/OpenRA.Mods.RA/UnitStances/UnitStance.cs +++ b/OpenRA.Mods.RA/UnitStances/UnitStance.cs @@ -1,79 +1,66 @@ using System; +using System.Drawing; using System.Linq; using OpenRA.Traits; namespace OpenRA.Mods.RA { - public interface IUnitStance - { - bool Active { get; set; } - bool IsDefault { get; } - void Activate(Actor self); - void Deactivate(Actor self); - } public class UnitStanceInfo : ITraitInfo { - public readonly bool Default; + public readonly bool Default = false; + public readonly int ScanDelayMin = 12; + public readonly int ScanDelayMax = 24; #region ITraitInfo Members public virtual object Create(ActorInitializer init) { - throw new Exception("Do not use UnitStance at the rules!"); + throw new Exception("UnitStanceInfo: Override me!"); } #endregion } - public abstract class UnitStance : IUnitStance, ITick + public abstract class UnitStance : ITick, IResolveOrder, ISelectionColorModifier { - public int NextScantime; - public int ScanDelay = 12; // 2x - second - private bool _unsetFirstTick; + [Sync] + public int NextScanTime; public UnitStanceInfo Info { get; protected set; } - - public bool IsFirstTick { get; private set; } - - public bool IsScanAvailable - { - get - { - NextScantime--; - if (NextScantime <= 0) - { - NextScantime = ScanDelay; - return true; - } - - return false; - } - } + public abstract Color SelectionColor { get; } #region ITick Members + protected UnitStance(Actor self, UnitStanceInfo info) + { + Info = info; + Active = Info.Default; + } + public virtual void Tick(Actor self) { if (!Active) return; - if (IsFirstTick && _unsetFirstTick) - { - IsFirstTick = false; - _unsetFirstTick = false; - } - else if (IsFirstTick) - { - _unsetFirstTick = true; - OnFirstTick(self); - } + TickScan(self); + } - if (IsScanAvailable) + private void TickScan(Actor self) + { + NextScanTime--; + + if (NextScanTime <= 0) { + NextScanTime = GetNextScanTime(self); OnScan(self); } } + private int GetNextScanTime(Actor self) + { + return self.World.SharedRandom.Next(Info.ScanDelayMin, Info.ScanDelayMax+1); + } + #endregion #region IUnitStance Members @@ -90,11 +77,9 @@ namespace OpenRA.Mods.RA if (Active) return; Active = true; - IsFirstTick = true; - NextScantime = 0; - _unsetFirstTick = false; - + NextScanTime = 0; DeactivateOthers(self); + OnActivate(self); } public virtual void Deactivate(Actor self) @@ -119,22 +104,12 @@ namespace OpenRA.Mods.RA return stance != null && stance.Active; } - public static void ActivateDefault(Actor self) + public static void DeactivateOthers(Actor self, UnitStance stance) { - if (!self.TraitsImplementing().Where(t => t.IsDefault).Any()) - { - // deactive all of them as a default if nobody has a default - DeactivateOthers(self, null); - return; - } - - self.TraitsImplementing().Where(t => t.IsDefault).First().Activate(self); + self.TraitsImplementing().Where(t => t != stance).Do(t => t.Deactivate(self)); } - public static void DeactivateOthers(Actor self, IUnitStance stance) - { - self.TraitsImplementing().Where(t => t != stance).Do(t => t.Deactivate(self)); - } + public abstract string OrderString { get; } public static bool ReturnFire(Actor self, AttackInfo e, bool allowActivity, bool allowTargetSwitch, bool holdStill) { @@ -185,13 +160,14 @@ namespace OpenRA.Mods.RA if (attack != null && target != null) { - self.World.IssueOrder(new Order((holdStill) ? "AttackHold" : "Attack", self, target, false)); + attack.ResolveOrder(self, new Order((holdStill) ? "AttackHold" : "Attack", self, target, false)); } } public static void StopAttack(Actor self) { - self.World.IssueOrder(new Order("StopAttack", self, self, false)); + if (self.GetCurrentActivity() is Activities.Attack) + self.GetCurrentActivity().Cancel(self); } /// @@ -206,7 +182,7 @@ namespace OpenRA.Mods.RA /// Called when on the first tick after the stance has been activated /// /// - protected virtual void OnFirstTick(Actor self) + protected virtual void OnActivate(Actor self) { } @@ -214,5 +190,28 @@ namespace OpenRA.Mods.RA { return self.Trait().ScanForTarget(self); } + + public void ResolveOrder(Actor self, Order order) + { + if (order.OrderString != OrderString) + return; + + // Its our order, activate the stance + Activate(self); + } + + public static void OrderStance(Actor self, UnitStance stance) + { + self.World.IssueOrder(new Order(stance.OrderString, self, false)); + } + + public Color GetSelectionColorModifier(Actor self, Color defaultColor) + { + + if (self.World.LocalPlayer != null && self.Owner.Stances[self.World.LocalPlayer] != Stance.Ally) + return defaultColor; + + return Active ? SelectionColor : defaultColor; + } } } \ No newline at end of file diff --git a/OpenRA.Mods.RA/UnitStances/UnitStanceAggressive.cs b/OpenRA.Mods.RA/UnitStances/UnitStanceAggressive.cs index e35ee05fd9..ade82d0de5 100644 --- a/OpenRA.Mods.RA/UnitStances/UnitStanceAggressive.cs +++ b/OpenRA.Mods.RA/UnitStances/UnitStanceAggressive.cs @@ -15,9 +15,14 @@ namespace OpenRA.Mods.RA public class UnitStanceAggressive : UnitStance, INotifyDamage, ISelectionColorModifier { public UnitStanceAggressive(Actor self, UnitStanceAggressiveInfo info) + : base(self, info) { - Info = info; - Active = (self.World.LocalPlayer == self.Owner || (self.Owner.IsBot && Game.IsHost)) ? Info.Default : false; + + } + + public override string OrderString + { + get { return "StanceAggressive"; } } protected override void OnScan(Actor self) @@ -32,7 +37,7 @@ namespace OpenRA.Mods.RA AttackTarget(self, target, false); } - protected override void OnFirstTick(Actor self) + protected override void OnActivate(Actor self) { if (!self.HasTrait()) return; @@ -48,9 +53,9 @@ namespace OpenRA.Mods.RA ReturnFire(self, e, false); // only triggers when standing still } - public Color GetSelectionColorModifier(Actor self, Color defaultColor) + public override Color SelectionColor { - return Active ? Color.Red : defaultColor; + get { return Color.Red; } } } } \ No newline at end of file diff --git a/OpenRA.Mods.RA/UnitStances/UnitStanceGuard.cs b/OpenRA.Mods.RA/UnitStances/UnitStanceGuard.cs index 82ade0777a..efc06d90d0 100644 --- a/OpenRA.Mods.RA/UnitStances/UnitStanceGuard.cs +++ b/OpenRA.Mods.RA/UnitStances/UnitStanceGuard.cs @@ -13,9 +13,15 @@ namespace OpenRA.Mods.RA public class UnitStanceGuard : UnitStance, INotifyDamage, ISelectionColorModifier { public UnitStanceGuard(Actor self, UnitStanceGuardInfo info) + : base(self, info) { - Info = info; - Active = (self.World.LocalPlayer == self.Owner || (self.Owner.IsBot && Game.IsHost)) ? Info.Default : false; + + } + + + public override string OrderString + { + get { return "StanceGuard"; } } protected override void OnScan(Actor self) @@ -30,7 +36,7 @@ namespace OpenRA.Mods.RA AttackTarget(self, target, true); } - protected override void OnFirstTick(Actor self) + protected override void OnActivate(Actor self) { if (!self.HasTrait()) return; @@ -46,9 +52,9 @@ namespace OpenRA.Mods.RA ReturnFire(self, e, false, false, true); // only triggers when standing still } - public virtual Color GetSelectionColorModifier(Actor self, Color defaultColor) + public override Color SelectionColor { - return Active ? Color.Yellow : defaultColor; + get { return Color.Yellow; } } } } \ No newline at end of file diff --git a/OpenRA.Mods.RA/UnitStances/UnitStanceHoldFire.cs b/OpenRA.Mods.RA/UnitStances/UnitStanceHoldFire.cs index 029f97fb47..6cc806c8ce 100644 --- a/OpenRA.Mods.RA/UnitStances/UnitStanceHoldFire.cs +++ b/OpenRA.Mods.RA/UnitStances/UnitStanceHoldFire.cs @@ -16,13 +16,18 @@ namespace OpenRA.Mods.RA /// public class UnitStanceHoldFire : UnitStance, ISelectionColorModifier { - public UnitStanceHoldFire(Actor self, UnitStanceHoldFireInfo info) + public UnitStanceHoldFire(Actor self, UnitStanceHoldFireInfo info) + : base(self, info) { - Info = info; - Active = (self.World.LocalPlayer == self.Owner || (self.Owner.IsBot && Game.IsHost)) ? Info.Default : false; + } - protected override void OnFirstTick(Actor self) + public override string OrderString + { + get { return "StanceHoldFire"; } + } + + protected override void OnActivate(Actor self) { if (!self.HasTrait()) return; @@ -30,9 +35,9 @@ namespace OpenRA.Mods.RA StopAttack(self); } - public Color GetSelectionColorModifier(Actor self, Color defaultColor) + public override Color SelectionColor { - return Active ? Color.SpringGreen : defaultColor; + get { return Color.SpringGreen; } } } } \ No newline at end of file diff --git a/OpenRA.Mods.RA/UnitStances/UnitStanceNone.cs b/OpenRA.Mods.RA/UnitStances/UnitStanceNone.cs deleted file mode 100644 index 826d4880a8..0000000000 --- a/OpenRA.Mods.RA/UnitStances/UnitStanceNone.cs +++ /dev/null @@ -1,21 +0,0 @@ -using OpenRA.Traits; - -namespace OpenRA.Mods.RA -{ - public class UnitStanceNoneInfo : ITraitInfo - { - public readonly bool Default = false; - - public object Create(ActorInitializer init) { return new UnitStanceNone(init.self, this); } - } - public class UnitStanceNone : UnitStance - { - public readonly UnitStanceNoneInfo Info; - - public UnitStanceNone(Actor self, UnitStanceNoneInfo info) - { - Info = info; - Active = (self.World.LocalPlayer == self.Owner || (self.Owner.IsBot && Game.IsHost)) ? Info.Default : false; - } - } -} \ No newline at end of file diff --git a/OpenRA.Mods.RA/UnitStances/UnitStanceReturnFire.cs b/OpenRA.Mods.RA/UnitStances/UnitStanceReturnFire.cs index 5e48eeaac1..0a2133260c 100644 --- a/OpenRA.Mods.RA/UnitStances/UnitStanceReturnFire.cs +++ b/OpenRA.Mods.RA/UnitStances/UnitStanceReturnFire.cs @@ -17,9 +17,9 @@ namespace OpenRA.Mods.RA public class UnitStanceReturnFire : UnitStance, INotifyDamage, ISelectionColorModifier { public UnitStanceReturnFire(Actor self, UnitStanceReturnFireInfo info) + : base(self, info) { - Info = info; - Active = (self.World.LocalPlayer == self.Owner || (self.Owner.IsBot && Game.IsHost)) ? Info.Default : false; + } public void Damaged(Actor self, AttackInfo e) @@ -30,9 +30,14 @@ namespace OpenRA.Mods.RA ReturnFire(self, e, false); // only triggers when standing still } - public Color GetSelectionColorModifier(Actor self, Color defaultColor) + public override string OrderString { - return Active ? Color.Orange : defaultColor; + get { return "StanceReturnFire"; } + } + + public override Color SelectionColor + { + get { return Color.Orange; } } } } \ No newline at end of file diff --git a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs index 194dc5de9a..1d349f1afa 100644 --- a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs +++ b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs @@ -90,7 +90,7 @@ namespace OpenRA.Mods.RA.Widgets World.Selection.Actors.Where(a => !a.Destroyed && a.Owner == World.LocalPlayer && a.TraitOrDefault() != null && !UnitStance.IsActive(a)). Select(a => new Pair(a, a.TraitOrDefault()) ); - World.AddFrameEndTask(w => traits.Do(p => p.Second.Activate(p.First))); + World.AddFrameEndTask(w => traits.Do(p => UnitStance.OrderStance(p.First, p.Second))); return traits.Any(); }