[SOLVED]Unit upgrades for AI

Everything about mods can be found here.

Moderator: Morax

[SOLVED]Unit upgrades for AI

Postby Mavr390 » 17 Jul 2020, 20:33

Hi All!!!
I have several units with upgrades.
Maybe you know how to activate all unit upgrades at the end of construction for AI?
Last edited by Mavr390 on 05 Aug 2020, 22:03, edited 1 time in total.
Mavr390
Crusader
 
Posts: 25
Joined: 26 Oct 2019, 07:54
Has liked: 8 times
Been liked: 0 time
FAF User Name: Mavr390

Re: Unit upgrades for AI

Postby Mavr390 » 19 Jul 2020, 14:46

And one more question: How to make the AI use the upgrades installed on other units? not on ACU and SCU.
Mavr390
Crusader
 
Posts: 25
Joined: 26 Oct 2019, 07:54
Has liked: 8 times
Been liked: 0 time
FAF User Name: Mavr390

Re: Unit upgrades for AI

Postby Uveso » 19 Jul 2020, 18:55

You can start scripts for AI only with this:

Code: Select all
if self.BrainType ~= 'Human' then
    -- do AI stuff
end


If you want to add a upgrade builder for the AI you need some hooks, AI buildertable and upgrade templates for the new unit.

As example from my Nuclear Repulsor Shields mod:

You can find the Builder for the upgrade inside the file \Mods\NuclearRepulsorShields\lua\AI\AIBuilders\NuclearRepulsorShields.lua
There are the buildconditions for the shield upgrades
Code: Select all
BuilderGroup {
    BuilderGroupName = 'NuclearRepulsorShields',
    BuildersType = 'EngineerBuilder',
    Builder {
        BuilderName = 'U4 Village Shield',
        PlatoonTemplate = 'T3EngineerBuilder',
        Priority = 16000,
        InstanceCount = 1,
        BuilderConditions = {
            -- When do we want to build this ?
            { UCBC, 'HaveGreaterThanUnitsWithCategory', { 2, categories.TECH3 * categories.ENERGYPRODUCTION }},
            -- Do we need additional conditions to build it ?
            -- Have we the eco to build it ?
            { EBC, 'GreaterThanEconTrend', { 0.0, 12.0 } },
            { EBC, 'GreaterThanEconStorageRatio', { 0.50, 0.99 } },
            -- Don't build it if...
            { UCBC, 'HaveLessThanUnitsInCategoryBeingBuilt', { 1, categories.STRUCTURE * categories.SHIELD * categories.EXPERIMENTAL }},
            { UCBC, 'UnitsLessAtLocation', { 'LocationType', 3, categories.STRUCTURE * categories.SHIELD * categories.EXPERIMENTAL * categories.VILLAGESHIELD  }},
            -- Respect UnitCap
            { UCBC, 'UnitCapCheckLess', { 0.95 } },
        },
        BuilderType = 'Any',
        BuilderData = {
            Construction = {
                DesiresAssist = true,
                NumAssistees = 4,
                BuildClose = true,
                AdjacencyCategory = 'STRUCTURE ENERGYPRODUCTION TECH3',
                AvoidCategory = categories.EXPERIMENTAL * categories.SHIELD,
                maxUnits = 1,
                maxRadius = 40,
                LocationType = 'LocationType',
                BuildStructures = {
                    'T4VillageShield',
                },
            }
        }
    },
},
NuclearRepulsorShields is the Buildergroupname that wee need to use in our AIAddBuilderTable.lua hook.
T4VillageShield is the name we use inside the BuildingTemplates.lua hook.


This file must be implemented to the AI by a hook that is located in \Mods\NuclearRepulsorShields\hook\lua\simInit.lua
Code: Select all
local OLDSetupSession = SetupSession
function SetupSession()
    OLDSetupSession()
    import('/mods/nuclearrepulsorshields/lua/AI/AIBuilders/NuclearRepulsorShields.lua')
end

Now you need to inject the upgrade buildergroup to all base locations and expansion builders with
a hook inside \Mods\NuclearRepulsorShields\hook\lua\AI\AIAddBuilderTable.lua
Code: Select all
local OLDAddGlobalBaseTemplate = AddGlobalBaseTemplate
function AddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
    SPEW('Nuclear Repulsor Shields: Injecting BuilderGroup "NuclearRepulsorShields"')
    AddGlobalBuilderGroup(aiBrain, locationType, 'NuclearRepulsorShields')
    OLDAddGlobalBaseTemplate(aiBrain, locationType, baseBuilderName)
end


Now you only need the Upgrtade building templates hooked from \Mods\NuclearRepulsorShields\hook\lua\BuildingTemplates.lua
Code: Select all
--key 5 is for the Nomands Mod and not present in normal game.
BuildingTemplates[5] = BuildingTemplates[5] or {}

