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

@@ -27,6 +27,7 @@ namespace OpenRA.Mods.Common.Activities
readonly bool alwaysLand;
readonly bool abortOnResupply;
bool isCalculated;
bool resupplied;
Actor dest;
WPos w1, w2, w3;
@@ -121,12 +122,29 @@ namespace OpenRA.Mods.Common.Activities
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.
// Special case: Don't kill other deploy hotkey activities.
if (aircraft.ForceLanding)
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;
if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self))
@@ -135,13 +153,13 @@ namespace OpenRA.Mods.Common.Activities
if (!isCalculated)
Calculate(self);
if (dest == null || dest.IsDead)
if (dest == null)
{
var nearestResupplier = ChooseResupplier(self, false);
if (nearestResupplier != null)
{
if (aircraft.Info.VTOL)
if (aircraft.Info.CanHover)
{
var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength;
var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length;
@@ -150,28 +168,32 @@ namespace OpenRA.Mods.Common.Activities
if (distanceFromResupplier > distanceLength)
{
var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024;
var target = Target.FromPos(nearestResupplier.CenterPosition + randomPosition);
return ActivityUtils.SequenceActivities(self,
new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green),
this);
QueueChild(self, new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), true);
}
return this;
}
else
return ActivityUtils.SequenceActivities(self,
{
QueueChild(self,
new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green),
new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport),
this);
true);
QueueChild(self, new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), true);
return this;
}
}
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)
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
{
@@ -184,19 +206,19 @@ namespace OpenRA.Mods.Common.Activities
var exit = dest.FirstExitOrDefault(null);
var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero;
List<Activity> landingProcedures = new List<Activity>();
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
{
var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed);
landingProcedures.Add(new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3)));
landingProcedures.Add(new Fly(self, Target.FromPos(w2)));
QueueChild(self, new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3)), true);
QueueChild(self, new Fly(self, Target.FromPos(w2)), true);
// 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))
@@ -206,20 +228,18 @@ namespace OpenRA.Mods.Common.Activities
if (aircraft.Info.VTOL)
{
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
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)
landingProcedures.Add(NextActivity);
return ActivityUtils.SequenceActivities(self, landingProcedures.ToArray());
return this;
}
}
}