refactor activity queueing

This commit is contained in:
Bob
2010-09-22 10:13:13 +12:00
parent 271a3eea8d
commit ef665df2e9
37 changed files with 229 additions and 236 deletions

View File

@@ -151,16 +151,9 @@ namespace OpenRA
public void QueueActivity( IActivity nextActivity ) public void QueueActivity( IActivity nextActivity )
{ {
if( currentActivity == null ) if( currentActivity == null )
{
currentActivity = nextActivity; currentActivity = nextActivity;
return; else
} currentActivity.Queue( nextActivity );
var act = currentActivity;
while( act.NextActivity != null )
{
act = act.NextActivity;
}
act.NextActivity = nextActivity;
} }
public void CancelActivity() public void CancelActivity()

View File

@@ -14,7 +14,7 @@ namespace OpenRA.Traits.Activities
{ {
public class Drag : IActivity public class Drag : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
float2 endLocation; float2 endLocation;
float2 startLocation; float2 startLocation;
@@ -40,5 +40,13 @@ namespace OpenRA.Traits.Activities
} }
public void Cancel(Actor self) { } public void Cancel(Actor self) { }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -12,9 +12,17 @@ namespace OpenRA.Traits.Activities
{ {
class Idle : IActivity class Idle : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
public IActivity Tick(Actor self) { return NextActivity; } public IActivity Tick(Actor self) { return NextActivity; }
public void Cancel(Actor self) {} public void Cancel(Actor self) {}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Traits.Activities
{ {
public class Move : IActivity public class Move : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
int2? destination; int2? destination;
int nearEnough; int nearEnough;
@@ -164,9 +164,7 @@ namespace OpenRA.Traits.Activities
Log.Write("debug", "Turn: #{0} from {1} to {2}", Log.Write("debug", "Turn: #{0} from {1} to {2}",
self.ActorID, mobile.Facing, firstFacing); self.ActorID, mobile.Facing, firstFacing);
return new Turn( firstFacing ) return Util.SequenceActivities( new Turn( firstFacing ), this ).Tick( self );
{ NextActivity = this }
.Tick( self );
} }
else else
{ {
@@ -261,6 +259,14 @@ namespace OpenRA.Traits.Activities
NextActivity = null; NextActivity = null;
} }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
abstract class MovePart : IActivity abstract class MovePart : IActivity
{ {
public readonly Move move; public readonly Move move;
@@ -280,11 +286,14 @@ namespace OpenRA.Traits.Activities
this.moveFractionTotal = (int)(( to - from ).Length*3); this.moveFractionTotal = (int)(( to - from ).Length*3);
} }
public IActivity NextActivity { get { return move; } set { move.NextActivity = value; } }
public void Cancel( Actor self ) public void Cancel( Actor self )
{ {
NextActivity.Cancel( self ); move.Cancel( self );
}
public void Queue( IActivity activity )
{
move.Queue( activity );
} }
public IActivity Tick( Actor self ) public IActivity Tick( Actor self )

View File

@@ -10,18 +10,13 @@
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
public class RemoveSelf : IActivity public class RemoveSelf : CancelableActivity
{ {
bool isCanceled; public override IActivity Tick(Actor self)
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
self.Destroy(); self.Destroy();
return null; return null;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -12,7 +12,7 @@ namespace OpenRA.Traits.Activities
{ {
class Sell : IActivity class Sell : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
bool started; bool started;
@@ -55,5 +55,13 @@ namespace OpenRA.Traits.Activities
} }
public void Cancel(Actor self) { /* never gonna give you up.. */ } public void Cancel(Actor self) { /* never gonna give you up.. */ }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -12,9 +12,8 @@ using System.Linq;
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
public class Turn : IActivity public class Turn : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
int desiredFacing; int desiredFacing;
public Turn( int desiredFacing ) public Turn( int desiredFacing )
@@ -22,8 +21,9 @@ namespace OpenRA.Traits.Activities
this.desiredFacing = desiredFacing; this.desiredFacing = desiredFacing;
} }
public IActivity Tick( Actor self ) public override IActivity Tick( Actor self )
{ {
if (IsCanceled) return NextActivity;
var facing = self.Trait<IFacing>(); var facing = self.Trait<IFacing>();
if( desiredFacing == facing.Facing ) if( desiredFacing == facing.Facing )
@@ -32,11 +32,5 @@ namespace OpenRA.Traits.Activities
return this; return this;
} }
public void Cancel( Actor self )
{
desiredFacing = self.Trait<IFacing>().Facing;
NextActivity = null;
}
} }
} }

