From daf4eb812c7cc9a80227273cb97e85c6c5ba4548 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Fri, 10 Feb 2017 21:30:44 -0600 Subject: [PATCH] Added ConditionExpression Requirements for plugs to Pluggable --- OpenRA.Mods.Common/Traits/Pluggable.cs | 46 ++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Pluggable.cs b/OpenRA.Mods.Common/Traits/Pluggable.cs index f68f467751..5e5c022d99 100644 --- a/OpenRA.Mods.Common/Traits/Pluggable.cs +++ b/OpenRA.Mods.Common/Traits/Pluggable.cs @@ -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 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 Requirements = new Dictionary(); + [GrantedConditionReference] public IEnumerable LinterConditions { get { return Conditions.Values; } } + [ConsumedConditionReference] + public IEnumerable 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 plugTypesAvailability = null; string active; @@ -46,6 +62,13 @@ namespace OpenRA.Mods.Common.Traits var plugInit = init.Contains() ? init.Get>() : new Dictionary(); if (plugInit.ContainsKey(Info.Offset)) initialPlug = plugInit[Info.Offset]; + + if (info.Requirements.Count > 0) + { + plugTypesAvailability = new Dictionary(); + 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 IConditionConsumer.Conditions { get { return Info.ConsumedConditions; } } + + void IConditionConsumer.ConditionsChanged(Actor self, IReadOnlyDictionary conditions) + { + foreach (var req in Info.Requirements) + plugTypesAvailability[req.Key] = req.Value.Evaluate(conditions) != 0; + } } public class PlugsInit : IActorInit>