Cache trait look-ups in the constructor where possible for Air activities/traits
This commit is contained in:
@@ -18,20 +18,21 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class FallToEarth : Activity
|
||||
{
|
||||
readonly Aircraft aircraft;
|
||||
readonly FallsToEarthInfo info;
|
||||
int acceleration = 0;
|
||||
int spin = 0;
|
||||
FallsToEarthInfo info;
|
||||
|
||||
public FallToEarth(Actor self, FallsToEarthInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
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)
|
||||
|
||||
@@ -52,9 +52,9 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
// TODO: This should fire each weapon at its maximum range
|
||||
if (target.IsInRange(self.CenterPosition, attackPlane.Armaments.Select(a => a.Weapon.MinRange).Min()))
|
||||
inner = Util.SequenceActivities(new FlyTimed(ticksUntilTurn), new Fly(self, target), new FlyTimed(ticksUntilTurn));
|
||||
inner = Util.SequenceActivities(new FlyTimed(ticksUntilTurn, self), new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
else
|
||||
inner = Util.SequenceActivities(new Fly(self, target), new FlyTimed(ticksUntilTurn));
|
||||
inner = Util.SequenceActivities(new Fly(self, target), new FlyTimed(ticksUntilTurn, self));
|
||||
}
|
||||
|
||||
inner = Util.RunActivity(self, inner);
|
||||
|
||||
@@ -16,16 +16,23 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class FlyCircle : Activity
|
||||
{
|
||||
readonly Plane plane;
|
||||
readonly WRange cruiseAltitude;
|
||||
|
||||
public FlyCircle(Actor self)
|
||||
{
|
||||
plane = self.Trait<Plane>();
|
||||
cruiseAltitude = plane.Info.CruiseAltitude;
|
||||
}
|
||||
|
||||
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);
|
||||
Fly.FlyToward(self, plane, desiredFacing, cruiseAltitude);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
35
OpenRA.Mods.Common/Activities/Air/FlyOffMap.cs
Normal file
35
OpenRA.Mods.Common/Activities/Air/FlyOffMap.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 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.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class FlyOffMap : Activity
|
||||
{
|
||||
readonly Plane plane;
|
||||
|
||||
public FlyOffMap(Actor self)
|
||||
{
|
||||
plane = self.Trait<Plane>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || !self.World.Map.Contains(self.Location))
|
||||
return NextActivity;
|
||||
|
||||
Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,37 +16,25 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class FlyTimed : Activity
|
||||
{
|
||||
readonly Plane plane;
|
||||
readonly WRange cruiseAltitude;
|
||||
int remainingTicks;
|
||||
|
||||
public FlyTimed(int ticks) { remainingTicks = ticks; }
|
||||
public FlyTimed(int ticks, Actor self)
|
||||
{
|
||||
remainingTicks = ticks;
|
||||
plane = self.Trait<Plane>();
|
||||
cruiseAltitude = plane.Info.CruiseAltitude;
|
||||
}
|
||||
|
||||
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);
|
||||
Fly.FlyToward(self, plane, plane.Facing, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
// If all ammo pools are depleted and none reload automatically, return to helipad to reload and then move to next activity
|
||||
// TODO: This should check whether there is ammo left that is actually suitable for the target
|
||||
if (ammoPools != null && ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
|
||||
return Util.SequenceActivities(new HeliReturn(), NextActivity);
|
||||
return Util.SequenceActivities(new HeliReturn(self), NextActivity);
|
||||
|
||||
var dist = target.CenterPosition - self.CenterPosition;
|
||||
|
||||
|
||||
@@ -15,11 +15,15 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class HeliLand : Activity
|
||||
{
|
||||
readonly Helicopter helicopter;
|
||||
readonly WRange landAltitude;
|
||||
bool requireSpace;
|
||||
|
||||
public HeliLand(bool requireSpace)
|
||||
public HeliLand(Actor self, bool requireSpace)
|
||||
{
|
||||
this.requireSpace = requireSpace;
|
||||
helicopter = self.Trait<Helicopter>();
|
||||
landAltitude = helicopter.Info.LandAltitude;
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
@@ -27,12 +31,10 @@ namespace OpenRA.Mods.Common.Activities
|
||||
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))
|
||||
if (HeliFly.AdjustAltitude(self, helicopter, landAltitude))
|
||||
return this;
|
||||
|
||||
return NextActivity;
|
||||
|
||||
@@ -17,7 +17,18 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class HeliReturn : Activity
|
||||
{
|
||||
static Actor ChooseHelipad(Actor self)
|
||||
readonly AircraftInfo aircraftInfo;
|
||||
readonly Helicopter heli;
|
||||
readonly HelicopterInfo heliInfo;
|
||||
|
||||
public HeliReturn(Actor self)
|
||||
{
|
||||
aircraftInfo = self.Info.Traits.Get<AircraftInfo>();
|
||||
heli = self.Trait<Helicopter>();
|
||||
heliInfo = self.Info.Traits.Get<HelicopterInfo>();
|
||||
}
|
||||
|
||||
public static Actor ChooseHelipad(Actor self)
|
||||
{
|
||||
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
|
||||
return self.World.Actors.Where(a => a.Owner == self.Owner).FirstOrDefault(
|
||||
@@ -30,24 +41,23 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return NextActivity;
|
||||
|
||||
var dest = ChooseHelipad(self);
|
||||
var initialFacing = self.Info.Traits.Get<AircraftInfo>().InitialFacing;
|
||||
var initialFacing = aircraftInfo.InitialFacing;
|
||||
|
||||
if (dest == null)
|
||||
{
|
||||
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
|
||||
var rearmBuildings = heliInfo.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);
|
||||
return Util.SequenceActivities(new Turn(self, initialFacing), new HeliLand(self, 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)
|
||||
{
|
||||
@@ -61,8 +71,8 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return Util.SequenceActivities(
|
||||
new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
|
||||
new Turn(self, initialFacing),
|
||||
new HeliLand(false),
|
||||
new ResupplyAircraft());
|
||||
new HeliLand(self, false),
|
||||
new ResupplyAircraft(self));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,14 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class Land : Activity
|
||||
{
|
||||
Target target;
|
||||
readonly Target target;
|
||||
readonly Plane plane;
|
||||
|
||||
public Land(Target t) { target = t; }
|
||||
public Land(Actor self, Target t)
|
||||
{
|
||||
target = t;
|
||||
plane = self.Trait<Plane>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
@@ -28,7 +33,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
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
|
||||
|
||||
@@ -20,9 +20,15 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class ResupplyAircraft : Activity
|
||||
{
|
||||
readonly Aircraft aircraft;
|
||||
|
||||
public ResupplyAircraft(Actor self)
|
||||
{
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
var aircraft = self.Trait<Aircraft>();
|
||||
var host = aircraft.GetActorBelow();
|
||||
|
||||
if (host == null)
|
||||
|
||||
@@ -18,10 +18,19 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class ReturnToBase : Activity
|
||||
{
|
||||
readonly Plane plane;
|
||||
readonly PlaneInfo planeInfo;
|
||||
bool isCalculated;
|
||||
Actor dest;
|
||||
WPos w1, w2, w3;
|
||||
|
||||
public ReturnToBase(Actor self, Actor dest)
|
||||
{
|
||||
this.dest = dest;
|
||||
plane = self.Trait<Plane>();
|
||||
planeInfo = self.Info.Traits.Get<PlaneInfo>();
|
||||
}
|
||||
|
||||
public static Actor ChooseAirfield(Actor self, bool unreservedOnly)
|
||||
{
|
||||
var rearmBuildings = self.Info.Traits.Get<PlaneInfo>().RearmBuildings;
|
||||
@@ -41,8 +50,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
if (dest == null)
|
||||
return;
|
||||
|
||||
var plane = self.Trait<Plane>();
|
||||
var planeInfo = self.Info.Traits.Get<PlaneInfo>();
|
||||
var res = dest.TraitOrDefault<Reservable>();
|
||||
if (res != null)
|
||||
{
|
||||
@@ -54,7 +61,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
var altitude = planeInfo.CruiseAltitude.Range;
|
||||
|
||||
// Distance required for descent.
|
||||
var landDistance = altitude * 1024 / plane.Info.MaximumPitch.Tan();
|
||||
var landDistance = altitude * 1024 / planeInfo.MaximumPitch.Tan();
|
||||
|
||||
// Land towards the east
|
||||
var approachStart = landPos + new WVec(-landDistance, 0, altitude);
|
||||
@@ -92,11 +99,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
isCalculated = true;
|
||||
}
|
||||
|
||||
public ReturnToBase(Actor self, Actor dest)
|
||||
{
|
||||
this.dest = dest;
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || self.IsDead)
|
||||
@@ -111,16 +113,16 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
self.CancelActivity();
|
||||
if (nearestAfld != null)
|
||||
return Util.SequenceActivities(new Fly(self, Target.FromActor(nearestAfld)), new FlyCircle());
|
||||
return Util.SequenceActivities(new Fly(self, Target.FromActor(nearestAfld)), new FlyCircle(self));
|
||||
else
|
||||
return new FlyCircle();
|
||||
return new FlyCircle(self);
|
||||
}
|
||||
|
||||
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)),
|
||||
new Land(self, Target.FromActor(dest)),
|
||||
NextActivity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,17 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class TakeOff : Activity
|
||||
{
|
||||
readonly Aircraft aircraft;
|
||||
readonly IMove move;
|
||||
|
||||
public TakeOff(Actor self)
|
||||
{
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
move = self.Trait<IMove>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
var aircraft = self.Trait<Aircraft>();
|
||||
|
||||
self.CancelActivity();
|
||||
|
||||
var reservation = aircraft.Reservation;
|
||||
@@ -36,7 +43,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
var destination = rp != null ? rp.Location :
|
||||
(hasHost ? self.World.Map.CellContaining(host.CenterPosition) : self.Location);
|
||||
|
||||
return new AttackMoveActivity(self, self.Trait<IMove>().MoveTo(destination, 1));
|
||||
return new AttackMoveActivity(self, move.MoveTo(destination, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user