Improve ReturnToBase Activity.cs adherance

And make it use child activities for queueability.
This commit is contained in:
reaperrr
2019-03-10 02:14:03 +01:00
committed by reaperrr
parent 8edf5b56ea
commit c7eee6ae5d
2 changed files with 47 additions and 27 deletions

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (NextActivity != null && remainingTicks <= 0) if (remainingTicks == 0 || (NextActivity != null && remainingTicks < 0))
return NextActivity; return NextActivity;
// Refuse to take off if it would land immediately again. // Refuse to take off if it would land immediately again.

View File

@@ -27,6 +27,7 @@ namespace OpenRA.Mods.Common.Activities
readonly bool alwaysLand; readonly bool alwaysLand;
readonly bool abortOnResupply; readonly bool abortOnResupply;
bool isCalculated; bool isCalculated;
bool resupplied;
Actor dest; Actor dest;
WPos w1, w2, w3; WPos w1, w2, w3;
@@ -121,12 +122,29 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (ChildActivity != null)
{
ChildActivity = ActivityUtils.RunActivity(self, ChildActivity);
if (ChildActivity != null)
return this;
}
// Refuse to take off if it would land immediately again. // Refuse to take off if it would land immediately again.
// Special case: Don't kill other deploy hotkey activities. // Special case: Don't kill other deploy hotkey activities.
if (aircraft.ForceLanding) if (aircraft.ForceLanding)
return NextActivity; return NextActivity;
if (IsCanceling || self.IsDead) // If a Cancel was triggered at this point, it's unlikely that previously queued child activities finished,
// so 'resupplied' needs to be set to false, else it + abortOnResupply might cause another Cancel
// that would cancel any other activities that were queued after the first Cancel was triggered.
// TODO: This is a mess, we need to somehow make the activity cancelling a bit less tricky.
if (resupplied && IsCanceling)
resupplied = false;
if (resupplied && abortOnResupply)
Cancel(self);
if (resupplied || IsCanceling || self.IsDead)
return NextActivity; return NextActivity;
if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self))
@@ -135,13 +153,13 @@ namespace OpenRA.Mods.Common.Activities
if (!isCalculated) if (!isCalculated)
Calculate(self); Calculate(self);
if (dest == null || dest.IsDead) if (dest == null)
{ {
var nearestResupplier = ChooseResupplier(self, false); var nearestResupplier = ChooseResupplier(self, false);
if (nearestResupplier != null) if (nearestResupplier != null)
{ {
if (aircraft.Info.VTOL) if (aircraft.Info.CanHover)
{ {
var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength; var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength;
var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length; var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length;
@@ -150,28 +168,32 @@ namespace OpenRA.Mods.Common.Activities
if (distanceFromResupplier > distanceLength) if (distanceFromResupplier > distanceLength)
{ {
var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024; var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024;
var target = Target.FromPos(nearestResupplier.CenterPosition + randomPosition); var target = Target.FromPos(nearestResupplier.CenterPosition + randomPosition);
return ActivityUtils.SequenceActivities(self, QueueChild(self, new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), true);
new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green),
this);
} }
return this; return this;
} }
else else
return ActivityUtils.SequenceActivities(self, {
QueueChild(self,
new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green),
new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), true);
this);
QueueChild(self, new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), true);
return this;
}
} }
else if (nearestResupplier == null && aircraft.Info.VTOL && aircraft.Info.LandWhenIdle) else if (nearestResupplier == null && aircraft.Info.VTOL && aircraft.Info.LandWhenIdle)
{ {
// Using Queue instead of QueueChild here is intentional, as we want VTOLs with LandWhenIdle to land and stay there in this situation
Cancel(self);
if (aircraft.Info.TurnToLand) if (aircraft.Info.TurnToLand)
return ActivityUtils.SequenceActivities(self, new Turn(self, aircraft.Info.InitialFacing), new HeliLand(self, true)); Queue(self, new Turn(self, aircraft.Info.InitialFacing));
return new HeliLand(self, true); Queue(self, new HeliLand(self, true));
return NextActivity;
} }
else else
{ {
@@ -184,19 +206,19 @@ namespace OpenRA.Mods.Common.Activities
var exit = dest.FirstExitOrDefault(null); var exit = dest.FirstExitOrDefault(null);
var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero;
List<Activity> landingProcedures = new List<Activity>();
if (aircraft.Info.CanHover) if (aircraft.Info.CanHover)
landingProcedures.Add(new HeliFly(self, Target.FromPos(dest.CenterPosition + offset))); QueueChild(self, new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)), true);
else if (aircraft.Info.VTOL)
QueueChild(self, new Fly(self, Target.FromPos(dest.CenterPosition + offset)), true);
else else
{ {
var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed); var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed);
landingProcedures.Add(new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3))); QueueChild(self, new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3)), true);
landingProcedures.Add(new Fly(self, Target.FromPos(w2))); QueueChild(self, new Fly(self, Target.FromPos(w2)), true);
// Fix a problem when the airplane is sent to resupply near the airport // Fix a problem when the airplane is sent to resupply near the airport
landingProcedures.Add(new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2))); QueueChild(self, new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2)), true);
} }
if (ShouldLandAtBuilding(self, dest)) if (ShouldLandAtBuilding(self, dest))
@@ -206,20 +228,18 @@ namespace OpenRA.Mods.Common.Activities
if (aircraft.Info.VTOL) if (aircraft.Info.VTOL)
{ {
if (aircraft.Info.TurnToDock) if (aircraft.Info.TurnToDock)
landingProcedures.Add(new Turn(self, aircraft.Info.InitialFacing)); QueueChild(self, new Turn(self, aircraft.Info.InitialFacing), true);
landingProcedures.Add(new HeliLand(self, false)); QueueChild(self, new HeliLand(self, false), true);
} }
else else
landingProcedures.Add(new Land(self, Target.FromPos(dest.CenterPosition + offset))); QueueChild(self, new Land(self, Target.FromPos(dest.CenterPosition + offset)), true);
landingProcedures.Add(new ResupplyAircraft(self)); QueueChild(self, new ResupplyAircraft(self), true);
resupplied = true;
} }
if (!abortOnResupply) return this;
landingProcedures.Add(NextActivity);
return ActivityUtils.SequenceActivities(self, landingProcedures.ToArray());
} }
} }
} }