Need help with Ai to upgrade buildings.

0

Hi.
Please I need help with Mod.

I've created mod for private use and only sharing it with few people i'm playing with. This mod has been created based on other mods like BalckOps, Jager Infrastructure Pack, Upgraded Power Generators, More Storage and Experimental Shields.

Everything is working fine with players, But I can't make AI to upgrade buildings.

In game log i have these wornings:

debug: Loading module '\000/mods/pj_infrastructure_pack/lua/ai/aibuilders/xshields.lua\000'
warning: attempt to get as UserData a nil value
warning: stack traceback:
warning:         [C]: ?
warning:         ...j_infrastructure_pack\lua\ai\aibuilders\xshields.lua(25): in main chunk
warning:         [C]: in function `doscript'
warning:         [C]: in function `pcall'
warning:         ...alliance\gamedata\mohodata.scd\lua\system\import.lua(48): in function `import'
warning:         ...ogramdata\faforever\gamedata\lua.nx2\lua\siminit.lua(447): in function `SetupSession'
warning: Error running SetupSession in SimInit.lua: ...ogramdata\faforever\gamedata\lua.nx2\lua\siminit.lua(447): Error importing '/mods/pj_infrastructure_pack/lua/ai/aibuilders/xshields.lua'
         stack traceback:
         	[C]: in function `error'
         	...alliance\gamedata\mohodata.scd\lua\system\import.lua(52): in function `import'
         	...ogramdata\faforever\gamedata\lua.nx2\lua\siminit.lua(447): in function `SetupSession'

and

debug: Pj_Infrastructure_Pack: Injecting BuilderGroups
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "XShields" does not exist!
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "Upgrade-HC" does not exist!
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "Upgrade-EXMEX" does not exist!
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "Upgrade-Storage-M" does not exist!
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "Upgrade-Storage-E" does not exist!
warning: [aiaddbuildertable.lua, line:34] *AddGlobalBuilderGroup ERROR: BuilderGroup "Upgrade-PGen" does not exist!

Could you please help me to fix this issue.
Please let me know what information needed and i will provide it.

Thank you.
Yuriy.

0

UserData will most probably be nonexistant category
Looking in the xshields.lua file, its mentioning categories.VILLAGESHIELD
I havent found this category in any of your units and its surely not a category from the base game units.

0
This post is deleted!
0

@speed2 said in Need help with Ai to upgrade buildings.:

UserData will most probably be nonexistant category
Looking in the xshields.lua file, its mentioning categories.VILLAGESHIELD
I havent found this category in any of your units and its surely not a category from the base game units.

Thank you for your reply and pointing to an error. Categories.VILLAGESHIELD should be defined in shield unit.bp.
Also I'm not getting any more 2nd error message about Injecting BuilderGroups.
This has been ixed.
Do you have any idea why AI doesn't upgrade buildings from this mod?
Going to test mod now.

0

categories.VILLAGESHIELD is a category from the Nuclear Repulsor Shields mod
https://forum.faforever.com/topic/357/nuclear-repulsor-shields-v21-for-all-game-versions

The AI is not upgrading the Shields because i did not added a upgrade platoonformer to the mod.
There are also no upgrade templates defined that are needed for an upgrade.

Btw, i am really sure i never granted any permission to use Black Ops or Total Mayhem Units for other mods.
So could you please delete the link here. (It's no longer private when you post it here)

0

Hi Uveso.
Mod link has been removed (it has been posted for debuging purpose). Please note, i didn't directly take any units from Black Ops or Nuclear Repulsor Shields. These units has been taken from other mods available for download from FAF. That's why i was confused about categories.VILLAGESHIELD, and where it came from. How ever i can see AI upgrade function has been taken as example from Black Ops but doesn't work.
Would you be able to help me please on how to make AI to upgrade storages (T1->T2->T3), extractors(T3->T3 shielded), power generator (T2-T2 adv, T3-T3 adv), HC (T3->Exp). All files are set. From what i can see it should work but it doesn't, AI connstructing new buildings but never upgrading them. Looks like i've missed something.

0

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 🙂

0

@uveso
Hi Uveso.
Thank you for your reply. This all has been alredy done as i've followed your guide: https://forums.faforever.com/viewtopic.php?f=41&t=19591
And looked myself at Black Ops mod (That's why I've mentioned it in first post)
I think i know what is the issue. In file /hook/lua/AI/AIAddBuilderTable.lua i don't know how to define multiple Templates or Builder Groups in one file? I have these:

local OLDAddGlobalBaseTemplate = AddGlobalBaseTemplate
function AddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
    SPEW('Pj_Infrastructure_Pack: Injecting BuilderGroups')
    AddGlobalBuilderGroup(aiBrain, locationType, 'XShields')
    AddGlobalBuilderGroup(aiBrain, locationType, 'Upgrade-HC')
    AddGlobalBuilderGroup(aiBrain, locationType, 'Upgrade-EXMEX')
    AddGlobalBuilderGroup(aiBrain, locationType, 'Upgrade-Storage-M')
    AddGlobalBuilderGroup(aiBrain, locationType, 'Upgrade-Storage-E')
    AddGlobalBuilderGroup(aiBrain, locationType, 'Upgrade-PGen')
    OLDAddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
end

Could you help me please?

0

I did only a quick overview.

First you need to delete the category VILLAGESHIELD from the file AIAddBuilderTable.lua
(Delete every * categories.VILLAGESHIELD)
This will resolve the error attempt to get as UserData a nil value

Adding several buildergrpups from the same file is no problem.
But you should rename the local save variable for your hook.

local OLDAddGlobalBaseTemplate = AddGlobalBaseTemplate
Change it to a unique name like
local InfrastructureAddGlobalBaseTemplate = AddGlobalBaseTemplate
Havig the same hook name in different mods can lead to deadloops (game freeze)
or simply overwriting a hook from another mod.

0

Upgrade for pgens is wrong:

PlatoonTemplate {
    Name = 'Upgrade-PGen-T2',
    Plan = 'UnitUpgradeAI',
    GlobalSquads = {
        {categories.ENERGYPRODUCTION * categories.TECH2 * categories.ECONOMIC, 1, 1, 'support', 'None'},
    }
}

Unit UEpowerup_unit has category ENERGYPRODUCTION * TECH2 * ECONOMIC but can't be upgraded.
This template should only catch normal powergens, not the upgraded ones.
(same to all other PlatoonTemplates)

0

@uveso

Thank you, I've corrected errors.

Could you please explain what I did wrong in Platoon template?
UEpowerup_unit is an upgraded version of UEB1201_unit
UEB1201_unit has category ENERGYPRODUCTION * TECH2 * ECONOMIC and upgradable via hook:

UnitBlueprint {
    Merge = true,
	BlueprintId = "ueb1201",
	
    Display = {
        Abilities = {
            '<LOC ability_upgradable>Upgradeable',
        },
    },
    Economy = {
        BuildRate = 12,
        BuildableCategory = {
            'uepowerup',
        },
        RebuildBonusIds = {
            "ueb1201",
            'uepowerup',
        },
    },
    General = {
		UpgradesTo = 'uepowerup',
        CommandCaps = {
            RULEUCC_Pause = true,
        },
    },
}
0

yes unit UEB1201 has categories ENERGYPRODUCTION * TECH2 * ECONOMIC
But unit UEpowerup has also categories ENERGYPRODUCTION * TECH2 * ECONOMIC

Now if you use the PlatoonTemplate and search for untis with the categories ENERGYPRODUCTION * TECH2 * ECONOMIC
what unit will you get ? - UEB1201 and UEpowerup.
You need to get sure that you only get UEB1201 with this PlatoonTemplate.

You can do this by adding a category like 'UPGRADED' to the unit UEpowerup.
Now if you want to get the UEB1201 unit, search for
ENERGYPRODUCTION * TECH2 * ECONOMIC - UPGRADED

{categories.ENERGYPRODUCTION * categories.TECH2 * categories.ECONOMIC - categories.UPGRADED, 1, 1, 'support', 'None'},
0

If you are curious for a mod that handles upgrading. My own mod has added upgrading units for TA factions. Might be of interest of tou see the other work you need to do/add.

0

@Dragun101
Thank you Dragun101. Will check your mod. Would you be ble to point to files of interest for me?

0

@uveso
Thank you Uveso. I've implemented these changes for T2 and T3 Power Generators and T3 Shielded Extractors. How ever issue you've mentioned shouldn't affect HC T3 to Experimental upgrade (categories.HYDROCARBON * categories.ENERGYPRODUCTION * categories.TECH3) and all storages upgrades(categories.MASSSTORAGE * categories.TECH1 * categories.ECONOMIC) and (categories.MASSSTORAGE * categories.TECH2 * categories.ECONOMIC).
Or i'm again missing something?
Also i can see this in game log, this is upgrade HCT3 to HCExperimental:

warning: [platoon.lua, line:2494] *UnitUpgradeAI ERROR: "brb1302":CanBuild( BRMBT1PERI ) failed!

Is it anything else wrong what you could spot?
From what you've seen, is it ok to list link for corrected version of this mod at this page for troubleshooting?

0

If you upload your mod, then please delete all files with '.dds' and '.scm'

This way no one can use it and i can merge the scripts into my version.

While walking through the scripts i saw you have some upper/lower case mismatch in unit IDs
As example please change all BRMBT1PERI to brmbt1peri
Upper case names don't work.

I used my own formbuilders in my AI, but lowercase IDs like brmbt1peri are working with the upgrade function.
Upper case are not working.

0

@uveso
Thanks will correct names and remove required files, then upload mod.

0

Test it before you upload it. Maybe you don't need to.

Changing the names will do the trick.
As i said i only changed the names and it was working.

But i did not used your Platoonformer.
It has a strange buildcondition:

			{ EBC, 'GreaterThanEconIncome', { 100.0, 4000.0 } },

its waiting for an base income of 1000 mass and 40000 energy (values are * 10)
(Thats why we write 100.0 to have in mind its 1000)

0

@uveso
Hi Uveso.
Changes has been done followed by your guide. Done some testing and can see that AI upgrading HC T1->T2->t3->Exp, Power Generator T2->T2+ and T3->T3+, Shields T3->Exp, storages T1->T2->T3.
But on 70min game AI doesn't have any T3+ extractors 😞

Platoonformer (buildconditions) has been reviewed too.

Link for mod without textures and models: [https://drive.google.com/file/d/12dr7Dw5JYMQLu5gD3wFtUNCDm6U3a0j9/view?usp=sharing](link url)

Would you be able to check if everything is ok with Extractors upgrade please?
Thank you.

0

Extractor upgrade from 1 to 2, 2 to 3 and 4 to experimental are all working.

Maybe you should decrease the eco requirement of the Builder ?

BuilderName = 'EXMEX-Upgrade',
{ EBC, 'GreaterThanEconIncome', { 10.0, 500.0 } },

It's waiting for 100 massincome and 5000 energy. Maybe you should use something like { 10.0, 100.0 }

Also found this warning:

WARNING: *AIExecuteBuildStructure: TECH1 Unit "euebest3" is assigned to build TECH3 buildplatoon! ("EnergyStorage")

Please check in CustomUnits.lua:

UnitList = {
    EnergyStorage = {
        UEF = {'euebest3', 50},
    },
},

EnergyStorage is a Tech1 building and will be assinged to a Tech 1 engineer.
It can't build a tech3 unit euebest3.

There are several other custom entries that have the same problem.