View File

@@ -170,9 +170,33 @@ namespace OpenRA.Traits
public interface IActivity public interface IActivity
{ {
IActivity NextActivity { get; set; }
IActivity Tick(Actor self); IActivity Tick(Actor self);
void Cancel(Actor self); void Cancel(Actor self);
void Queue(IActivity activity);
}
public abstract class CancelableActivity : IActivity
{
protected IActivity NextActivity { get; private set; }
protected bool IsCanceled { get; private set; }
public abstract IActivity Tick( Actor self );
protected virtual void OnCancel() {}
public void Cancel( Actor self )
{
IsCanceled = true;
NextActivity = null;
OnCancel();
}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
public interface IRenderOverlay { void Render(); } public interface IRenderOverlay { void Render(); }

View File

@@ -113,7 +113,7 @@ namespace OpenRA.Traits
public static IActivity SequenceActivities(params IActivity[] acts) public static IActivity SequenceActivities(params IActivity[] acts)
{ {
return acts.Reverse().Aggregate( return acts.Reverse().Aggregate(
(next, a) => { a.NextActivity = next; return a; }); (next, a) => { a.Queue( next ); return a; });
} }
public static Color ArrayToColor(int[] x) { return Color.FromArgb(x[0], x[1], x[2]); } public static Color ArrayToColor(int[] x) { return Color.FromArgb(x[0], x[1], x[2]); }

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
/* non-turreted attack */ /* non-turreted attack */
public class Attack : IActivity public class Attack : CancelableActivity
{ {
Target Target; Target Target;
int Range; int Range;
@@ -26,10 +26,11 @@ namespace OpenRA.Mods.RA.Activities
Range = range; Range = range;
} }
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
public IActivity Tick( Actor self ) public override IActivity Tick( Actor self )
{ {
if (IsCanceled) return NextActivity;
var facing = self.Trait<IFacing>(); var facing = self.Trait<IFacing>();
if (!Target.IsValid) if (!Target.IsValid)
return NextActivity; return NextActivity;
@@ -37,7 +38,7 @@ namespace OpenRA.Mods.RA.Activities
var targetCell = Util.CellContaining(Target.CenterLocation); var targetCell = Util.CellContaining(Target.CenterLocation);
if ((targetCell - self.Location).LengthSquared >= Range * Range) if ((targetCell - self.Location).LengthSquared >= Range * Range)
return new Move( Target, Range ) { NextActivity = this }; return Util.SequenceActivities( new Move( Target, Range ), this );
var desiredFacing = Util.GetFacing((targetCell - self.Location).ToFloat2(), 0); var desiredFacing = Util.GetFacing((targetCell - self.Location).ToFloat2(), 0);
var renderUnit = self.TraitOrDefault<RenderUnit>(); var renderUnit = self.TraitOrDefault<RenderUnit>();
@@ -47,7 +48,7 @@ namespace OpenRA.Mods.RA.Activities
if (Util.QuantizeFacing(facing.Facing, numDirs) if (Util.QuantizeFacing(facing.Facing, numDirs)
!= Util.QuantizeFacing(desiredFacing, numDirs)) != Util.QuantizeFacing(desiredFacing, numDirs))
{ {
return new Turn( desiredFacing ) { NextActivity = this }; return Util.SequenceActivities( new Turn( desiredFacing ), this );
} }
var attack = self.Trait<AttackBase>(); var attack = self.Trait<AttackBase>();
@@ -55,7 +56,5 @@ namespace OpenRA.Mods.RA.Activities
attack.DoAttack(self); attack.DoAttack(self);
return this; return this;
} }
public void Cancel(Actor self) { Target = Target.None; }
} }
} }

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA.Activities
Action a; Action a;
bool interruptable; bool interruptable;
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
public IActivity Tick(Actor self) public IActivity Tick(Actor self)
{ {
@@ -40,5 +40,13 @@ namespace OpenRA.Mods.RA.Activities
a = null; a = null;
NextActivity = null; NextActivity = null;
} }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -12,16 +12,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class CaptureBuilding : IActivity class CaptureBuilding : CancelableActivity
{ {
Target target; Target target;
public CaptureBuilding(Actor target) { this.target = Target.FromActor(target); } public CaptureBuilding(Actor target) { this.target = Target.FromActor(target); }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity;
if (!target.IsValid) return NextActivity; if (!target.IsValid) return NextActivity;
if ((target.Actor.Location - self.Location).Length > 1) if ((target.Actor.Location - self.Location).Length > 1)
return NextActivity; return NextActivity;
@@ -41,7 +40,5 @@ namespace OpenRA.Mods.RA.Activities
}); });
return NextActivity; return NextActivity;
} }
public void Cancel(Actor self) { target = Target.None; NextActivity = null; }
} }
} }

