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 29, 2019
1 parent e895879 commit 68086bf
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 80 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
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Activities/Air/TakeOff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override Activity Tick(Actor self)
if (moveToRallyPoint && NextActivity == null)
{
if (!aircraft.Info.VTOL && assignTargetOnFirstRun)
return new FlyTimed(1, self);
return NextActivity;

QueueChild(self, new AttackMoveActivity(self, () => move.MoveToTarget(self, target)), true);
moveToRallyPoint = false;
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.BeforeUnloadDelay, 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
49 changes: 24 additions & 25 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.BeforeUnloadDelay));
}

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 @@ -132,14 +132,13 @@ public override Activity Tick(Actor self)
if (!unloadAll || !cargo.CanUnload())
{
Cancel(self, true);
if (cargo.Info.WaitAfterUnload > 0)
QueueChild(self, new Wait(cargo.Info.WaitAfterUnload, false), true);
if (cargo.Info.AfterUnloadDelay > 0)
QueueChild(self, new Wait(cargo.Info.AfterUnloadDelay, false), true);

if (aircraft != null && !aircraft.Info.LandWhenIdle)
QueueChild(self, new TakeOff(self), true);
}

cargo.Unloading = true;
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public LuaTable ReinforceWithTransport(Player owner, string actorType, string[]
{
var aircraft = transport.TraitOrDefault<Aircraft>();
if (aircraft != null)
transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, entryPath.Last()), aircraft.Info.InitialFacing, nearEnough: dropRange));
transport.QueueActivity(new Land(transport, aircraft.Info.InitialFacing, nearEnough: dropRange));

if (cargo != null)
transport.QueueActivity(new UnloadCargo(transport));
Expand Down
19 changes: 7 additions & 12 deletions OpenRA.Mods.Common/Traits/Air/Aircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder

Repairable repairable;
Rearmable rearmable;
IPositionOffset[] positionOffsets;
IAircaftCenterPositionOffset[] positionOffsets;
ConditionManager conditionManager;
IDisposable reservation;
IEnumerable<int> speedModifiers;
Expand All @@ -207,7 +207,7 @@ public static WPos GroundPosition(Actor self)
return self.CenterPosition - new WVec(WDist.Zero, WDist.Zero, self.World.Map.DistanceAboveTerrain(self.CenterPosition));
}

public bool AtLandAltitude { get { return self.World.Map.DistanceAboveTerrain(CenterPosition) == LandAltitude; } }
public bool AtLandAltitude { get { return self.World.Map.DistanceAboveTerrain(GetPosition()) == LandAltitude; } }

bool requireForceMove;
IEnumerable<CPos> landingCells = Enumerable.Empty<CPos>();
Expand Down Expand Up @@ -289,7 +289,7 @@ protected virtual void Created(Actor self)
speedModifiers = self.TraitsImplementing<ISpeedModifier>().ToArray().Select(sm => sm.GetSpeedModifier());
cachedPosition = self.CenterPosition;
notifyMoving = self.TraitsImplementing<INotifyMoving>().ToArray();
positionOffsets = self.TraitsImplementing<IPositionOffset>().ToArray();
positionOffsets = self.TraitsImplementing<IAircaftCenterPositionOffset>().ToArray();
}

void INotifyAddedToWorld.AddedToWorld(Actor self)
Expand Down Expand Up @@ -531,13 +531,7 @@ public int MovementSpeed
public Pair<CPos, SubCell>[] OccupiedCells()
{
if (!self.IsAtGroundLevel())
{
var pairs = new Pair<CPos, SubCell>[landingCells.Count()];
for (int i = 0; i < landingCells.Count(); i++)
pairs[i] = Pair.New(landingCells.ElementAt(i), SubCell.FullCell);

return pairs;
}
return landingCells.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();

return new[] { Pair.New(TopLeft, SubCell.FullCell) };
}
Expand Down Expand Up @@ -609,6 +603,7 @@ bool IsBlockedBy(Actor self, Actor otherActor, Actor ignoreActor, bool blockedBy
if (otherActor == self || otherActor == ignoreActor)
return false;

// We are not blocked by actors we can nudge out of the way
if (!blockedByMobile && self.Owner.Stances[otherActor.Owner] == Stance.Ally &&
otherActor.TraitOrDefault<Mobile>() != null && otherActor.CurrentActivity == null)
return false;
Expand Down Expand Up @@ -906,7 +901,7 @@ public IEnumerable<IOrderTargeter> Orders
(target, modifiers) => AircraftCanEnter(target) && modifiers.HasModifier(TargetModifiers.ForceMove), target => Reservable.IsAvailableFor(target, self));

yield return new EnterAlliedActorTargeter<BuildingInfo>("Enter", 5,
(target, modifiers) => AircraftCanEnter(target) && (!requireForceMove || modifiers.HasModifier(TargetModifiers.ForceMove)), target => Reservable.IsAvailableFor(target, self));
(target, modifiers) => AircraftCanEnter(target), target => Reservable.IsAvailableFor(target, self));

