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;
} // 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 */

View File

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

View File

@@ -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<IUnitStance>().Where(t => t.IsDefault).Any())
{
// deactive all of them as a default if nobody has a default
DeactivateOthers(self, null);
return;
self.TraitsImplementing<UnitStance>().Where(t => t != stance).Do(t => t.Deactivate(self));
}
self.TraitsImplementing<IUnitStance>().Where(t => t.IsDefault).First().Activate(self);
}
public static void DeactivateOthers(Actor self, IUnitStance stance)
{
self.TraitsImplementing<IUnitStance>().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);
}
/// <summary>
@@ -206,7 +182,7 @@ namespace OpenRA.Mods.RA
/// Called when on the first tick after the stance has been activated
/// </summary>
/// <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);
}
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 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<AttackBase>()) 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; }
}
}
}

View File

@@ -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<AttackBase>()) 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; }
}
}
}

View File

@@ -17,12 +17,17 @@ namespace OpenRA.Mods.RA
public class UnitStanceHoldFire : UnitStance, ISelectionColorModifier
{
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;
@@ -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; }
}
}
}

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

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)).
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();
}