View File

@@ -15,7 +15,7 @@ namespace OpenRA.Mods.RA.Activities
{ {
public class DeliverResources : IActivity public class DeliverResources : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
bool isDocking; bool isDocking;
@@ -32,25 +32,33 @@ namespace OpenRA.Mods.RA.Activities
harv.ChooseNewProc(self, null); harv.ChooseNewProc(self, null);
if (harv.LinkedProc == null) // no procs exist; check again in 1s. if (harv.LinkedProc == null) // no procs exist; check again in 1s.
return new Wait(25) { NextActivity = this }; return Util.SequenceActivities( new Wait(25), this );
var proc = harv.LinkedProc; var proc = harv.LinkedProc;
if( self.Location != proc.Location + proc.Trait<IAcceptOre>().DeliverOffset ) if( self.Location != proc.Location + proc.Trait<IAcceptOre>().DeliverOffset )
{ {
return new Move(proc.Location + proc.Trait<IAcceptOre>().DeliverOffset, 0) { NextActivity = this }; return Util.SequenceActivities( new Move(proc.Location + proc.Trait<IAcceptOre>().DeliverOffset, 0), this );
} }
else if (!isDocking) else if (!isDocking)
{ {
isDocking = true; isDocking = true;
proc.Trait<IAcceptOre>().OnDock(self, this); proc.Trait<IAcceptOre>().OnDock(self, this);
} }
return new Wait(10) { NextActivity = this }; return Util.SequenceActivities( new Wait(10), this );
} }
public void Cancel(Actor self) public void Cancel(Actor self)
{ {
// TODO: allow canceling of deliver orders? // TODO: allow canceling of deliver orders?
} }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -13,15 +13,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class Demolish : IActivity class Demolish : CancelableActivity
{ {
Target target; Target target;
public IActivity NextActivity { get; set; }
public Demolish( Actor target ) { this.target = Target.FromActor(target); } public Demolish( Actor target ) { this.target = Target.FromActor(target); }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if( IsCanceled ) return NextActivity;
if (!target.IsValid) return NextActivity; if (!target.IsValid) return NextActivity;
if ((target.Actor.Location - self.Location).Length > 1) if ((target.Actor.Location - self.Location).Length > 1)
return NextActivity; return NextActivity;
@@ -31,7 +31,5 @@ namespace OpenRA.Mods.RA.Activities
() => { if (target.IsValid) target.Actor.Kill(self); }))); () => { if (target.IsValid) target.Actor.Kill(self); })));
return NextActivity; return NextActivity;
} }
public void Cancel(Actor self) { target = Target.None; NextActivity = null; }
} }
} }

