Add ScriptUpgradesCache class to enable maps to explicitly declare what upgrades their Lua scripts will use

This commit is contained in:
penev92
2015-05-27 17:00:33 +03:00
parent f1ec71b17d
commit 2ab7abcee4
5 changed files with 67 additions and 5 deletions

View File

@@ -199,6 +199,7 @@
<Compile Include="Pathfinder\CellInfo.cs" />
<Compile Include="PlayerExtensions.cs" />
<Compile Include="Scripting\Global\FacingGlobal.cs" />
<Compile Include="Scripting\ScriptUpgradesCache.cs" />
<Compile Include="Scripting\Global\HSLColorGlobal.cs" />
<Compile Include="Scripting\Global\UserInterfaceGlobal.cs" />
<Compile Include="ServerTraits\ColorValidator.cs" />

View File

@@ -8,6 +8,9 @@
*/
#endregion
using System;
using System.IO;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -17,35 +20,56 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptPropertyGroup("General")]
public class UpgradeProperties : ScriptActorProperties, Requires<UpgradeManagerInfo>
{
UpgradeManager um;
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)
{
um.GrantUpgrade(Self, upgrade, this);
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)
{
um.RevokeUpgrade(Self, upgrade, this);
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)
{
um.GrantTimedUpgrade(Self, upgrade, 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 um.AcceptsUpgrade(Self, upgrade);
return validUpgrades != null && validUpgrades.Info.Upgrades.Contains(upgrade) && um.AcceptsUpgrade(Self, upgrade);
}
}
}

View File

@@ -0,0 +1,33 @@
#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.Scripting
{
[Desc("Allows granting upgrades to actors from Lua scripts.")]
public class ScriptUpgradesCacheInfo : ITraitInfo
{
[Desc("Upgrades that can be granted from the scripts.")]
public readonly string[] Upgrades = { };
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

@@ -1259,6 +1259,8 @@ Rules:
ValuePerUnit: 0
LuaScript:
Scripts: desert-shellmap.lua
ScriptUpgradesCache:
Upgrades: unkillable
-StartGameNotification:
^Vehicle:
GivesBounty:

View File

@@ -488,6 +488,8 @@ Rules:
-MPStartLocations:
LuaScript:
Scripts: fort-lonestar.lua
ScriptUpgradesCache:
Upgrades: unkillable
FORTCRATE:
Inherits: ^Crate
SupportPowerCrateAction@parabombs: