Well, adding a upgrade builder to a mod needs some work.
I will use Black Ops unleashed Hydrocarbon Power Plant as example.
First you need the PlatoonFormbuilder for upgrading the Hydro:
https://github.com/Uveso/BlackOpsFAF-Unleashed/blob/master/lua/AI/AIBuilders/HydroCarbonUpgrade.lua
local UCBC = '/lua/editor/UnitCountBuildConditions.lua'
local EBC = '/lua/editor/EconomyBuildConditions.lua'
BuilderGroup {
BuilderGroupName = 'BO-HydroCarbonUpgrade',
BuildersType = 'PlatoonFormBuilder',
Builder {
BuilderName = 'BO1 HydroUpgrade',
PlatoonTemplate = 'T1PowerHydroUpgrade',
Priority = 200,
BuilderConditions = {
{ UCBC, 'HaveGreaterThanUnitsWithCategory', { 0, categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH1 } },
{ UCBC, 'HaveLessThanUnitsInCategoryBeingBuilt', { 1, categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH2 } },
{ EBC, 'GreaterThanEconIncome', { 2, 10 } },
{ EBC, 'GreaterThanEconTrend', { 0.0, 0.0 } },
},
FormRadius = 10000,
BuilderType = 'Any',
},
Builder {
BuilderName = 'BO2 HydroUpgrade',
PlatoonTemplate = 'T2PowerHydroUpgrade',
Priority = 200,
BuilderConditions = {
{ UCBC, 'HaveLessThanUnitsWithCategory', { 1, categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH1 } },
{ UCBC, 'HaveGreaterThanUnitsWithCategory', { 0, categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH2 } },
{ UCBC, 'HaveLessThanUnitsInCategoryBeingBuilt', { 1, categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH3 } },
{ EBC, 'GreaterThanEconIncome', { 2.6, 60 } },
{ EBC, 'GreaterThanEconTrend', { 0.0, 0.0 } },
},
FormRadius = 10000,
BuilderType = 'Any',
},
}
These are the builders with buildconditions and PlatoonTemplate names
To inject the Buildergroup to the actual running AI you need a hook inside simInit.lua
https://github.com/Uveso/BlackOpsFAF-Unleashed/blob/master/hook/lua/simInit.lua
local OLDSetupSession = SetupSession
function SetupSession()
OLDSetupSession()
import('/mods/BlackOpsFAF-Unleashed/lua/AI/AIBuilders/HydroCarbonUpgrade.lua')
end
This hook will load all BuilderGroups located in the file HydroCarbonUpgrade.lua
Now we need to add the builders to every AI base and new opened expansion.
To do so we need a hook inside AIAddBuilderTable.lua:
https://github.com/Uveso/BlackOpsFAF-Unleashed/blob/master/hook/lua/AI/AIAddBuilderTable.lua
local OLDAddGlobalBaseTemplate = AddGlobalBaseTemplate
function AddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
SPEW('BlackOpsFAF-Unleashed: Injecting BuilderGroup "BO-HydroCarbonUpgrade"')
AddGlobalBuilderGroup(aiBrain, locationType, 'BO-HydroCarbonUpgrade')
OLDAddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
end
Now every base/expansion can call the upgradebuilder.
To make the upgrade work we need an upgradetemplate.
The upgradebuilder is using the PlatoonTemplate T1PowerHydroUpgrade
So we need to create a PlatoonTemplate with the UnitUpgradeAI plan and a GlobalSquad named T1PowerHydroUpgrade:
https://github.com/Uveso/BlackOpsFAF-Unleashed/blob/master/hook/lua/upgradetemplates.lua
PlatoonTemplate {
Name = 'T1PowerHydroUpgrade',
Plan = 'UnitUpgradeAI',
GlobalSquads = {
{categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH1, 1, 1, 'support', 'None'},
}
}
PlatoonTemplate {
Name = 'T2PowerHydroUpgrade',
Plan = 'UnitUpgradeAI',
GlobalSquads = {
{categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH2, 1, 1, 'support', 'None'},
}
}
Finally we need the StructureUpgradeTemplates with unitIds so the AI knows how to upgrade to the right unit:
-- earth structure upgrades
table.insert(StructureUpgradeTemplates[1], {'ueb1102', 'beb1202'}) -- Hydrocarbon Power Plant. Upgrade from TECH1 to TECH2
table.insert(StructureUpgradeTemplates[1], {'beb1202', 'beb1302'}) -- Hydrocarbon Power Plant. Upgrade from TECH2 to TECH3
-- alien structure upgrades
table.insert(StructureUpgradeTemplates[2], {'uab1102', 'bab1202'}) -- Hydrocarbon Power Plant. Upgrade from TECH1 to TECH2
table.insert(StructureUpgradeTemplates[2], {'bab1202', 'bab1302'}) -- Hydrocarbon Power Plant. Upgrade from TECH2 to TECH3
-- recycler structure upgrades
table.insert(StructureUpgradeTemplates[3], {'urb1102', 'brb1202'}) -- Hydrocarbon Power Plant. Upgrade from TECH1 to TECH2
table.insert(StructureUpgradeTemplates[3], {'brb1202', 'brb1302'}) -- Hydrocarbon Power Plant. Upgrade from TECH2 to TECH3
-- seraphim structure upgrades
table.insert(StructureUpgradeTemplates[4], {'xsb1102', 'bsb1202'}) -- Hydrocarbon Power Plant. Upgrade from TECH1 to TECH2
table.insert(StructureUpgradeTemplates[4], {'bsb1202', 'bsb1302'}) -- Hydrocarbon Power Plant. Upgrade from TECH2 to TECH3
Happy coding