Migrate lua upgrades to conditions.

This commit is contained in:
Paul Chote
2016-12-02 13:35:10 +00:00
parent 37394b6269
commit ea7ad2e337
8 changed files with 108 additions and 119 deletions

View File

@@ -201,7 +201,6 @@
<Compile Include="Pathfinder\CellInfo.cs" />
<Compile Include="PlayerExtensions.cs" />
<Compile Include="Scripting\Properties\AircraftProperties.cs" />
<Compile Include="Scripting\ScriptUpgradesCache.cs" />
<Compile Include="Scripting\Properties\CaptureProperties.cs" />
<Compile Include="ServerTraits\LobbyCommands.cs" />
<Compile Include="ServerTraits\LobbySettingsNotification.cs" />
@@ -245,7 +244,7 @@
<Compile Include="Scripting\Properties\SellableProperties.cs" />
<Compile Include="Scripting\Properties\TransformProperties.cs" />
<Compile Include="Scripting\Properties\TransportProperties.cs" />
<Compile Include="Scripting\Properties\UpgradeProperties.cs" />
<Compile Include="Scripting\Properties\ConditionProperties.cs" />
<Compile Include="TraitsInterfaces.cs" />
<Compile Include="Traits\AcceptsSupplies.cs" />
<Compile Include="Traits\Air\Aircraft.cs" />

View File

@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Scripting
{
[ScriptPropertyGroup("General")]
public class ConditionProperties : ScriptActorProperties, Requires<UpgradeManagerInfo>
{
readonly UpgradeManager um;
readonly Dictionary<string, Stack<int>> legacyShim = new Dictionary<string, Stack<int>>();
public ConditionProperties(ScriptContext context, Actor self)
: base(context, self)
{
um = self.Trait<UpgradeManager>();
}
[Desc("Grant an external condition on this actor and return the revocation token.",
"Conditions must be defined on an ExternalConditions trait on the actor.",
"If duration > 0 the condition will be automatically revoked after the defined number of ticks")]
public int GrantCondition(string condition, int duration = 0)
{
if (!um.AcceptsExternalCondition(Self, condition))
throw new InvalidDataException("Condition `{0}` has not been listed on an ExternalConditions trait".F(condition));
return um.GrantCondition(Self, condition, true, duration);
}
[Desc("Revoke a condition using the token returned by GrantCondition.")]
public void RevokeCondition(int token)
{
um.RevokeCondition(Self, token);
}
[Desc("Check whether this actor accepts a specific external condition.")]
public bool AcceptsCondition(string condition)
{
return um.AcceptsExternalCondition(Self, condition);
}
[Desc("Grant an upgrade to this actor. DEPRECATED! Will be removed.")]
public void GrantUpgrade(string upgrade)
{
Game.Debug("GrantUpgrade is deprecated. Use GrantCondition instead.");
legacyShim.GetOrAdd(upgrade).Push(GrantCondition(upgrade));
}
[Desc("Revoke an upgrade that was previously granted using GrantUpgrade. DEPRECATED! Will be removed.")]
public void RevokeUpgrade(string upgrade)
{
Game.Debug("RevokeUpgrade is deprecated. Use RevokeCondition instead.");
Stack<int> tokens;
if (!legacyShim.TryGetValue(upgrade, out tokens) || !tokens.Any())
throw new InvalidDataException("Attempting to revoke upgrade `{0}` that has not been granted.".F(upgrade));
RevokeCondition(tokens.Pop());
}
[Desc("Grant a limited-time upgrade to this actor. DEPRECATED! Will be removed.")]
public void GrantTimedUpgrade(string upgrade, int duration)
{
Game.Debug("GrantTimedUpgrade is deprecated. Use GrantCondition instead.");
GrantCondition(upgrade, duration);
}
[Desc("Check whether this actor accepts a specific upgrade. DEPRECATED! Will be removed.")]
public bool AcceptsUpgrade(string upgrade)
{
Game.Debug("AcceptsUpgrade is deprecated. Use AcceptsCondition instead.");
return AcceptsCondition(upgrade);
}
}
}

View File

@@ -1,75 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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 System;
using System.IO;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Scripting
{
[ScriptPropertyGroup("General")]
public class UpgradeProperties : ScriptActorProperties, Requires<UpgradeManagerInfo>
{
readonly UpgradeManager um;
readonly ScriptUpgradesCache validUpgrades;
public UpgradeProperties(ScriptContext context, Actor self)
: base(context, self)
{
um = self.Trait<UpgradeManager>();
validUpgrades = self.World.WorldActor.TraitOrDefault<ScriptUpgradesCache>();
}
[Desc("Grant an upgrade to this actor.")]
public void GrantUpgrade(string upgrade)
{
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.GrantUpgrade(Self, upgrade, this);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Revoke an upgrade that was previously granted using GrantUpgrade.")]
public void RevokeUpgrade(string upgrade)
{
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.RevokeUpgrade(Self, upgrade, this);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Grant a limited-time upgrade to this actor.")]
public void GrantTimedUpgrade(string upgrade, int duration)
{
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.GrantTimedUpgrade(Self, upgrade, duration);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Check whether this actor accepts a specific upgrade.")]
public bool AcceptsUpgrade(string upgrade)
{
return validUpgrades != null && validUpgrades.Info.Upgrades.Contains(upgrade) && um.AcceptsUpgrade(Self, upgrade);
}
}
}

View File

@@ -1,36 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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 System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Scripting
{
[Desc("Allows granting upgrades to actors from Lua scripts.")]
public class ScriptUpgradesCacheInfo : ITraitInfo
{
[UpgradeGrantedReference]
[Desc("Upgrades that can be granted from the scripts.")]
public readonly HashSet<string> Upgrades = new HashSet<string>();
public object Create(ActorInitializer init) { return new ScriptUpgradesCache(this); }
}
public sealed class ScriptUpgradesCache
{
public readonly ScriptUpgradesCacheInfo Info;
public ScriptUpgradesCache(ScriptUpgradesCacheInfo info)
{
Info = info;
}
}
}

View File

@@ -555,6 +555,18 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
if (engineVersion < 20161210)
{
// Migrated lua upgrades to conditions
if (node.Key.StartsWith("ScriptUpgradesCache", StringComparison.Ordinal))
{
RenameNodeKey(node, "ExternalConditions");
var conditions = node.Value.Nodes.FirstOrDefault(n => n.Key == "Upgrades");
if (conditions != null)
conditions.Key = "Conditions";
}
}
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);
}

View File

@@ -11,8 +11,8 @@ World:
ValuePerUnit: 0
LuaScript:
Scripts: desert-shellmap.lua
ScriptUpgradesCache:
Upgrades: unkillable
ExternalConditions:
Conditions: unkillable
-StartGameNotification:
^Vehicle:

View File

@@ -28,8 +28,8 @@ World:
Type: LightningStrike
LuaScript:
Scripts: fort-lonestar.lua, fort-lonestar-AI.lua
ScriptUpgradesCache:
Upgrades: invulnerability
ExternalConditions:
Conditions: invulnerability
MapBuildRadius:
AllyBuildRadiusLocked: True
AllyBuildRadiusEnabled: True

View File

@@ -1436,8 +1436,8 @@ Rules:
GlobalLightingPaletteEffect:
Blue: 0.7
Ambient: 0.7
ScriptUpgradesCache:
Upgrades: unkillable
ExternalConditions:
Conditions: unkillable
HARV:
-Targetable:
GALITE: