Overhaul Carryall behaviour, adding support for manual control.
This commit is contained in:
@@ -10,206 +10,336 @@
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Mods.Common.Traits.Render;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Mods.Common.Orders;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries.")]
|
||||
public class CarryallInfo : ITraitInfo, Requires<BodyOrientationInfo>
|
||||
[Desc("Transports actors with the `Carryable` trait.")]
|
||||
public class CarryallInfo : ITraitInfo, Requires<BodyOrientationInfo>, Requires<AircraftInfo>
|
||||
{
|
||||
[Desc("Set to false when the carryall should not automatically get new jobs.")]
|
||||
public readonly bool Automatic = true;
|
||||
[Desc("Delay on the ground while attaching an actor to the carryall.")]
|
||||
public readonly int LoadingDelay = 0;
|
||||
|
||||
public object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
|
||||
[Desc("Delay on the ground while detacting an actor to the carryall.")]
|
||||
public readonly int UnloadingDelay = 0;
|
||||
|
||||
[Desc("Carryable attachment point relative to body.")]
|
||||
public readonly WVec LocalOffset = WVec.Zero;
|
||||
|
||||
[Desc("Radius around the target drop location that are considered if the target tile is blocked.")]
|
||||
public readonly WDist DropRange = WDist.FromCells(5);
|
||||
|
||||
[VoiceReference]
|
||||
public readonly string Voice = "Action";
|
||||
|
||||
public virtual object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Carryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender, INotifyActorDisposing
|
||||
public class Carryall : INotifyKilled, ISync, IRender, INotifyActorDisposing, IIssueOrder, IResolveOrder, IOrderVoice
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly WDist carryHeight;
|
||||
readonly CarryallInfo info;
|
||||
public enum CarryallState
|
||||
{
|
||||
Idle,
|
||||
Reserved,
|
||||
Carrying
|
||||
}
|
||||
|
||||
public readonly CarryallInfo Info;
|
||||
readonly AircraftInfo aircraftInfo;
|
||||
readonly BodyOrientation body;
|
||||
readonly IMove move;
|
||||
readonly IFacing facing;
|
||||
|
||||
// The actor we are currently carrying.
|
||||
[Sync] public Actor Carrying { get; internal set; }
|
||||
public bool IsCarrying { get; internal set; }
|
||||
[Sync] public Actor Carryable { get; private set; }
|
||||
public CarryallState State { get; private set; }
|
||||
|
||||
// TODO: Use ActorPreviews so that this can support actors with multiple sprites
|
||||
Animation anim;
|
||||
IActorPreview[] carryablePreview = null;
|
||||
|
||||
public bool IsBusy { get; internal set; }
|
||||
/// <summary>Offset between the carryall's and the carried actor's CenterPositions</summary>
|
||||
public WVec CarryableOffset { get; private set; }
|
||||
|
||||
public Carryall(Actor self, CarryallInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
this.Info = info;
|
||||
|
||||
IsBusy = false;
|
||||
IsCarrying = false;
|
||||
Carryable = null;
|
||||
State = CarryallState.Idle;
|
||||
|
||||
var helicopter = self.Info.TraitInfoOrDefault<AircraftInfo>();
|
||||
carryHeight = helicopter != null ? helicopter.LandAltitude : WDist.Zero;
|
||||
aircraftInfo = self.Info.TraitInfoOrDefault<AircraftInfo>();
|
||||
body = self.Trait<BodyOrientation>();
|
||||
move = self.Trait<IMove>();
|
||||
facing = self.Trait<IFacing>();
|
||||
}
|
||||
|
||||
public void OnBecomingIdle(Actor self)
|
||||
void INotifyActorDisposing.Disposing(Actor self)
|
||||
{
|
||||
if (info.Automatic)
|
||||
FindCarryableForTransport();
|
||||
|
||||
if (!IsBusy)
|
||||
self.QueueActivity(new HeliFlyCircle(self));
|
||||
}
|
||||
|
||||
// A carryable notifying us that he'd like to be carried
|
||||
public bool RequestTransportNotify(Actor carryable)
|
||||
{
|
||||
if (IsBusy || !info.Automatic)
|
||||
return false;
|
||||
|
||||
if (ReserveCarryable(carryable))
|
||||
if (State == CarryallState.Carrying)
|
||||
{
|
||||
self.QueueActivity(false, new PickupUnit(self, carryable));
|
||||
self.QueueActivity(true, new DeliverUnit(self));
|
||||
return true;
|
||||
Carryable.Dispose();
|
||||
Carryable = null;
|
||||
}
|
||||
|
||||
UnreserveCarryable(self);
|
||||
}
|
||||
|
||||
void INotifyKilled.Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
if (State == CarryallState.Carrying)
|
||||
{
|
||||
if (Carryable.IsInWorld && !Carryable.IsDead)
|
||||
Carryable.Kill(e.Attacker);
|
||||
Carryable = null;
|
||||
}
|
||||
|
||||
UnreserveCarryable(self);
|
||||
}
|
||||
|
||||
public virtual bool RequestTransportNotify(Actor self, Actor carryable, CPos destination)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void FindCarryableForTransport()
|
||||
public virtual WVec OffsetForCarryable(Actor self, Actor carryable)
|
||||
{
|
||||
if (!self.IsInWorld)
|
||||
return;
|
||||
return Info.LocalOffset - carryable.Info.TraitInfo<CarryableInfo>().LocalOffset;
|
||||
}
|
||||
|
||||
// Get all carryables who want transport
|
||||
var carryables = self.World.ActorsWithTrait<Carryable>()
|
||||
.Where(p =>
|
||||
{
|
||||
var actor = p.Actor;
|
||||
if (actor == null)
|
||||
return false;
|
||||
public virtual bool AttachCarryable(Actor self, Actor carryable)
|
||||
{
|
||||
if (State == CarryallState.Carrying)
|
||||
return false;
|
||||
|
||||
if (actor.Owner != self.Owner)
|
||||
return false;
|
||||
Carryable = carryable;
|
||||
State = CarryallState.Carrying;
|
||||
|
||||
if (actor.IsDead)
|
||||
return false;
|
||||
CarryableOffset = OffsetForCarryable(self, carryable);
|
||||
return true;
|
||||
}
|
||||
|
||||
var trait = p.Trait;
|
||||
if (trait.Reserved)
|
||||
return false;
|
||||
public virtual void DetachCarryable(Actor self)
|
||||
{
|
||||
UnreserveCarryable(self);
|
||||
|
||||
if (!trait.WantsTransport)
|
||||
return false;
|
||||
carryablePreview = null;
|
||||
CarryableOffset = WVec.Zero;
|
||||
}
|
||||
|
||||
if (actor.IsIdle)
|
||||
return false;
|
||||
public virtual bool ReserveCarryable(Actor self, Actor carryable)
|
||||
{
|
||||
if (State == CarryallState.Reserved)
|
||||
UnreserveCarryable(self);
|
||||
|
||||
return true;
|
||||
})
|
||||
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
if (State != CarryallState.Idle || !carryable.Trait<Carryable>().Reserve(carryable, self))
|
||||
return false;
|
||||
|
||||
foreach (var p in carryables)
|
||||
Carryable = carryable;
|
||||
State = CarryallState.Reserved;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void UnreserveCarryable(Actor self)
|
||||
{
|
||||
if (Carryable != null && Carryable.IsInWorld && !Carryable.IsDead)
|
||||
Carryable.Trait<Carryable>().UnReserve(Carryable);
|
||||
|
||||
Carryable = null;
|
||||
State = CarryallState.Idle;
|
||||
}
|
||||
|
||||
IEnumerable<IRenderable> IRender.Render(Actor self, WorldRenderer wr)
|
||||
{
|
||||
if (State == CarryallState.Carrying)
|
||||
{
|
||||
// Check if its actually me who's the best candidate
|
||||
if (p.Trait.GetClosestIdleCarrier() == self && ReserveCarryable(p.Actor))
|
||||
if (carryablePreview == null)
|
||||
{
|
||||
self.QueueActivity(false, new PickupUnit(self, p.Actor));
|
||||
self.QueueActivity(true, new DeliverUnit(self));
|
||||
break;
|
||||
var carryableInits = new TypeDictionary()
|
||||
{
|
||||
new OwnerInit(Carryable.Owner),
|
||||
new DynamicFacingInit(() => facing.Facing),
|
||||
};
|
||||
|
||||
foreach (var api in Carryable.TraitsImplementing<IActorPreviewInitModifier>())
|
||||
api.ModifyActorPreviewInit(Carryable, carryableInits);
|
||||
|
||||
var init = new ActorPreviewInitializer(Carryable.Info, wr, carryableInits);
|
||||
carryablePreview = Carryable.Info.TraitInfos<IRenderActorPreviewInfo>()
|
||||
.SelectMany(rpi => rpi.RenderPreview(init))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
var offset = body.LocalToWorld(CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation)));
|
||||
var previewRenderables = carryablePreview
|
||||
.SelectMany(p => p.Render(wr, self.CenterPosition + offset))
|
||||
.OrderBy(WorldRenderer.RenderableScreenZPositionComparisonKey);
|
||||
|
||||
foreach (var r in previewRenderables)
|
||||
yield return r;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<IOrderTargeter> IIssueOrder.Orders
|
||||
{
|
||||
get
|
||||
{
|
||||
if (State != CarryallState.Carrying)
|
||||
yield return new CarryallPickupOrderTargeter();
|
||||
else
|
||||
yield return new CarryallDeliverUnitTargeter(aircraftInfo, CarryableOffset);
|
||||
}
|
||||
}
|
||||
|
||||
Order IIssueOrder.IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
|
||||
{
|
||||
if (order.OrderID == "PickupUnit")
|
||||
{
|
||||
if (target.Type == TargetType.FrozenActor)
|
||||
return new Order(order.OrderID, self, queued) { ExtraData = target.FrozenActor.ID };
|
||||
|
||||
return new Order(order.OrderID, self, queued) { TargetActor = target.Actor };
|
||||
}
|
||||
else if (order.OrderID == "DeliverUnit")
|
||||
{
|
||||
return new Order(order.OrderID, self, queued) { TargetLocation = self.World.Map.CellContaining(target.CenterPosition) };
|
||||
}
|
||||
else if (order.OrderID == "Unload")
|
||||
{
|
||||
return new Order(order.OrderID, self, queued) { TargetLocation = self.World.Map.CellContaining(target.CenterPosition) };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void IResolveOrder.ResolveOrder(Actor self, Order order)
|
||||
{
|
||||
if (State == CarryallState.Carrying)
|
||||
{
|
||||
if (order.OrderString == "DeliverUnit")
|
||||
{
|
||||
var cell = self.World.Map.Clamp(order.TargetLocation);
|
||||
|
||||
if (!aircraftInfo.MoveIntoShroud && !self.Owner.Shroud.IsExplored(cell))
|
||||
return;
|
||||
|
||||
var targetLocation = move.NearestMoveableCell(order.TargetLocation);
|
||||
self.SetTargetLine(Target.FromCell(self.World, targetLocation), Color.Yellow);
|
||||
self.QueueActivity(order.Queued, new DeliverUnit(self, targetLocation));
|
||||
}
|
||||
else if (order.OrderString == "Unload")
|
||||
{
|
||||
var targetLocation = move.NearestMoveableCell(self.Location);
|
||||
self.SetTargetLine(Target.FromCell(self.World, targetLocation), Color.Yellow);
|
||||
self.QueueActivity(order.Queued, new DeliverUnit(self, targetLocation));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (order.OrderString == "PickupUnit")
|
||||
{
|
||||
var target = self.ResolveFrozenActorOrder(order, Color.Yellow);
|
||||
if (target.Type != TargetType.Actor)
|
||||
return;
|
||||
|
||||
if (!ReserveCarryable(self, target.Actor))
|
||||
return;
|
||||
|
||||
if (!order.Queued)
|
||||
self.CancelActivity();
|
||||
|
||||
self.SetTargetLine(target, Color.Yellow);
|
||||
self.QueueActivity(order.Queued, new PickupUnit(self, target.Actor, Info.LoadingDelay));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve the carryable so its ours exclusively
|
||||
public bool ReserveCarryable(Actor carryable)
|
||||
string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)
|
||||
{
|
||||
if (Carrying != null)
|
||||
return false;
|
||||
|
||||
if (carryable.Trait<Carryable>().Reserve(self))
|
||||
switch (order.OrderString)
|
||||
{
|
||||
Carrying = carryable;
|
||||
IsBusy = true;
|
||||
case "DeliverUnit":
|
||||
case "Unload":
|
||||
case "PickupUnit":
|
||||
return Info.Voice;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class CarryallPickupOrderTargeter : UnitOrderTargeter
|
||||
{
|
||||
public CarryallPickupOrderTargeter()
|
||||
: base("PickupUnit", 5, "ability", false, true)
|
||||
{
|
||||
}
|
||||
|
||||
static bool CanTarget(Actor self, Actor target)
|
||||
{
|
||||
if (!target.AppearsFriendlyTo(self))
|
||||
return false;
|
||||
var carryable = target.TraitOrDefault<Carryable>();
|
||||
if (carryable == null)
|
||||
return false;
|
||||
if (carryable.Reserved && carryable.Carrier != self)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Unreserve the carryable
|
||||
public void UnreserveCarryable()
|
||||
{
|
||||
if (Carrying != null)
|
||||
public override bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor)
|
||||
{
|
||||
if (Carrying.IsInWorld && !Carrying.IsDead)
|
||||
Carrying.Trait<Carryable>().UnReserve(self);
|
||||
|
||||
Carrying = null;
|
||||
return CanTarget(self, target);
|
||||
}
|
||||
|
||||
CarryableReleased();
|
||||
}
|
||||
|
||||
// INotifyKilled
|
||||
public void Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
if (Carrying != null)
|
||||
public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
|
||||
{
|
||||
if (IsCarrying && Carrying.IsInWorld && !Carrying.IsDead)
|
||||
Carrying.Kill(e.Attacker);
|
||||
|
||||
Carrying = null;
|
||||
}
|
||||
|
||||
UnreserveCarryable();
|
||||
}
|
||||
|
||||
public void Disposing(Actor self)
|
||||
{
|
||||
if (Carrying != null && IsCarrying)
|
||||
{
|
||||
Carrying.Dispose();
|
||||
Carrying = null;
|
||||
return CanTarget(self, target.Actor);
|
||||
}
|
||||
}
|
||||
|
||||
// Called when carryable is inside.
|
||||
public void AttachCarryable(Actor carryable)
|
||||
class CarryallDeliverUnitTargeter : AircraftMoveOrderTargeter
|
||||
{
|
||||
IsBusy = true;
|
||||
IsCarrying = true;
|
||||
Carrying = carryable;
|
||||
readonly AircraftInfo aircraftInfo;
|
||||
readonly WVec carryableOffset;
|
||||
|
||||
// Create a new animation for our carryable unit
|
||||
var rs = carryable.Trait<RenderSprites>();
|
||||
anim = new Animation(self.World, rs.GetImage(carryable), RenderSprites.MakeFacingFunc(self));
|
||||
anim.PlayRepeating("idle");
|
||||
anim.IsDecoration = true;
|
||||
}
|
||||
|
||||
// Called when released
|
||||
public void CarryableReleased()
|
||||
{
|
||||
IsBusy = false;
|
||||
IsCarrying = false;
|
||||
anim = null;
|
||||
}
|
||||
|
||||
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
|
||||
{
|
||||
// Render the carryable below us TODO: Implement RenderSprites trait
|
||||
if (anim != null && !self.World.FogObscures(self))
|
||||
public CarryallDeliverUnitTargeter(AircraftInfo aircraftInfo, WVec carryableOffset)
|
||||
: base(aircraftInfo)
|
||||
{
|
||||
anim.Tick();
|
||||
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Length),
|
||||
wr.Palette("player" + Carrying.Owner.InternalName));
|
||||
OrderID = "DeliverUnit";
|
||||
OrderPriority = 6;
|
||||
this.carryableOffset = carryableOffset;
|
||||
this.aircraftInfo = aircraftInfo;
|
||||
}
|
||||
|
||||
foreach (var rr in renderables)
|
||||
yield return rr;
|
||||
public override bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, ref TargetModifiers modifiers, ref string cursor)
|
||||
{
|
||||
if (modifiers.HasModifier(TargetModifiers.ForceMove))
|
||||
return false;
|
||||
|
||||
var type = target.Type;
|
||||
if (type == TargetType.Actor && self == target.Actor)
|
||||
{
|
||||
var altitude = self.World.Map.DistanceAboveTerrain(self.CenterPosition);
|
||||
if (altitude.Length - carryableOffset.Z < aircraftInfo.MinAirborneAltitude)
|
||||
{
|
||||
cursor = "deploy";
|
||||
OrderID = "Unload";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if ((type == TargetType.Actor && target.Actor.Info.HasTraitInfo<BuildingInfo>())
|
||||
|| (target.Type == TargetType.FrozenActor && target.FrozenActor.Info.HasTraitInfo<BuildingInfo>()))
|
||||
{
|
||||
cursor = "move-blocked";
|
||||
return true;
|
||||
}
|
||||
|
||||
return base.CanTarget(self, target, othersAtTarget, ref modifiers, ref cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user