Add Lua API support for actors with multiple Production traits.

This commit is contained in:
abcdefg30
2018-11-03 14:58:59 +00:00
committed by abcdefg30
parent c0ee199ad1
commit cde18221e6

View File

@@ -24,12 +24,12 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptPropertyGroup("Production")] [ScriptPropertyGroup("Production")]
public class ProductionProperties : ScriptActorProperties, Requires<ProductionInfo> public class ProductionProperties : ScriptActorProperties, Requires<ProductionInfo>
{ {
readonly Production p; readonly Production[] productionTraits;
public ProductionProperties(ScriptContext context, Actor self) public ProductionProperties(ScriptContext context, Actor self)
: base(context, self) : base(context, self)
{ {
p = self.Trait<Production>(); productionTraits = self.TraitsImplementing<Production>().ToArray();
} }
[ScriptActorPropertyActivity] [ScriptActorPropertyActivity]
@@ -41,14 +41,27 @@ namespace OpenRA.Mods.Common.Scripting
if (!Self.World.Map.Rules.Actors.TryGetValue(actorType, out actorInfo)) if (!Self.World.Map.Rules.Actors.TryGetValue(actorType, out actorInfo))
throw new LuaException("Unknown actor type '{0}'".F(actorType)); throw new LuaException("Unknown actor type '{0}'".F(actorType));
var faction = factionVariant ?? BuildableInfo.GetInitialFaction(actorInfo, p.Faction); Self.QueueActivity(new WaitFor(() =>
var inits = new TypeDictionary
{ {
new OwnerInit(Self.Owner), // Go through all available traits and see which one successfully produces
new FactionInit(faction) foreach (var p in productionTraits)
}; {
if (!string.IsNullOrEmpty(productionType) && !p.Info.Produces.Contains(productionType))
continue;
Self.QueueActivity(new WaitFor(() => p.Produce(Self, actorInfo, productionType, inits))); var inits = new TypeDictionary
{
new OwnerInit(Self.Owner),
new FactionInit(factionVariant ?? BuildableInfo.GetInitialFaction(actorInfo, p.Faction))
};
if (p.Produce(Self, actorInfo, productionType, inits))
return true;
}
// We didn't produce anything, wait until we do
return false;
}));
} }
} }