Move Air activities and traits to Activities\Air and Traits\Air, respectively

This commit is contained in:
reaperrr
2014-11-16 17:08:04 +01:00
parent 797ac17e61
commit 127969d014
44 changed files with 175 additions and 102 deletions

View File

@@ -0,0 +1,63 @@
#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.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FallToEarth : Activity
{
int acceleration = 0;
int spin = 0;
FallsToEarthInfo info;
public FallToEarth(Actor self, FallsToEarthInfo info)
{
this.info = info;
if (info.Spins)
acceleration = self.World.SharedRandom.Next(2) * 2 - 1;
}
public override Activity Tick(Actor self)
{
var aircraft = self.Trait<Aircraft>();
if (self.CenterPosition.Z <= 0)
{
if (info.Explosion != null)
{
var weapon = self.World.Map.Rules.Weapons[info.Explosion.ToLowerInvariant()];
// Use .FromPos since this actor is killed. Cannot use Target.FromActor
weapon.Impact(Target.FromPos(self.CenterPosition), self, Enumerable.Empty<int>());
}
self.Destroy();
return null;
}
if (info.Spins)
{
spin += acceleration;
aircraft.Facing = (aircraft.Facing + spin) % 256;
}
var move = info.Moves ? aircraft.FlyStep(aircraft.Facing) : WVec.Zero;
move -= new WVec(WRange.Zero, WRange.Zero, info.Velocity);
aircraft.SetPosition(self, aircraft.CenterPosition + move);
return this;
}
// Cannot be cancelled
public override void Cancel(Actor self) { }
}
}

View File

@@ -0,0 +1,86 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Fly : Activity
{
readonly Plane plane;
readonly Target target;
readonly WRange maxRange;
readonly WRange minRange;
public Fly(Actor self, Target t)
{
plane = self.Trait<Plane>();
target = t;
}
public Fly(Actor self, Target t, WRange minRange, WRange maxRange)
: this(self, t)
{
this.maxRange = maxRange;
this.minRange = minRange;
}
public static void FlyToward(Actor self, Plane plane, int desiredFacing, WRange desiredAltitude)
{
var move = plane.FlyStep(plane.Facing);
var altitude = plane.CenterPosition.Z;
plane.Facing = Util.TickFacing(plane.Facing, desiredFacing, plane.ROT);
if (altitude != desiredAltitude.Range)
{
var delta = move.HorizontalLength * plane.Info.MaximumPitch.Tan() / 1024;
var dz = (desiredAltitude.Range - altitude).Clamp(-delta, delta);
move += new WVec(0, 0, dz);
}
plane.SetPosition(self, plane.CenterPosition + move);
}
public override Activity Tick(Actor self)
{
if (IsCanceled || !target.IsValidFor(self))
return NextActivity;
// Inside the target annulus, so we're done
var insideMaxRange = maxRange.Range > 0 && target.IsInRange(plane.CenterPosition, maxRange);
var insideMinRange = minRange.Range > 0 && target.IsInRange(plane.CenterPosition, minRange);
if (insideMaxRange && !insideMinRange)
return NextActivity;
// Close enough (ported from old code which checked length against sqrt(50) px)
var d = target.CenterPosition - self.CenterPosition;
if (d.HorizontalLengthSquared < 91022)
return NextActivity;
var desiredFacing = Util.GetFacing(d, plane.Facing);
// Don't turn until we've reached the cruise altitude
if (plane.CenterPosition.Z < plane.Info.CruiseAltitude.Range)
desiredFacing = plane.Facing;
FlyToward(self, plane, desiredFacing, plane.Info.CruiseAltitude);
return this;
}
public override IEnumerable<Target> GetTargets(Actor self)
{
yield return target;
}
}
}

View File

@@ -0,0 +1,58 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyAttack : Activity
{
readonly Target target;
Activity inner;
public FlyAttack(Target target) { this.target = target; }
public override Activity Tick(Actor self)
{
if (!target.IsValidFor(self))
return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
return NextActivity;
var attack = self.TraitOrDefault<AttackPlane>();
if (attack != null)
attack.DoAttack(self, target);
if (inner == null)
{
if (IsCanceled)
return NextActivity;
inner = Util.SequenceActivities(new Fly(self, target), new FlyTimed(50));
}
inner = Util.RunActivity(self, inner);
return this;
}
public override void Cancel(Actor self)
{
if (!IsCanceled && inner != null)
inner.Cancel(self);
// NextActivity must always be set to null:
base.Cancel(self);
}
}
}

