Changed: From running on the local player, to running on all players

This commit is contained in:
geckosoft
2010-11-11 06:49:26 +01:00
committed by Bob
parent bd9c748b17
commit 05f6958286
9 changed files with 102 additions and 111 deletions

View File

@@ -167,13 +167,6 @@ namespace OpenRA.Mods.RA
return; return;
} // else not an attack order } // 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; target = Target.None;
/* hack */ /* hack */

View File

@@ -98,7 +98,6 @@
<Compile Include="Buildings\Util.cs" /> <Compile Include="Buildings\Util.cs" />
<Compile Include="UnitStances\AssignUnitStance.cs" /> <Compile Include="UnitStances\AssignUnitStance.cs" />
<Compile Include="UnitStances\UnitStanceHoldFire.cs" /> <Compile Include="UnitStances\UnitStanceHoldFire.cs" />
<Compile Include="UnitStances\UnitStanceNone.cs" />
<Compile Include="UnitStances\UnitStanceReturnFire.cs" /> <Compile Include="UnitStances\UnitStanceReturnFire.cs" />
<Compile Include="UnitStances\UnitStanceGuard.cs" /> <Compile Include="UnitStances\UnitStanceGuard.cs" />
<Compile Include="Valued.cs" /> <Compile Include="Valued.cs" />

View File

@@ -1,79 +1,66 @@
using System; using System;
using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA 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 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 #region ITraitInfo Members
public virtual object Create(ActorInitializer init) public virtual object Create(ActorInitializer init)
{ {
throw new Exception("Do not use UnitStance at the rules!"); throw new Exception("UnitStanceInfo: Override me!");
} }
#endregion #endregion
} }
public abstract class UnitStance : IUnitStance, ITick public abstract class UnitStance : ITick, IResolveOrder, ISelectionColorModifier
{ {
public int NextScantime; [Sync]
public int ScanDelay = 12; // 2x - second public int NextScanTime;
private bool _unsetFirstTick;
public UnitStanceInfo Info { get; protected set; } public UnitStanceInfo Info { get; protected set; }
public abstract Color SelectionColor { get; }
public bool IsFirstTick { get; private set; }
public bool IsScanAvailable
{
get
{
NextScantime--;
if (NextScantime <= 0)
{
NextScantime = ScanDelay;
return true;
}
return false;
}
}
#region ITick Members #region ITick Members
protected UnitStance(Actor self, UnitStanceInfo info)
{
Info = info;
Active = Info.Default;
}
public virtual void Tick(Actor self) public virtual void Tick(Actor self)
{ {
if (!Active) return; if (!Active) return;
if (IsFirstTick && _unsetFirstTick) TickScan(self);
{ }
IsFirstTick = false;
_unsetFirstTick = false;
}
else if (IsFirstTick)
{
_unsetFirstTick = true;
OnFirstTick(self);
}
if (IsScanAvailable) private void TickScan(Actor self)
{
NextScanTime--;
if (NextScanTime <= 0)
{ {
NextScanTime = GetNextScanTime(self);
OnScan(self); OnScan(self);
} }
} }
private int GetNextScanTime(Actor self)
{
return self.World.SharedRandom.Next(Info.ScanDelayMin, Info.ScanDelayMax+1);
}
#endregion #endregion
#region IUnitStance Members #region IUnitStance Members
@@ -90,11 +77,9 @@ namespace OpenRA.Mods.RA
if (Active) return; if (Active) return;
Active = true; Active = true;
IsFirstTick = true; NextScanTime = 0;
NextScantime = 0;
_unsetFirstTick = false;
DeactivateOthers(self); DeactivateOthers(self);
OnActivate(self);
} }
public virtual void Deactivate(Actor self) public virtual void Deactivate(Actor self)
@@ -119,22 +104,12 @@ namespace OpenRA.Mods.RA
return stance != null && stance.Active; return stance != null && stance.Active;
} }
public static void ActivateDefault(Actor self) public static void DeactivateOthers(Actor self, UnitStance stance)
{ {
if (!self.TraitsImplementing<IUnitStance>().Where(t => t.IsDefault).Any()) self.TraitsImplementing<UnitStance>().Where(t => t != stance).Do(t => t.Deactivate(self));
{
// deactive all of them as a default if nobody has a default
DeactivateOthers(self, null);
return;
}
self.TraitsImplementing<IUnitStance>().Where(t => t.IsDefault).First().Activate(self);
} }
public static void DeactivateOthers(Actor self, IUnitStance stance) public abstract string OrderString { get; }
{
self.TraitsImplementing<IUnitStance>().Where(t => t != stance).Do(t => t.Deactivate(self));
}
public static bool ReturnFire(Actor self, AttackInfo e, bool allowActivity, bool allowTargetSwitch, bool holdStill) 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) 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) 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);
} }
/// <summary> /// <summary>
@@ -206,7 +182,7 @@ namespace OpenRA.Mods.RA
/// Called when on the first tick after the stance has been activated /// Called when on the first tick after the stance has been activated
/// </summary> /// </summary>
/// <param name="self"></param> /// <param name="self"></param>
protected virtual void OnFirstTick(Actor self) protected virtual void OnActivate(Actor self)
{ {
} }
@@ -214,5 +190,28 @@ namespace OpenRA.Mods.RA
{ {
return self.Trait<AttackBase>().ScanForTarget(self); return self.Trait<AttackBase>().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;
}
} }
} }

View File

@@ -15,9 +15,14 @@ namespace OpenRA.Mods.RA
public class UnitStanceAggressive : UnitStance, INotifyDamage, ISelectionColorModifier public class UnitStanceAggressive : UnitStance, INotifyDamage, ISelectionColorModifier
{ {
public UnitStanceAggressive(Actor self, UnitStanceAggressiveInfo info) 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) protected override void OnScan(Actor self)
@@ -32,7 +37,7 @@ namespace OpenRA.Mods.RA
AttackTarget(self, target, false); AttackTarget(self, target, false);
} }
protected override void OnFirstTick(Actor self) protected override void OnActivate(Actor self)
{ {
if (!self.HasTrait<AttackBase>()) return; if (!self.HasTrait<AttackBase>()) return;
@@ -48,9 +53,9 @@ namespace OpenRA.Mods.RA
ReturnFire(self, e, false); // only triggers when standing still 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; }
} }
} }
} }

View File

@@ -13,9 +13,15 @@ namespace OpenRA.Mods.RA
public class UnitStanceGuard : UnitStance, INotifyDamage, ISelectionColorModifier public class UnitStanceGuard : UnitStance, INotifyDamage, ISelectionColorModifier
{ {
public UnitStanceGuard(Actor self, UnitStanceGuardInfo info) 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) protected override void OnScan(Actor self)
@@ -30,7 +36,7 @@ namespace OpenRA.Mods.RA
AttackTarget(self, target, true); AttackTarget(self, target, true);
} }
protected override void OnFirstTick(Actor self) protected override void OnActivate(Actor self)
{ {
if (!self.HasTrait<AttackBase>()) return; if (!self.HasTrait<AttackBase>()) return;
@@ -46,9 +52,9 @@ namespace OpenRA.Mods.RA
ReturnFire(self, e, false, false, true); // only triggers when standing still 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; }
} }
} }
} }

View File

@@ -16,13 +16,18 @@ namespace OpenRA.Mods.RA
/// </summary> /// </summary>
public class UnitStanceHoldFire : UnitStance, ISelectionColorModifier 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<AttackBase>()) return; if (!self.HasTrait<AttackBase>()) return;
@@ -30,9 +35,9 @@ namespace OpenRA.Mods.RA
StopAttack(self); StopAttack(self);
} }
public Color GetSelectionColorModifier(Actor self, Color defaultColor) public override Color SelectionColor
{ {
return Active ? Color.SpringGreen : defaultColor; get { return Color.SpringGreen; }
} }
} }
} }

View File

@@ -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;
}
}
}

View File

@@ -17,9 +17,9 @@ namespace OpenRA.Mods.RA
public class UnitStanceReturnFire : UnitStance, INotifyDamage, ISelectionColorModifier public class UnitStanceReturnFire : UnitStance, INotifyDamage, ISelectionColorModifier
{ {
public UnitStanceReturnFire(Actor self, UnitStanceReturnFireInfo info) 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) public void Damaged(Actor self, AttackInfo e)
@@ -30,9 +30,14 @@ namespace OpenRA.Mods.RA
ReturnFire(self, e, false); // only triggers when standing still 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; }
} }
} }
} }

View File

@@ -90,7 +90,7 @@ namespace OpenRA.Mods.RA.Widgets
World.Selection.Actors.Where(a => !a.Destroyed && a.Owner == World.LocalPlayer && a.TraitOrDefault<T>() != null && !UnitStance.IsActive<T>(a)). World.Selection.Actors.Where(a => !a.Destroyed && a.Owner == World.LocalPlayer && a.TraitOrDefault<T>() != null && !UnitStance.IsActive<T>(a)).
Select(a => new Pair<Actor, T>(a, a.TraitOrDefault<T>()) ); Select(a => new Pair<Actor, T>(a, a.TraitOrDefault<T>()) );
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(); return traits.Any();
} }