Prevent movement pausing at invalid position.

This commit is contained in:
Paul Chote
2019-10-18 22:52:31 +01:00
committed by reaperrr
parent ae34410c80
commit 69970d42f3
8 changed files with 84 additions and 49 deletions

View File

@@ -23,12 +23,14 @@ namespace OpenRA.Mods.Common.Activities
readonly int turnSpeed; readonly int turnSpeed;
int remainingTicks; int remainingTicks;
public FlyIdle(Actor self, int ticks = -1) public FlyIdle(Actor self, int ticks = -1, bool tickIdle = true)
{ {
aircraft = self.Trait<Aircraft>(); aircraft = self.Trait<Aircraft>();
tickIdles = self.TraitsImplementing<INotifyIdle>().ToArray();
turnSpeed = aircraft.Info.IdleTurnSpeed > -1 ? aircraft.Info.IdleTurnSpeed : aircraft.TurnSpeed; turnSpeed = aircraft.Info.IdleTurnSpeed > -1 ? aircraft.Info.IdleTurnSpeed : aircraft.TurnSpeed;
remainingTicks = ticks; remainingTicks = ticks;
if (tickIdle)
tickIdles = self.TraitsImplementing<INotifyIdle>().ToArray();
} }
public override bool Tick(Actor self) public override bool Tick(Actor self)
@@ -42,8 +44,9 @@ namespace OpenRA.Mods.Common.Activities
if (remainingTicks > 0) if (remainingTicks > 0)
remainingTicks--; remainingTicks--;
foreach (var tickIdle in tickIdles) if (tickIdles != null)
tickIdle.TickIdle(self); foreach (var tickIdle in tickIdles)
tickIdle.TickIdle(self);
if (!aircraft.Info.CanHover) if (!aircraft.Info.CanHover)
{ {

View File

@@ -210,6 +210,13 @@ namespace OpenRA.Mods.Common.Activities
var nextCell = path[path.Count - 1]; var nextCell = path[path.Count - 1];
// Something else might have moved us, so the path is no longer valid.
if (!Util.AreAdjacentCells(mobile.ToCell, nextCell))
{
path = EvalPath(BlockedByActor.Immovable);
return null;
}
var containsTemporaryBlocker = WorldUtils.ContainsTemporaryBlocker(self.World, nextCell, self); var containsTemporaryBlocker = WorldUtils.ContainsTemporaryBlocker(self.World, nextCell, self);
// Next cell in the move is blocked by another actor // Next cell in the move is blocked by another actor
@@ -407,9 +414,6 @@ namespace OpenRA.Mods.Common.Activities
public override bool Tick(Actor self) public override bool Tick(Actor self)
{ {
if (Move.mobile.IsTraitDisabled || Move.mobile.IsTraitPaused)
return false;
var ret = InnerTick(self, Move.mobile); var ret = InnerTick(self, Move.mobile);
if (moveFraction > MoveFractionTotal) if (moveFraction > MoveFractionTotal)
@@ -491,7 +495,7 @@ namespace OpenRA.Mods.Common.Activities
var nextCell = parent.PopPath(self); var nextCell = parent.PopPath(self);
if (nextCell != null) if (nextCell != null)
{ {
if (IsTurn(mobile, nextCell.Value.First, map)) if (!mobile.IsTraitPaused && !mobile.IsTraitDisabled && IsTurn(mobile, nextCell.Value.First, map))
{ {
var nextSubcellOffset = map.Grid.OffsetOfSubCell(nextCell.Value.Second); var nextSubcellOffset = map.Grid.OffsetOfSubCell(nextCell.Value.Second);
var ret = new MoveFirstHalf( var ret = new MoveFirstHalf(

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
@@ -20,20 +21,18 @@ namespace OpenRA.Mods.Common.Activities
public class PickupUnit : Activity public class PickupUnit : Activity
{ {
readonly Actor cargo; readonly Actor cargo;
readonly IMove movement;
readonly Carryall carryall; readonly Carryall carryall;
readonly IFacing carryallFacing;
readonly Carryable carryable; readonly Carryable carryable;
readonly IFacing carryableFacing; readonly IFacing carryableFacing;
readonly BodyOrientation carryableBody; readonly BodyOrientation carryableBody;
readonly int delay; readonly int delay;
enum PickupState { Intercept, LockCarryable, Pickup } // TODO: Expose this to yaml
readonly WDist targetLockRange = WDist.FromCells(4);
PickupState state; enum PickupState { Intercept, LockCarryable, Pickup }
PickupState state = PickupState.Intercept;
public PickupUnit(Actor self, Actor cargo, int delay) public PickupUnit(Actor self, Actor cargo, int delay)
{ {
@@ -43,16 +42,20 @@ namespace OpenRA.Mods.Common.Activities
carryableFacing = cargo.Trait<IFacing>(); carryableFacing = cargo.Trait<IFacing>();
carryableBody = cargo.Trait<BodyOrientation>(); carryableBody = cargo.Trait<BodyOrientation>();
movement = self.Trait<IMove>();
carryall = self.Trait<Carryall>(); carryall = self.Trait<Carryall>();
carryallFacing = self.Trait<IFacing>();
state = PickupState.Intercept; ChildHasPriority = false;
} }
protected override void OnFirstRun(Actor self) protected override void OnFirstRun(Actor self)
{ {
carryall.ReserveCarryable(self, cargo); if (carryall.ReserveCarryable(self, cargo))
{
// Fly to the target and wait for it to be locked for pickup
// These activities will be cancelled and replaced by Land once the target has been locked
QueueChild(new Fly(self, Target.FromActor(cargo)));
QueueChild(new FlyIdle(self, tickIdle: false));
}
} }
public override bool Tick(Actor self) public override bool Tick(Actor self)
@@ -74,26 +77,21 @@ namespace OpenRA.Mods.Common.Activities
return true; return true;
} }
if (carryall.State != Carryall.CarryallState.Reserved) // Wait until we are near the target before we try to lock it
return true; var distSq = (cargo.CenterPosition - self.CenterPosition).HorizontalLengthSquared;
if (state == PickupState.Intercept && distSq <= targetLockRange.LengthSquared)
state = PickupState.LockCarryable;
switch (state) if (state == PickupState.LockCarryable)
{ {
case PickupState.Intercept: var lockResponse = carryable.LockForPickup(cargo, self);
QueueChild(movement.MoveWithinRange(Target.FromActor(cargo), WDist.FromCells(4))); if (lockResponse == LockResponse.Failed)
state = PickupState.LockCarryable; Cancel(self);
return false; else if (lockResponse == LockResponse.Success)
case PickupState.LockCarryable:
if (!carryable.LockForPickup(cargo, self))
Cancel(self);
state = PickupState.Pickup;
return false;
case PickupState.Pickup:
{ {
// Land at the target location // Pickup position and facing are now known - swap the fly/wait activity with Land
ChildActivity.Cancel(self);
var localOffset = carryall.OffsetForCarryable(self, cargo).Rotate(carryableBody.QuantizeOrientation(self, cargo.Orientation)); var localOffset = carryall.OffsetForCarryable(self, cargo).Rotate(carryableBody.QuantizeOrientation(self, cargo.Orientation));
QueueChild(new Land(self, Target.FromActor(cargo), -carryableBody.LocalToWorld(localOffset), carryableFacing.Facing)); QueueChild(new Land(self, Target.FromActor(cargo), -carryableBody.LocalToWorld(localOffset), carryableFacing.Facing));
@@ -104,11 +102,13 @@ namespace OpenRA.Mods.Common.Activities
// Remove our carryable from world // Remove our carryable from world
QueueChild(new AttachUnit(self, cargo)); QueueChild(new AttachUnit(self, cargo));
QueueChild(new TakeOff(self)); QueueChild(new TakeOff(self));
return false;
state = PickupState.Pickup;
} }
} }
return true; // Return once we are in the pickup state and the pickup activities have completed
return TickChild(self) && state == PickupState.Pickup;
} }
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self) public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)

View File

@@ -102,10 +102,10 @@ namespace OpenRA.Mods.Common.Traits
} }
// Prepare for transport pickup // Prepare for transport pickup
public override bool LockForPickup(Actor self, Actor carrier) public override LockResponse LockForPickup(Actor self, Actor carrier)
{ {
if (state == State.Locked || !WantsTransport) if ((state == State.Locked && Carrier != carrier) || !WantsTransport)
return false; return LockResponse.Failed;
// Last chance to change our mind... // Last chance to change our mind...
var delta = self.World.Map.CenterOfCell(Destination.Value) - self.CenterPosition; var delta = self.World.Map.CenterOfCell(Destination.Value) - self.CenterPosition;
@@ -113,7 +113,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
// Cancel pickup // Cancel pickup
MovementCancelled(self); MovementCancelled(self);
return false; return LockResponse.Failed;
} }
return base.LockForPickup(self, carrier); return base.LockForPickup(self, carrier);

View File

@@ -221,6 +221,11 @@ namespace OpenRA.Mods.Common.Traits
if (captures == null) if (captures == null)
return false; return false;
// HACK: Make sure the target is not moving and at its normal position with respect to the cell grid
var enterMobile = target.TraitOrDefault<Mobile>();
if (enterMobile != null && enterMobile.IsMovingBetweenCells)
return false;
if (progressWatchers.Any() || targetManager.progressWatchers.Any()) if (progressWatchers.Any() || targetManager.progressWatchers.Any())
{ {
currentTargetTotal = captures.Info.CaptureDelay; currentTargetTotal = captures.Info.CaptureDelay;

View File

@@ -35,6 +35,8 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new Carryable(init.Self, this); } public override object Create(ActorInitializer init) { return new Carryable(init.Self, this); }
} }
public enum LockResponse { Success, Pending, Failed }
public class Carryable : ConditionalTrait<CarryableInfo> public class Carryable : ConditionalTrait<CarryableInfo>
{ {
ConditionManager conditionManager; ConditionManager conditionManager;
@@ -42,6 +44,8 @@ namespace OpenRA.Mods.Common.Traits
int carriedToken = ConditionManager.InvalidConditionToken; int carriedToken = ConditionManager.InvalidConditionToken;
int lockedToken = ConditionManager.InvalidConditionToken; int lockedToken = ConditionManager.InvalidConditionToken;
Mobile mobile;
public Actor Carrier { get; private set; } public Actor Carrier { get; private set; }
public bool Reserved { get { return state != State.Free; } } public bool Reserved { get { return state != State.Free; } }
public CPos? Destination { get; protected set; } public CPos? Destination { get; protected set; }
@@ -57,6 +61,7 @@ namespace OpenRA.Mods.Common.Traits
protected override void Created(Actor self) protected override void Created(Actor self)
{ {
conditionManager = self.Trait<ConditionManager>(); conditionManager = self.Trait<ConditionManager>();
mobile = self.TraitOrDefault<Mobile>();
base.Created(self); base.Created(self);
} }
@@ -111,18 +116,25 @@ namespace OpenRA.Mods.Common.Traits
} }
// Prepare for transport pickup // Prepare for transport pickup
public virtual bool LockForPickup(Actor self, Actor carrier) public virtual LockResponse LockForPickup(Actor self, Actor carrier)
{ {
if (state == State.Locked) if (state == State.Locked && Carrier != carrier)
return false; return LockResponse.Failed;
state = State.Locked; if (state != State.Locked)
Carrier = carrier; {
state = State.Locked;
Carrier = carrier;
if (lockedToken == ConditionManager.InvalidConditionToken && !string.IsNullOrEmpty(Info.LockedCondition)) if (lockedToken == ConditionManager.InvalidConditionToken && !string.IsNullOrEmpty(Info.LockedCondition))
lockedToken = conditionManager.GrantCondition(self, Info.LockedCondition); lockedToken = conditionManager.GrantCondition(self, Info.LockedCondition);
}
return true; // Make sure we are not moving and at our normal position with respect to the cell grid
if (mobile != null && mobile.IsMovingBetweenCells)
return LockResponse.Pending;
return LockResponse.Success;
} }
} }
} }

View File

@@ -184,6 +184,11 @@ namespace OpenRA.Mods.Common.Traits
public bool TurnToMove; public bool TurnToMove;
public bool IsBlocking { get; private set; } public bool IsBlocking { get; private set; }
public bool IsMovingBetweenCells
{
get { return FromCell != ToCell; }
}
#region IFacing #region IFacing
[Sync] [Sync]
public int Facing public int Facing

View File

@@ -141,6 +141,12 @@ namespace OpenRA.Mods.Common
} }
} }
public static bool AreAdjacentCells(CPos a, CPos b)
{
var offset = b - a;
return Math.Abs(offset.X) < 2 && Math.Abs(offset.Y) < 2;
}
public static IEnumerable<CPos> ExpandFootprint(IEnumerable<CPos> cells, bool allowDiagonal) public static IEnumerable<CPos> ExpandFootprint(IEnumerable<CPos> cells, bool allowDiagonal)
{ {
return cells.SelectMany(c => Neighbours(c, allowDiagonal)).Distinct(); return cells.SelectMany(c => Neighbours(c, allowDiagonal)).Distinct();