Merge pull request #12461 from obrakmann/activities-pt2
Activities re-write, part 2 - Child and composite activities
This commit is contained in:
@@ -23,7 +23,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
readonly AttackPlane attackPlane;
|
||||
readonly AmmoPool[] ammoPools;
|
||||
|
||||
Activity inner;
|
||||
int ticksUntilTurn;
|
||||
|
||||
public FlyAttack(Actor self, Target target)
|
||||
@@ -47,34 +46,25 @@ namespace OpenRA.Mods.Common.Activities
|
||||
if (attackPlane != null)
|
||||
attackPlane.DoAttack(self, target);
|
||||
|
||||
if (inner == null)
|
||||
if (ChildActivity == null)
|
||||
{
|
||||
if (IsCanceled)
|
||||
return NextActivity;
|
||||
|
||||
// TODO: This should fire each weapon at its maximum range
|
||||
if (attackPlane != null && target.IsInRange(self.CenterPosition, attackPlane.Armaments.Select(a => a.Weapon.MinRange).Min()))
|
||||
inner = ActivityUtils.SequenceActivities(new FlyTimed(ticksUntilTurn, self), new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
ChildActivity = ActivityUtils.SequenceActivities(new FlyTimed(ticksUntilTurn, self), new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
else
|
||||
inner = ActivityUtils.SequenceActivities(new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
ChildActivity = ActivityUtils.SequenceActivities(new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
|
||||
// HACK: This needs to be done in this round-about way because TakeOff doesn't behave as expected when it doesn't have a NextActivity.
|
||||
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraft.Info.MinAirborneAltitude)
|
||||
inner = ActivityUtils.SequenceActivities(new TakeOff(self), inner);
|
||||
ChildActivity = ActivityUtils.SequenceActivities(new TakeOff(self), ChildActivity);
|
||||
}
|
||||
|
||||
inner = ActivityUtils.RunActivity(self, inner);
|
||||
ActivityUtils.RunActivity(self, ChildActivity);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public override bool Cancel(Actor self)
|
||||
{
|
||||
if (!IsCanceled && inner != null && !inner.Cancel(self))
|
||||
return false;
|
||||
|
||||
// NextActivity must always be set to null:
|
||||
return base.Cancel(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,59 +16,40 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class ResupplyAircraft : Activity
|
||||
public class ResupplyAircraft : CompositeActivity
|
||||
{
|
||||
readonly Aircraft aircraft;
|
||||
Activity inner;
|
||||
public ResupplyAircraft(Actor self) { }
|
||||
|
||||
public ResupplyAircraft(Actor self)
|
||||
protected override void OnFirstRun(Actor self)
|
||||
{
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
var aircraft = self.Trait<Aircraft>();
|
||||
var host = aircraft.GetActorBelow();
|
||||
|
||||
if (host == null)
|
||||
return;
|
||||
|
||||
if (aircraft.IsPlane)
|
||||
{
|
||||
ChildActivity = ActivityUtils.SequenceActivities(
|
||||
aircraft.GetResupplyActivities(host)
|
||||
.Append(new AllowYieldingReservation(self))
|
||||
.Append(new WaitFor(() => NextInQueue != null || aircraft.ReservedActor == null))
|
||||
.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Helicopters should take off from their helipad immediately after resupplying.
|
||||
// HACK: Append NextInQueue to TakeOff to avoid moving to the Rallypoint (if NextInQueue is non-null).
|
||||
ChildActivity = ActivityUtils.SequenceActivities(
|
||||
aircraft.GetResupplyActivities(host)
|
||||
.Append(new AllowYieldingReservation(self))
|
||||
.Append(new TakeOff(self)).Append(NextInQueue).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled)
|
||||
return NextActivity;
|
||||
|
||||
if (inner == null)
|
||||
{
|
||||
var host = aircraft.GetActorBelow();
|
||||
|
||||
if (host == null)
|
||||
return NextActivity;
|
||||
|
||||
if (aircraft.IsPlane)
|
||||
{
|
||||
inner = ActivityUtils.SequenceActivities(
|
||||
aircraft.GetResupplyActivities(host)
|
||||
.Append(new AllowYieldingReservation(self))
|
||||
.Append(new WaitFor(() => NextActivity != null || aircraft.ReservedActor == null))
|
||||
.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Helicopters should take off from their helipad immediately after resupplying.
|
||||
// HACK: Append NextActivity to TakeOff to avoid moving to the Rallypoint (if NextActivity is non-null).
|
||||
inner = ActivityUtils.SequenceActivities(
|
||||
aircraft.GetResupplyActivities(host)
|
||||
.Append(new AllowYieldingReservation(self))
|
||||
.Append(new TakeOff(self)).Append(NextActivity).ToArray());
|
||||
}
|
||||
}
|
||||
else
|
||||
inner = ActivityUtils.RunActivity(self, inner);
|
||||
|
||||
// The inner == NextActivity check is needed here because of the TakeOff issue mentioned in the comment above.
|
||||
return inner == null || inner == NextActivity ? NextActivity : this;
|
||||
}
|
||||
|
||||
public override bool Cancel(Actor self)
|
||||
{
|
||||
if (!IsCanceled && inner != null && !inner.Cancel(self))
|
||||
return false;
|
||||
|
||||
return base.Cancel(self);
|
||||
return NextActivity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,10 +37,10 @@ namespace OpenRA.Mods.Common.Activities
|
||||
var destination = rp != null ? rp.Location :
|
||||
(hasHost ? self.World.Map.CellContaining(host.CenterPosition) : self.Location);
|
||||
|
||||
if (NextActivity == null)
|
||||
if (NextInQueue == null)
|
||||
return new AttackMoveActivity(self, move.MoveTo(destination, 1));
|
||||
else
|
||||
return NextActivity;
|
||||
return NextInQueue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (NextActivity != null)
|
||||
return NextActivity;
|
||||
if (NextInQueue != null)
|
||||
return NextInQueue;
|
||||
|
||||
// Find the nearest best refinery if not explicitly ordered to a specific refinery:
|
||||
if (harv.OwnerLinkedProc == null || !harv.OwnerLinkedProc.IsInWorld)
|
||||
|
||||
@@ -51,9 +51,12 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || NextActivity != null)
|
||||
if (IsCanceled)
|
||||
return NextActivity;
|
||||
|
||||
if (NextInQueue != null)
|
||||
return NextInQueue;
|
||||
|
||||
var deliver = new DeliverResources(self);
|
||||
|
||||
if (harv.IsFull)
|
||||
@@ -81,8 +84,8 @@ namespace OpenRA.Mods.Common.Activities
|
||||
var randFrames = self.World.SharedRandom.Next(100, 175);
|
||||
|
||||
// Avoid creating an activity cycle
|
||||
var next = NextActivity;
|
||||
NextActivity = null;
|
||||
var next = NextInQueue;
|
||||
NextInQueue = null;
|
||||
return ActivityUtils.SequenceActivities(next, new Wait(randFrames), this);
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user