Merge pull request #7316 from reaperrr/move-stuff28

Moved Upgrades, SupportPowers and some misc features to Mods.Common
This commit is contained in:
Oliver Brakmann
2015-01-15 20:57:40 +01:00
38 changed files with 72 additions and 85 deletions

View File

@@ -0,0 +1,110 @@
#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 System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class C4DemolitionInfo : ITraitInfo
{
[Desc("Delay to demolish the target once the C4 is planted." +
"Measured in game ticks. Default is 1.8 seconds.")]
public readonly int C4Delay = 45;
[Desc("Number of times to flash the target")]
public readonly int Flashes = 3;
[Desc("Delay before the flashing starts")]
public readonly int FlashesDelay = 4;
[Desc("Interval between each flash")]
public readonly int FlashInterval = 4;
[Desc("Duration of each flash")]
public readonly int FlashDuration = 3;
[Desc("Voice string when planting explosive charges.")]
public readonly string Voice = "Attack";
public object Create(ActorInitializer init) { return new C4Demolition(this); }
}
class C4Demolition : IIssueOrder, IResolveOrder, IOrderVoice
{
readonly C4DemolitionInfo info;
public C4Demolition(C4DemolitionInfo info)
{
this.info = info;
}
public IEnumerable<IOrderTargeter> Orders
{
get { yield return new C4DemolitionOrderTargeter(); }
}
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if (order.OrderID != "C4")
return null;
if (target.Type == TargetType.FrozenActor)
return new Order(order.OrderID, self, queued) { ExtraData = target.FrozenActor.ID };
return new Order(order.OrderID, self, queued) { TargetActor = target.Actor };
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString != "C4")
return;
var target = self.ResolveFrozenActorOrder(order, Color.Red);
if (target.Type != TargetType.Actor)
return;
var demolishable = target.Actor.TraitOrDefault<IDemolishable>();
if (demolishable == null || !demolishable.IsValidTarget(target.Actor, self))
return;
if (!order.Queued)
self.CancelActivity();
self.SetTargetLine(target, Color.Red);
self.QueueActivity(new Demolish(self,
target.Actor, info.C4Delay, info.Flashes, info.FlashesDelay, info.FlashInterval, info.FlashDuration));
}
public string VoicePhraseForOrder(Actor self, Order order)
{
return order.OrderString == "C4" ? info.Voice : null;
}
class C4DemolitionOrderTargeter : UnitOrderTargeter
{
public C4DemolitionOrderTargeter()
: base("C4", 6, "c4", true, false) { }
public override bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor)
{
// Obey force moving onto bridges
if (modifiers.HasModifier(TargetModifiers.ForceMove))
return false;
return target.TraitsImplementing<IDemolishable>().Any(i => i.IsValidTarget(target, self));
}
public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
{
return target.Info.Traits.WithInterface<IDemolishableInfo>().Any(i => i.IsValidTarget(target.Info, self));
}
}
}
}

View File

@@ -0,0 +1,66 @@
#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 System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawn new actors when sold.")]
class EmitInfantryOnSellInfo : TraitInfo<EmitInfantryOnSell>
{
public readonly float ValuePercent = 40;
public readonly float MinHpPercent = 30;
[ActorReference]
[Desc("Be sure to use lowercase. Default value is \"e1\".")]
public readonly string[] ActorTypes = { "e1" };
}
class EmitInfantryOnSell : INotifySold
{
public void Selling(Actor self) { }
static void Emit(Actor self)
{
var info = self.Info.Traits.Get<EmitInfantryOnSellInfo>();
var csv = self.Info.Traits.GetOrDefault<CustomSellValueInfo>();
var valued = self.Info.Traits.GetOrDefault<ValuedInfo>();
var cost = csv != null ? csv.Value : (valued != null ? valued.Cost : 0);
var health = self.TraitOrDefault<Health>();
var dudesValue = info.ValuePercent * cost;
if (health != null)
dudesValue = dudesValue * health.HP / health.MaxHP;
dudesValue /= 100;
var eligibleLocations = FootprintUtils.Tiles(self).ToList();
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToArray();
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
{
var at = actorTypes.Where(a => a.Cost <= dudesValue).Random(self.World.SharedRandom);
var loc = eligibleLocations.Random(self.World.SharedRandom);
eligibleLocations.Remove(loc);
dudesValue -= at.Cost;
self.World.AddFrameEndTask(w => w.CreateActor(at.Name, new TypeDictionary
{
new LocationInit(loc),
new OwnerInit(self.Owner),
}));
}
}
public void Sold(Actor self) { Emit(self); }
}
}

View File

@@ -0,0 +1,23 @@
#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.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This unit cannot be damaged.")]
class InvulnerableInfo : TraitInfo<Invulnerable> { }
class Invulnerable : IDamageModifier
{
public int GetDamageModifier(Actor attacker, DamageWarhead warhead) { return 0; }
}
}

View File

@@ -0,0 +1,51 @@
#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 System.Drawing;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Display the time remaining until the super weapon attached to the actor is ready to the player and his allies.")]
class SupportPowerChargeBarInfo : ITraitInfo
{
public readonly Color Color = Color.Magenta;
public object Create(ActorInitializer init) { return new SupportPowerChargeBar(init.Self, this); }
}
class SupportPowerChargeBar : ISelectionBar
{
readonly Actor self;
readonly SupportPowerChargeBarInfo info;
public SupportPowerChargeBar(Actor self, SupportPowerChargeBarInfo info)
{
this.self = self;
this.info = info;
}
public float GetValue()
{
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0;
var spm = self.Owner.PlayerActor.Trait<SupportPowerManager>();
var power = spm.GetPowersForActor(self).FirstOrDefault(sp => !sp.Disabled);
if (power == null) return 0;
return 1 - (float)power.RemainingTime / power.TotalTime;
}
public Color GetColor() { return info.Color; }
}
}

View File

@@ -0,0 +1,182 @@
#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 System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class AirstrikePowerInfo : SupportPowerInfo
{
[ActorReference]
public readonly string UnitType = "badr.bomber";
public readonly int SquadSize = 1;
public readonly WVec SquadOffset = new WVec(-1536, 1536, 0);
public readonly int QuantizedFacings = 32;
public readonly WRange Cordon = new WRange(5120);
[ActorReference]
[Desc("Actor to spawn when the aircraft start attacking")]
public readonly string CameraActor = null;
[Desc("Amount of time to keep the camera alive after the aircraft have finished attacking")]
public readonly int CameraRemoveDelay = 25;
[Desc("Weapon range offset to apply during the beacon clock calculation")]
public readonly WRange BeaconDistanceOffset = WRange.FromCells(6);
public override object Create(ActorInitializer init) { return new AirstrikePower(init.Self, this); }
}
public class AirstrikePower : SupportPower
{
public AirstrikePower(Actor self, AirstrikePowerInfo info)
: base(self, info) { }
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
SendAirstrike(self, self.World.Map.CenterOfCell(order.TargetLocation));
}
public void SendAirstrike(Actor self, WPos target, bool randomize = true, int attackFacing = 0)
{
var info = Info as AirstrikePowerInfo;
if (randomize)
attackFacing = Util.QuantizeFacing(self.World.SharedRandom.Next(256), info.QuantizedFacings) * (256 / info.QuantizedFacings);
var altitude = self.World.Map.Rules.Actors[info.UnitType].Traits.Get<PlaneInfo>().CruiseAltitude.Range;
var attackRotation = WRot.FromFacing(attackFacing);
var delta = new WVec(0, -1024, 0).Rotate(attackRotation);
target = target + new WVec(0, 0, altitude);
var startEdge = target - (self.World.Map.DistanceToEdge(target, -delta) + info.Cordon).Range * delta / 1024;
var finishEdge = target + (self.World.Map.DistanceToEdge(target, delta) + info.Cordon).Range * delta / 1024;
Actor camera = null;
Beacon beacon = null;
var aircraftInRange = new Dictionary<Actor, bool>();
Action<Actor> onEnterRange = a =>
{
// Spawn a camera and remove the beacon when the first plane enters the target area
if (info.CameraActor != null && !aircraftInRange.Any(kv => kv.Value))
{
self.World.AddFrameEndTask(w =>
{
camera = w.CreateActor(info.CameraActor, new TypeDictionary
{
new LocationInit(self.World.Map.CellContaining(target)),
new OwnerInit(self.Owner),
});
});
}
if (beacon != null)
{
self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
}
aircraftInRange[a] = true;
};
Action<Actor> onExitRange = a =>
{
aircraftInRange[a] = false;
// Remove the camera when the final plane leaves the target area
if (!aircraftInRange.Any(kv => kv.Value))
{
if (camera != null)
{
camera.QueueActivity(new Wait(info.CameraRemoveDelay));
camera.QueueActivity(new RemoveSelf());
}
camera = null;
if (beacon != null)
{
self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
}
}
};
self.World.AddFrameEndTask(w =>
{
var notification = self.Owner.IsAlliedWith(self.World.RenderPlayer) ? Info.LaunchSound : Info.IncomingSound;
Sound.Play(notification);
Actor distanceTestActor = null;
for (var i = -info.SquadSize / 2; i <= info.SquadSize / 2; i++)
{
// Even-sized squads skip the lead plane
if (i == 0 && (info.SquadSize & 1) == 0)
continue;
// Includes the 90 degree rotation between body and world coordinates
var so = info.SquadOffset;
var spawnOffset = new WVec(i * so.Y, -Math.Abs(i) * so.X, 0).Rotate(attackRotation);
var targetOffset = new WVec(i * so.Y, 0, 0).Rotate(attackRotation);
var a = w.CreateActor(info.UnitType, new TypeDictionary
{
new CenterPositionInit(startEdge + spawnOffset),
new OwnerInit(self.Owner),
new FacingInit(attackFacing),
});
var attack = a.Trait<AttackBomber>();
attack.SetTarget(w, target + targetOffset);
attack.OnEnteredAttackRange += onEnterRange;
attack.OnExitedAttackRange += onExitRange;
attack.OnRemovedFromWorld += onExitRange;
a.QueueActivity(new Fly(a, Target.FromPos(target + spawnOffset)));
a.QueueActivity(new Fly(a, Target.FromPos(finishEdge + spawnOffset)));
a.QueueActivity(new RemoveSelf());
aircraftInRange.Add(a, false);
distanceTestActor = a;
}
if (Info.DisplayBeacon)
{
var distance = (target - startEdge).HorizontalLength;
beacon = new Beacon(
self.Owner,
target,
Info.BeaconPalettePrefix,
Info.BeaconPoster,
Info.BeaconPosterPalette,
() => 1 - ((distanceTestActor.CenterPosition - target).HorizontalLength - info.BeaconDistanceOffset.Range) * 1f / distance);
w.Add(beacon);
}
});
}
}
}