View File

@@ -0,0 +1,32 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyCircle : Activity
{
public override Activity Tick(Actor self)
{
if (IsCanceled)
return NextActivity;
var plane = self.Trait<Plane>();
// We can't possibly turn this fast
var desiredFacing = plane.Facing + 64;
Fly.FlyToward(self, plane, desiredFacing, plane.Info.CruiseAltitude);
return this;
}
}
}

View File

@@ -8,7 +8,7 @@
*/
#endregion
using OpenRA.Mods.RA.Air;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities

View File

@@ -0,0 +1,51 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyTimed : Activity
{
int remainingTicks;
public FlyTimed(int ticks) { remainingTicks = ticks; }
public override Activity Tick(Actor self)
{
if (IsCanceled || remainingTicks-- == 0)
return NextActivity;
var plane = self.Trait<Plane>();
Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
return this;
}
}
public class FlyOffMap : Activity
{
public override Activity Tick(Actor self)
{
if (IsCanceled || !self.World.Map.Contains(self.Location))
return NextActivity;
var plane = self.Trait<Plane>();
Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
return this;
}
public override void Cancel(Actor self)
{
base.Cancel(self);
}
}
}

View File

@@ -0,0 +1,51 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class HeliAttack : Activity
{
Target target;
public HeliAttack(Target target) { this.target = target; }
public override Activity Tick(Actor self)
{
if (IsCanceled || !target.IsValidFor(self))
return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
var reloads = self.TraitOrDefault<Reloads>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo() && reloads == null)
return Util.SequenceActivities(new HeliReturn(), NextActivity);
var helicopter = self.Trait<Helicopter>();
var attack = self.Trait<AttackHeli>();
var dist = target.CenterPosition - self.CenterPosition;
// Can rotate facing while ascending
var desiredFacing = Util.GetFacing(dist, helicopter.Facing);
helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT);
if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude))
return this;
// Fly towards the target
if (!target.IsInRange(self.CenterPosition, attack.GetMaximumRange()))
helicopter.SetPosition(self, helicopter.CenterPosition + helicopter.FlyStep(desiredFacing));
attack.DoAttack(self, target);
return this;
}
}
}

View File

@@ -0,0 +1,94 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliFly : Activity
{
readonly Helicopter helicopter;
readonly Target target;
readonly WRange maxRange;
readonly WRange minRange;
public HeliFly(Actor self, Target t)
{
helicopter = self.Trait<Helicopter>();
target = t;
}
public HeliFly(Actor self, Target t, WRange minRange, WRange maxRange)
: this(self, t)
{
this.maxRange = maxRange;
this.minRange = minRange;
}
public static bool AdjustAltitude(Actor self, Helicopter helicopter, WRange targetAltitude)
{
var altitude = helicopter.CenterPosition.Z;
if (altitude == targetAltitude.Range)
return false;
var delta = helicopter.Info.AltitudeVelocity.Range;
var dz = (targetAltitude.Range - altitude).Clamp(-delta, delta);
helicopter.SetPosition(self, helicopter.CenterPosition + new WVec(0, 0, dz));
return true;
}
public override Activity Tick(Actor self)
{
if (IsCanceled || !target.IsValidFor(self))
return NextActivity;
if (AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude))
return this;
var pos = target.CenterPosition;
// Rotate towards the target
var dist = pos - self.CenterPosition;
var desiredFacing = Util.GetFacing(dist, helicopter.Facing);
helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.ROT);
var move = helicopter.FlyStep(desiredFacing);
// Inside the minimum range, so reverse
if (minRange.Range > 0 && target.IsInRange(helicopter.CenterPosition, minRange))
{
helicopter.SetPosition(self, helicopter.CenterPosition - move);
return this;
}
// Inside the maximum range, so we're done
if (maxRange.Range > 0 && target.IsInRange(helicopter.CenterPosition, maxRange))
return NextActivity;
// The next move would overshoot, so just set the final position
if (dist.HorizontalLengthSquared < move.HorizontalLengthSquared)
{
helicopter.SetPosition(self, pos + new WVec(0, 0, helicopter.Info.CruiseAltitude.Range - pos.Z));
return NextActivity;
}
helicopter.SetPosition(self, helicopter.CenterPosition + move);
return this;
}
public override IEnumerable<Target> GetTargets(Actor self)
{
yield return target;
}
}
}