View File

@@ -12,10 +12,8 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class EnterTransport : IActivity class EnterTransport : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
public Actor transport; public Actor transport;
public EnterTransport(Actor self, Actor transport) public EnterTransport(Actor self, Actor transport)
@@ -23,9 +21,9 @@ namespace OpenRA.Mods.RA.Activities
this.transport = transport; this.transport = transport;
} }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (transport == null || !transport.IsInWorld) return NextActivity; if (transport == null || !transport.IsInWorld) return NextActivity;
var cargo = transport.Trait<Cargo>(); var cargo = transport.Trait<Cargo>();
@@ -43,7 +41,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -14,21 +14,18 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Fly : IActivity public class Fly : CancelableActivity
{ {
public readonly float2 Pos; public readonly float2 Pos;
bool isCanceled;
public Fly(float2 pos) { Pos = pos; } public Fly(float2 pos) { Pos = pos; }
public Fly(int2 pos) { Pos = Util.CenterOfCell(pos); } public Fly(int2 pos) { Pos = Util.CenterOfCell(pos); }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
var cruiseAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude; var cruiseAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
var d = Pos - self.CenterLocation; var d = Pos - self.CenterLocation;
if (d.LengthSquared < 50) /* close enough */ if (d.LengthSquared < 50) /* close enough */
@@ -46,8 +43,6 @@ namespace OpenRA.Mods.RA.Activities
FlyUtil.Fly(self, cruiseAltitude); FlyUtil.Fly(self, cruiseAltitude);
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
public static class FlyUtil public static class FlyUtil

View File

@@ -12,15 +12,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class FlyAttack : IActivity public class FlyAttack : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
Target Target; Target Target;
public FlyAttack(Target target) { Target = target; } public FlyAttack(Target target) { Target = target; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity;
if (!Target.IsValid) if (!Target.IsValid)
return NextActivity; return NextActivity;
@@ -33,29 +33,22 @@ namespace OpenRA.Mods.RA.Activities
new FlyTimed(50), new FlyTimed(50),
this); this);
} }
public void Cancel(Actor self) { Target = Target.None; NextActivity = null; }
} }
public class FlyCircle : IActivity public class FlyCircle : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
int2 Target; int2 Target;
bool isCanceled;
public FlyCircle(int2 target) { Target = target; } public FlyCircle(int2 target) { Target = target; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) if( IsCanceled ) return NextActivity;
return NextActivity;
return Util.SequenceActivities( return Util.SequenceActivities(
new Fly(Util.CenterOfCell(Target)), new Fly(Util.CenterOfCell(Target)),
new FlyTimed(50), new FlyTimed(50),
this); this);
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -12,27 +12,25 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class FlyTimed : IActivity public class FlyTimed : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
int remainingTicks; int remainingTicks;
public FlyTimed(int ticks) { remainingTicks = ticks; } public FlyTimed(int ticks) { remainingTicks = ticks; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if( IsCanceled ) return NextActivity;
var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude; var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (remainingTicks-- == 0) return NextActivity; if (remainingTicks-- == 0) return NextActivity;
FlyUtil.Fly(self, targetAltitude); FlyUtil.Fly(self, targetAltitude);
return this; return this;
} }
public void Cancel(Actor self) { remainingTicks = 0; NextActivity = null; }
} }
public class FlyOffMap : IActivity public class FlyOffMap : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
bool isCanceled; bool isCanceled;
public bool Interruptible = true; public bool Interruptible = true;
@@ -52,6 +50,13 @@ namespace OpenRA.Mods.RA.Activities
NextActivity = null; NextActivity = null;
} }
} }
}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
}
} }

View File