View File

@@ -0,0 +1,148 @@
#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 System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class GrantUpgradePowerInfo : SupportPowerInfo
{
[Desc("The upgrades to apply.")]
public readonly string[] Upgrades = { };
[Desc("Duration of the upgrade (in ticks). Set to 0 for a permanent upgrade.")]
public readonly int Duration = 0;
[Desc("Cells")]
public readonly int Range = 1;
public readonly string GrantUpgradeSound = "ironcur9.aud";
public override object Create(ActorInitializer init) { return new GrantUpgradePower(init.Self, this); }
}
class GrantUpgradePower : SupportPower
{
GrantUpgradePowerInfo info;
public GrantUpgradePower(Actor self, GrantUpgradePowerInfo info)
: base(self, info)
{
this.info = info;
}
public override IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectTarget(Self.World, order, manager, this);
}
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
self.Trait<RenderBuilding>().PlayCustomAnim(self, "active");
Sound.Play(info.GrantUpgradeSound, self.World.Map.CenterOfCell(order.TargetLocation));
foreach (var a in UnitsInRange(order.TargetLocation))
{
var um = a.TraitOrDefault<UpgradeManager>();
if (um == null)
continue;
foreach (var u in info.Upgrades)
{
if (!um.AcceptsUpgrade(a, u))
continue;
if (info.Duration > 0)
um.GrantTimedUpgrade(a, u, info.Duration);
else
um.GrantUpgrade(a, u, this);
}
}
}
public IEnumerable<Actor> UnitsInRange(CPos xy)
{
var range = info.Range;
var tiles = Self.World.Map.FindTilesInCircle(xy, range);
var units = new List<Actor>();
foreach (var t in tiles)
units.AddRange(Self.World.ActorMap.GetUnitsAt(t));
return units.Distinct().Where(a =>
{
if (!a.Owner.IsAlliedWith(Self.Owner))
return false;
var um = a.TraitOrDefault<UpgradeManager>();
return um != null && info.Upgrades.Any(u => um.AcceptsUpgrade(a, u));
});
}
class SelectTarget : IOrderGenerator
{
readonly GrantUpgradePower power;
readonly int range;
readonly Sprite tile;
readonly SupportPowerManager manager;
readonly string order;
public SelectTarget(World world, string order, SupportPowerManager manager, GrantUpgradePower power)
{
this.manager = manager;
this.order = order;
this.power = power;
this.range = power.info.Range;
tile = world.Map.SequenceProvider.GetSequence("overlay", "target-select").GetSprite(0);
}
public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
{
world.CancelInputMode();
if (mi.Button == MouseButton.Left && power.UnitsInRange(xy).Any())
yield return new Order(order, manager.Self, false) { TargetLocation = xy, SuppressVisualFeedback = true };
}
public void Tick(World world)
{
// Cancel the OG if we can't use the power
if (!manager.Powers.ContainsKey(order))
world.CancelInputMode();
}
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
{
var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
foreach (var unit in power.UnitsInRange(xy))
yield return new SelectionBoxRenderable(unit, Color.Red);
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world)
{
var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
var pal = wr.Palette("terrain");
foreach (var t in world.Map.FindTilesInCircle(xy, range))
yield return new SpriteRenderable(tile, wr.World.Map.CenterOfCell(t), WVec.Zero, -511, pal, 1f, true);
}
public string GetCursor(World world, CPos xy, MouseInput mi)
{
return power.UnitsInRange(xy).Any() ? "ability" : "move-blocked";
}
}
}
}

