Overhaul Carryall behaviour, adding support for manual control.

This commit is contained in:
Paul Chote
2016-07-31 02:01:29 +01:00
parent d08cc10abb
commit 1ae86f34f8
20 changed files with 842 additions and 402 deletions

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
@@ -18,86 +19,145 @@ namespace OpenRA.Mods.Common.Activities
public class DeliverUnit : Activity
{
readonly Actor self;
readonly Actor cargo;
readonly IMove movement;
readonly Carryable carryable;
readonly Carryall carryall;
readonly Aircraft aircraft;
readonly IPositionable positionable;
readonly IFacing cargoFacing;
readonly IFacing selfFacing;
readonly BodyOrientation body;
readonly IFacing carryableFacing;
readonly IFacing carryallFacing;
readonly CPos destination;
enum State { Transport, Land, Release }
enum State { Transport, Land, Wait, Release, TakeOff, Aborted }
State state;
Activity innerActivity;
public DeliverUnit(Actor self)
public DeliverUnit(Actor self, CPos destination)
{
carryall = self.Trait<Carryall>();
this.self = self;
cargo = carryall.Carrying;
movement = self.Trait<IMove>();
carryable = cargo.Trait<Carryable>();
aircraft = self.Trait<Aircraft>();
positionable = cargo.Trait<IPositionable>();
cargoFacing = cargo.Trait<IFacing>();
selfFacing = self.Trait<IFacing>();
this.destination = destination;
carryallFacing = self.Trait<IFacing>();
carryall = self.Trait<Carryall>();
body = self.Trait<BodyOrientation>();
carryable = carryall.Carryable.Trait<Carryable>();
positionable = carryall.Carryable.Trait<IPositionable>();
carryableFacing = carryall.Carryable.Trait<IFacing>();
state = State.Transport;
}
// Find a suitable location to drop our carryable
CPos GetLocationToDrop(CPos requestedPosition)
CPos? FindDropLocation(CPos targetCell, WDist maxSearchDistance)
{
if (positionable.CanEnterCell(requestedPosition))
return requestedPosition;
// The easy case
if (positionable.CanEnterCell(targetCell))
return targetCell;
var candidateCells = Util.AdjacentCells(self.World, Target.FromCell(self.World, requestedPosition));
// TODO: This will behave badly if there is no suitable drop point nearby
do
var cellRange = (maxSearchDistance.Length + 1023) / 1024;
var centerPosition = self.World.Map.CenterOfCell(targetCell);
foreach (var c in self.World.Map.FindTilesInCircle(targetCell, cellRange))
{
foreach (var c in candidateCells)
if (positionable.CanEnterCell(c))
return c;
if (!positionable.CanEnterCell(c))
continue;
// Expanding dropable cells search area
// TODO: This also includes all of the cells we have just checked
candidateCells = Util.ExpandFootprint(candidateCells, true);
} while (true);
var delta = self.World.Map.CenterOfCell(c) - centerPosition;
if (delta.LengthSquared < maxSearchDistance.LengthSquared)
return c;
}
return null;
}
// Check if we can drop the unit at our current location.
bool CanDropHere()
{
return positionable.CanEnterCell(self.Location);
var localOffset = carryall.CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation));
var targetCell = self.World.Map.CellContaining(self.CenterPosition + body.LocalToWorld(localOffset));
return positionable.CanEnterCell(targetCell);
}
public override Activity Tick(Actor self)
{
if (cargo.IsDead || !carryall.IsBusy)
if (innerActivity != null)
{
carryall.UnreserveCarryable();
return NextActivity;
innerActivity = ActivityUtils.RunActivity(self, innerActivity);
return this;
}
if (IsCanceled)
return NextActivity;
if ((carryall.State == Carryall.CarryallState.Idle || carryall.Carryable.IsDead) && state != State.TakeOff)
state = State.Aborted;
switch (state)
{
case State.Transport:
var targetl = GetLocationToDrop(carryable.Destination);
{
var targetLocation = FindDropLocation(destination, carryall.Info.DropRange);
// Can't land, so wait at the target until something changes
if (!targetLocation.HasValue)
{
innerActivity = ActivityUtils.SequenceActivities(
new HeliFly(self, Target.FromCell(self.World, destination)),
new Wait(25));
return this;
}
var targetPosition = self.World.Map.CenterOfCell(targetLocation.Value);
var localOffset = carryall.CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation));
var carryablePosition = self.CenterPosition + body.LocalToWorld(localOffset);
if ((carryablePosition - targetPosition).HorizontalLengthSquared != 0)
{
// For non-zero offsets the drop position depends on the carryall facing
// We therefore need to predict/correct for the facing *at the drop point*
if (carryall.CarryableOffset.HorizontalLengthSquared != 0)
{
var facing = (targetPosition - self.CenterPosition).Yaw.Facing;
localOffset = carryall.CarryableOffset.Rotate(body.QuantizeOrientation(self, WRot.FromFacing(facing)));
innerActivity = ActivityUtils.SequenceActivities(
new HeliFly(self, Target.FromPos(targetPosition - body.LocalToWorld(localOffset))),
new Turn(self, facing));
return this;
}
innerActivity = new HeliFly(self, Target.FromPos(targetPosition));
return this;
}
state = State.Land;
return ActivityUtils.SequenceActivities(movement.MoveTo(targetl, 0), this);
return this;
}
case State.Land:
{
if (!CanDropHere())
{
state = State.Transport;
return this;
}
if (HeliFly.AdjustAltitude(self, aircraft, aircraft.Info.LandAltitude))
// Make sure that the carried actor is on the ground before releasing it
var localOffset = carryall.CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation));
var carryablePosition = self.CenterPosition + body.LocalToWorld(localOffset);
if (self.World.Map.DistanceAboveTerrain(carryablePosition) != WDist.Zero)
{
innerActivity = new HeliLand(self, false, -new WDist(carryall.CarryableOffset.Z));
return this;
}
state = carryall.Info.UnloadingDelay > 0 ? State.Wait : State.Release;
return this;
}
case State.Wait:
state = State.Release;
return ActivityUtils.SequenceActivities(new Wait(15), this);
innerActivity = new Wait(carryall.Info.UnloadingDelay, false);
return this;
case State.Release:
if (!CanDropHere())
@@ -107,7 +167,15 @@ namespace OpenRA.Mods.Common.Activities
}
Release();
return NextActivity;
state = State.TakeOff;
return this;
case State.TakeOff:
return ActivityUtils.SequenceActivities(new HeliFly(self, Target.FromPos(self.CenterPosition)), NextActivity);
case State.Aborted:
carryall.UnreserveCarryable(self);
break;
}
return NextActivity;
@@ -115,24 +183,29 @@ namespace OpenRA.Mods.Common.Activities
void Release()
{
positionable.SetPosition(cargo, self.Location, SubCell.FullCell);
cargoFacing.Facing = selfFacing.Facing;
var localOffset = carryall.CarryableOffset.Rotate(body.QuantizeOrientation(self, self.Orientation));
var targetPosition = self.CenterPosition + body.LocalToWorld(localOffset);
var targetLocation = self.World.Map.CellContaining(targetPosition);
positionable.SetPosition(carryall.Carryable, targetLocation, SubCell.FullCell);
carryableFacing.Facing = carryallFacing.Facing;
// Put back into world
self.World.AddFrameEndTask(w =>
{
cargo.World.Add(cargo);
carryall.UnreserveCarryable();
var cargo = carryall.Carryable;
w.Add(cargo);
carryall.DetachCarryable(self);
carryable.UnReserve(cargo);
carryable.Detached(cargo);
});
// Unlock carryable
carryall.CarryableReleased();
carryable.Dropped();
}
public override void Cancel(Actor self)
{
// TODO: Drop the unit at the nearest available cell
if (innerActivity != null)
innerActivity.Cancel(self);
base.Cancel(self);
}
}
}