From ca786f6039c833a6aa6eda0032d1e762a360603d Mon Sep 17 00:00:00 2001 From: Oliver Brakmann Date: Thu, 13 Mar 2014 22:21:07 +0100 Subject: [PATCH] Add Production.BuildTeamFromTemplate Production.BuildTeamFromTemplate allows the production of teams from a list of actor types, assigning a function to that team once it is complete. A 'template' in this case looks like this: template = { {factory, {list of units to build using aforementioned factory}} } e.g. template = { {barracks, {"e1", "e1", "e1", "e3", "e3"}} } You can also build teams using multiple factories like this: template = { {barracks, {"e3", "e3", "e3"}}, {war_factory, {"3tnk", "3tnk"}} } Before using Production.BTFT you need to call Production.EventHandlers.Setup once. --- mods/common/lua/production.lua | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/mods/common/lua/production.lua b/mods/common/lua/production.lua index 5beaa1f899..04d2fdfd75 100644 --- a/mods/common/lua/production.lua +++ b/mods/common/lua/production.lua @@ -1,4 +1,5 @@ Production = { } +Production.EventHandlers = { } Production.BuildWithSharedQueue = function(player, unit, amount) Internal.BuildWithSharedQueue(player, unit, amount or 1) @@ -8,6 +9,17 @@ Production.BuildWithPerFactoryQueue = function(factory, unit, amount) Internal.BuildWithPerFactoryQueue(factory, unit, amount or 1) end +Production.Build = function(factory, unit, amount) + if Actor.HasTrait(factory, "ProductionQueue") then + Production.BuildWithPerFactoryQueue(factory, unit, amount) + elseif Actor.HasTrait(factory, "Production") then + Production.SetPrimaryBuilding(factory) + Production.BuildWithSharedQueue(Actor.Owner(factory), unit, amount) + else + error("Production.Build: not a factory") + end +end + Production.SharedQueueIsBusy = function(player, category) return Internal.SharedQueueIsBusy(player, category) end @@ -29,3 +41,54 @@ Production.SetPrimaryBuilding = function(factory) pb:SetPrimaryProducer(factory, true) end end + +Production.BuildTeamFromTemplate = function(player, template, func) + local factories = { } + Utils.Do(template, function(t) table.insert(factories, t[1]) end) + + if Utils.Any(factories, Actor.IsDead) then + return + end + + if Utils.Any(factories, function(fact) return Production.EventHandlers[fact] end) then + OpenRA.RunAfterDelay(Utils.Seconds(10), function() Production.BuildTeamFromTemplate(player, template, func) end) + return + end + + local team = Team.New({ }) + local teamSize = 0 + Utils.Do(template, function(t) teamSize = teamSize + #t[2] end) + + local eventHandler = function(unit) + Team.Add(team, unit) + + if #team.Actors >= teamSize then + func(team) + Utils.Do(factories, function(factory) + Production.EventHandlers[factory] = nil + end) + end + end + + Utils.Do(factories, function(factory) + Production.EventHandlers[factory] = eventHandler + end) + + Utils.Do(template, function(t) + Utils.Do(t[2], function(unit) + Production.Build(t[1], unit) + end) + end) +end + +Production.EventHandlers.Setup = function(player) + Utils.Do(Actor.ActorsWithTrait("Production"), function(factory) + if Actor.Owner(factory) == player then + Actor.OnProduced(factory, function(fact, unit) + if Production.EventHandlers[fact] then + Production.EventHandlers[fact](unit) + end + end) + end + end) +end