View File

@@ -0,0 +1,127 @@
#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 System;
using OpenRA.Effects;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class NukePowerInfo : SupportPowerInfo, Requires<IBodyOrientationInfo>
{
[WeaponReference]
public readonly string MissileWeapon = "";
public readonly WVec SpawnOffset = WVec.Zero;
[Desc("Travel time - split equally between ascent and descent")]
public readonly int FlightDelay = 400;
[Desc("Visual ascent velocity in WRange / tick")]
public readonly WRange FlightVelocity = new WRange(512);
[Desc("Descend immediately on the target, with half the FlightDelay")]
public readonly bool SkipAscent = false;
[Desc("Amount of time before detonation to remove the beacon")]
public readonly int BeaconRemoveAdvance = 25;
[ActorReference]
[Desc("Actor to spawn before detonation")]
public readonly string CameraActor = null;
[Desc("Amount of time before detonation to spawn the camera")]
public readonly int CameraSpawnAdvance = 25;
[Desc("Amount of time after detonation to remove the camera")]
public readonly int CameraRemoveDelay = 25;
public override object Create(ActorInitializer init) { return new NukePower(init.Self, this); }
}
class NukePower : SupportPower
{
IBodyOrientation body;
public NukePower(Actor self, NukePowerInfo info)
: base(self, info)
{
body = self.Trait<IBodyOrientation>();
}
public override IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectGenericPowerTarget(order, manager, "nuke", MouseButton.Left);
}
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
if (self.Owner.IsAlliedWith(self.World.RenderPlayer))
Sound.Play(Info.LaunchSound);
else
Sound.Play(Info.IncomingSound);
var npi = Info as NukePowerInfo;
var rb = self.Trait<RenderSimple>();
rb.PlayCustomAnim(self, "active");
var targetPosition = self.World.Map.CenterOfCell(order.TargetLocation);
var missile = new NukeLaunch(self.Owner, npi.MissileWeapon,
self.CenterPosition + body.LocalToWorld(npi.SpawnOffset),
targetPosition,
npi.FlightVelocity, npi.FlightDelay, npi.SkipAscent);
self.World.AddFrameEndTask(w => w.Add(missile));
if (npi.CameraActor != null)
{
var camera = self.World.CreateActor(false, npi.CameraActor, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner),
});
camera.QueueActivity(new Wait(npi.CameraSpawnAdvance + npi.CameraRemoveDelay));
camera.QueueActivity(new RemoveSelf());
Action addCamera = () => self.World.AddFrameEndTask(w => w.Add(camera));
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(npi.FlightDelay - npi.CameraSpawnAdvance, addCamera)));
}
if (Info.DisplayBeacon)
{
var beacon = new Beacon(
order.Player,
targetPosition,
Info.BeaconPalettePrefix,
Info.BeaconPoster,
Info.BeaconPosterPalette,
() => missile.FractionComplete);
Action removeBeacon = () => self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
self.World.AddFrameEndTask(w =>
{
w.Add(beacon);
w.Add(new DelayedAction(npi.FlightDelay - npi.BeaconRemoveAdvance, removeBeacon));
});
}
}
}
}

View File

@@ -0,0 +1,67 @@
#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.Effects;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawns an actor that stays for a limited amount of time.")]
public class SpawnActorPowerInfo : SupportPowerInfo
{
[Desc("Actor to spawn.")]
public readonly string Actor = null;
[Desc("Amount of time to keep the actor alive in ticks.")]
public readonly int LifeTime = 250;
public readonly string DeploySound = null;
public readonly string EffectSequence = null;
public readonly string EffectPalette = null;
public override object Create(ActorInitializer init) { return new SpawnActorPower(init.Self, this); }
}
public class SpawnActorPower : SupportPower
{
public SpawnActorPower(Actor self, SpawnActorPowerInfo info) : base(self, info) { }
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
var info = Info as SpawnActorPowerInfo;
if (info.Actor != null)
{
self.World.AddFrameEndTask(w =>
{
var location = self.World.Map.CenterOfCell(order.TargetLocation);
Sound.Play(info.DeploySound, location);
if (!string.IsNullOrEmpty(info.EffectSequence) && !string.IsNullOrEmpty(info.EffectPalette))
w.Add(new SpriteEffect(location, w, info.EffectSequence, info.EffectPalette));
var actor = w.CreateActor(info.Actor, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner),
});
actor.QueueActivity(new Wait(info.LifeTime));
actor.QueueActivity(new RemoveSelf());
});
}
}
}
}

