Refactor more power and infiltration stuff. Create new power sabotage ability for spies in ra and ts.

This commit is contained in:
ScottNZ
2014-08-07 00:52:23 +12:00
parent 4fa199fb10
commit b70395e27c
31 changed files with 292 additions and 109 deletions

View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Traits;
namespace OpenRA.Mods.RA.Power
{
public class AffectedByPowerOutageInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new AffectedByPowerOutage(init.self); }
}
public class AffectedByPowerOutage : INotifyCapture, ISelectionBar, IPowerModifier, IDisable
{
PowerManager playerPower;
public AffectedByPowerOutage(Actor self)
{
playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
}
public float GetValue()
{
if (playerPower.PowerOutageRemainingTicks <= 0)
return 0;
return (float)playerPower.PowerOutageRemainingTicks / playerPower.PowerOutageTotalTicks;
}
public Color GetColor()
{
return Color.Yellow;
}
public int GetPowerModifier()
{
return playerPower.PowerOutageRemainingTicks > 0 ? 0 : 100;
}
public bool Disabled
{
get { return playerPower.PowerOutageRemainingTicks > 0; }
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
playerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}

View File

@@ -0,0 +1,52 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Power
{
[Desc("The player can disable the power individually on this actor.")]
public class CanPowerDownInfo : ITraitInfo, Requires<PowerInfo>
{
public object Create(ActorInitializer init) { return new CanPowerDown(init.self); }
}
public class CanPowerDown : IPowerModifier, IResolveOrder, IDisable, ISync
{
[Sync] bool disabled = false;
readonly Power power;
public CanPowerDown(Actor self)
{
power = self.Trait<Power>();
}
public bool Disabled { get { return disabled; } }
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PowerDown")
{
disabled = !disabled;
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", disabled ? "EnablePower" : "DisablePower", self.Owner.Country.Race);
power.PlayerPower.UpdateActor(self);
if (disabled)
self.World.AddFrameEndTask(w => w.Add(new PowerdownIndicator(self)));
}
}
public int GetPowerModifier()
{
return disabled ? 0 : 100;
}
}
}

View File

@@ -0,0 +1,154 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Power
{
public class PowerManagerInfo : ITraitInfo, Requires<DeveloperModeInfo>
{
public readonly int AdviceInterval = 250;
public object Create(ActorInitializer init) { return new PowerManager(init.self, this); }
}
public class PowerManager : ITick, ISync
{
readonly Actor self;
readonly PowerManagerInfo info;
readonly DeveloperMode devMode;
readonly Dictionary<Actor, int> powerDrain = new Dictionary<Actor, int>();
[Sync] int totalProvided;
public int PowerProvided { get { return totalProvided; } }
[Sync] int totalDrained;
public int PowerDrained { get { return totalDrained; } }
public int ExcessPower { get { return totalProvided - totalDrained; } }
public int PowerOutageRemainingTicks { get; private set; }
public int PowerOutageTotalTicks { get; private set; }
public PowerManager(Actor self, PowerManagerInfo info)
{
this.self = self;
this.info = info;
self.World.ActorAdded += UpdateActor;
self.World.ActorRemoved += RemoveActor;
devMode = self.Trait<DeveloperMode>();
wasHackEnabled = devMode.UnlimitedPower;
}
public void UpdateActor(Actor a)
{
UpdateActors(new[] { a });
}
public void UpdateActors(IEnumerable<Actor> actors)
{
foreach (var a in actors)
{
if (a.Owner != self.Owner)
return;
var power = a.TraitOrDefault<Power>();
if (power == null)
return;
powerDrain[a] = power.GetCurrentPower();
}
UpdateTotals();
}
void RemoveActor(Actor a)
{
if (a.Owner != self.Owner || !a.HasTrait<Power>())
return;
powerDrain.Remove(a);
UpdateTotals();
}
public void UpdateTotals()
{
totalProvided = 0;
totalDrained = 0;
foreach (var kv in powerDrain)
{
var p = kv.Value;
if (p > 0)
totalProvided += p;
else
totalDrained -= p;
}
if (devMode.UnlimitedPower)
totalProvided = 1000000;
}
int nextPowerAdviceTime = 0;
bool wasLowPower = false;
bool wasHackEnabled;
public void Tick(Actor self)
{
if (wasHackEnabled != devMode.UnlimitedPower)
{
UpdateTotals();
wasHackEnabled = devMode.UnlimitedPower;
}
var lowPower = totalProvided < totalDrained;
if (lowPower && !wasLowPower)
nextPowerAdviceTime = 0;
wasLowPower = lowPower;
if (--nextPowerAdviceTime <= 0)
{
if (lowPower)
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", "LowPower", self.Owner.Country.Race);
nextPowerAdviceTime = info.AdviceInterval;
}
if (PowerOutageRemainingTicks > 0 && --PowerOutageRemainingTicks == 0)
UpdatePowerOutageActors();
}
public PowerState PowerState
{
get
{
if (PowerProvided >= PowerDrained) return PowerState.Normal;
if (PowerProvided > PowerDrained / 2) return PowerState.Low;
return PowerState.Critical;
}
}
public void TriggerPowerOutage(int totalTicks)
{
PowerOutageTotalTicks = PowerOutageRemainingTicks = totalTicks;
UpdatePowerOutageActors();
}
void UpdatePowerOutageActors()
{
var actors = self.World.ActorsWithTrait<AffectedByPowerOutage>()
.Select(tp => tp.Actor)
.Where(a => !a.IsDead() && a.IsInWorld && a.Owner == self.Owner);
UpdateActors(actors);
}
}
}

View File

@@ -0,0 +1,49 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Power
{
public class PowerInfo : ITraitInfo
{
[Desc("If negative, it will drain power. If positive, it will provide power.")]
public readonly int Amount = 0;
public object Create(ActorInitializer init) { return new Power(init.self, this); }
}
public class Power : INotifyCapture
{
readonly PowerInfo info;
readonly Lazy<IPowerModifier[]> powerModifiers;
public PowerManager PlayerPower { get; private set; }
public int GetCurrentPower()
{
return Util.ApplyPercentageModifiers(info.Amount, powerModifiers.Value.Select(m => m.GetPowerModifier()));
}
public Power(Actor self, PowerInfo info)
{
this.info = info;
PlayerPower = self.Owner.PlayerActor.Trait<PowerManager>();
powerModifiers = Exts.Lazy(() => self.TraitsImplementing<IPowerModifier>().ToArray());
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PlayerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}

View File

@@ -0,0 +1,39 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.RA.Power
{
class RequiresPowerInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new RequiresPower(init.self); }
}
class RequiresPower : IDisable, INotifyCapture
{
PowerManager playerPower;
public RequiresPower(Actor self)
{
playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
}
public bool Disabled
{
get { return playerPower.PowerProvided < playerPower.PowerDrained; }
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
playerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}

View File

@@ -0,0 +1,42 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.RA.Power
{
[Desc("Scale power amount with the current health.")]
public class ScalePowerWithHealthInfo : ITraitInfo, Requires<PowerInfo>, Requires<HealthInfo>
{
public object Create(ActorInitializer init) { return new ScalePowerWithHealth(init.self); }
}
public class ScalePowerWithHealth : IPowerModifier, INotifyDamage
{
readonly Power power;
readonly Health health;
public ScalePowerWithHealth(Actor self)
{
power = self.Trait<Power>();
health = self.Trait<Health>();
}
public int GetPowerModifier()
{
return 100 * health.HP / health.MaxHP;
}
public void Damaged(Actor self, AttackInfo e)
{
power.PlayerPower.UpdateActor(self);
}
}
}