@@ -13,7 +13,7 @@ using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Follow : IActivity public class Follow : CancelableActivity
{ {
Target Target; Target Target;
int Range; int Range;
@@ -24,24 +24,18 @@ namespace OpenRA.Mods.RA.Activities
Range = range; Range = range;
} }
public IActivity NextActivity { get; set; } public override IActivity Tick( Actor self )
public IActivity Tick( Actor self )
{ {
if (!Target.IsValid) if (IsCanceled) return NextActivity;
return NextActivity; if (!Target.IsValid) return NextActivity;
var inRange = ( Util.CellContaining( Target.CenterLocation ) - self.Location ).LengthSquared < Range * Range; var inRange = ( Util.CellContaining( Target.CenterLocation ) - self.Location ).LengthSquared < Range * Range;
if( !inRange ) if( inRange ) return this;
return new Move( Target, Range ) { NextActivity = this };
return this; var ret = new Move( Target, Range );
} ret.Queue( this );
return ret;
public void Cancel(Actor self)
{
Target = Target.None;
} }
} }
} }

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.RA.Activities
{ {
public class Harvest : IActivity public class Harvest : IActivity
{ {
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
bool isHarvesting = false; bool isHarvesting = false;
public IActivity Tick( Actor self ) public IActivity Tick( Actor self )
@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA.Activities
harv.LastHarvestedCell = self.Location; harv.LastHarvestedCell = self.Location;
if( harv.IsFull ) if( harv.IsFull )
return new DeliverResources { NextActivity = NextActivity }; return Util.SequenceActivities( new DeliverResources(), NextActivity );
if (HarvestThisTile(self)) if (HarvestThisTile(self))
return this; return this;
@@ -74,5 +74,13 @@ namespace OpenRA.Mods.RA.Activities
} }
public void Cancel(Actor self) { } public void Cancel(Actor self) { }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -14,17 +14,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class HeliAttack : IActivity public class HeliAttack : CancelableActivity
{ {
Target target; Target target;
public HeliAttack( Target target ) { this.target = target; } public HeliAttack( Target target ) { this.target = target; }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
if (!target.IsValid) if (IsCanceled) return NextActivity;
return NextActivity; if (!target.IsValid) return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>(); var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo()) if (limitedAmmo != null && !limitedAmmo.HasAmmo())
@@ -53,7 +51,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { target = Target.None; NextActivity = null; }
} }
} }

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class HeliFly : IActivity class HeliFly : CancelableActivity
{ {
public readonly float2 Dest; public readonly float2 Dest;
public HeliFly(float2 dest) public HeliFly(float2 dest)
@@ -22,13 +22,9 @@ namespace OpenRA.Mods.RA.Activities
Dest = dest; Dest = dest;
} }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
bool isCanceled;
public IActivity Tick(Actor self)
{ {
if (isCanceled) if (IsCanceled) return NextActivity;
return NextActivity;
var info = self.Info.Traits.Get<HelicopterInfo>(); var info = self.Info.Traits.Get<HelicopterInfo>();
var aircraft = self.Trait<Aircraft>(); var aircraft = self.Trait<Aircraft>();
@@ -57,7 +53,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -12,17 +12,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class HeliLand : IActivity class HeliLand : CancelableActivity
{ {
public HeliLand(bool requireSpace) { this.requireSpace = requireSpace; } public HeliLand(bool requireSpace) { this.requireSpace = requireSpace; }
bool requireSpace; bool requireSpace;
bool isCanceled;
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
var aircraft = self.Trait<Aircraft>(); var aircraft = self.Trait<Aircraft>();
if (aircraft.Altitude == 0) if (aircraft.Altitude == 0)
return NextActivity; return NextActivity;
@@ -33,7 +31,5 @@ namespace OpenRA.Mods.RA.Activities
--aircraft.Altitude; --aircraft.Altitude;
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -14,11 +14,8 @@ using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class HeliReturn : IActivity public class HeliReturn : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
static Actor ChooseHelipad(Actor self) static Actor ChooseHelipad(Actor self)
{ {
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings; var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
@@ -27,9 +24,9 @@ namespace OpenRA.Mods.RA.Activities
!Reservable.IsReserved(a)); !Reservable.IsReserved(a));
} }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
var dest = ChooseHelipad(self); var dest = ChooseHelipad(self);
var initialFacing = self.Info.Traits.Get<AircraftInfo>().InitialFacing; var initialFacing = self.Info.Traits.Get<AircraftInfo>().InitialFacing;
@@ -54,7 +51,5 @@ namespace OpenRA.Mods.RA.Activities
new Rearm(), new Rearm(),
NextActivity); NextActivity);
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA.Activities
this.delay = delay; this.delay = delay;
} }
public IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }
bool active = true; bool active = true;
public IActivity Tick(Actor self) public IActivity Tick(Actor self)
@@ -43,5 +43,13 @@ namespace OpenRA.Mods.RA.Activities
active = false; active = false;
NextActivity = null; NextActivity = null;
} }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -12,14 +12,14 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class Infiltrate : IActivity class Infiltrate : CancelableActivity
{ {
Actor target; Actor target;
public Infiltrate(Actor target) { this.target = target; } public Infiltrate(Actor target) { this.target = target; }
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity;
if (target == null || target.IsDead()) return NextActivity; if (target == null || target.IsDead()) return NextActivity;
if (target.Owner == self.Owner) return NextActivity; if (target.Owner == self.Owner) return NextActivity;
@@ -30,7 +30,5 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity; return NextActivity;
} }
public void Cancel(Actor self) { target = null; NextActivity = null; }
} }
} }