View File

@@ -0,0 +1,89 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
public abstract class SupportPowerInfo : ITraitInfo
{
public readonly int ChargeTime = 0;
public readonly string Icon = null;
public readonly string Description = "";
public readonly string LongDesc = "";
public readonly bool AllowMultiple = false;
public readonly bool OneShot = false;
public readonly string[] Prerequisites = { };
public readonly string BeginChargeSound = null;
public readonly string EndChargeSound = null;
public readonly string SelectTargetSound = null;
public readonly string InsufficientPowerSound = null;
public readonly string LaunchSound = null;
public readonly string IncomingSound = null;
public readonly bool DisplayTimer = false;
[Desc("Beacons are only supported on the Airstrike and Nuke powers")]
public readonly bool DisplayBeacon = false;
public readonly string BeaconPalettePrefix = "player";
public readonly string BeaconPoster = null;
public readonly string BeaconPosterPalette = "chrome";
public readonly bool DisplayRadarPing = false;
public readonly int RadarPingDuration = 5 * 25;
public readonly string OrderName;
public abstract object Create(ActorInitializer init);
public SupportPowerInfo() { OrderName = GetType().Name + "Order"; }
}
public class SupportPower
{
public readonly Actor Self;
public readonly SupportPowerInfo Info;
protected RadarPing ping;
public SupportPower(Actor self, SupportPowerInfo info)
{
Info = info;
Self = self;
}
public virtual void Charging(Actor self, string key)
{
Sound.PlayToPlayer(self.Owner, Info.BeginChargeSound);
}
public virtual void Charged(Actor self, string key)
{
Sound.PlayToPlayer(self.Owner, Info.EndChargeSound);
}
public virtual void Activate(Actor self, Order order, SupportPowerManager manager)
{
if (Info.DisplayRadarPing && manager.RadarPings != null)
{
ping = manager.RadarPings.Value.Add(
() => order.Player.IsAlliedWith(self.World.RenderPlayer),
self.World.Map.CenterOfCell(order.TargetLocation),
order.Player.Color.RGB,
Info.RadarPingDuration);
}
}
public virtual IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectGenericPowerTarget(order, manager, "ability", MouseButton.Left);
}
}
}

View File

