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")]
public class ProductionProperties : ScriptActorProperties, Requires<ProductionInfo>
{
readonly Production p;
readonly Production[] productionTraits;
public ProductionProperties(ScriptContext context, Actor self)
: base(context, self)
{
p = self.Trait<Production>();
productionTraits = self.TraitsImplementing<Production>().ToArray();
}
[ScriptActorPropertyActivity]
@@ -41,14 +41,27 @@ namespace OpenRA.Mods.Common.Scripting
if (!Self.World.Map.Rules.Actors.TryGetValue(actorType, out actorInfo))
throw new LuaException("Unknown actor type '{0}'".F(actorType));
var faction = factionVariant ?? BuildableInfo.GetInitialFaction(actorInfo, p.Faction);
var inits = new TypeDictionary
Self.QueueActivity(new WaitFor(() =>
{
new OwnerInit(Self.Owner),
new FactionInit(faction)
};
// Go through all available traits and see which one successfully produces
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;
}));
}
}