Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made both Cargo and Carryall conditional #20534

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Activities/PickupUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PickupUnit(Actor self, Actor cargo, int delay, Color? targetLineColor)
protected override void OnFirstRun(Actor self)
{
// The cargo might have become invalid while we were moving towards it.
if (cargo.IsDead || carryable.IsTraitDisabled || !cargo.AppearsFriendlyTo(self))
if (cargo.IsDead || carryable.IsTraitDisabled || carryall.IsTraitDisabled || !cargo.AppearsFriendlyTo(self))
return;

if (carryall.ReserveCarryable(self, cargo))
Expand All @@ -68,7 +68,7 @@ public override bool Tick(Actor self)
if (IsCanceling)
return true;

if (cargo.IsDead || carryable.IsTraitDisabled || !cargo.AppearsFriendlyTo(self) || cargo != carryall.Carryable)
if (cargo.IsDead || carryable.IsTraitDisabled || carryall.IsTraitDisabled || !cargo.AppearsFriendlyTo(self) || cargo != carryall.Carryable)
{
Cancel(self, true);
return false;
Expand Down Expand Up @@ -160,7 +160,7 @@ public AttachUnit(Actor self, Actor cargo)
protected override void OnFirstRun(Actor self)
{
// The cargo might have become invalid while we were moving towards it.
if (cargo == null || cargo.IsDead || carryable.IsTraitDisabled || carryall.Carryable != cargo || !cargo.AppearsFriendlyTo(self))
if (cargo == null || cargo.IsDead || carryable.IsTraitDisabled || carryall.IsTraitDisabled || carryall.Carryable != cargo || !cargo.AppearsFriendlyTo(self))
return;

self.World.AddFrameEndTask(w =>
Expand Down
8 changes: 7 additions & 1 deletion OpenRA.Mods.Common/Activities/RideTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override bool TryStartEnter(Actor self, Actor targetActor)

// Make sure we can still enter the transport
// (but not before, because this may stop the actor in the middle of nowhere)
if (enterCargo == null || !passenger.Reserve(self, enterCargo))
if (enterCargo == null || enterCargo.IsTraitDisabled || !passenger.Reserve(self, enterCargo))
{
Cancel(self, true);
return false;
Expand All @@ -49,6 +49,12 @@ protected override bool TryStartEnter(Actor self, Actor targetActor)
return true;
}

protected override void TickInner(Actor self, in Target target, bool targetIsDeadOrHiddenActor)
{
if (enterCargo != null && enterCargo.IsTraitDisabled)
Cancel(self, true);
}

protected override void OnEnterComplete(Actor self, Actor targetActor)
{
self.World.AddFrameEndTask(w =>
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/AutoCarryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace OpenRA.Mods.Common.Traits
{
[Desc("Can be carried by units with the trait `Carryall`.")]
[Desc("Can be carried by units with the trait `" + nameof(Carryall) + "`.")]
public class AutoCarryableInfo : CarryableInfo
{
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
Expand Down Expand Up @@ -64,7 +64,7 @@ void RequestTransport(Actor self, CPos destination)

// Inform all idle carriers
var carriers = self.World.ActorsWithTrait<Carryall>()
.Where(c => c.Trait.State == Carryall.CarryallState.Idle && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
.Where(c => c.Trait.State == Carryall.CarryallState.Idle && !c.Trait.IsTraitDisabled && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);

// Enumerate idle carriers to find the first that is able to transport us
Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Mods.Common/Traits/AutoCarryall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void INotifyBecomingIdle.OnBecomingIdle(Actor self)
// A carryable notifying us that he'd like to be carried
public override bool RequestTransportNotify(Actor self, Actor carryable, CPos destination)
{
if (busy)
if (busy || IsTraitDisabled)
return false;

if (ReserveCarryable(self, carryable))
Expand All @@ -61,7 +61,7 @@ static bool IsBestAutoCarryallForCargo(Actor self, Actor candidateCargo)

void FindCarryableForTransport(Actor self)
{
if (!self.IsInWorld)
if (!self.IsInWorld || IsTraitDisabled)
return;

// Get all carryables who want transport
Expand Down Expand Up @@ -117,14 +117,14 @@ public FerryUnit(Actor self, Actor cargo)

protected override void OnFirstRun(Actor self)
{
if (!cargo.IsDead)
if (!cargo.IsDead && !carryall.IsTraitDisabled)
QueueChild(new PickupUnit(self, cargo, 0, carryall.Info.TargetLineColor));
}

public override bool Tick(Actor self)
{
// Cargo may have become invalid or PickupUnit cancelled.
if (carryall.Carryable == null || carryall.Carryable.IsDead)
if (carryall.IsTraitDisabled || carryall.Carryable == null || carryall.Carryable.IsDead)
return true;

var dropRange = carryall.Info.DropRange;
Expand Down
20 changes: 13 additions & 7 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor can transport Passenger actors.")]
public class CargoInfo : TraitInfo, Requires<IOccupySpaceInfo>
public class CargoInfo : ConditionalTraitInfo, Requires<IOccupySpaceInfo>
{
[Desc("The maximum sum of Passenger.Weight that this actor can support.")]
public readonly int MaxWeight = 0;
Expand Down Expand Up @@ -88,11 +88,10 @@ public class CargoInfo : TraitInfo, Requires<IOccupySpaceInfo>
public override object Create(ActorInitializer init) { return new Cargo(init, this); }
}

public class Cargo : IIssueOrder, IResolveOrder, IOrderVoice, INotifyCreated, INotifyKilled,
public class Cargo : ConditionalTrait<CargoInfo>, IIssueOrder, IResolveOrder, IOrderVoice,
INotifyOwnerChanged, INotifySold, INotifyActorDisposing, IIssueDeployOrder,
ITransformActorInitModifier
INotifyCreated, INotifyKilled, ITransformActorInitModifier
{
public readonly CargoInfo Info;
readonly Actor self;
readonly List<Actor> cargo = new();
readonly HashSet<Actor> reserves = new();
Expand All @@ -119,9 +118,9 @@ enum State { Free, Locked }
State state = State.Free;

public Cargo(ActorInitializer init, CargoInfo info)
: base(info)
{
self = init.Self;
Info = info;
checkTerrainType = info.UnloadTerrainTypes.Count > 0;

currentAdjacentCells = new CachedTransform<CPos, IEnumerable<CPos>>(loc =>
Expand Down Expand Up @@ -162,8 +161,9 @@ public Cargo(ActorInitializer init, CargoInfo info)
facing = Exts.Lazy(self.TraitOrDefault<IFacing>);
}

void INotifyCreated.Created(Actor self)
protected override void Created(Actor self)
{
base.Created(self);
aircraft = self.TraitOrDefault<Aircraft>();
Mailaender marked this conversation as resolved.
Show resolved Hide resolved

if (cargo.Count > 0)
Expand Down Expand Up @@ -200,6 +200,9 @@ public IEnumerable<IOrderTargeter> Orders
{
get
{
if (IsTraitDisabled)
yield break;

yield return new DeployOrderTargeter("Unload", 10,
() => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor);
}
Expand Down Expand Up @@ -233,6 +236,9 @@ public void ResolveOrder(Actor self, Order order)

public bool CanUnload(BlockedByActor check = BlockedByActor.None)
{
if (IsTraitDisabled)
return false;

if (checkTerrainType)
{
var terrainType = self.World.Map.GetTerrainInfo(self.Location).Type;
Expand All @@ -247,7 +253,7 @@ public bool CanUnload(BlockedByActor check = BlockedByActor.None)

public bool CanLoad(Actor a)
{
return reserves.Contains(a) || HasSpace(GetWeight(a));
return !IsTraitDisabled && (reserves.Contains(a) || HasSpace(GetWeight(a)));
}

internal bool ReserveSpace(Actor a)
Expand Down
22 changes: 15 additions & 7 deletions OpenRA.Mods.Common/Traits/Carryall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace OpenRA.Mods.Common.Traits
{
[Desc("Transports actors with the `" + nameof(Carryable) + "` trait.")]
public class CarryallInfo : TraitInfo, Requires<BodyOrientationInfo>, Requires<AircraftInfo>
public class CarryallInfo : ConditionalTraitInfo, Requires<BodyOrientationInfo>, Requires<AircraftInfo>
{
[ActorReference(typeof(CarryableInfo))]
[Desc("Actor type that is initially spawned into this actor.")]
Expand Down Expand Up @@ -83,8 +83,9 @@ public class CarryallInfo : TraitInfo, Requires<BodyOrientationInfo>, Requires<A
public override object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
}

public class Carryall : INotifyKilled, ISync, ITick, IRender, INotifyActorDisposing, IIssueOrder, IResolveOrder,
IOrderVoice, IIssueDeployOrder, IAircraftCenterPositionOffset, IOverrideAircraftLanding
public class Carryall : ConditionalTrait<CarryallInfo>, INotifyKilled, ISync, ITick, IRender,
INotifyActorDisposing, IIssueOrder, IResolveOrder, IOrderVoice, IIssueDeployOrder,
IAircraftCenterPositionOffset, IOverrideAircraftLanding
{
public enum CarryallState
{
Expand All @@ -93,7 +94,6 @@ public enum CarryallState
Carrying
}

public readonly CarryallInfo Info;
readonly AircraftInfo aircraftInfo;
readonly Aircraft aircraft;
readonly BodyOrientation body;
Expand All @@ -115,9 +115,8 @@ public enum CarryallState
public WVec CarryableOffset { get; private set; }

public Carryall(Actor self, CarryallInfo info)
: base(info)
{
Info = info;

Carryable = null;
State = CarryallState.Idle;

Expand Down Expand Up @@ -311,6 +310,9 @@ IEnumerable<Rectangle> IRender.ScreenBounds(Actor self, WorldRenderer wr)
// Check if we can drop the unit at our current location.
public bool CanUnload()
{
if (IsTraitDisabled)
return false;

var targetCell = self.World.Map.CellContaining(aircraft.GetPosition());
return Carryable != null && aircraft.CanLand(targetCell, blockedByMobile: false);
}
Expand All @@ -319,6 +321,9 @@ IEnumerable<IOrderTargeter> IIssueOrder.Orders
{
get
{
if (IsTraitDisabled)
yield break;

yield return new CarryallPickupOrderTargeter(Info);
yield return new DeployOrderTargeter("Unload", 10,
() => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor);
Expand All @@ -339,7 +344,10 @@ Order IIssueDeployOrder.IssueDeployOrder(Actor self, bool queued)
return new Order("Unload", self, queued);
}

bool IIssueDeployOrder.CanIssueDeployOrder(Actor self, bool queued) { return true; }
bool IIssueDeployOrder.CanIssueDeployOrder(Actor self, bool queued)
{
return !IsTraitDisabled;
}

void IResolveOrder.ResolveOrder(Actor self, Order order)
{
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Traits/Passenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ bool IsCorrectCargoType(Actor target, TargetModifiers modifiers)

bool IsCorrectCargoType(Actor target)
{
var ci = target.Info.TraitInfo<CargoInfo>();
return ci.Types.Contains(Info.CargoType);
var cargo = target.Trait<Cargo>();
return !cargo.IsTraitDisabled && cargo.Info.Types.Contains(Info.CargoType);
}

bool CanEnter(Cargo cargo)
{
return cargo != null && cargo.HasSpace(Info.Weight);
return cargo != null && !cargo.IsTraitDisabled && cargo.HasSpace(Info.Weight);
Mailaender marked this conversation as resolved.
Show resolved Hide resolved
}

bool CanEnter(Actor target)
Expand Down