View File

@@ -0,0 +1,41 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliLand : Activity
{
bool requireSpace;
public HeliLand(bool requireSpace)
{
this.requireSpace = requireSpace;
}
public override Activity Tick(Actor self)
{
if (IsCanceled)
return NextActivity;
var helicopter = self.Trait<Helicopter>();
if (requireSpace && !helicopter.CanLand(self.Location))
return this;
if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.LandAltitude))
return this;
return NextActivity;
}
}
}

View File

@@ -0,0 +1,66 @@
#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.Linq;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class HeliReturn : Activity
{
static Actor ChooseHelipad(Actor self)
{
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
return self.World.Actors.Where(a => a.Owner == self.Owner).FirstOrDefault(
a => rearmBuildings.Contains(a.Info.Name) && !Reservable.IsReserved(a));
}
public override Activity Tick(Actor self)
{
if (IsCanceled)
return NextActivity;
var dest = ChooseHelipad(self);
var initialFacing = self.Info.Traits.Get<AircraftInfo>().InitialFacing;
if (dest == null)
{
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
var nearestHpad = self.World.ActorsWithTrait<Reservable>()
.Where(a => a.Actor.Owner == self.Owner && rearmBuildings.Contains(a.Actor.Info.Name))
.Select(a => a.Actor)
.ClosestTo(self);
if (nearestHpad == null)
return Util.SequenceActivities(new Turn(self, initialFacing), new HeliLand(true), NextActivity);
else
return Util.SequenceActivities(new HeliFly(self, Target.FromActor(nearestHpad)));
}
var res = dest.TraitOrDefault<Reservable>();
var heli = self.Trait<Helicopter>();
if (res != null)
{
heli.UnReserve();
heli.Reservation = res.Reserve(dest, self, heli);
}
var exit = dest.Info.Traits.WithInterface<ExitInfo>().FirstOrDefault();
var offset = (exit != null) ? exit.SpawnOffset : WVec.Zero;
return Util.SequenceActivities(
new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
new Turn(self, initialFacing),
new HeliLand(false),
new ResupplyAircraft());
}
}
}

View File

@@ -0,0 +1,47 @@
#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.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Land : Activity
{
Target target;
public Land(Target t) { target = t; }
public override Activity Tick(Actor self)
{
if (!target.IsValidFor(self))
Cancel(self);
if (IsCanceled)
return NextActivity;
var plane = self.Trait<Plane>();
var d = target.CenterPosition - self.CenterPosition;
// The next move would overshoot, so just set the final position
var move = plane.FlyStep(plane.Facing);
if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
{
plane.SetPosition(self, target.CenterPosition);
return NextActivity;
}
var desiredFacing = Util.GetFacing(d, plane.Facing);
Fly.FlyToward(self, plane, desiredFacing, WRange.Zero);
return this;
}
}
}

View File

@@ -0,0 +1,34 @@
#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.Mods.RA.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class ResupplyAircraft : Activity
{
public override Activity Tick(Actor self)
{
var aircraft = self.Trait<Aircraft>();
var host = aircraft.GetActorBelow();
if (host == null)
return NextActivity;
return Util.SequenceActivities(
aircraft.GetResupplyActivities(host).Append(NextActivity).ToArray());
}
}
}

View File