@@ -0,0 +1,266 @@
#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 System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Attach this to the player actor.")]
public class SupportPowerManagerInfo : ITraitInfo, Requires<DeveloperModeInfo>, Requires<TechTreeInfo>
{
public object Create(ActorInitializer init) { return new SupportPowerManager(init); }
}
public class SupportPowerManager : ITick, IResolveOrder, ITechTreeElement
{
public readonly Actor Self;
public readonly Dictionary<string, SupportPowerInstance> Powers = new Dictionary<string, SupportPowerInstance>();
public readonly DeveloperMode DevMode;
public readonly TechTree TechTree;
public readonly Lazy<RadarPings> RadarPings;
public SupportPowerManager(ActorInitializer init)
{
Self = init.Self;
DevMode = Self.Trait<DeveloperMode>();
TechTree = Self.Trait<TechTree>();
RadarPings = Exts.Lazy(() => init.World.WorldActor.TraitOrDefault<RadarPings>());
init.World.ActorAdded += ActorAdded;
init.World.ActorRemoved += ActorRemoved;
}
static string MakeKey(SupportPower sp)
{
return sp.Info.AllowMultiple ? sp.Info.OrderName + "_" + sp.Self.ActorID : sp.Info.OrderName;
}
void ActorAdded(Actor a)
{
if (a.Owner != Self.Owner)
return;
foreach (var t in a.TraitsImplementing<SupportPower>())
{
var key = MakeKey(t);
if (!Powers.ContainsKey(key))
{
Powers.Add(key, new SupportPowerInstance(key, this)
{
Instances = new List<SupportPower>(),
RemainingTime = t.Info.ChargeTime * 25,
TotalTime = t.Info.ChargeTime * 25,
});
if (t.Info.Prerequisites.Any())
{
TechTree.Add(key, t.Info.Prerequisites, 0, this);
TechTree.Update();
}
}
Powers[key].Instances.Add(t);
}
}
void ActorRemoved(Actor a)
{
if (a.Owner != Self.Owner || !a.HasTrait<SupportPower>())
return;
foreach (var t in a.TraitsImplementing<SupportPower>())
{
var key = MakeKey(t);
Powers[key].Instances.Remove(t);
if (Powers[key].Instances.Count == 0 && !Powers[key].Disabled)
{
Powers.Remove(key);
TechTree.Remove(key);
TechTree.Update();
}
}
}
public void Tick(Actor self)
{
foreach (var power in Powers.Values)
power.Tick();
}
public void ResolveOrder(Actor self, Order order)
{
// order.OrderString is the key of the support power
if (Powers.ContainsKey(order.OrderString))
Powers[order.OrderString].Activate(order);
}
// Deprecated. Remove after SupportPowerBinWidget is removed.
public void Target(string key)
{
if (Powers.ContainsKey(key))
Powers[key].Target();
}
static readonly SupportPowerInstance[] NoInstances = { };
public IEnumerable<SupportPowerInstance> GetPowersForActor(Actor a)
{
if (a.Owner != Self.Owner || !a.HasTrait<SupportPower>())
return NoInstances;
return a.TraitsImplementing<SupportPower>()
.Select(t => Powers[MakeKey(t)]);
}
public void PrerequisitesAvailable(string key)
{
SupportPowerInstance sp;
if (!Powers.TryGetValue(key, out sp))
return;
sp.Disabled = false;
}
public void PrerequisitesUnavailable(string key)
{
SupportPowerInstance sp;
if (!Powers.TryGetValue(key, out sp))
return;
sp.Disabled = true;
sp.RemainingTime = sp.TotalTime;
}
public void PrerequisitesItemHidden(string key) { }
public void PrerequisitesItemVisible(string key) { }
}
public class SupportPowerInstance
{
readonly SupportPowerManager manager;
readonly string key;
public List<SupportPower> Instances;
public int RemainingTime;
public int TotalTime;
public bool Active { get; private set; }
public bool Disabled { get; set; }
public SupportPowerInfo Info { get { return Instances.Select(i => i.Info).FirstOrDefault(); } }
public bool Ready { get { return Active && RemainingTime == 0; } }
public SupportPowerInstance(string key, SupportPowerManager manager)
{
this.manager = manager;
this.key = key;
}
static bool InstanceDisabled(SupportPower sp)
{
return sp.Self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
bool notifiedCharging;
bool notifiedReady;
public void Tick()
{
Active = !Disabled && Instances.Any(i => !i.Self.IsDisabled());
if (!Active)
return;
if (Active)
{
var power = Instances.First();
if (manager.DevMode.FastCharge && RemainingTime > 25)
RemainingTime = 25;
if (RemainingTime > 0)
--RemainingTime;
if (!notifiedCharging)
{
power.Charging(power.Self, key);
notifiedCharging = true;
}
if (RemainingTime == 0
&& !notifiedReady)
{
power.Charged(power.Self, key);
notifiedReady = true;
}
}
}
public void Target()
{
if (!Ready)
return;
manager.Self.World.OrderGenerator = Instances.First().OrderGenerator(key, manager);
}
public void Activate(Order order)
{
if (!Ready)
return;
var power = Instances.First(i => !InstanceDisabled(i));
// Note: order.Subject is the *player* actor
power.Activate(power.Self, order, manager);
RemainingTime = TotalTime;
notifiedCharging = notifiedReady = false;
if (Info.OneShot)
Disabled = true;
}
}
public class SelectGenericPowerTarget : IOrderGenerator
{
readonly SupportPowerManager manager;
readonly string order;
readonly string cursor;
readonly MouseButton expectedButton;
public SelectGenericPowerTarget(string order, SupportPowerManager manager, string cursor, MouseButton button)
{
this.manager = manager;
this.order = order;
this.cursor = cursor;
expectedButton = button;
}
public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
{
world.CancelInputMode();
if (mi.Button == expectedButton && world.Map.Contains(xy))
yield return new Order(order, manager.Self, false) { TargetLocation = xy, SuppressVisualFeedback = true };
}
public virtual void Tick(World world)
{
// Cancel the OG if we can't use the power
if (!manager.Powers.ContainsKey(order))
world.CancelInputMode();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world) { yield break; }
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world) { yield break; }
public string GetCursor(World world, CPos xy, MouseInput mi) { return world.Map.Contains(xy) ? cursor : "generic-blocked"; }
}
}

View File