table.insert(BuildingTemplates[1], { 'T4VillageShield', 'qeb4408'} )
table.insert(BuildingTemplates[2], { 'T4VillageShield', 'qab4408'} )
table.insert(BuildingTemplates[3], { 'T4VillageShield', 'qrb4208'} )
table.insert(BuildingTemplates[4], { 'T4VillageShield', 'qsb4408'} )
table.insert(BuildingTemplates[5], { 'T4VillageShield', 'qnb4408'} )

table.insert(BuildingTemplates[1], { 'T4CityShield', 'qeb4409'} )
table.insert(BuildingTemplates[2], { 'T4CityShield', 'qab4409'} )
table.insert(BuildingTemplates[3], { 'T4CityShield', 'qrb4409'} )
table.insert(BuildingTemplates[4], { 'T4CityShield', 'qsb4409'} )
table.insert(BuildingTemplates[5], { 'T4CityShield', 'qnb4409'} )

table.insert(BuildingTemplates[1], { 'T4MetropolisShield', 'qeb4410'} )
table.insert(BuildingTemplates[2], { 'T4MetropolisShield', 'qab4410'} )
table.insert(BuildingTemplates[3], { 'T4MetropolisShield', 'qrb4410'} )
table.insert(BuildingTemplates[4], { 'T4MetropolisShield', 'qsb4410'} )
table.insert(BuildingTemplates[5], { 'T4MetropolisShield', 'qnb4410'} )




Nuclear Repulsor Shields mod:
/viewtopic.php?f=41&t=13083

I also have build some upgrade builder for hydrocarbon power plants inside the Black Ops Unleashed mod:
/viewtopic.php?f=31&t=17161
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: Unit upgrades for AI

Postby Mavr390 » 19 Jul 2020, 20:31

Hi Uveso!!!!
Thank you so much for the hydrocarbon power plants upgrade code. It was he who helped me with tech-level upgrades for my units. But I do not have tech-level upgrades, but the customizable upgrades that ACU and SCU have.
Mavr390
Crusader
 
Posts: 25
Joined: 26 Oct 2019, 07:54
Has liked: 8 times
Been liked: 0 time
FAF User Name: Mavr390

Re: Unit upgrades for AI

Postby Sprouto » 19 Jul 2020, 23:26

Right - these are enhancements rather than upgrades - and as such - they have to be detected and managed in a fashion similar to the way that ACU's do theirs.

That's going to be tricky - and computationally expensive, especially as you start getting more and more units that have available enhancements.

The LOUD Project has many units with enhancements, and we had to approach the situation from the unit side of the equation. In simple terms, without going into details, each unit that has enhancements like this, launches it's own thread for self enhancement - so that instead of taxing the platoon managers (which is how the ACU's are handled), each unit is responsible for itself - and will only upgrade itself if the conditions for itself are met (ie. - not busy fighting, not in a combat platoon, enough resources available, etc).

This approach, of course, will require you creating new code to implement in FAF, but if you're interested in seeing how it's done, you can investigate The LOUD Project and see it in action there.
Sprouto
Priest
 
Posts: 366
Joined: 08 Sep 2012, 05:40
Has liked: 54 times
Been liked: 74 times
FAF User Name: Sprouto

Re: Unit upgrades for AI

Postby Uveso » 19 Jul 2020, 23:50

So you mean "Enhancements" not "Upgrades" ^^

If you want to issue an enhancement you need first the enhancement scripts inside the unit like ACU or SACu have them.

If this is the case then you need custom functions to trigger/ build the enhancement.
Here is my function to decide what enhancement should be build for an ACU:
https://github.com/Uveso/AI-Uveso/blob/master/hook/lua/platoon.lua#L1393

I use a simple table with enhancement names.
After getting the enhancement name i call also my custom function to build the enhancement:
https://github.com/Uveso/AI-Uveso/blob/master/hook/lua/platoon.lua#L1449


Code: Select all
platoon:BuildEnhancement(cdr, NextEnhancement)
platoon is only the location from the function, its in this case a function inside the platoon class.
cdr is the unit
NextEnhancement is a string with the name of the enhancement like `HeavyAntiMatterCannon` or `ElectronicsEnhancment`


The function BuildEnhancement() is located here:
https://github.com/Uveso/AI-Uveso/blob/master/hook/lua/platoon.lua#L1487

