Added ConditionExpression Requirements for plugs to Pluggable

This commit is contained in:
atlimit8
2017-02-10 21:30:44 -06:00
parent d86ff6696a
commit daf4eb812c

View File

@@ -10,6 +10,8 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -20,22 +22,36 @@ namespace OpenRA.Mods.Common.Traits
public readonly CVec Offset = CVec.Zero;
[FieldLoader.Require]
[Desc("Conditions to grant for each accepted plug type.")]
[Desc("Conditions to grant for each accepted plug type.",
"Key is the plug type.",
"Value is the condition that is granted when the plug is enabled.")]
public readonly Dictionary<string, string> Conditions = null;
[Desc("Requirements for accepting a plug type.",
"Key is the plug type that the requirements applies to.",
"Value is the condition expression defining the requirements to place the plug.")]
public readonly Dictionary<string, ConditionExpression> Requirements = new Dictionary<string, ConditionExpression>();
[GrantedConditionReference]
public IEnumerable<string> LinterConditions { get { return Conditions.Values; } }
[ConsumedConditionReference]
public IEnumerable<string> ConsumedConditions
{
get { return Requirements.Values.SelectMany(r => r.Variables).Distinct(); }
}
public object Create(ActorInitializer init) { return new Pluggable(init, this); }
}
public class Pluggable : INotifyCreated
public class Pluggable : IConditionConsumer, INotifyCreated
{
public readonly PluggableInfo Info;
readonly string initialPlug;
ConditionManager conditionManager;
int conditionToken = ConditionManager.InvalidConditionToken;
Dictionary<string, bool> plugTypesAvailability = null;
string active;
@@ -46,6 +62,13 @@ namespace OpenRA.Mods.Common.Traits
var plugInit = init.Contains<PlugsInit>() ? init.Get<PlugsInit, Dictionary<CVec, string>>() : new Dictionary<CVec, string>();
if (plugInit.ContainsKey(Info.Offset))
initialPlug = plugInit[Info.Offset];
if (info.Requirements.Count > 0)
{
plugTypesAvailability = new Dictionary<string, bool>();
foreach (var plug in info.Requirements)
plugTypesAvailability[plug.Key] = true;
}
}
public void Created(Actor self)
@@ -58,7 +81,16 @@ namespace OpenRA.Mods.Common.Traits
public bool AcceptsPlug(Actor self, string type)
{
return active == null && Info.Conditions.ContainsKey(type);
if (active != null)
return false;
if (!Info.Conditions.ContainsKey(type))
return false;
if (!Info.Requirements.ContainsKey(type))
return true;
return plugTypesAvailability[type];
}
public void EnablePlug(Actor self, string type)
@@ -81,6 +113,14 @@ namespace OpenRA.Mods.Common.Traits
active = null;
}
IEnumerable<string> IConditionConsumer.Conditions { get { return Info.ConsumedConditions; } }
void IConditionConsumer.ConditionsChanged(Actor self, IReadOnlyDictionary<string, int> conditions)
{
foreach (var req in Info.Requirements)
plugTypesAvailability[req.Key] = req.Value.Evaluate(conditions) != 0;
}
}
public class PlugsInit : IActorInit<Dictionary<CVec, string>>