yield return new AircraftMoveOrderTargeter(this);
}
Expand Down Expand Up @@ -987,7 +982,7 @@ public void ResolveOrder(Actor self, Order order)
}
else if (orderString == "Enter" || orderString == "ForceEnter" || orderString == "Repair")
{
// Enter and Repair orders are only valid for own/allied actors,
// Enter, ForceEnter and Repair orders are only valid for own/allied actors,
// which are guaranteed to never be frozen.
if (order.Target.Type != TargetType.Actor)
return;
Expand Down
18 changes: 8 additions & 10 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public class CargoInfo : ITraitInfo, Requires<IOccupySpaceInfo>
[Desc("Voice to play when ordered to unload the passengers.")]
public readonly string UnloadVoice = "Action";

[Desc("Radius within which the actor will search for a suitable unloading spot when the ordered location is blocked")]
[Desc("Radius within which the actor will search for a suitable unloading spot when the ordered location is blocked.")]
public readonly int UnloadSearchRadius = 5;

[Desc("Which direction the passenger will face (relative to the transport) when unloading.")]
public readonly int PassengerFacing = 128;

[Desc("How long should this actor wait after loading")]
public readonly int WaitAfterLoad = 8;
[Desc("Delay (in ticks) before continuing after loading a passenger.")]
public readonly int AfterLoadDelay = 8;

[Desc("How long should this actor wait before unloading")]
public readonly int WaitBeforeUnload = 8;
[Desc("Delay (in ticks) before unloading the first passenger.")]
public readonly int BeforeUnloadDelay = 8;

[Desc("How long should this actor wait after unloading")]
public readonly int WaitAfterUnload = 25;
[Desc("Delay (in ticks) before continuing after unloading a passenger.")]
public readonly int AfterUnloadDelay = 25;

[Desc("Cursor to display when able to unload the passengers.")]
public readonly string UnloadCursor = "deploy";
Expand Down 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 Expand Up @@ -295,7 +293,7 @@ void ReleaseLock(Actor self)

state = State.Free;

self.QueueActivity(new Wait(Info.WaitAfterLoad, false));
self.QueueActivity(new Wait(Info.AfterLoadDelay, false));
var air = self.TraitOrDefault<Aircraft>();
if (air != null)
self.QueueActivity(new TakeOff(self));
Expand Down
14 changes: 7 additions & 7 deletions OpenRA.Mods.Common/Traits/Carryall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Transports actors with the `Carryable` trait.")]
public class CarryallInfo : ITraitInfo, Requires<BodyOrientationInfo>, Requires<AircraftInfo>
{
[Desc("Delay on the ground while attaching an actor to the carryall.")]
public readonly int LoadingDelay = 0;
[Desc("Delay (in ticks) on the ground while attaching an actor to the carryall.")]
public readonly int BeforeLoadDelay = 0;

[Desc("Delay on the ground while detaching an actor from the carryall.")]
public readonly int UnloadingDelay = 0;
[Desc("Delay (in ticks) on the ground while detaching an actor from the carryall.")]
public readonly int BeforeUnloadDelay = 0;

[Desc("Carryable attachment point relative to body.")]
public readonly WVec LocalOffset = WVec.Zero;
Expand Down Expand Up @@ -57,7 +57,7 @@ public class CarryallInfo : ITraitInfo, Requires<BodyOrientationInfo>, Requires<
}

public class Carryall : INotifyKilled, ISync, ITick, IRender, INotifyActorDisposing, IIssueOrder, IResolveOrder,
IOrderVoice, IIssueDeployOrder, IPositionOffset, IOverrideLandableTerrain
IOrderVoice, IIssueDeployOrder, IAircaftCenterPositionOffset, IOverrideLandableTerrain
{
public enum CarryallState
{
Expand Down Expand Up @@ -154,7 +154,7 @@ public virtual WVec OffsetForCarryable(Actor self, Actor carryable)
return Info.LocalOffset - carryable.Info.TraitInfo<CarryableInfo>().LocalOffset;
}

WVec IPositionOffset.PositionOffset()
WVec IAircaftCenterPositionOffset.PositionOffset()
{
var localOffset = CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation));
return body.LocalToWorld(localOffset);
Expand Down Expand Up @@ -319,7 +319,7 @@ void IResolveOrder.ResolveOrder(Actor self, Order order)
self.CancelActivity();

self.SetTargetLine(order.Target, Color.Yellow);
self.QueueActivity(order.Queued, new PickupUnit(self, order.Target.Actor, Info.LoadingDelay));
self.QueueActivity(order.Queued, new PickupUnit(self, order.Target.Actor, Info.BeforeLoadDelay));
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/TraitsInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public interface IWrapMove
Activity WrapMove(Activity moveInner);
}

public interface IPositionOffset
public interface IAircaftCenterPositionOffset
{
WVec PositionOffset();
}
Expand Down

0 comments on commit 68086bf

Please sign in to comment.