Moves Attack, Armament, Move, Air traits and activities as well as anything required by them to Mods.Common.

Extracts Exit from Production into its own trait.
This commit is contained in:
reaperrr
2015-01-01 18:08:08 +01:00
parent 158517c09f
commit 654f56c5d5
113 changed files with 255 additions and 244 deletions

View File

@@ -0,0 +1,64 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class AttackMoveActivity : Activity
{
const int ScanInterval = 7;
Activity inner;
int scanTicks;
AutoTarget autoTarget;
public AttackMoveActivity(Actor self, Activity inner)
{
this.inner = inner;
autoTarget = self.TraitOrDefault<AutoTarget>();
}
public override Activity Tick(Actor self)
{
if (autoTarget != null && --scanTicks <= 0)
{
autoTarget.ScanAndAttack(self);
scanTicks = ScanInterval;
}
if (inner == null)
return NextActivity;
inner = Util.RunActivity(self, inner);
return this;
}
public override void Cancel(Actor self)
{
if (inner != null)
inner.Cancel(self);
base.Cancel(self);
}
public override IEnumerable<Target> GetTargets(Actor self)
{
if (inner != null)
return inner.GetTargets(self);
return Target.None;
}
}
}

View File

@@ -0,0 +1,50 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class Follow : Activity
{
readonly Target target;
readonly WRange minRange;
readonly WRange maxRange;
readonly IMove move;
public Follow(Actor self, Target target, WRange minRange, WRange maxRange)
{
this.target = target;
this.minRange = minRange;
this.maxRange = maxRange;
move = self.Trait<IMove>();
}
public override Activity Tick(Actor self)
{
if (IsCanceled || !target.IsValidFor(self))
return NextActivity;
var cachedPosition = target.CenterPosition;
var path = move.MoveWithinRange(target, minRange, maxRange);
// We are already in range, so wait until the target moves before doing anything
if (target.IsInRange(self.CenterPosition, maxRange) && !target.IsInRange(self.CenterPosition, minRange))
{
var wait = new WaitFor(() => !target.IsValidFor(self) || target.CenterPosition != cachedPosition);
return Util.SequenceActivities(wait, path, this);
}
return Util.SequenceActivities(path, this);
}
}
}

View File

