Merge pull request #11501 from pchote/tibsun-carryall
Add Carryall/Carryable traits, merging ts and d2k carryall behavior
This commit is contained in:
147
OpenRA.Mods.Common/Traits/AutoCarryable.cs
Normal file
147
OpenRA.Mods.Common/Traits/AutoCarryable.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Can be carried by units with the trait `Carryall`.")]
|
||||
public class AutoCarryableInfo : CarryableInfo
|
||||
{
|
||||
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
|
||||
public readonly WDist MinDistance = WDist.FromCells(6);
|
||||
|
||||
public override object Create(ActorInitializer init) { return new AutoCarryable(init.Self, this); }
|
||||
}
|
||||
|
||||
public class AutoCarryable : Carryable, INotifyHarvesterAction, ICallForTransport
|
||||
{
|
||||
readonly AutoCarryableInfo info;
|
||||
Activity afterLandActivity;
|
||||
|
||||
public AutoCarryable(Actor self, AutoCarryableInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public WDist MinimumDistance { get { return info.MinDistance; } }
|
||||
|
||||
void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(self, targetCell, next); }
|
||||
void INotifyHarvesterAction.MovingToRefinery(Actor self, CPos targetCell, Activity next) { RequestTransport(self, targetCell, next); }
|
||||
void INotifyHarvesterAction.MovementCancelled(Actor self) { MovementCancelled(self); }
|
||||
|
||||
// We do not handle Harvested notification
|
||||
void INotifyHarvesterAction.Harvested(Actor self, ResourceType resource) { }
|
||||
void INotifyHarvesterAction.Docked() { }
|
||||
void INotifyHarvesterAction.Undocked() { }
|
||||
|
||||
// No longer want to be carried
|
||||
void ICallForTransport.MovementCancelled(Actor self) { MovementCancelled(self); }
|
||||
void ICallForTransport.RequestTransport(Actor self, CPos destination, Activity afterLandActivity) { RequestTransport(self, destination, afterLandActivity); }
|
||||
|
||||
void MovementCancelled(Actor self)
|
||||
{
|
||||
if (state == State.Locked)
|
||||
return;
|
||||
|
||||
Destination = null;
|
||||
afterLandActivity = null;
|
||||
|
||||
// TODO: We could implement something like a carrier.Trait<Carryall>().CancelTransportNotify(self) and call it here
|
||||
}
|
||||
|
||||
void RequestTransport(Actor self, CPos destination, Activity afterLandActivity)
|
||||
{
|
||||
var delta = self.World.Map.CenterOfCell(destination) - self.CenterPosition;
|
||||
if (delta.HorizontalLengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
Destination = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Destination = destination;
|
||||
this.afterLandActivity = afterLandActivity;
|
||||
|
||||
if (state != State.Free)
|
||||
return;
|
||||
|
||||
// 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)
|
||||
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
|
||||
// Enumerate idle carriers to find the first that is able to transport us
|
||||
foreach (var carrier in carriers)
|
||||
if (carrier.Trait.RequestTransportNotify(carrier.Actor, self, destination))
|
||||
return;
|
||||
}
|
||||
|
||||
// This gets called by carrier after we touched down
|
||||
public override void Detached(Actor self)
|
||||
{
|
||||
if (!attached)
|
||||
return;
|
||||
|
||||
Destination = null;
|
||||
|
||||
if (afterLandActivity != null)
|
||||
{
|
||||
// HACK: Harvesters need special treatment to avoid getting stuck on resource fields,
|
||||
// so if a Harvester's afterLandActivity is not DeliverResources, queue a new FindResources activity
|
||||
var findResources = self.Info.HasTraitInfo<HarvesterInfo>() && !(afterLandActivity is DeliverResources);
|
||||
if (findResources)
|
||||
self.QueueActivity(new FindResources(self));
|
||||
else
|
||||
self.QueueActivity(false, afterLandActivity);
|
||||
}
|
||||
|
||||
base.Detached(self);
|
||||
}
|
||||
|
||||
public override bool Reserve(Actor self, Actor carrier)
|
||||
{
|
||||
if (Reserved || !WantsTransport)
|
||||
return false;
|
||||
|
||||
var delta = self.World.Map.CenterOfCell(Destination.Value) - self.CenterPosition;
|
||||
if (delta.HorizontalLengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
// Cancel pickup
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
return base.Reserve(self, carrier);
|
||||
}
|
||||
|
||||
// Prepare for transport pickup
|
||||
public override bool LockForPickup(Actor self, Actor carrier)
|
||||
{
|
||||
if (state == State.Locked || !WantsTransport)
|
||||
return false;
|
||||
|
||||
// Last chance to change our mind...
|
||||
var delta = self.World.Map.CenterOfCell(Destination.Value) - self.CenterPosition;
|
||||
if (delta.HorizontalLengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
// Cancel pickup
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
return base.LockForPickup(self, carrier);
|
||||
}
|
||||
}
|
||||
}
|
||||
110
OpenRA.Mods.Common/Traits/AutoCarryall.cs
Normal file
110
OpenRA.Mods.Common/Traits/AutoCarryall.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries.")]
|
||||
public class AutoCarryallInfo : CarryallInfo
|
||||
{
|
||||
public override object Create(ActorInitializer init) { return new AutoCarryall(init.Self, this); }
|
||||
}
|
||||
|
||||
public class AutoCarryall : Carryall, INotifyBecomingIdle
|
||||
{
|
||||
bool busy;
|
||||
|
||||
public AutoCarryall(Actor self, AutoCarryallInfo info)
|
||||
: base(self, info) { }
|
||||
|
||||
void INotifyBecomingIdle.OnBecomingIdle(Actor self)
|
||||
{
|
||||
busy = false;
|
||||
FindCarryableForTransport(self);
|
||||
|
||||
// TODO: This should be handled by the aircraft trait
|
||||
if (!busy)
|
||||
self.QueueActivity(new HeliFlyCircle(self));
|
||||
}
|
||||
|
||||
// A carryable notifying us that he'd like to be carried
|
||||
public override bool RequestTransportNotify(Actor self, Actor carryable, CPos destination)
|
||||
{
|
||||
if (busy)
|
||||
return false;
|
||||
|
||||
if (ReserveCarryable(self, carryable))
|
||||
{
|
||||
self.QueueActivity(false, new PickupUnit(self, carryable, 0));
|
||||
self.QueueActivity(true, new DeliverUnit(self, destination));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsBestAutoCarryallForCargo(Actor self, Actor candidateCargo)
|
||||
{
|
||||
// Find carriers
|
||||
var carriers = self.World.ActorsHavingTrait<AutoCarryall>(c => !c.busy)
|
||||
.Where(a => a.Owner == self.Owner && a.IsInWorld);
|
||||
|
||||
return carriers.ClosestTo(candidateCargo) == self;
|
||||
}
|
||||
|
||||
void FindCarryableForTransport(Actor self)
|
||||
{
|
||||
if (!self.IsInWorld)
|
||||
return;
|
||||
|
||||
// Get all carryables who want transport
|
||||
var carryables = self.World.ActorsWithTrait<Carryable>().Where(p =>
|
||||
{
|
||||
var actor = p.Actor;
|
||||
if (actor == null)
|
||||
return false;
|
||||
|
||||
if (actor.Owner != self.Owner)
|
||||
return false;
|
||||
|
||||
if (actor.IsDead)
|
||||
return false;
|
||||
|
||||
var trait = p.Trait;
|
||||
if (trait.Reserved)
|
||||
return false;
|
||||
|
||||
if (!trait.WantsTransport)
|
||||
return false;
|
||||
|
||||
if (actor.IsIdle)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}).OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
|
||||
foreach (var p in carryables)
|
||||
{
|
||||
// Check if its actually me who's the best candidate
|
||||
if (IsBestAutoCarryallForCargo(self, p.Actor) && ReserveCarryable(self, p.Actor))
|
||||
{
|
||||
busy = true;
|
||||
self.QueueActivity(false, new PickupUnit(self, p.Actor, 0));
|
||||
self.QueueActivity(true, new DeliverUnit(self, p.Trait.Destination.Value));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,12 +54,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
CreateActors(actorName, carrierActorName, out cargo, out carrier);
|
||||
|
||||
var carryable = cargo.Trait<Carryable>();
|
||||
carryable.Destination = location;
|
||||
carryable.Reserve(carrier);
|
||||
carryable.Reserve(cargo, carrier);
|
||||
|
||||
carrier.Trait<Carryall>().AttachCarryable(cargo);
|
||||
|
||||
carrier.QueueActivity(new DeliverUnit(carrier));
|
||||
carrier.Trait<Carryall>().AttachCarryable(carrier, cargo);
|
||||
carrier.QueueActivity(new DeliverUnit(carrier, location));
|
||||
carrier.QueueActivity(new HeliFly(carrier, Target.FromCell(self.World, self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom))));
|
||||
carrier.QueueActivity(new RemoveSelf());
|
||||
|
||||
|
||||
@@ -9,191 +9,90 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Can be carried by units with the trait `Carryall`.")]
|
||||
[Desc("Can be carried by actors with the `Carryall` trait.")]
|
||||
public class CarryableInfo : ITraitInfo, Requires<UpgradeManagerInfo>
|
||||
{
|
||||
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
|
||||
public readonly WDist MinDistance = WDist.FromCells(6);
|
||||
|
||||
[UpgradeGrantedReference]
|
||||
[Desc("The upgrades to grant to self while waiting or being carried.")]
|
||||
public readonly string[] CarryableUpgrades = { };
|
||||
|
||||
public object Create(ActorInitializer init) { return new Carryable(init.Self, this); }
|
||||
[Desc("Carryall attachment point relative to body.")]
|
||||
public readonly WVec LocalOffset = WVec.Zero;
|
||||
|
||||
public virtual object Create(ActorInitializer init) { return new Carryable(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Carryable : INotifyHarvesterAction, ICallForTransport
|
||||
public class Carryable
|
||||
{
|
||||
readonly CarryableInfo info;
|
||||
readonly Actor self;
|
||||
readonly UpgradeManager upgradeManager;
|
||||
|
||||
public bool Reserved { get; private set; }
|
||||
public Actor Carrier { get; private set; }
|
||||
public bool Reserved { get { return state != State.Free; } }
|
||||
public CPos? Destination { get; protected set; }
|
||||
public bool WantsTransport { get { return Destination != null; } }
|
||||
|
||||
// If we're locked there isn't much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
|
||||
bool locked;
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
if (!locked)
|
||||
return;
|
||||
|
||||
locked = false;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.RevokeUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
if (locked)
|
||||
return;
|
||||
|
||||
locked = true;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
public bool WantsTransport { get; set; }
|
||||
public CPos Destination;
|
||||
Activity afterLandActivity;
|
||||
protected enum State { Free, Reserved, Locked }
|
||||
protected State state = State.Free;
|
||||
protected bool attached;
|
||||
|
||||
public Carryable(Actor self, CarryableInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
this.self = self;
|
||||
upgradeManager = self.Trait<UpgradeManager>();
|
||||
|
||||
locked = false;
|
||||
Reserved = false;
|
||||
WantsTransport = false;
|
||||
}
|
||||
|
||||
public void MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
|
||||
public void MovingToRefinery(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
|
||||
|
||||
public WDist MinimumDistance { get { return info.MinDistance; } }
|
||||
|
||||
public void RequestTransport(CPos destination, Activity afterLandActivity)
|
||||
public virtual void Attached(Actor self)
|
||||
{
|
||||
var destPos = self.World.Map.CenterOfCell(destination);
|
||||
if (destination == CPos.Zero || (self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
WantsTransport = false; // Be sure to cancel any pending transports
|
||||
return;
|
||||
}
|
||||
|
||||
Destination = destination;
|
||||
this.afterLandActivity = afterLandActivity;
|
||||
WantsTransport = true;
|
||||
|
||||
if (locked || Reserved)
|
||||
if (attached)
|
||||
return;
|
||||
|
||||
// Inform all idle carriers
|
||||
var carriers = self.World.ActorsWithTrait<Carryall>()
|
||||
.Where(c => !c.Trait.IsBusy && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
|
||||
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
|
||||
// Is any carrier able to transport the actor?
|
||||
// Any will return once it finds a carrier that returns true.
|
||||
carriers.Any(carrier => carrier.Trait.RequestTransportNotify(self));
|
||||
}
|
||||
|
||||
// No longer want to be carried
|
||||
public void MovementCancelled(Actor self)
|
||||
{
|
||||
if (locked)
|
||||
return;
|
||||
|
||||
WantsTransport = false;
|
||||
afterLandActivity = null;
|
||||
|
||||
// TODO: We could implement something like a carrier.Trait<Carryall>().CancelTransportNotify(self) and call it here
|
||||
}
|
||||
|
||||
// We do not handle Harvested notification
|
||||
public void Harvested(Actor self, ResourceType resource) { }
|
||||
public void Docked() { }
|
||||
public void Undocked() { }
|
||||
|
||||
public Actor GetClosestIdleCarrier()
|
||||
{
|
||||
// Find carriers
|
||||
var carriers = self.World.ActorsHavingTrait<Carryall>(c => !c.IsBusy)
|
||||
.Where(a => a.Owner == self.Owner && a.IsInWorld);
|
||||
|
||||
return carriers.ClosestTo(self);
|
||||
attached = true;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
// This gets called by carrier after we touched down
|
||||
public void Dropped()
|
||||
public virtual void Detached(Actor self)
|
||||
{
|
||||
WantsTransport = false;
|
||||
Unlock();
|
||||
if (!attached)
|
||||
return;
|
||||
|
||||
if (afterLandActivity != null)
|
||||
{
|
||||
// HACK: Harvesters need special treatment to avoid getting stuck on resource fields,
|
||||
// so if a Harvester's afterLandActivity is not DeliverResources, queue a new FindResources activity
|
||||
var findResources = self.Info.HasTraitInfo<HarvesterInfo>() && !(afterLandActivity is DeliverResources);
|
||||
if (findResources)
|
||||
self.QueueActivity(new FindResources(self));
|
||||
else
|
||||
self.QueueActivity(false, afterLandActivity);
|
||||
}
|
||||
attached = false;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.RevokeUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
public bool Reserve(Actor carrier)
|
||||
public virtual bool Reserve(Actor self, Actor carrier)
|
||||
{
|
||||
if (Reserved)
|
||||
return false;
|
||||
|
||||
var destPos = self.World.Map.CenterOfCell(Destination);
|
||||
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reserved = true;
|
||||
|
||||
state = State.Reserved;
|
||||
Carrier = carrier;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UnReserve(Actor carrier)
|
||||
public virtual void UnReserve(Actor self)
|
||||
{
|
||||
Reserved = false;
|
||||
Unlock();
|
||||
state = State.Free;
|
||||
Carrier = null;
|
||||
}
|
||||
|
||||
// Prepare for transport pickup
|
||||
public bool StandbyForPickup(Actor carrier)
|
||||
public virtual bool LockForPickup(Actor self, Actor carrier)
|
||||
{
|
||||
if (Destination == CPos.Zero)
|
||||
if (state == State.Locked)
|
||||
return false;
|
||||
|
||||
if (locked || !WantsTransport)
|
||||
return false;
|
||||
|
||||
// Last chance to change our mind...
|
||||
var destPos = self.World.Map.CenterOfCell(Destination);
|
||||
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cancel our activities
|
||||
self.CancelActivity();
|
||||
Lock();
|
||||
|
||||
state = State.Locked;
|
||||
Carrier = carrier;
|
||||
self.QueueActivity(false, new WaitFor(() => state != State.Locked, false));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public void ContinueHarvesting(Actor self)
|
||||
{
|
||||
// Move out of the refinery dock and continue harvesting:
|
||||
// Move out of the refinery dock and continue harvesting
|
||||
UnblockRefinery(self);
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
@@ -220,6 +220,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
// Get out of the way:
|
||||
var unblockCell = LastHarvestedCell ?? (deliveryLoc + Info.UnblockCell);
|
||||
var moveTo = mobile.NearestMoveableCell(unblockCell, 1, 5);
|
||||
|
||||
// TODO: The harvest-deliver-return sequence is a horrible mess of duplicated code and edge-cases
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
var findResources = new FindResources(self);
|
||||
foreach (var n in notify)
|
||||
n.MovingToResources(self, moveTo, findResources);
|
||||
|
||||
self.QueueActivity(mobile.MoveTo(moveTo, 1));
|
||||
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Gray, false);
|
||||
}
|
||||
@@ -363,13 +370,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
loc = self.Location;
|
||||
}
|
||||
|
||||
var next = new FindResources(self);
|
||||
self.QueueActivity(next);
|
||||
var findResources = new FindResources(self);
|
||||
self.QueueActivity(findResources);
|
||||
self.SetTargetLine(Target.FromCell(self.World, loc.Value), Color.Red);
|
||||
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
foreach (var n in notify)
|
||||
n.MovingToResources(self, loc.Value, next);
|
||||
n.MovingToResources(self, loc.Value, findResources);
|
||||
|
||||
LastOrderLocation = loc;
|
||||
|
||||
@@ -392,12 +399,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
self.CancelActivity();
|
||||
|
||||
var next = new DeliverResources(self);
|
||||
self.QueueActivity(next);
|
||||
var deliver = new DeliverResources(self);
|
||||
self.QueueActivity(deliver);
|
||||
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
foreach (var n in notify)
|
||||
n.MovingToRefinery(self, order.TargetLocation, next);
|
||||
n.MovingToRefinery(self, order.TargetLocation, deliver);
|
||||
}
|
||||
else if (order.OrderString == "Stop" || order.OrderString == "Move")
|
||||
{
|
||||
|
||||
@@ -149,7 +149,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if ((self.CenterPosition - target.CenterPosition).LengthSquared < transport.MinimumDistance.LengthSquared)
|
||||
return;
|
||||
|
||||
transport.RequestTransport(targetCell, nextActivity);
|
||||
transport.RequestTransport(self, targetCell, nextActivity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user