Files
OpenRA/OpenRA.Mods.RA/Air/Aircraft.cs
2013-07-21 17:35:47 +12:00

235 lines
6.2 KiB
C#
Executable File

#region Copyright & License Information
/*
* Copyright 2007-2013 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.FileFormats;
using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Orders;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Air
{
public class AircraftInfo : ITraitInfo, IFacingInfo, IOccupySpaceInfo, UsesInit<AltitudeInit>, UsesInit<LocationInit>, UsesInit<FacingInit>
{
public readonly int CruiseAltitude = 30;
[ActorReference]
public readonly string[] RepairBuildings = { "fix" };
[ActorReference]
public readonly string[] RearmBuildings = { "hpad", "afld" };
public readonly int InitialFacing = 128;
public readonly int ROT = 255;
public readonly int Speed = 1;
public readonly string[] LandableTerrainTypes = { };
public virtual object Create(ActorInitializer init) { return new Aircraft(init, this); }
public int GetInitialFacing() { return InitialFacing; }
}
public class Aircraft : IFacing, IPositionable, ISync, INotifyKilled, IIssueOrder, IOrderVoice
{
static readonly Pair<CPos, SubCell>[] NoCells = new Pair<CPos, SubCell>[] { };
readonly AircraftInfo info;
readonly Actor self;
[Sync] public int Facing { get; set; }
[Sync] public WPos CenterPosition { get; private set; }
public CPos TopLeft { get { return CenterPosition.ToCPos(); } }
public IDisposable Reservation;
public int ROT { get { return info.ROT; } }
public Aircraft(ActorInitializer init, AircraftInfo info)
{
this.info = info;
this.self = init.self;
if (init.Contains<LocationInit>())
SetPosition(self, init.Get<LocationInit, CPos>());
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing;
if (init.Contains<AltitudeInit>())
{
var z = init.Get<AltitudeInit, int>() * 1024 / Game.CellSize;
SetPosition(self, CenterPosition + new WVec(0, 0, z - CenterPosition.Z));
}
}
public Actor GetActorBelow()
{
if (self.CenterPosition.Z != 0)
return null; // not on the ground.
return self.World.FindActorsInBox(self.CenterPosition, self.CenterPosition)
.FirstOrDefault(a => a.HasTrait<Reservable>());
}
protected void ReserveSpawnBuilding()
{
/* HACK: not spawning in the air, so try to assoc. with our afld. */
var afld = GetActorBelow();
if (afld == null)
return;
var res = afld.Trait<Reservable>();
if (res != null)
Reservation = res.Reserve(afld, self, this);
}
public void UnReserve()
{
if (Reservation != null)
{
Reservation.Dispose();
Reservation = null;
}
}
public void Killed(Actor self, AttackInfo e)
{
UnReserve();
}
public void SetPosition(Actor self, CPos cell)
{
// Changes position, but not altitude
CenterPosition = cell.CenterPosition + new WVec(0, 0, CenterPosition.Z);
}
public void SetPosition(Actor self, WPos pos) { CenterPosition = pos; }
public void SetVisualPosition(Actor self, WPos pos) { SetPosition(self, pos); }
public bool AircraftCanEnter(Actor a)
{
if (self.AppearsHostileTo(a))
return false;
return info.RearmBuildings.Contains(a.Info.Name)
|| info.RepairBuildings.Contains(a.Info.Name);
}
public bool CanEnterCell(CPos location) { return true; }
public int MovementSpeed
{
get
{
decimal ret = info.Speed;
foreach (var t in self.TraitsImplementing<ISpeedModifier>())
ret *= t.GetSpeedModifier();
return (int)ret;
}
}
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return NoCells; }
public WVec FlyStep(int facing)
{
var speed = MovementSpeed * 7 * 1024 / (Game.CellSize * 32);
var dir = new WVec(0, -1024, 0).Rotate(WRot.FromFacing(facing));
return speed * dir / 1024;
}
public bool CanLand(CPos cell)
{
if (!self.World.Map.IsInMap(cell))
return false;
if (self.World.ActorMap.AnyUnitsAt(cell))
return false;
var type = self.World.GetTerrainType(cell);
return info.LandableTerrainTypes.Contains(type);
}
public IEnumerable<Activity> GetResupplyActivities(Actor a)
{
var name = a.Info.Name;
if (info.RearmBuildings.Contains(name))
yield return new Rearm(self);
if (info.RepairBuildings.Contains(name))
yield return new Repair(a);
}
public IEnumerable<IOrderTargeter> Orders
{
get
{
yield return new EnterOrderTargeter<Building>("Enter", 5, false, true,
target => AircraftCanEnter(target), target => !Reservable.IsReserved(target));
yield return new AircraftMoveOrderTargeter();
}
}
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if (order.OrderID == "Enter")
return new Order(order.OrderID, self, queued) { TargetActor = target.Actor };
if (order.OrderID == "Move")
return new Order(order.OrderID, self, queued) { TargetLocation = target.CenterPosition.ToCPos() };
return null;
}
public string VoicePhraseForOrder(Actor self, Order order)
{
switch (order.OrderString)
{
case "Move":
case "Enter":
case "ReturnToBase":
return "Move";
default: return null;
}
}
}
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());
}
}
class AircraftMoveOrderTargeter : IOrderTargeter
{
public string OrderID { get { return "Move"; } }
public int OrderPriority { get { return 4; } }
public bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor)
{
return false;
}
public bool CanTargetLocation(Actor self, CPos location, List<Actor> actorsAtLocation, TargetModifiers modifiers, ref string cursor)
{
IsQueued = modifiers.HasModifier(TargetModifiers.ForceQueue);
cursor = self.World.Map.IsInMap(location) ? "move" : "move-blocked";
return true;
}
public bool IsQueued { get; protected set; }
}
}