@@ -0,0 +1,451 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class Move : Activity
{
static readonly List<CPos> NoPath = new List<CPos>();
readonly Mobile mobile;
readonly IEnumerable<IDisableMove> moveDisablers;
readonly WRange nearEnough;
readonly Func<List<CPos>> getPath;
readonly Actor ignoredActor;
List<CPos> path;
CPos? destination;
// For dealing with blockers
bool hasWaited;
bool hasNotifiedBlocker;
int waitTicksRemaining;
// Scriptable move order
// Ignores lane bias and nearby units
public Move(Actor self, CPos destination)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
getPath = () =>
self.World.WorldActor.Trait<PathFinder>().FindPath(
PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, destination, false)
.WithoutLaneBias());
this.destination = destination;
this.nearEnough = WRange.Zero;
}
// HACK: for legacy code
public Move(Actor self, CPos destination, int nearEnough)
: this(self, destination, WRange.FromCells(nearEnough)) { }
public Move(Actor self, CPos destination, WRange nearEnough)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
getPath = () => self.World.WorldActor.Trait<PathFinder>()
.FindUnitPath(mobile.toCell, destination, self);
this.destination = destination;
this.nearEnough = nearEnough;
}
public Move(Actor self, CPos destination, SubCell subCell, WRange nearEnough)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
getPath = () => self.World.WorldActor.Trait<PathFinder>()
.FindUnitPathToRange(mobile.fromCell, subCell, self.World.Map.CenterOfSubCell(destination, subCell), nearEnough, self);
this.destination = destination;
this.nearEnough = nearEnough;
}
public Move(Actor self, CPos destination, Actor ignoredActor)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
getPath = () =>
self.World.WorldActor.Trait<PathFinder>().FindPath(
PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, destination, false)
.WithIgnoredActor(ignoredActor));
this.destination = destination;
this.nearEnough = WRange.Zero;
this.ignoredActor = ignoredActor;
}
public Move(Actor self, Target target, WRange range)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
getPath = () =>
{
if (!target.IsValidFor(self))
return NoPath;
return self.World.WorldActor.Trait<PathFinder>().FindUnitPathToRange(
mobile.toCell, mobile.toSubCell, target.CenterPosition, range, self);
};
destination = null;
nearEnough = range;
}
public Move(Actor self, Func<List<CPos>> getPath)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
this.getPath = getPath;
destination = null;
nearEnough = WRange.Zero;
}
static int HashList<T>(List<T> xs)
{
var hash = 0;
var n = 0;
foreach (var x in xs)
hash += n++ * x.GetHashCode();
return hash;
}
List<CPos> EvalPath(Actor self, Mobile mobile)
{
var path = getPath().TakeWhile(a => a != mobile.toCell).ToList();
mobile.PathHash = HashList(path);
return path;
}
public override Activity Tick(Actor self)
{
if (moveDisablers.Any(d => d.MoveDisabled(self)))
return this;
if (destination == mobile.toCell)
return NextActivity;
if (path == null)
{
if (mobile.ticksBeforePathing > 0)
{
--mobile.ticksBeforePathing;
return this;
}
path = EvalPath(self, mobile);
SanityCheckPath(mobile);
}
if (path.Count == 0)
{
destination = mobile.toCell;
return this;
}
destination = path[0];
var nextCell = PopPath(self, mobile);
if (nextCell == null)
return this;
var firstFacing = self.World.Map.FacingBetween(mobile.fromCell, nextCell.Value.First, mobile.Facing);
if (firstFacing != mobile.Facing)
{
path.Add(nextCell.Value.First);
return Util.SequenceActivities(new Turn(self, firstFacing), this);
}
else
{
mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second);
var move = new MoveFirstHalf(
this,
self.World.Map.CenterOfSubCell(mobile.fromCell, mobile.fromSubCell),
Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (self.World.Map.OffsetOfSubCell(mobile.fromSubCell) + self.World.Map.OffsetOfSubCell(mobile.toSubCell)) / 2,
mobile.Facing,
mobile.Facing,
0);
return move;
}
}
[Conditional("SANITY_CHECKS")]
void SanityCheckPath(Mobile mobile)
{
if (path.Count == 0)
return;
var d = path[path.Count - 1] - mobile.toCell;
if (d.LengthSquared > 2)
throw new InvalidOperationException("(Move) Sanity check failed");
}
static void NotifyBlocker(Actor self, CPos nextCell)
{
foreach (var blocker in self.World.ActorMap.GetUnitsAt(nextCell))
{
// Notify the blocker that he's blocking our move:
foreach (var moveBlocked in blocker.TraitsImplementing<INotifyBlockingMove>())
moveBlocked.OnNotifyBlockingMove(blocker, self);
}
}
Pair<CPos, SubCell>? PopPath(Actor self, Mobile mobile)
{
if (path.Count == 0)
return null;
var nextCell = path[path.Count - 1];
// Next cell in the move is blocked by another actor
if (!mobile.CanEnterCell(nextCell, ignoredActor, true))
{
// Are we close enough?
var cellRange = nearEnough.Range / 1024;
if ((mobile.toCell - destination.Value).LengthSquared <= cellRange * cellRange)
{
path.Clear();
return null;
}
// See if they will move
if (!hasNotifiedBlocker)
{
NotifyBlocker(self, nextCell);
hasNotifiedBlocker = true;
}
// Wait a bit to see if they leave
if (!hasWaited)
{
var info = self.Info.Traits.Get<MobileInfo>();
waitTicksRemaining = info.WaitAverage + self.World.SharedRandom.Next(-info.WaitSpread, info.WaitSpread);
hasWaited = true;
}
if (--waitTicksRemaining >= 0)
return null;
if (mobile.ticksBeforePathing > 0)
{
--mobile.ticksBeforePathing;
return null;
}
// Calculate a new path
mobile.RemoveInfluence();
var newPath = EvalPath(self, mobile);
mobile.AddInfluence();
if (newPath.Count != 0)
path = newPath;
return null;
}
hasNotifiedBlocker = false;
hasWaited = false;
path.RemoveAt(path.Count - 1);
var subCell = mobile.GetAvailableSubCell(nextCell, SubCell.Any, ignoredActor);
return Pair.New(nextCell, subCell);
}
public override void Cancel(Actor self)
{
path = NoPath;
base.Cancel(self);
}
public override IEnumerable<Target> GetTargets(Actor self)
{
if (path != null)
return Enumerable.Reverse(path).Select(c => Target.FromCell(self.World, c));
if (destination != null)
return new Target[] { Target.FromCell(self.World, destination.Value) };
return Target.None;
}
abstract class MovePart : Activity
{
protected readonly Move move;
protected readonly WPos from, to;
protected readonly int fromFacing, toFacing;
protected readonly int moveFractionTotal;
protected int moveFraction;
public MovePart(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction)
{
this.move = move;
this.from = from;
this.to = to;
this.fromFacing = fromFacing;
this.toFacing = toFacing;
this.moveFraction = startingFraction;
this.moveFractionTotal = (to - from).Length;
}
public override void Cancel(Actor self)
{
move.Cancel(self);
base.Cancel(self);
}
public override void Queue(Activity activity)
{
move.Queue(activity);
}
public override Activity Tick(Actor self)
{
var mobile = self.Trait<Mobile>();
var ret = InnerTick(self, move.mobile);
mobile.IsMoving = ret is MovePart;
if (moveFraction > moveFractionTotal)
moveFraction = moveFractionTotal;
UpdateCenterLocation(self, mobile);
return ret;
}
Activity InnerTick(Actor self, Mobile mobile)
{
moveFraction += mobile.MovementSpeedForCell(self, mobile.toCell);
if (moveFraction <= moveFractionTotal)
return this;
var next = OnComplete(self, mobile, move);
if (next != null)
return next;
return move;
}
void UpdateCenterLocation(Actor self, Mobile mobile)
{
// avoid division through zero
if (moveFractionTotal != 0)
mobile.SetVisualPosition(self, WPos.Lerp(from, to, moveFraction, moveFractionTotal));
else
mobile.SetVisualPosition(self, to);
if (moveFraction >= moveFractionTotal)
mobile.Facing = toFacing & 0xFF;
else
mobile.Facing = int2.Lerp(fromFacing, toFacing, moveFraction, moveFractionTotal) & 0xFF;
}
protected abstract MovePart OnComplete(Actor self, Mobile mobile, Move parent);
public override IEnumerable<Target> GetTargets(Actor self)
{
return move.GetTargets(self);
}
}
class MoveFirstHalf : MovePart
{
public MoveFirstHalf(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction)
: base(move, from, to, fromFacing, toFacing, startingFraction) { }
static bool IsTurn(Mobile mobile, CPos nextCell)
{
return nextCell - mobile.toCell !=
mobile.toCell - mobile.fromCell;
}
protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent)
{
var fromSubcellOffset = self.World.Map.OffsetOfSubCell(mobile.fromSubCell);
var toSubcellOffset = self.World.Map.OffsetOfSubCell(mobile.toSubCell);
var nextCell = parent.PopPath(self, mobile);
if (nextCell != null)
{
if (IsTurn(mobile, nextCell.Value.First))
{
var nextSubcellOffset = self.World.Map.OffsetOfSubCell(nextCell.Value.Second);
var ret = new MoveFirstHalf(
move,
Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2,
Util.BetweenCells(self.World, mobile.toCell, nextCell.Value.First) + (toSubcellOffset + nextSubcellOffset) / 2,
mobile.Facing,
Util.GetNearestFacing(mobile.Facing, self.World.Map.FacingBetween(mobile.toCell, nextCell.Value.First, mobile.Facing)),
moveFraction - moveFractionTotal);
mobile.FinishedMoving(self);
mobile.SetLocation(mobile.toCell, mobile.toSubCell, nextCell.Value.First, nextCell.Value.Second);
return ret;
}
parent.path.Add(nextCell.Value.First);
}
var ret2 = new MoveSecondHalf(
move,
Util.BetweenCells(self.World, mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2,
self.World.Map.CenterOfCell(mobile.toCell) + toSubcellOffset,
mobile.Facing,
mobile.Facing,
moveFraction - moveFractionTotal);
mobile.EnteringCell(self);
mobile.SetLocation(mobile.toCell, mobile.toSubCell, mobile.toCell, mobile.toSubCell);
return ret2;
}
}
class MoveSecondHalf : MovePart
{
public MoveSecondHalf(Move move, WPos from, WPos to, int fromFacing, int toFacing, int startingFraction)
: base(move, from, to, fromFacing, toFacing, startingFraction) { }
protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent)
{
mobile.SetPosition(self, mobile.toCell);
return null;
}
}
}
public static class ActorExtensionsForMove
{
public static bool IsMoving(this Actor self)
{
var a = self.GetCurrentActivity();
if (a == null)
return false;
// HACK: Dirty, but it suffices until we do something better:
if (a.GetType() == typeof(Move)) return true;
if (a.GetType() == typeof(MoveAdjacentTo)) return true;
if (a.GetType() == typeof(AttackMoveActivity)) return true;
// Not a move:
return false;
}
}
}