@@ -0,0 +1,107 @@
#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 System.Collections.Generic;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Actor becomes a specified actor type when this trait is triggered.")]
public class TransformsInfo : ITraitInfo
{
[ActorReference] public readonly string IntoActor = null;
public readonly CVec Offset = CVec.Zero;
public readonly int Facing = 96;
public readonly string[] TransformSounds = { };
public readonly string[] NoTransformSounds = { };
public virtual object Create(ActorInitializer init) { return new Transforms(init, this); }
}
public class Transforms : IIssueOrder, IResolveOrder, IOrderVoice
{
readonly Actor self;
readonly TransformsInfo info;
readonly BuildingInfo bi;
readonly string race;
public Transforms(ActorInitializer init, TransformsInfo info)
{
self = init.Self;
this.info = info;
bi = self.World.Map.Rules.Actors[info.IntoActor].Traits.GetOrDefault<BuildingInfo>();
race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : self.Owner.Country.Race;
}
public string VoicePhraseForOrder(Actor self, Order order)
{
return (order.OrderString == "DeployTransform") ? "Move" : null;
}
bool CanDeploy()
{
var b = self.TraitOrDefault<Building>();
if (b != null && b.Locked)
return false;
return bi == null || self.World.CanPlaceBuilding(info.IntoActor, bi, self.Location + info.Offset, self);
}
public IEnumerable<IOrderTargeter> Orders
{
get { yield return new DeployOrderTargeter("DeployTransform", 5, () => CanDeploy()); }
}
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if (order.OrderID == "DeployTransform")
return new Order(order.OrderID, self, queued);
return null;
}
public void DeployTransform(bool queued)
{
var b = self.TraitOrDefault<Building>();
if (!CanDeploy() || (b != null && !b.Lock()))
{
foreach (var s in info.NoTransformSounds)
Sound.PlayToPlayer(self.Owner, s);
return;
}
if (!queued)
self.CancelActivity();
if (self.HasTrait<IFacing>())
self.QueueActivity(new Turn(self, info.Facing));
foreach (var nt in self.TraitsImplementing<INotifyTransform>())
nt.BeforeTransform(self);
var transform = new Transform(self, info.IntoActor) { Offset = info.Offset, Facing = info.Facing, Sounds = info.TransformSounds, Race = race };
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, transform);
else
self.QueueActivity(transform);
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "DeployTransform")
DeployTransform(order.Queued);
}
}
}

View File

@@ -0,0 +1,32 @@
#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 System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class DisableUpgradeInfo : UpgradableTraitInfo, ITraitInfo
{
public object Create(ActorInitializer init) { return new DisableUpgrade(this); }
}
public class DisableUpgrade : UpgradableTrait<DisableUpgradeInfo>, IDisable, IDisableMove
{
public DisableUpgrade(DisableUpgradeInfo info)
: base(info) { }
// Disable the actor when this trait is enabled.
public bool Disabled { get { return !IsTraitDisabled; } }
public bool MoveDisabled(Actor self) { return !IsTraitDisabled; }
}
}

View File

@@ -0,0 +1,127 @@
#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 System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor has properties that upgrade when a specific criteria is met.")]
public class GainsStatUpgradesInfo : ITraitInfo
{
public readonly string FirepowerUpgrade = "firepower";
public readonly int[] FirepowerModifier = { 110, 115, 120, 130 };
public readonly string DamageUpgrade = "damage";
public readonly int[] DamageModifier = { 91, 87, 83, 65 };
public readonly string SpeedUpgrade = "speed";
public readonly int[] SpeedModifier = { 110, 115, 120, 150 };
public readonly string ReloadUpgrade = "reload";
public readonly int[] ReloadModifier = { 95, 90, 85, 75 };
public readonly string InaccuracyUpgrade = "inaccuracy";
public readonly int[] InaccuracyModifier = { 90, 80, 70, 50 };
public object Create(ActorInitializer init) { return new GainsStatUpgrades(this); }
}
public class GainsStatUpgrades : IUpgradable, IFirepowerModifier, IDamageModifier, ISpeedModifier, IReloadModifier, IInaccuracyModifier, IDisabledTrait
{
readonly GainsStatUpgradesInfo info;
[Sync] int firepowerLevel = 0;
[Sync] int speedLevel = 0;
[Sync] int damageLevel = 0;
[Sync] int reloadLevel = 0;
[Sync] int inaccuracyLevel = 0;
public bool IsTraitDisabled { get { return firepowerLevel == 0 && speedLevel == 0 && damageLevel == 0 && reloadLevel == 0 && inaccuracyLevel == 0; } }
public IEnumerable<string> UpgradeTypes
{
get
{
yield return info.FirepowerUpgrade;
yield return info.DamageUpgrade;
yield return info.SpeedUpgrade;
yield return info.ReloadUpgrade;
yield return info.InaccuracyUpgrade;
}
}
public GainsStatUpgrades(GainsStatUpgradesInfo info)
{
this.info = info;
}
public bool AcceptsUpgradeLevel(Actor self, string type, int level)
{
if (level < 0)
return false;
if (type == info.FirepowerUpgrade)
return level <= info.FirepowerModifier.Length;
if (type == info.DamageUpgrade)
return level <= info.DamageModifier.Length;
if (type == info.SpeedUpgrade)
return level <= info.SpeedModifier.Length;
if (type == info.ReloadUpgrade)
return level <= info.ReloadModifier.Length;
if (type == info.InaccuracyUpgrade)
return level <= info.InaccuracyModifier.Length;
return false;
}
public void UpgradeLevelChanged(Actor self, string type, int oldLevel, int newLevel)
{
if (type == info.FirepowerUpgrade)
firepowerLevel = newLevel.Clamp(0, info.FirepowerModifier.Length);
else if (type == info.DamageUpgrade)
damageLevel = newLevel.Clamp(0, info.DamageModifier.Length);
else if (type == info.SpeedUpgrade)
speedLevel = newLevel.Clamp(0, info.SpeedModifier.Length);
else if (type == info.ReloadUpgrade)
reloadLevel = newLevel.Clamp(0, info.ReloadModifier.Length);
else if (type == info.InaccuracyUpgrade)
inaccuracyLevel = newLevel.Clamp(0, info.InaccuracyModifier.Length);
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
{
return damageLevel > 0 ? info.DamageModifier[damageLevel - 1] : 100;
}
public int GetFirepowerModifier()
{
return firepowerLevel > 0 ? info.FirepowerModifier[firepowerLevel - 1] : 100;
}
public int GetSpeedModifier()
{
return speedLevel > 0 ? info.SpeedModifier[speedLevel - 1] : 100;
}
public int GetReloadModifier()
{
return reloadLevel > 0 ? info.ReloadModifier[reloadLevel - 1] : 100;
}
public int GetInaccuracyModifier()
{
return inaccuracyLevel > 0 ? info.InaccuracyModifier[inaccuracyLevel - 1] : 100;
}
}
}

View File

@@ -0,0 +1,137 @@
#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 System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Applies an upgrade to actors within a specified range.")]
public class UpgradeActorsNearInfo : ITraitInfo
{
[Desc("The upgrades to grant.")]
public readonly string[] Upgrades = { };
[Desc("The range to search for actors to upgrade.")]
public readonly WRange Range = WRange.FromCells(3);
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally;
[Desc("Grant the upgrades apply to this actor.")]
public readonly bool AffectsParent = false;
public readonly string EnableSound = null;
public readonly string DisableSound = null;
public object Create(ActorInitializer init) { return new UpgradeActorsNear(init.Self, this); }
}
public class UpgradeActorsNear : ITick, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyOtherProduction
{
readonly UpgradeActorsNearInfo info;
readonly Actor self;
int proximityTrigger;
WPos cachedPosition;
WRange cachedRange;
WRange desiredRange;
bool cachedDisabled = true;
public UpgradeActorsNear(Actor self, UpgradeActorsNearInfo info)
{
this.info = info;
this.self = self;
cachedRange = info.Range;
}
public void AddedToWorld(Actor self)
{
cachedPosition = self.CenterPosition;
proximityTrigger = self.World.ActorMap.AddProximityTrigger(cachedPosition, cachedRange, ActorEntered, ActorExited);
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveProximityTrigger(proximityTrigger);
}
public void Tick(Actor self)
{
var disabled = self.IsDisabled();
if (cachedDisabled != disabled)
{
Sound.Play(disabled ? info.DisableSound : info.EnableSound, self.CenterPosition);
desiredRange = disabled ? WRange.Zero : info.Range;
cachedDisabled = disabled;
}
if (self.CenterPosition != cachedPosition || desiredRange != cachedRange)
{
cachedPosition = self.CenterPosition;
cachedRange = desiredRange;
self.World.ActorMap.UpdateProximityTrigger(proximityTrigger, cachedPosition, cachedRange);
}
}
void ActorEntered(Actor a)
{
if (a.Destroyed)
return;
if (a == self && !info.AffectsParent)
return;
var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantUpgrade(a, u, this);
}
public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
{
// Work around for actors produced within the region not triggering until the second tick
if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.Range * info.Range.Range)
{
var stance = self.Owner.Stances[produced.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = produced.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantTimedUpgrade(produced, u, 1);
}
}
void ActorExited(Actor a)
{
if (a == self || a.Destroyed)
return;
var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.RevokeUpgrade(a, u, this);
}
}
}