moved air unit support to ra

This commit is contained in:
Chris Forbes
2010-05-20 18:54:44 +12:00
parent 7fc5007ac8
commit b0d2bf2e51
23 changed files with 48 additions and 23 deletions

View File

@@ -0,0 +1,78 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Fly : IActivity
{
readonly float2 Pos;
bool isCanceled;
public Fly(float2 pos) { Pos = pos; }
public Fly(int2 pos) { Pos = Util.CenterOfCell(pos); }
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self)
{
var cruiseAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (isCanceled) return NextActivity;
var d = Pos - self.CenterLocation;
if (d.LengthSquared < 50) /* close enough */
return NextActivity;
var unit = self.traits.Get<Unit>();
var desiredFacing = Util.GetFacing(d, unit.Facing);
if (unit.Altitude == cruiseAltitude)
Util.TickFacing(ref unit.Facing, desiredFacing,
self.Info.Traits.Get<UnitInfo>().ROT);
if (unit.Altitude < cruiseAltitude)
++unit.Altitude;
FlyUtil.Fly(self, cruiseAltitude);
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
public static class FlyUtil
{
public static void Fly(Actor self, int desiredAltitude )
{
var unit = self.traits.Get<Unit>();
var speed = .2f * Util.GetEffectiveSpeed(self, UnitMovementType.Fly);
var angle = unit.Facing / 128f * Math.PI;
self.CenterLocation += speed * -float2.FromAngle((float)angle);
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
unit.Altitude += Math.Sign(desiredAltitude - unit.Altitude);
}
}
}

View File

@@ -0,0 +1,71 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyAttack : IActivity
{
public IActivity NextActivity { get; set; }
Actor Target;
public FlyAttack(Actor target) { Target = target; }
public IActivity Tick(Actor self)
{
if (Target == null || Target.IsDead)
return NextActivity;
var limitedAmmo = self.traits.GetOrDefault<LimitedAmmo>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
return NextActivity;
return Util.SequenceActivities(
new Fly(Target.CenterLocation),
new FlyTimed(50),
this);
}
public void Cancel(Actor self) { Target = null; NextActivity = null; }
}
public class FlyCircle : IActivity
{
public IActivity NextActivity { get; set; }
int2 Target;
bool isCanceled;
public FlyCircle(int2 target) { Target = target; }
public IActivity Tick(Actor self)
{
if (isCanceled)
return NextActivity;
return Util.SequenceActivities(
new Fly(Util.CenterOfCell(Target)),
new FlyTimed(50),
this);
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -0,0 +1,68 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyTimed : IActivity
{
public IActivity NextActivity { get; set; }
int remainingTicks;
public FlyTimed(int ticks) { remainingTicks = ticks; }
public IActivity Tick(Actor self)
{
var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (remainingTicks-- == 0) return NextActivity;
FlyUtil.Fly(self, targetAltitude);
return this;
}
public void Cancel(Actor self) { remainingTicks = 0; NextActivity = null; }
}
public class FlyOffMap : IActivity
{
public IActivity NextActivity { get; set; }
bool isCanceled;
public bool Interruptible = true;
public IActivity Tick(Actor self)
{
var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (isCanceled || !self.World.Map.IsInMap(self.Location)) return NextActivity;
FlyUtil.Fly(self, targetAltitude);
return this;
}
public void Cancel(Actor self)
{
if (Interruptible)
{
isCanceled = true;
NextActivity = null;
}
}
}
}

View File

@@ -0,0 +1,92 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class HeliAttack : IActivity
{
Actor target;
public HeliAttack( Actor target ) { this.target = target; }
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self)
{
if (target == null || target.IsDead)
return NextActivity;
var limitedAmmo = self.traits.GetOrDefault<LimitedAmmo>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
return NextActivity;
var unit = self.traits.Get<Unit>();
var info = self.Info.Traits.Get<HelicopterInfo>();
if (unit.Altitude != info.CruiseAltitude)
{
unit.Altitude += Math.Sign(info.CruiseAltitude - unit.Altitude);
return this;
}
var range = self.GetPrimaryWeapon().Range - 1;
var dist = target.CenterLocation - self.CenterLocation;
var desiredFacing = Util.GetFacing(dist, unit.Facing);
Util.TickFacing(ref unit.Facing, desiredFacing, self.Info.Traits.Get<UnitInfo>().ROT);
var rawSpeed = .2f * Util.GetEffectiveSpeed(self, UnitMovementType.Fly);
if (!float2.WithinEpsilon(float2.Zero, dist, range * Game.CellSize))
self.CenterLocation += (rawSpeed / dist.Length) * dist;
var otherHelis = self.World.FindUnitsInCircle(self.CenterLocation, info.IdealSeparation)
.Where(a => a.traits.Contains<Helicopter>());
var f = otherHelis
.Select(h => GetRepulseForce(self, h))
.Aggregate(float2.Zero, (a, b) => a + b);
self.CenterLocation += rawSpeed * f;
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return this;
}
const float Epsilon = .5f;
float2 GetRepulseForce(Actor self, Actor h)
{
if (self == h)
return float2.Zero;
var d = self.CenterLocation - h.CenterLocation;
if (d.LengthSquared < Epsilon)
return float2.FromAngle((float)self.World.SharedRandom.NextDouble() * 3.14f);
return (2 / d.LengthSquared) * d;
}
public void Cancel(Actor self) { target = null; NextActivity = null; }
}
}