View File

@@ -0,0 +1,144 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class MoveAdjacentTo : Activity
{
static readonly List<CPos> NoPath = new List<CPos>();
readonly Mobile mobile;
readonly PathFinder pathFinder;
readonly DomainIndex domainIndex;
readonly uint movementClass;
protected Target target { get; private set; }
protected CPos targetPosition;
Activity inner;
bool repath;
public MoveAdjacentTo(Actor self, Target target)
{
this.target = target;
mobile = self.Trait<Mobile>();
pathFinder = self.World.WorldActor.Trait<PathFinder>();
domainIndex = self.World.WorldActor.Trait<DomainIndex>();
movementClass = (uint)mobile.Info.GetMovementClass(self.World.TileSet);
if (target.IsValidFor(self))
targetPosition = self.World.Map.CellContaining(target.CenterPosition);
repath = true;
}
protected virtual bool ShouldStop(Actor self, CPos oldTargetPosition)
{
return false;
}
protected virtual bool ShouldRepath(Actor self, CPos oldTargetPosition)
{
return targetPosition != oldTargetPosition;
}
protected virtual IEnumerable<CPos> CandidateMovementCells(Actor self)
{
return Util.AdjacentCells(self.World, target);
}
public override Activity Tick(Actor self)
{
var targetIsValid = target.IsValidFor(self);
// Inner move order has completed.
if (inner == null)
{
// We are done here if the order was cancelled for any
// reason except the target moving.
if (IsCanceled || !repath || !targetIsValid)
return NextActivity;
// Target has moved, and MoveAdjacentTo is still valid.
inner = mobile.MoveTo(() => CalculatePathToTarget(self));
repath = false;
}
if (targetIsValid)
{
// Check if the target has moved
var oldTargetPosition = targetPosition;
targetPosition = self.World.Map.CellContaining(target.CenterPosition);
var shouldStop = ShouldStop(self, oldTargetPosition);
if (shouldStop || (!repath && ShouldRepath(self, oldTargetPosition)))
{
// Finish moving into the next cell and then repath.
if (inner != null)
inner.Cancel(self);
repath = !shouldStop;
}
}
else
{
// Target became invalid. Move to its last known position.
target = Target.FromCell(self.World, targetPosition);
}
// Ticks the inner move activity to actually move the actor.
inner = Util.RunActivity(self, inner);
return this;
}
List<CPos> CalculatePathToTarget(Actor self)
{
var targetCells = CandidateMovementCells(self);
var searchCells = new List<CPos>();
var loc = self.Location;
foreach (var cell in targetCells)
if (domainIndex.IsPassable(loc, cell, movementClass) && mobile.CanEnterCell(cell))
searchCells.Add(cell);
if (!searchCells.Any())
return NoPath;
var fromSrc = PathSearch.FromPoints(self.World, mobile.Info, self, searchCells, loc, true);
var fromDest = PathSearch.FromPoint(self.World, mobile.Info, self, loc, targetPosition, true).Reverse();
return pathFinder.FindBidiPath(fromSrc, fromDest);
}
public override IEnumerable<Target> GetTargets(Actor self)
{
if (inner != null)
return inner.GetTargets(self);
return Target.None;
}
public override void Cancel(Actor self)
{
if (inner != null)
inner.Cancel(self);
base.Cancel(self);
}
}
}