View File

@@ -14,21 +14,18 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Land : IActivity public class Land : CancelableActivity
{ {
bool isCanceled;
Target Target; Target Target;
public Land(Target t) { Target = t; } public Land(Target t) { Target = t; }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
if (!Target.IsValid) if (!Target.IsValid)
Cancel(self); Cancel(self);
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
var d = Target.CenterLocation - self.CenterLocation; var d = Target.CenterLocation - self.CenterLocation;
if (d.LengthSquared < 50) /* close enough */ if (d.LengthSquared < 50) /* close enough */
@@ -49,7 +46,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -17,14 +17,11 @@ namespace OpenRA.Mods.RA.Activities
{ {
// assumes you have Minelayer on that unit // assumes you have Minelayer on that unit
class LayMines : IActivity class LayMines : CancelableActivity
{ {
bool canceled = false; public override IActivity Tick( Actor self )
public IActivity NextActivity { get; set; }
public IActivity Tick( Actor self )
{ {
if (canceled) return NextActivity; if (IsCanceled) return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>(); var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (!limitedAmmo.HasAmmo()) if (!limitedAmmo.HasAmmo())
@@ -37,8 +34,11 @@ namespace OpenRA.Mods.RA.Activities
if (rearmTarget == null) if (rearmTarget == null)
return new Wait(20); return new Wait(20);
return new Move(Util.CellContaining(rearmTarget.CenterLocation), rearmTarget) return Util.SequenceActivities(
{ NextActivity = new Rearm() { NextActivity = new Repair(rearmTarget) { NextActivity = this } } }; new Move(Util.CellContaining(rearmTarget.CenterLocation), rearmTarget),
new Rearm(),
new Repair(rearmTarget),
this );
} }
var ml = self.Trait<Minelayer>(); var ml = self.Trait<Minelayer>();
@@ -46,14 +46,14 @@ namespace OpenRA.Mods.RA.Activities
ShouldLayMine(self, self.Location)) ShouldLayMine(self, self.Location))
{ {
LayMine(self); LayMine(self);
return new Wait(20) { NextActivity = this }; // a little wait after placing each mine, for show return Util.SequenceActivities( new Wait(20), this ); // a little wait after placing each mine, for show
} }
for (var n = 0; n < 20; n++) // dont get stuck forever here for (var n = 0; n < 20; n++) // dont get stuck forever here
{ {
var p = ml.minefield.Random(self.World.SharedRandom); var p = ml.minefield.Random(self.World.SharedRandom);
if (ShouldLayMine(self, p)) if (ShouldLayMine(self, p))
return new Move(p, 0) { NextActivity = this }; return Util.SequenceActivities( new Move(p, 0), this );
} }
// todo: return somewhere likely to be safe (near fix) so we're not sitting out in the minefield. // todo: return somewhere likely to be safe (near fix) so we're not sitting out in the minefield.
@@ -80,7 +80,5 @@ namespace OpenRA.Mods.RA.Activities
new OwnerInit( self.Owner ), new OwnerInit( self.Owner ),
})); }));
} }
public void Cancel( Actor self ) { canceled = true; NextActivity = null; }
} }
} }

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class Leap : IActivity class Leap : CancelableActivity
{ {
Target target; Target target;
float2 initialLocation; float2 initialLocation;
@@ -31,12 +31,10 @@ namespace OpenRA.Mods.RA.Activities
Sound.Play("dogg5p.aud", self.CenterLocation); Sound.Play("dogg5p.aud", self.CenterLocation);
} }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
if (!target.IsValid) if (IsCanceled) return NextActivity;
return NextActivity; if (!target.IsValid) return NextActivity;
t += (1f / delay); t += (1f / delay);
@@ -54,7 +52,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { target = new Target(); NextActivity = null; }
} }
} }