View File

@@ -0,0 +1,73 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliFly : IActivity
{
readonly float2 Dest;
public HeliFly(float2 dest)
{
Dest = dest;
}
public IActivity NextActivity { get; set; }
bool isCanceled;
public IActivity Tick(Actor self)
{
if (isCanceled)
return NextActivity;
var unit = self.traits.Get<Unit>();
var info = self.Info.Traits.Get<HelicopterInfo>();
if (unit.Altitude != info.CruiseAltitude)
{
unit.Altitude += Math.Sign(info.CruiseAltitude - unit.Altitude);
return this;
}
var dist = Dest - self.CenterLocation;
if (float2.WithinEpsilon(float2.Zero, dist, 2))
{
self.CenterLocation = Dest;
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return NextActivity;
}
var desiredFacing = Util.GetFacing(dist, unit.Facing);
Util.TickFacing(ref unit.Facing, desiredFacing,
self.Info.Traits.Get<UnitInfo>().ROT);
var rawSpeed = .2f * Util.GetEffectiveSpeed(self, UnitMovementType.Fly);
self.CenterLocation += (rawSpeed / dist.Length) * dist;
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -0,0 +1,50 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliLand : IActivity
{
public HeliLand(bool requireSpace) { this.requireSpace = requireSpace; }
bool requireSpace;
bool isCanceled;
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self)
{
if (isCanceled) return NextActivity;
var unit = self.traits.Get<Unit>();
if (unit.Altitude == 0)
return NextActivity;
if (requireSpace && !self.World.IsPathableCell(self.Location, UnitMovementType.Foot))
return this; // fail to land if no space
--unit.Altitude;
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -0,0 +1,71 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{
public class HeliReturn : IActivity
{
public IActivity NextActivity { get; set; }
bool isCanceled;
static Actor ChooseHelipad(Actor self)
{
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
return self.World.Queries.OwnedBy[self.Owner].FirstOrDefault(
a => rearmBuildings.Contains(a.Info.Name) &&
!Reservable.IsReserved(a));
}
public IActivity Tick(Actor self)
{
if (isCanceled) return NextActivity;
var dest = ChooseHelipad(self);
var initialFacing = self.Info.Traits.Get<UnitInfo>().InitialFacing;
if (dest == null)
return Util.SequenceActivities(
new Turn(initialFacing),
new HeliLand(true),
NextActivity);
var res = dest.traits.GetOrDefault<Reservable>();
if (res != null)
self.traits.Get<Helicopter>().reservation = res.Reserve(self);
var pi = dest.Info.Traits.GetOrDefault<ProductionInfo>();
var offset = pi != null ? pi.SpawnOffset : null;
var offsetVec = offset != null ? new float2(offset[0], offset[1]) : float2.Zero;
return Util.SequenceActivities(
new HeliFly(dest.CenterLocation + offsetVec),
new Turn(initialFacing),
new HeliLand(false),
new Rearm(),
NextActivity);
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -0,0 +1,70 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Land : IActivity
{
readonly float2 Pos;
bool isCanceled;
Actor Structure;
public Land(float2 pos) { Pos = pos; }
public Land(Actor structure) { Structure = structure; Pos = Structure.CenterLocation; }
public IActivity NextActivity { get; set; }
public IActivity Tick(Actor self)
{
if (Structure != null && Structure.IsDead)
{
Structure = null;
isCanceled = true;
}
if (isCanceled) return NextActivity;
var d = Pos - self.CenterLocation;
if (d.LengthSquared < 50) /* close enough */
return NextActivity;
var unit = self.traits.Get<Unit>();
if (unit.Altitude > 0)
--unit.Altitude;
var desiredFacing = Util.GetFacing(d, unit.Facing);
Util.TickFacing(ref unit.Facing, desiredFacing, self.Info.Traits.Get<UnitInfo>().ROT);
var speed = .2f * Util.GetEffectiveSpeed(self, UnitMovementType.Fly);
var angle = unit.Facing / 128f * Math.PI;
self.CenterLocation += speed * -float2.FromAngle((float)angle);
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -0,0 +1,109 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class ReturnToBase : IActivity
{
public IActivity NextActivity { get; set; }
bool isCanceled;
bool isCalculated;
Actor dest;
float2 w1, w2, w3; /* tangent points to turn circles */
float2 landPoint;
public static Actor ChooseAirfield(Actor self)
{
return self.World.Queries.OwnedBy[self.Owner]
.Where(a => self.Info.Traits.Get<PlaneInfo>().RearmBuildings.Contains(a.Info.Name)
&& !Reservable.IsReserved(a))
.FirstOrDefault();
}
void Calculate(Actor self)
{
if (dest == null) dest = ChooseAirfield(self);
var res = dest.traits.GetOrDefault<Reservable>();
if (res != null)
self.traits.Get<Plane>().reservation = res.Reserve(self);
var landPos = dest.CenterLocation;
var unit = self.traits.Get<Unit>();
var speed = .2f * Util.GetEffectiveSpeed(self, UnitMovementType.Fly);
var approachStart = landPos - new float2(unit.Altitude * speed, 0);
var turnRadius = (128f / self.Info.Traits.Get<UnitInfo>().ROT) * speed / (float)Math.PI;
/* work out the center points */
var fwd = -float2.FromAngle(unit.Facing / 128f * (float)Math.PI);
var side = new float2(-fwd.Y, fwd.X); /* rotate */
var sideTowardBase = new[] { side, -side }
.OrderBy(a => float2.Dot(a, self.CenterLocation - approachStart))
.First();
var c1 = self.CenterLocation + turnRadius * sideTowardBase;
var c2 = approachStart + new float2(0,
turnRadius * Math.Sign(self.CenterLocation.Y - approachStart.Y)); // above or below start point
/* work out tangent points */
var d = c2 - c1;
var e = (turnRadius / d.Length) * d;
var f = new float2(-e.Y, e.X); /* rotate */
/* todo: support internal tangents, too! */
if (f.X > 0) f = -f;
w1 = c1 + f;
w2 = c2 + f;
w3 = approachStart;
landPoint = landPos;
isCalculated = true;
}
public ReturnToBase(Actor self, Actor dest)
{
this.dest = dest;
}
public IActivity Tick(Actor self)
{
if (isCanceled) return NextActivity;
if (!isCalculated)
Calculate(self);
return Util.SequenceActivities(
new Fly(w1),
new Fly(w2),
new Fly(w3),
new Land(landPoint),
NextActivity);
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}