Move Power out of Building and into its own trait

Conflicts:
	mods/ts/rules/structures.yaml
This commit is contained in:
ScottNZ
2014-08-02 01:38:14 +12:00
parent 9527d7c2f4
commit 692e3a9c88
23 changed files with 383 additions and 211 deletions

View File

@@ -138,8 +138,8 @@ namespace OpenRA.Mods.RA.AI
// First priority is to get out of a low power situation
if (playerPower.ExcessPower < 0)
{
var power = GetProducibleBuilding("Power", buildableThings, a => a.Traits.Get<BuildingInfo>().Power);
if (power != null && power.Traits.Get<BuildingInfo>().Power > 0)
var power = GetProducibleBuilding("Power", buildableThings, a => a.Traits.Get<PowerInfo>().Amount);
if (power != null && power.Traits.Get<PowerInfo>().Amount > 0)
{
HackyAI.BotDebug("AI: {0} decided to build {1}: Priority override (low power)", queue.Actor.Owner, power.Name);
return power;
@@ -198,12 +198,12 @@ namespace OpenRA.Mods.RA.AI
// Will this put us into low power?
var actor = world.Map.Rules.Actors[frac.Key];
var bi = actor.Traits.Get<BuildingInfo>();
if (playerPower.ExcessPower < 0 || playerPower.ExcessPower < bi.Power)
var pi = actor.Traits.GetOrDefault<PowerInfo>();
if (playerPower.ExcessPower < 0 || (pi != null && playerPower.ExcessPower < pi.Amount))
{
// Try building a power plant instead
var power = GetProducibleBuilding("Power", buildableThings, a => a.Traits.Get<BuildingInfo>().Power);
if (power != null && power.Traits.Get<BuildingInfo>().Power > 0)
var power = GetProducibleBuilding("Power", buildableThings, a => a.Traits.Get<PowerInfo>().Amount);
if (power != null && power.Traits.Get<PowerInfo>().Amount > 0)
{
HackyAI.BotDebug("{0} decided to build {1}: Priority override (would be low power)", queue.Actor.Owner, power.Name);
return power;

View File

@@ -23,8 +23,6 @@ namespace OpenRA.Mods.RA.Buildings
public class BuildingInfo : ITraitInfo, IOccupySpaceInfo, UsesInit<LocationInit>
{
[Desc("If negative, it will drain power, if positive, it will provide power.")]
public readonly int Power = 0;
[Desc("Where you are allowed to place the building (Water, Clear, ...)")]
public readonly string[] TerrainTypes = {};
[Desc("The range to the next building it can be constructed. Set it higher for walls.")]
@@ -101,7 +99,7 @@ namespace OpenRA.Mods.RA.Buildings
}
}
public class Building : INotifyDamage, IOccupySpace, INotifyCapture, INotifySold, INotifyTransform, ISync, ITechTreePrerequisite, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
public class Building : IOccupySpace, INotifySold, INotifyTransform, ISync, ITechTreePrerequisite, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
{
public readonly BuildingInfo Info;
public bool BuildComplete { get; private set; }
@@ -109,8 +107,6 @@ namespace OpenRA.Mods.RA.Buildings
readonly Actor self;
public readonly bool SkipMakeAnimation;
PowerManager PlayerPower;
/* shared activity lock: undeploy, sell, capture, etc */
[Sync] public bool Locked = true;
@@ -135,7 +131,6 @@ namespace OpenRA.Mods.RA.Buildings
this.self = init.self;
this.topLeft = init.Get<LocationInit, CPos>();
this.Info = info;
this.PlayerPower = init.self.Owner.PlayerActor.Trait<PowerManager>();
occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft )
.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();
@@ -144,30 +139,9 @@ namespace OpenRA.Mods.RA.Buildings
SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>();
}
public int GetPowerUsage()
{
if (Info.Power <= 0)
return Info.Power;
var health = self.TraitOrDefault<Health>();
return health != null ? (Info.Power * health.HP / health.MaxHP) : Info.Power;
}
public void Damaged(Actor self, AttackInfo e)
{
// Power plants lose power with damage
if (Info.Power > 0)
PlayerPower.UpdateActor(self, GetPowerUsage());
}
Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PlayerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
public void Created(Actor self)
{
if (SkipMakeAnimation || !self.HasTrait<WithMakeAnimation>())

View File

@@ -21,11 +21,11 @@ namespace OpenRA.Mods.RA.Buildings
public class PowerManager : ITick, ISync
{
PowerManagerInfo Info;
Player Player;
DeveloperMode devMode;
readonly PowerManagerInfo info;
readonly Player player;
readonly DeveloperMode devMode;
Dictionary<Actor, int> PowerDrain = new Dictionary<Actor, int>();
readonly Dictionary<Actor, int> powerDrain = new Dictionary<Actor, int>();
[Sync] int totalProvided;
public int PowerProvided { get { return totalProvided; } }
@@ -36,8 +36,8 @@ namespace OpenRA.Mods.RA.Buildings
public PowerManager(ActorInitializer init, PowerManagerInfo info)
{
Info = info;
Player = init.self.Owner;
this.info = info;
player = init.self.Owner;
init.world.ActorAdded += ActorAdded;
init.world.ActorRemoved += ActorRemoved;
@@ -48,17 +48,22 @@ namespace OpenRA.Mods.RA.Buildings
void ActorAdded(Actor a)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player)
return;
PowerDrain.Add(a, a.Trait<Building>().GetPowerUsage());
var power = a.TraitOrDefault<Power>();
if (power == null)
return;
powerDrain.Add(a, power.CurrentPower);
UpdateTotals();
}
void ActorRemoved(Actor a)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player || !a.HasTrait<Power>())
return;
PowerDrain.Remove(a);
powerDrain.Remove(a);
UpdateTotals();
}
@@ -66,7 +71,7 @@ namespace OpenRA.Mods.RA.Buildings
{
totalProvided = 0;
totalDrained = 0;
foreach (var kv in PowerDrain)
foreach (var kv in powerDrain)
{
var p = kv.Value;
if (p > 0)
@@ -81,10 +86,10 @@ namespace OpenRA.Mods.RA.Buildings
public void UpdateActor(Actor a, int newPower)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player || !a.HasTrait<Power>())
return;
PowerDrain[a] = newPower;
powerDrain[a] = newPower;
UpdateTotals();
}
@@ -109,7 +114,7 @@ namespace OpenRA.Mods.RA.Buildings
{
if (lowPower)
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", "LowPower", self.Owner.Country.Race);
nextPowerAdviceTime = Info.AdviceInterval;
nextPowerAdviceTime = info.AdviceInterval;
}
}

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Buildings
{
[Desc("The player can disable the power individually on this actor.")]
public class CanPowerDownInfo : ITraitInfo
public class CanPowerDownInfo : ITraitInfo, Requires<PowerInfo>
{
public object Create(ActorInitializer init) { return new CanPowerDown(init); }
}
@@ -28,7 +28,7 @@ namespace OpenRA.Mods.RA.Buildings
public CanPowerDown(ActorInitializer init)
{
PowerManager = init.self.Owner.PlayerActor.Trait<PowerManager>();
normalPower = init.self.Info.Traits.Get<BuildingInfo>().Power;
normalPower = init.self.Info.Traits.Get<PowerInfo>().Amount;
}
public bool Disabled { get { return disabled; } }

View File

@@ -118,6 +118,7 @@
<Compile Include="Air\AttackHeli.cs" />
<Compile Include="Air\AttackPlane.cs" />
<Compile Include="Crates\DuplicateUnitCrateAction.cs" />
<Compile Include="Power.cs" />
<Compile Include="Effects\Beacon.cs" />
<Compile Include="Player\PlaceBeacon.cs" />
<Compile Include="MenuPaletteEffect.cs" />
@@ -164,7 +165,7 @@
<Compile Include="CaptureNotification.cs" />
<Compile Include="Buildings\Building.cs" />
<Compile Include="Buildings\BuildingInfluence.cs" />
<Compile Include="Buildings\CanPowerDown.cs" />
<Compile Include="CanPowerDown.cs" />
<Compile Include="Buildings\CustomSellValue.cs" />
<Compile Include="Buildings\CustomBuildTimeValue.cs" />
<Compile Include="Buildings\DeadBuildingState.cs" />
@@ -588,7 +589,5 @@ cd "$(SolutionDir)thirdparty/"
copy "FuzzyLogicLibrary.dll" "$(SolutionDir)"
cd "$(SolutionDir)"</PostBuildEvent>
</PropertyGroup>
<ItemGroup>
<Folder Include="Widgets\Logic\Ingame\" />
</ItemGroup>
<ItemGroup />
</Project>

63
OpenRA.Mods.RA/Power.cs Normal file
View File

@@ -0,0 +1,63 @@
#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 OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class PowerInfo : ITraitInfo
{
[Desc("If negative, it will drain power. If positive, it will provide power.")]
public readonly int Amount = 0;
[Desc("Scale power amount with the current health.")]
public readonly bool ScaleWithHealth = false;
public object Create(ActorInitializer init) { return new Power(init.self, this); }
}
public class Power : INotifyDamage, INotifyCapture
{
readonly PowerInfo info;
readonly Lazy<Health> health;
PowerManager playerPower;
public int CurrentPower
{
get
{
if (info.Amount <= 0 || health == null || !info.ScaleWithHealth)
return info.Amount;
return info.Amount * health.Value.HP / health.Value.MaxHP;
}
}
public Power(Actor self, PowerInfo info)
{
this.info = info;
health = Exts.Lazy(self.TraitOrDefault<Health>);
playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
}
public void Damaged(Actor self, AttackInfo e)
{
if (info.ScaleWithHealth)
playerPower.UpdateActor(self, CurrentPower);
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
playerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}

View File

@@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var tooltip = info.Traits.Get<TooltipInfo>();
var buildable = info.Traits.Get<BuildableInfo>();
var cost = info.Traits.Get<ValuedInfo>().Cost;
var bi = info.Traits.GetOrDefault<BuildingInfo>();
var pi = info.Traits.GetOrDefault<PowerInfo>();
nameLabel.GetText = () => tooltip.Name;
@@ -72,7 +72,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var requiresString = prereqs.Any() ? requiresLabel.Text.F(prereqs.JoinWith(", ")) : "";
requiresLabel.GetText = () => requiresString;
var power = bi != null ? bi.Power : 0;
var power = pi != null ? pi.Amount : 0;
var powerString = power.ToString();
powerLabel.GetText = () => powerString;
powerLabel.GetColor = () => ((pm.PowerProvided - pm.PowerDrained) >= -power || power > 0)