View File

@@ -14,17 +14,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Rearm : IActivity public class Rearm : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
int remainingTicks = ticksPerPip; int remainingTicks = ticksPerPip;
const int ticksPerPip = 25 * 2; const int ticksPerPip = 25 * 2;
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>(); var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (limitedAmmo == null) return NextActivity; if (limitedAmmo == null) return NextActivity;
@@ -43,7 +41,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -14,18 +14,16 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Repair : IActivity public class Repair : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
int remainingTicks; int remainingTicks;
Actor host; Actor host;
public Repair(Actor host) { this.host = host; } public Repair(Actor host) { this.host = host; }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (host != null && !host.IsInWorld) return NextActivity; if (host != null && !host.IsInWorld) return NextActivity;
if (remainingTicks == 0) if (remainingTicks == 0)
{ {
@@ -58,7 +56,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -12,16 +12,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class RepairBuilding : IActivity class RepairBuilding : CancelableActivity
{ {
Target target; Target target;
public RepairBuilding(Actor target) { this.target = Target.FromActor(target); } public RepairBuilding(Actor target) { this.target = Target.FromActor(target); }
public IActivity NextActivity { get; set; } public override IActivity Tick(Actor self)
public IActivity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity;
if (!target.IsValid) return NextActivity; if (!target.IsValid) return NextActivity;
if ((target.Actor.Location - self.Location).Length > 1) if ((target.Actor.Location - self.Location).Length > 1)
return NextActivity; return NextActivity;
@@ -35,7 +34,5 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity; return NextActivity;
} }
public void Cancel(Actor self) { target = Target.None; NextActivity = null; }
} }
} }

View File

@@ -14,11 +14,8 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class ReturnToBase : IActivity public class ReturnToBase : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
bool isCalculated; bool isCalculated;
Actor dest; Actor dest;
@@ -88,9 +85,9 @@ namespace OpenRA.Mods.RA.Activities
this.dest = dest; this.dest = dest;
} }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (!isCalculated) if (!isCalculated)
Calculate(self); Calculate(self);
@@ -101,7 +98,5 @@ namespace OpenRA.Mods.RA.Activities
new Land(Target.FromActor(dest)), new Land(Target.FromActor(dest)),
NextActivity); NextActivity);
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -13,10 +13,8 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Teleport : IActivity public class Teleport : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
int2 destination; int2 destination;
public Teleport(int2 destination) public Teleport(int2 destination)
@@ -24,12 +22,10 @@ namespace OpenRA.Mods.RA.Activities
this.destination = destination; this.destination = destination;
} }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
self.TraitsImplementing<IMove>().FirstOrDefault().SetPosition(self, destination); self.TraitsImplementing<IMove>().FirstOrDefault().SetPosition(self, destination);
return NextActivity; return NextActivity;
} }
public void Cancel(Actor self) { }
} }
} }

