move aircraft code into Mods.RA/Air/

This commit is contained in:
Bob
2010-11-06 12:15:14 +13:00
parent aa0c14c214
commit 801aa1156f
26 changed files with 46 additions and 38 deletions

View File

@@ -1,66 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Fly : CancelableActivity
{
public readonly float2 Pos;
private Fly( float2 px ) { Pos = px; }
public static Fly ToPx( float2 px ) { return new Fly( px ); }
public static Fly ToCell( int2 pos ) { return new Fly( Util.CenterOfCell( pos ) ); }
public override 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 aircraft = self.Trait<Aircraft>();
var desiredFacing = Util.GetFacing(d, aircraft.Facing);
if (aircraft.Altitude == cruiseAltitude)
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT);
if (aircraft.Altitude < cruiseAltitude)
++aircraft.Altitude;
FlyUtil.Fly(self, cruiseAltitude);
return this;
}
public override IEnumerable<float2> GetCurrentPath()
{
yield return Pos;
}
}
public static class FlyUtil
{
public static void Fly(Actor self, int desiredAltitude )
{
var aircraft = self.Trait<Aircraft>();
var speed = .2f * aircraft.MovementSpeed;
var angle = aircraft.Facing / 128f * Math.PI;
aircraft.center += speed * -float2.FromAngle((float)angle);
aircraft.Altitude += Math.Sign(desiredAltitude - aircraft.Altitude);
}
}
}

View File

@@ -1,69 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyAttack : CancelableActivity
{
readonly Target Target;
IActivity inner;
public FlyAttack(Target target) { Target = target; }
public override IActivity Tick(Actor self)
{
if( !Target.IsValid )
Cancel( self );
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if( limitedAmmo != null && !limitedAmmo.HasAmmo() )
Cancel( self );
self.Trait<AttackPlane>().DoAttack( self, Target );
if( inner == null )
{
if( IsCanceled )
return NextActivity;
inner = Util.SequenceActivities(
Fly.ToPx(Target.CenterLocation),
new FlyTimed(50));
}
inner = inner.Tick( self );
return this;
}
protected override bool OnCancel( Actor self )
{
if( inner != null )
inner.Cancel( self );
return base.OnCancel( self );
}
}
public class FlyCircle : CancelableActivity
{
int2 Target;
public FlyCircle(int2 target) { Target = target; }
public override IActivity Tick(Actor self)
{
if( IsCanceled ) return NextActivity;
return Util.SequenceActivities(
Fly.ToCell(Target),
new FlyTimed(50),
this);
}
}
}

View File

@@ -1,48 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class FlyTimed : CancelableActivity
{
int remainingTicks;
public FlyTimed(int ticks) { remainingTicks = ticks; }
public override IActivity Tick(Actor self)
{
if( IsCanceled ) return NextActivity;
var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (remainingTicks-- == 0) return NextActivity;
FlyUtil.Fly(self, targetAltitude);
return this;
}
}
public class FlyOffMap : CancelableActivity
{
public bool Interruptible = true;
public override 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;
}
protected override bool OnCancel( Actor self )
{
return Interruptible;
}
}
}

View File

@@ -1,58 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class HeliAttack : CancelableActivity
{
Target target;
public HeliAttack( Target target ) { this.target = target; }
public override IActivity Tick(Actor self)
{
if (IsCanceled) return NextActivity;
if (!target.IsValid) return NextActivity;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
{
self.QueueActivity(new HeliReturn());
return NextActivity;
}
var aircraft = self.Trait<Aircraft>();
var info = self.Info.Traits.Get<HelicopterInfo>();
if (aircraft.Altitude != info.CruiseAltitude)
{
aircraft.Altitude += Math.Sign(info.CruiseAltitude - aircraft.Altitude);
return this;
}
var attack = self.Trait<AttackBase>();
var range = attack.GetMaximumRange() - 1;
var dist = target.CenterLocation - self.CenterLocation;
var desiredFacing = Util.GetFacing(dist, aircraft.Facing);
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT);
var rawSpeed = .2f * aircraft.MovementSpeed;
if (!float2.WithinEpsilon(float2.Zero, dist, range * Game.CellSize))
aircraft.center += (rawSpeed / dist.Length) * dist;
attack.DoAttack( self, target );
return this;
}
}
}

View File

