Make Cargo and Carryall conditional.

This commit is contained in:
Matthias Mailänder
2023-07-25 21:43:23 +02:00
committed by Gustas
parent a14cc8cc4d
commit 98896f9a75
7 changed files with 47 additions and 27 deletions

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Activities
protected override void OnFirstRun(Actor self) protected override void OnFirstRun(Actor self)
{ {
// The cargo might have become invalid while we were moving towards it. // 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; return;
if (carryall.ReserveCarryable(self, cargo)) if (carryall.ReserveCarryable(self, cargo))
@@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.Activities
if (IsCanceling) if (IsCanceling)
return true; 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); Cancel(self, true);
return false; return false;
@@ -160,7 +160,7 @@ namespace OpenRA.Mods.Common.Activities
protected override void OnFirstRun(Actor self) protected override void OnFirstRun(Actor self)
{ {
// The cargo might have become invalid while we were moving towards it. // 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; return;
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Activities
// Make sure we can still enter the transport // Make sure we can still enter the transport
// (but not before, because this may stop the actor in the middle of nowhere) // (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); Cancel(self, true);
return false; return false;
@@ -49,6 +49,12 @@ namespace OpenRA.Mods.Common.Activities
return true; 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) protected override void OnEnterComplete(Actor self, Actor targetActor)
{ {
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>

View File

@@ -13,7 +13,7 @@ using System.Linq;
namespace OpenRA.Mods.Common.Traits 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 public class AutoCarryableInfo : CarryableInfo
{ {
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")] [Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
@@ -64,7 +64,7 @@ namespace OpenRA.Mods.Common.Traits
// Inform all idle carriers // Inform all idle carriers
var carriers = self.World.ActorsWithTrait<Carryall>() 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); .OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
// Enumerate idle carriers to find the first that is able to transport us // Enumerate idle carriers to find the first that is able to transport us

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Traits
// A carryable notifying us that he'd like to be carried // A carryable notifying us that he'd like to be carried
public override bool RequestTransportNotify(Actor self, Actor carryable, CPos destination) public override bool RequestTransportNotify(Actor self, Actor carryable, CPos destination)
{ {
if (busy) if (busy || IsTraitDisabled)
return false; return false;
if (ReserveCarryable(self, carryable)) if (ReserveCarryable(self, carryable))
@@ -61,7 +61,7 @@ namespace OpenRA.Mods.Common.Traits
void FindCarryableForTransport(Actor self) void FindCarryableForTransport(Actor self)
{ {
if (!self.IsInWorld) if (!self.IsInWorld || IsTraitDisabled)
return; return;
// Get all carryables who want transport // Get all carryables who want transport
@@ -117,14 +117,14 @@ namespace OpenRA.Mods.Common.Traits
protected override void OnFirstRun(Actor self) protected override void OnFirstRun(Actor self)
{ {
if (!cargo.IsDead) if (!cargo.IsDead && !carryall.IsTraitDisabled)
QueueChild(new PickupUnit(self, cargo, 0, carryall.Info.TargetLineColor)); QueueChild(new PickupUnit(self, cargo, 0, carryall.Info.TargetLineColor));
} }
public override bool Tick(Actor self) public override bool Tick(Actor self)
{ {
// Cargo may have become invalid or PickupUnit cancelled. // 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; return true;
var dropRange = carryall.Info.DropRange; var dropRange = carryall.Info.DropRange;

View File

@@ -21,7 +21,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("This actor can transport Passenger actors.")] [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.")] [Desc("The maximum sum of Passenger.Weight that this actor can support.")]
public readonly int MaxWeight = 0; public readonly int MaxWeight = 0;
@@ -88,11 +88,10 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new Cargo(init, this); } 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, INotifyOwnerChanged, INotifySold, INotifyActorDisposing, IIssueDeployOrder,
ITransformActorInitModifier INotifyCreated, INotifyKilled, ITransformActorInitModifier
{ {
public readonly CargoInfo Info;
readonly Actor self; readonly Actor self;
readonly List<Actor> cargo = new(); readonly List<Actor> cargo = new();
readonly HashSet<Actor> reserves = new(); readonly HashSet<Actor> reserves = new();
@@ -119,9 +118,9 @@ namespace OpenRA.Mods.Common.Traits
State state = State.Free; State state = State.Free;
public Cargo(ActorInitializer init, CargoInfo info) public Cargo(ActorInitializer init, CargoInfo info)
: base(info)
{ {
self = init.Self; self = init.Self;
Info = info;
checkTerrainType = info.UnloadTerrainTypes.Count > 0; checkTerrainType = info.UnloadTerrainTypes.Count > 0;
currentAdjacentCells = new CachedTransform<CPos, IEnumerable<CPos>>(loc => currentAdjacentCells = new CachedTransform<CPos, IEnumerable<CPos>>(loc =>
@@ -162,8 +161,9 @@ namespace OpenRA.Mods.Common.Traits
facing = Exts.Lazy(self.TraitOrDefault<IFacing>); facing = Exts.Lazy(self.TraitOrDefault<IFacing>);
} }
void INotifyCreated.Created(Actor self) protected override void Created(Actor self)
{ {
base.Created(self);
aircraft = self.TraitOrDefault<Aircraft>(); aircraft = self.TraitOrDefault<Aircraft>();
if (cargo.Count > 0) if (cargo.Count > 0)
@@ -200,6 +200,9 @@ namespace OpenRA.Mods.Common.Traits
{ {
get get
{ {
if (IsTraitDisabled)
yield break;
yield return new DeployOrderTargeter("Unload", 10, yield return new DeployOrderTargeter("Unload", 10,
() => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor); () => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor);
} }
@@ -233,6 +236,9 @@ namespace OpenRA.Mods.Common.Traits
public bool CanUnload(BlockedByActor check = BlockedByActor.None) public bool CanUnload(BlockedByActor check = BlockedByActor.None)
{ {
if (IsTraitDisabled)
return false;
if (checkTerrainType) if (checkTerrainType)
{ {
var terrainType = self.World.Map.GetTerrainInfo(self.Location).Type; var terrainType = self.World.Map.GetTerrainInfo(self.Location).Type;
@@ -247,7 +253,7 @@ namespace OpenRA.Mods.Common.Traits
public bool CanLoad(Actor a) 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) internal bool ReserveSpace(Actor a)

View File

@@ -21,7 +21,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Transports actors with the `" + nameof(Carryable) + "` trait.")] [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))] [ActorReference(typeof(CarryableInfo))]
[Desc("Actor type that is initially spawned into this actor.")] [Desc("Actor type that is initially spawned into this actor.")]
@@ -83,8 +83,9 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new Carryall(init.Self, this); } public override object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
} }
public class Carryall : INotifyKilled, ISync, ITick, IRender, INotifyActorDisposing, IIssueOrder, IResolveOrder, public class Carryall : ConditionalTrait<CarryallInfo>, INotifyKilled, ISync, ITick, IRender,
IOrderVoice, IIssueDeployOrder, IAircraftCenterPositionOffset, IOverrideAircraftLanding INotifyActorDisposing, IIssueOrder, IResolveOrder, IOrderVoice, IIssueDeployOrder,
IAircraftCenterPositionOffset, IOverrideAircraftLanding
{ {
public enum CarryallState public enum CarryallState
{ {
@@ -93,7 +94,6 @@ namespace OpenRA.Mods.Common.Traits
Carrying Carrying
} }
public readonly CarryallInfo Info;
readonly AircraftInfo aircraftInfo; readonly AircraftInfo aircraftInfo;
readonly Aircraft aircraft; readonly Aircraft aircraft;
readonly BodyOrientation body; readonly BodyOrientation body;
@@ -115,9 +115,8 @@ namespace OpenRA.Mods.Common.Traits
public WVec CarryableOffset { get; private set; } public WVec CarryableOffset { get; private set; }
public Carryall(Actor self, CarryallInfo info) public Carryall(Actor self, CarryallInfo info)
: base(info)
{ {
Info = info;
Carryable = null; Carryable = null;
State = CarryallState.Idle; State = CarryallState.Idle;
@@ -311,6 +310,9 @@ namespace OpenRA.Mods.Common.Traits
// Check if we can drop the unit at our current location. // Check if we can drop the unit at our current location.
public bool CanUnload() public bool CanUnload()
{ {
if (IsTraitDisabled)
return false;
var targetCell = self.World.Map.CellContaining(aircraft.GetPosition()); var targetCell = self.World.Map.CellContaining(aircraft.GetPosition());
return Carryable != null && aircraft.CanLand(targetCell, blockedByMobile: false); return Carryable != null && aircraft.CanLand(targetCell, blockedByMobile: false);
} }
@@ -319,6 +321,9 @@ namespace OpenRA.Mods.Common.Traits
{ {
get get
{ {
if (IsTraitDisabled)
yield break;
yield return new CarryallPickupOrderTargeter(Info); yield return new CarryallPickupOrderTargeter(Info);
yield return new DeployOrderTargeter("Unload", 10, yield return new DeployOrderTargeter("Unload", 10,
() => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor); () => CanUnload() ? Info.UnloadCursor : Info.UnloadBlockedCursor);
@@ -339,7 +344,10 @@ namespace OpenRA.Mods.Common.Traits
return new Order("Unload", self, 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) void IResolveOrder.ResolveOrder(Actor self, Order order)
{ {

View File

@@ -109,13 +109,13 @@ namespace OpenRA.Mods.Common.Traits
bool IsCorrectCargoType(Actor target) bool IsCorrectCargoType(Actor target)
{ {
var ci = target.Info.TraitInfo<CargoInfo>(); var cargo = target.Trait<Cargo>();
return ci.Types.Contains(Info.CargoType); return !cargo.IsTraitDisabled && cargo.Info.Types.Contains(Info.CargoType);
} }
bool CanEnter(Cargo cargo) bool CanEnter(Cargo cargo)
{ {
return cargo != null && cargo.HasSpace(Info.Weight); return cargo != null && !cargo.IsTraitDisabled && cargo.HasSpace(Info.Weight);
} }
bool CanEnter(Actor target) bool CanEnter(Actor target)