View File

@@ -17,12 +17,11 @@ using OpenRA.FileFormats;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
class Transform : IActivity class Transform : CancelableActivity
{ {
string actor = null; string actor = null;
int2 offset; int2 offset;
string[] sounds = null; string[] sounds = null;
bool isCanceled;
int facing; int facing;
RenderBuilding rb; RenderBuilding rb;
@@ -35,9 +34,6 @@ namespace OpenRA.Mods.RA.Activities
rb = self.TraitOrDefault<RenderBuilding>(); rb = self.TraitOrDefault<RenderBuilding>();
} }
public IActivity NextActivity { get; set; }
void DoTransform(Actor self) void DoTransform(Actor self)
{ {
// Hack: repeat the first frame of the make anim instead // Hack: repeat the first frame of the make anim instead
@@ -70,9 +66,9 @@ namespace OpenRA.Mods.RA.Activities
} }
bool started = false; bool started = false;
public IActivity Tick( Actor self ) public override IActivity Tick( Actor self )
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (started) return this; if (started) return this;
if (rb == null) if (rb == null)
@@ -88,7 +84,5 @@ namespace OpenRA.Mods.RA.Activities
} }
return this; return this;
} }
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
} }
} }

View File

@@ -16,11 +16,8 @@ using System.Drawing;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class UnloadCargo : IActivity public class UnloadCargo : CancelableActivity
{ {
public IActivity NextActivity { get; set; }
bool isCanceled;
int2? ChooseExitTile(Actor self, Actor cargo) int2? ChooseExitTile(Actor self, Actor cargo)
{ {
// is anyone still hogging this tile? // is anyone still hogging this tile?
@@ -38,16 +35,16 @@ namespace OpenRA.Mods.RA.Activities
return null; return null;
} }
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (isCanceled) return NextActivity; if (IsCanceled) return NextActivity;
// if we're a thing that can turn, turn to the // if we're a thing that can turn, turn to the
// right facing for the unload animation // right facing for the unload animation
var facing = self.TraitOrDefault<IFacing>(); var facing = self.TraitOrDefault<IFacing>();
var unloadFacing = self.Info.Traits.Get<CargoInfo>().UnloadFacing; var unloadFacing = self.Info.Traits.Get<CargoInfo>().UnloadFacing;
if (facing != null && facing.Facing != unloadFacing) if (facing != null && facing.Facing != unloadFacing)
return new Turn(unloadFacing) { NextActivity = this }; return Util.SequenceActivities( new Turn(unloadFacing), this );
// todo: handle the BS of open/close sequences, which are inconsistent, // todo: handle the BS of open/close sequences, which are inconsistent,
// for reasons that probably make good sense to the westwood guys. // for reasons that probably make good sense to the westwood guys.
@@ -82,7 +79,5 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public void Cancel(Actor self) { NextActivity = null; isCanceled = true; }
} }
} }

View File

@@ -16,6 +16,7 @@ namespace OpenRA.Mods.RA.Activities
{ {
int remainingTicks; int remainingTicks;
bool interruptable = true; bool interruptable = true;
IActivity NextActivity { get; set; }
public Wait(int period) { remainingTicks = period; } public Wait(int period) { remainingTicks = period; }
public Wait(int period, bool interruptable) public Wait(int period, bool interruptable)
@@ -24,7 +25,7 @@ namespace OpenRA.Mods.RA.Activities
this.interruptable = interruptable; this.interruptable = interruptable;
} }
public IActivity Tick(Actor self) public virtual IActivity Tick(Actor self)
{ {
if (remainingTicks-- == 0) return NextActivity; if (remainingTicks-- == 0) return NextActivity;
return this; return this;
@@ -38,6 +39,13 @@ namespace OpenRA.Mods.RA.Activities
remainingTicks = 0; remainingTicks = 0;
NextActivity = null; NextActivity = null;
} }
public IActivity NextActivity { get; set; }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }