Migrate ProvidesRadar to conditions and remove JamsRadar.

This commit is contained in:
Paul Chote
2017-01-21 11:08:46 +00:00
parent bb972fa46d
commit 1f44e91746
15 changed files with 130 additions and 78 deletions

View File

@@ -752,7 +752,6 @@
<Compile Include="Traits\Radar\AppearsOnRadar.cs" />
<Compile Include="Traits\Radar\ProvidesRadar.cs" />
<Compile Include="Traits\Radar\RadarColorFromTerrain.cs" />
<Compile Include="Traits\Radar\JamsRadar.cs" />
<Compile Include="AI\DummyAI.cs" />
<Compile Include="UtilityCommands\ListInstallShieldContentsCommand.cs" />
<Compile Include="Widgets\Logic\Installation\ModContentLogic.cs" />
@@ -792,6 +791,7 @@
<Compile Include="UtilityCommands\OutputResolvedSequencesCommand.cs" />
<Compile Include="UtilityCommands\OutputResolvedWeaponsCommand.cs" />
<Compile Include="Traits\RevealOnFire.cs" />
<Compile Include="Traits\Conditions\GrantConditionOnDisabled.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Applies a condition to the actor when it is disabled.",
"This is a temporary shim to help migration away from the legacy IDisable code")]
public class GrantConditionOnDisabledInfo : ITraitInfo, Requires<HealthInfo>
{
[FieldLoader.Require]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;
public object Create(ActorInitializer init) { return new GrantConditionOnDisabled(this); }
}
public class GrantConditionOnDisabled : INotifyCreated, ITick
{
readonly GrantConditionOnDisabledInfo info;
ConditionManager conditionManager;
int conditionToken = ConditionManager.InvalidConditionToken;
public GrantConditionOnDisabled(GrantConditionOnDisabledInfo info)
{
this.info = info;
}
void INotifyCreated.Created(Actor self)
{
conditionManager = self.TraitOrDefault<ConditionManager>();
// Set initial disabled state
Tick(self);
}
public void Tick(Actor self)
{
if (conditionManager == null)
return;
var disabled = self.IsDisabled();
if (disabled && conditionToken == ConditionManager.InvalidConditionToken)
conditionToken = conditionManager.GrantCondition(self, info.Condition);
else if (!disabled && conditionToken != ConditionManager.InvalidConditionToken)
conditionToken = conditionManager.RevokeCondition(self, conditionToken);
}
}
}

View File

@@ -1,37 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Radar
{
[Desc("When an actor with this trait is in range of an actor with ProvidesRadar, it will temporarily disable the radar minimap for the enemy player.")]
public class JamsRadarInfo : ITraitInfo
{
[Desc("Range for jamming.")]
public readonly WDist Range = WDist.Zero;
[Desc("Which diplomatic stances are affected.")]
public readonly Stance Stances = Stance.Enemy | Stance.Neutral;
public object Create(ActorInitializer init) { return new JamsRadar(this); }
}
public class JamsRadar
{
public readonly JamsRadarInfo Info;
public JamsRadar(JamsRadarInfo info)
{
Info = info;
}
}
}

View File

@@ -15,21 +15,14 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Radar
{
[Desc("This actor enables the radar minimap.")]
public class ProvidesRadarInfo : TraitInfo<ProvidesRadar> { }
public class ProvidesRadar : ITick
public class ProvidesRadarInfo : ConditionalTraitInfo
{
public bool IsActive { get; private set; }
public override object Create(ActorInitializer init) { return new ProvidesRadar(this); }
}
public void Tick(Actor self) { IsActive = UpdateActive(self); }
static bool UpdateActive(Actor self)
{
// Check if powered
if (self.IsDisabled()) return false;
return self.World.ActorsWithTrait<JamsRadar>().All(a => !a.Trait.Info.Stances.HasStance(a.Actor.Owner.Stances[self.Owner])
|| (self.CenterPosition - a.Actor.CenterPosition).HorizontalLengthSquared > a.Trait.Info.Range.LengthSquared);
}
public class ProvidesRadar : ConditionalTrait<ProvidesRadarInfo>
{
public ProvidesRadar(ProvidesRadarInfo info)
: base(info) { }
}
}

View File

@@ -746,6 +746,36 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
if (engineVersion < 20170121)
{
if (node.Key.StartsWith("ProvidesRadar", StringComparison.Ordinal))
{
if (node.Value.Nodes.Any(n => n.Key == "RequiresCondition"))
Console.WriteLine("You must manually add the `disabled` condition to the ProvidesRadar RequiresCondition expression");
else
node.Value.Nodes.Add(new MiniYamlNode("RequiresCondition", "!disabled"));
if (!parent.Value.Nodes.Any(n => n.Key == "GrantConditionOnDisabled@IDISABLE"))
addNodes.Add(new MiniYamlNode("GrantConditionOnDisabled@IDISABLE", new MiniYaml("",
new List<MiniYamlNode>() { new MiniYamlNode("Condition", "disabled") })));
}
if (node.Key.StartsWith("JamsRadar", StringComparison.Ordinal))
{
Console.WriteLine("JamsRadar has been replaced with trait conditions.");
Console.WriteLine("You must manually add the `jammed` condition to the ProvidesRadar traits that you want to be affected.");
Console.WriteLine("You must manually add a WithRangeCircle trait to render the radar jamming range.");
RenameNodeKey(node, "ProximityExternalCondition@JAMMER");
var stances = node.Value.Nodes.FirstOrDefault(n => n.Key == "Stances");
if (stances != null)
stances.Key = "ValidStances";
else
node.Value.Nodes.Add(new MiniYamlNode("ValidStances", "Enemy, Neutral"));
node.Value.Nodes.Add(new MiniYamlNode("Condition", "jammed"));
}
}
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);
}

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var ticker = widget.Get<LogicTickerWidget>("RADAR_TICKER");
ticker.OnTick = () =>
{
radarEnabled = devMode.DisableShroud || world.ActorsHavingTrait<ProvidesRadar>(r => r.IsActive)
radarEnabled = devMode.DisableShroud || world.ActorsHavingTrait<ProvidesRadar>(r => !r.IsTraitDisabled)
.Any(a => a.Owner == world.LocalPlayer);
if (radarEnabled != cachedRadarEnabled)