@@ -1,60 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliFly : CancelableActivity
{
public readonly float2 Dest;
public HeliFly(float2 dest)
{
Dest = dest;
}
public override IActivity Tick(Actor self)
{
if (IsCanceled) return NextActivity;
var info = self.Info.Traits.Get<HelicopterInfo>();
var aircraft = self.Trait<Aircraft>();
if (aircraft.Altitude != info.CruiseAltitude)
{
aircraft.Altitude += Math.Sign(info.CruiseAltitude - aircraft.Altitude);
return this;
}
var dist = Dest - self.CenterLocation;
if (float2.WithinEpsilon(float2.Zero, dist, 2))
{
aircraft.center = Dest;
return NextActivity;
}
var desiredFacing = Util.GetFacing(dist, aircraft.Facing);
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing,
aircraft.ROT);
var rawSpeed = .2f * aircraft.MovementSpeed;
aircraft.center += (rawSpeed / dist.Length) * dist;
return this;
}
public override IEnumerable<float2> GetCurrentPath()
{
yield return Dest;
}
}
}

View File

@@ -1,35 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
class HeliLand : CancelableActivity
{
public HeliLand(bool requireSpace) { this.requireSpace = requireSpace; }
bool requireSpace;
public override IActivity Tick(Actor self)
{
if (IsCanceled) return NextActivity;
var aircraft = self.Trait<Aircraft>();
if (aircraft.Altitude == 0)
return NextActivity;
if (requireSpace && !aircraft.CanEnterCell(self.Location))
return this;
--aircraft.Altitude;
return this;
}
}
}

View File

@@ -1,55 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{
public class HeliReturn : CancelableActivity
{
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 override IActivity Tick(Actor self)
{
if (IsCanceled) return NextActivity;
var dest = ChooseHelipad(self);
var initialFacing = self.Info.Traits.Get<AircraftInfo>().InitialFacing;
if (dest == null)
return Util.SequenceActivities(
new Turn(initialFacing),
new HeliLand(true),
NextActivity);
var res = dest.TraitOrDefault<Reservable>();
if (res != null)
self.Trait<Helicopter>().reservation = res.Reserve(self);
var exit = dest.Info.Traits.WithInterface<ExitInfo>().FirstOrDefault();
var offset = exit != null ? exit.SpawnOffset : float2.Zero;
return Util.SequenceActivities(
new HeliFly(dest.CenterLocation + offset),
new Turn(initialFacing),
new HeliLand(false),
new Rearm(),
NextActivity);
}
}
}

View File

@@ -1,49 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class Land : CancelableActivity
{
Target Target;
public Land(Target t) { Target = t; }
public override IActivity Tick(Actor self)
{
if (!Target.IsValid)
Cancel(self);
if (IsCanceled) return NextActivity;
var d = Target.CenterLocation - self.CenterLocation;
if (d.LengthSquared < 50) /* close enough */
return NextActivity;
var aircraft = self.Trait<Aircraft>();
if (aircraft.Altitude > 0)
--aircraft.Altitude;
var desiredFacing = Util.GetFacing(d, aircraft.Facing);
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.ROT);
var speed = .2f * aircraft.MovementSpeed;
var angle = aircraft.Facing / 128f * Math.PI;
aircraft.center += speed * -float2.FromAngle((float)angle);
return this;
}
}
}

View File

@@ -1,102 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class ReturnToBase : CancelableActivity
{
bool isCalculated;
Actor dest;
float2 w1, w2, w3; /* tangent points to turn circles */
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))
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
}
void Calculate(Actor self)
{
if (dest == null)
{
dest = ChooseAirfield(self);
}
var res = dest.TraitOrDefault<Reservable>();
if (res != null)
{
var plane = self.Trait<Plane>();
plane.UnReserve();
plane.reservation = res.Reserve(self);
}
var landPos = dest.CenterLocation;
var aircraft = self.Trait<Aircraft>();
var speed = .2f * aircraft.MovementSpeed;
var approachStart = landPos - new float2(aircraft.Altitude * speed, 0);
var turnRadius = (128f / self.Info.Traits.Get<AircraftInfo>().ROT) * speed / (float)Math.PI;
/* work out the center points */
var fwd = -float2.FromAngle(aircraft.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;
isCalculated = true;
}
public ReturnToBase(Actor self, Actor dest)
{
this.dest = dest;
}
public override IActivity Tick(Actor self)
{
if (IsCanceled) return NextActivity;
if (!isCalculated)
Calculate(self);
return Util.SequenceActivities(
Fly.ToPx(w1),
Fly.ToPx(w2),
Fly.ToPx(w3),
new Land(Target.FromActor(dest)),
NextActivity);
}
}
}