Skip to content

Commit

Permalink
Remove dependency on default Target in Land, UnloadCargo and DeliverU…
Browse files Browse the repository at this point in the history
…nit.
  • Loading branch information
tovl committed Jun 16, 2019
1 parent 3c4c4aa commit 7b5e3ae
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
5 changes: 0 additions & 5 deletions OpenRA.Game/Traits/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ public bool IsValidFor(Actor targeter)
}
}

public bool IsNone()
{
return type == TargetType.Invalid;
}

// Currently all or nothing.
// TODO: either replace based on target type or put in singleton trait
public bool RequiresForceFire
Expand Down
11 changes: 9 additions & 2 deletions OpenRA.Mods.Common/Activities/Air/Land.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ public class Land : Activity
readonly int desiredFacing;
readonly CPos[] clearCells;
readonly int nearEnough;
readonly bool assignTargetOnFirstRun;

Target target;
WPos targetPosition;
CPos landingCell;
bool landingInitiated;
bool finishedApproach;

public Land(Actor self, Target target = default(Target), int facing = -1, WVec offset = default(WVec), CPos[] clearCells = null, int nearEnough = 5)
public Land(Actor self, int facing = -1, WVec offset = default(WVec), CPos[] clearCells = null, int nearEnough = 5)
: this(self, Target.Invalid, facing, offset, clearCells, nearEnough)
{
assignTargetOnFirstRun = true;
}

public Land(Actor self, Target target, int facing = -1, WVec offset = default(WVec), CPos[] clearCells = null, int nearEnough = 5)
{
aircraft = self.Trait<Aircraft>();
this.target = target;
Expand All @@ -51,7 +58,7 @@ protected override void OnFirstRun(Actor self)
{
// When no target is provided we should land in the most direct manner possible.
// TODO: For fixed-wing aircraft self.Location is not necessarily the most direct landing site.
if (target.IsNone())
if (assignTargetOnFirstRun)
target = Target.FromCell(self.World, self.Location);
}

Expand Down
38 changes: 22 additions & 16 deletions OpenRA.Mods.Common/Activities/DeliverUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ public class DeliverUnit : Activity
readonly Actor self;
readonly Carryall carryall;
readonly BodyOrientation body;
readonly Target destination;
readonly int nearEnough;
readonly bool assignTargetOnFirstRun;

public DeliverUnit(Actor self, Target destination = default(Target), int nearEnough = 5)
Target destination;

public DeliverUnit(Actor self, int nearEnough = 5)
: this(self, Target.Invalid, nearEnough)
{
assignTargetOnFirstRun = true;
}

public DeliverUnit(Actor self, Target destination, int nearEnough = 5)
{
this.self = self;
this.destination = destination;
Expand All @@ -33,6 +41,17 @@ public class DeliverUnit : Activity
body = self.Trait<BodyOrientation>();
}

protected override void OnFirstRun(Actor self)
{
if (assignTargetOnFirstRun)
destination = Target.FromCell(self.World, self.Location);

QueueChild(self, new Land(self, destination, nearEnough: nearEnough), true);
QueueChild(self, new Wait(carryall.Info.UnloadingDelay, false), true);
QueueChild(self, new ReleaseUnit(self));
QueueChild(self, new TakeOff(self));
}

public override Activity Tick(Actor self)
{
if (ChildActivity != null)
Expand All @@ -42,20 +61,7 @@ public override Activity Tick(Actor self)
return this;
}

if (IsCanceling || carryall.State != Carryall.CarryallState.Carrying || carryall.Carryable.IsDead)
return NextActivity;

// Land at the target location
QueueChild(self, new Land(self, destination, nearEnough: nearEnough), true);

// Pause briefly before releasing for visual effect
if (carryall.Info.UnloadingDelay > 0)
QueueChild(self, new Wait(carryall.Info.UnloadingDelay, false), true);

// Release carried actor
QueueChild(self, new ReleaseUnit(self));
QueueChild(self, new TakeOff(self));
return this;
return NextActivity;
}

class ReleaseUnit : Activity
Expand Down
45 changes: 22 additions & 23 deletions OpenRA.Mods.Common/Activities/UnloadCargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public class UnloadCargo : Activity
readonly INotifyUnload[] notifiers;
readonly bool unloadAll;
readonly Aircraft aircraft;
readonly Target destination;
readonly int nearEnough;
readonly bool assignTargetOnFirstRun;

bool moved;
Target destination;

public UnloadCargo(Actor self, Target destination = default(Target), bool unloadAll = true, int nearEnough = 5)
public UnloadCargo(Actor self, bool unloadAll = true, int nearEnough = 5)
: this(self, Target.Invalid, unloadAll, nearEnough)
{
assignTargetOnFirstRun = true;
}

public UnloadCargo(Actor self, Target destination, bool unloadAll = true, int nearEnough = 5)
{
this.self = self;
cargo = self.Trait<Cargo>();
Expand Down Expand Up @@ -63,7 +69,19 @@ IEnumerable<CPos> BlockedExitCells(Actor passenger)

protected override void OnFirstRun(Actor self)
{
cargo.Unloading = true;
if (assignTargetOnFirstRun)
destination = Target.FromCell(self.World, self.Location);

// Move to the target destination
if (aircraft != null)
QueueChild(self, new Land(self, destination, nearEnough: nearEnough));
else
{
var cell = self.World.Map.Clamp(this.self.World.Map.CellContaining(destination.CenterPosition));
QueueChild(self, new Move(self, cell, WDist.FromCells(nearEnough)));
}

QueueChild(self, new Wait(cargo.Info.WaitBeforeUnload));
}

public override Activity Tick(Actor self)
Expand All @@ -78,24 +96,6 @@ public override Activity Tick(Actor self)
if (IsCanceling || cargo.IsEmpty(self))
return NextActivity;

// Move to the target destination
if (!moved)
{
if (aircraft != null)
QueueChild(self, new Land(self, destination, nearEnough: nearEnough), true);
else if (!destination.IsNone())
{
var cell = self.World.Map.Clamp(this.self.World.Map.CellContaining(destination.CenterPosition));
QueueChild(self, new Move(self, cell, WDist.FromCells(nearEnough)), true);
}

QueueChild(self, new Wait(cargo.Info.WaitBeforeUnload));
moved = true;
return this;
}

cargo.Unloading = false;

if (cargo.CanUnload())
{
foreach (var inu in notifiers)
Expand Down Expand Up @@ -139,7 +139,6 @@ public override Activity Tick(Actor self)
QueueChild(self, new TakeOff(self), true);
}

cargo.Unloading = true;
return this;
}
}
Expand Down
2 changes: 0 additions & 2 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public class Cargo : IPips, IIssueOrder, IResolveOrder, IOrderVoice, INotifyCrea

CPos currentCell;
public IEnumerable<CPos> CurrentAdjacentCells { get; private set; }
public bool Unloading { get; internal set; }
public IEnumerable<Actor> Passengers { get { return cargo; } }
public int PassengerCount { get { return cargo.Count; } }

Expand All @@ -119,7 +118,6 @@ public Cargo(ActorInitializer init, CargoInfo info)
{
self = init.Self;
Info = info;
Unloading = false;
checkTerrainType = info.UnloadTerrainTypes.Count > 0;

if (init.Contains<RuntimeCargoInit>())
Expand Down

0 comments on commit 7b5e3ae

Please sign in to comment.