@@ -0,0 +1,126 @@
#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.Linq;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class ReturnToBase : Activity
{
bool isCalculated;
Actor dest;
WPos w1, w2, w3;
public static Actor ChooseAirfield(Actor self, bool unreservedOnly)
{
var rearmBuildings = self.Info.Traits.Get<PlaneInfo>().RearmBuildings;
return self.World.ActorsWithTrait<Reservable>()
.Where(a => a.Actor.Owner == self.Owner)
.Where(a => rearmBuildings.Contains(a.Actor.Info.Name)
&& (!unreservedOnly || !Reservable.IsReserved(a.Actor)))
.Select(a => a.Actor)
.ClosestTo(self);
}
void Calculate(Actor self)
{
if (dest == null || Reservable.IsReserved(dest))
dest = ChooseAirfield(self, true);
if (dest == null)
return;
var plane = self.Trait<Plane>();
var planeInfo = self.Info.Traits.Get<PlaneInfo>();
var res = dest.TraitOrDefault<Reservable>();
if (res != null)
{
plane.UnReserve();
plane.Reservation = res.Reserve(dest, self, plane);
}
var landPos = dest.CenterPosition;
var altitude = planeInfo.CruiseAltitude.Range;
// Distance required for descent.
var landDistance = altitude * 1024 / plane.Info.MaximumPitch.Tan();
// Land towards the east
var approachStart = landPos + new WVec(-landDistance, 0, altitude);
// Add 10% to the turning radius to ensure we have enough room
var speed = plane.MovementSpeed * 32 / 35;
var turnRadius = (int)(141 * speed / planeInfo.ROT / (float)Math.PI);
// Find the center of the turning circles for clockwise and counterclockwise turns
var angle = WAngle.FromFacing(plane.Facing);
var fwd = -new WVec(angle.Sin(), angle.Cos(), 0);
// Work out whether we should turn clockwise or counter-clockwise for approach
var side = new WVec(-fwd.Y, fwd.X, fwd.Z);
var approachDelta = self.CenterPosition - approachStart;
var sideTowardBase = new[] { side, -side }
.MinBy(a => WVec.Dot(a, approachDelta));
// Calculate the tangent line that joins the turning circles at the current and approach positions
var cp = self.CenterPosition + turnRadius * sideTowardBase / 1024;
var posCenter = new WPos(cp.X, cp.Y, altitude);
var approachCenter = approachStart + new WVec(0, turnRadius * Math.Sign(self.CenterPosition.Y - approachStart.Y), 0);
var tangentDirection = approachCenter - posCenter;
var tangentOffset = new WVec(-tangentDirection.Y, tangentDirection.X, 0) * turnRadius / tangentDirection.Length;
// TODO: correctly handle CCW <-> CW turns
if (tangentOffset.X > 0)
tangentOffset = -tangentOffset;
w1 = posCenter + tangentOffset;
w2 = approachCenter + tangentOffset;
w3 = approachStart;
plane.RTBPathHash = w1 + (WVec)w2 + (WVec)w3;
isCalculated = true;
}
public ReturnToBase(Actor self, Actor dest)
{
this.dest = dest;
}
public override Activity Tick(Actor self)
{
if (IsCanceled || self.IsDead())
return NextActivity;
if (!isCalculated)
Calculate(self);
if (dest == null)
{
var nearestAfld = ChooseAirfield(self, false);
self.CancelActivity();
if (nearestAfld != null)
return Util.SequenceActivities(new Fly(self, Target.FromActor(nearestAfld)), new FlyCircle());
else
return new FlyCircle();
}
return Util.SequenceActivities(
new Fly(self, Target.FromPos(w1)),
new Fly(self, Target.FromPos(w2)),
new Fly(self, Target.FromPos(w3)),
new Land(Target.FromActor(dest)),
NextActivity);
}
}
}

View File

@@ -0,0 +1,42 @@
#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.Mods.Common;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class TakeOff : Activity
{
public override Activity Tick(Actor self)
{
var aircraft = self.Trait<Aircraft>();
self.CancelActivity();
var reservation = aircraft.Reservation;
if (reservation != null)
{
reservation.Dispose();
reservation = null;
}
var host = aircraft.GetActorBelow();
var hasHost = host != null;
var rp = hasHost ? host.TraitOrDefault<RallyPoint>() : null;
var destination = rp != null ? rp.Location :
(hasHost ? self.World.Map.CellContaining(host.CenterPosition) : self.Location);
return new AttackMove.AttackMoveActivity(self, self.Trait<IMove>().MoveTo(destination, 1));
}
}
}