View File

@@ -0,0 +1,62 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class MoveWithinRange : MoveAdjacentTo
{
readonly WRange maxRange;
readonly WRange minRange;
public MoveWithinRange(Actor self, Target target, WRange minRange, WRange maxRange)
: base(self, target)
{
this.minRange = minRange;
this.maxRange = maxRange;
}
protected override bool ShouldStop(Actor self, CPos oldTargetPosition)
{
// We are now in range. Don't move any further!
// HACK: This works around the pathfinder not returning the shortest path
var cp = self.CenterPosition;
return target.IsInRange(cp, maxRange) && !target.IsInRange(cp, minRange);
}
protected override bool ShouldRepath(Actor self, CPos oldTargetPosition)
{
var cp = self.CenterPosition;
return targetPosition != oldTargetPosition && (!target.IsInRange(cp, maxRange) || target.IsInRange(cp, minRange));
}
protected override IEnumerable<CPos> CandidateMovementCells(Actor self)
{
var map = self.World.Map;
var maxCells = (maxRange.Range + 1023) / 1024;
var minCells = minRange.Range / 1024;
var outerSq = maxRange.Range * maxRange.Range;
var innerSq = minRange.Range * minRange.Range;
var center = target.CenterPosition;
return map.FindTilesInAnnulus(targetPosition, minCells + 1, maxCells).Where(c =>
{
var dxSq = (map.CenterOfCell(c) - center).HorizontalLengthSquared;
return dxSq >= innerSq && dxSq <= outerSq;
});
}
}
}