Basically you can just copy this function:
Code: Select all
    BuildEnhancement = function(unit,enhancement)
        IssueStop({unit})
        IssueClearCommands({unit})
        if not unit:HasEnhancement(enhancement) then
            local tempEnhanceBp = unit:GetBlueprint().Enhancements[enhancement]
            local unitEnhancements = import('/lua/enhancementcommon.lua').GetEnhancements(unit.EntityId)
            -- Do we have already a enhancment in this slot ?
            if unitEnhancements[tempEnhanceBp.Slot] and unitEnhancements[tempEnhanceBp.Slot] ~= tempEnhanceBp.Prerequisite then
                -- remove the enhancement
                --LOG('* AI-Uveso: * ACUAttackAIUveso: Found enhancement ['..unitEnhancements[tempEnhanceBp.Slot]..'] in Slot ['..tempEnhanceBp.Slot..']. - Removing...')
                local order = { TaskName = "EnhanceTask", Enhancement = unitEnhancements[tempEnhanceBp.Slot]..'Remove' }
                IssueScript({unit}, order)
                coroutine.yield(10)
            end
            --LOG('* AI-Uveso: * ACUAttackAIUveso: BuildEnhancement: '..platoon:GetBrain().Nickname..' IssueScript: '..enhancement)
            local order = { TaskName = "EnhanceTask", Enhancement = enhancement }
            IssueScript({unit}, order)
        end
        while not unit.Dead and not unit:HasEnhancement(enhancement) do
            -- waiting for the enhancement to build
            coroutine.yield(10)
        end
        --LOG('* AI-Uveso: * ACUAttackAIUveso: BuildEnhancement: '..platoon:GetBrain().Nickname..' Upgrade finished '..enhancement)
        return true
    end,


And call it with
Code: Select all
BuildEnhancement( unit, enhancementName )


My function also can uninstall enhancements on a already filled slot so you can also use it for exchanging enhancements that are using the same slot.

Feel free to use it.
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: Unit upgrades for AI

Postby Mavr390 » 20 Jul 2020, 17:29

Hi Uveso!!!
Thanks for the voluminous answer.
I'll try to explain the idea of my mod: each faction will have an experimental unit with an assault and siege set of enhancements. I want the AI to use them.

I understood you correctly that I need to take this function
Uveso wrote:Basically you can just copy this function:
Code: Select all
    BuildEnhancement = function(unit,enhancement)
        IssueStop({unit})
        IssueClearCommands({unit})
        if not unit:HasEnhancement(enhancement) then
            local tempEnhanceBp = unit:GetBlueprint().Enhancements[enhancement]
            local unitEnhancements = import('/lua/enhancementcommon.lua').GetEnhancements(unit.EntityId)
            -- Do we have already a enhancment in this slot ?
            if unitEnhancements[tempEnhanceBp.Slot] and unitEnhancements[tempEnhanceBp.Slot] ~= tempEnhanceBp.Prerequisite then
                -- remove the enhancement
                --LOG('* AI-Uveso: * ACUAttackAIUveso: Found enhancement ['..unitEnhancements[tempEnhanceBp.Slot]..'] in Slot ['..tempEnhanceBp.Slot..']. - Removing...')
                local order = { TaskName = "EnhanceTask", Enhancement = unitEnhancements[tempEnhanceBp.Slot]..'Remove' }
                IssueScript({unit}, order)
                coroutine.yield(10)
            end
            --LOG('* AI-Uveso: * ACUAttackAIUveso: BuildEnhancement: '..platoon:GetBrain().Nickname..' IssueScript: '..enhancement)
            local order = { TaskName = "EnhanceTask", Enhancement = enhancement }
            IssueScript({unit}, order)
        end
        while not unit.Dead and not unit:HasEnhancement(enhancement) do
            -- waiting for the enhancement to build
            coroutine.yield(10)
        end
        --LOG('* AI-Uveso: * ACUAttackAIUveso: BuildEnhancement: '..platoon:GetBrain().Nickname..' Upgrade finished '..enhancement)
        return true
    end,

insert into platoon.lua and everything will work ??? Or do I need something else ???
Mavr390
Crusader
 
Posts: 25
Joined: 26 Oct 2019, 07:54
Has liked: 8 times
Been liked: 0 time
FAF User Name: Mavr390

Re: Unit upgrades for AI

Postby Uveso » 20 Jul 2020, 17:38

This function can run in any lua file that is executed in SIM-state of the game.
No need to put it into Platoon.lua. (i removed any platoon.lua related values)

In case your units have the enhancement scripts like ACu and SACU, then you only need this function to build the enhancement.

From here its hard to help without looking at your unit/scripts
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: Unit upgrades for AI

Postby Mavr390 » 20 Jul 2020, 18:01

Uveso, where can I download the mod, so that you can watch it ???
Mavr390
Crusader
 
Posts: 25
Joined: 26 Oct 2019, 07:54
Has liked: 8 times
Been liked: 0 time
FAF User Name: Mavr390

Re: Unit upgrades for AI

Postby Uveso » 20 Jul 2020, 21:49

Well, i don't use any file upload service,
but many player are using MEGA

https://mega.nz/

Don't ask me how it works :)
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Next

Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest