How to make the unitcap related to the number of mass-ex?

Interesting mapping tools and mapping help.

Moderator: Morax

How to make the unitcap related to the number of mass-ex?

Postby Ghoustaq » 20 Feb 2018, 13:58

I want the unitcap related to the number of the mass-extractors that I own. For example, when I build or capture a mass-extractor, the unitcaop adds 2, if I lose one , it reduce 2. I have no idea to realize it by scripting. I think I need some
technical support from this forum. :roll:
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: How to make the unitcap related to the number of mass-ex

Postby Uveso » 21 Feb 2018, 06:49

Hello Ghoustaq,

You need the following commands:

To get a list of all your massextractors:
Code: Select all
MassExtractorUnitList = aiBrain:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)

To count all extractors inside the list:
Code: Select all
count = table.getn(MassExtractorUnitList)

If you want to know the actual unit cap of your army:
Code: Select all
currentCap = GetArmyUnitCap(brain.index)

And to set a new unit cap:
Code: Select all
SetArmyUnitCap(brain.index, newCap)
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: How to make the unitcap related to the number of mass-ex

Postby Ghoustaq » 21 Feb 2018, 15:33

Uveso wrote:Hello Ghoustaq,

You need the following commands:

To get a list of all your massextractors:
Code: Select all
MassExtractorUnitList = aiBrain:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)

To count all extractors inside the list:
Code: Select all
count = table.getn(MassExtractorUnitList)

If you want to know the actual unit cap of your army:
Code: Select all
currentCap = GetArmyUnitCap(brain.index)

And to set a new unit cap:
Code: Select all
SetArmyUnitCap(brain.index, newCap)

truly grateful for your help!Master! :lol:
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: How to make the unitcap related to the number of mass-ex

Postby Ghoustaq » 23 Feb 2018, 18:08

Uveso wrote:Hello Ghoustaq,

You need the following commands:

To get a list of all your massextractors:
Code: Select all
MassExtractorUnitList = aiBrain:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)

To count all extractors inside the list:
Code: Select all
count = table.getn(MassExtractorUnitList)

If you want to know the actual unit cap of your army:
Code: Select all
currentCap = GetArmyUnitCap(brain.index)

And to set a new unit cap:
Code: Select all
SetArmyUnitCap(brain.index, newCap)

hello!Uveso:

I wrote a segment of the script.lua as below:

local tblArmy = ListArmies()
for index,army in tblArmy do
local MassExtractorUnitList = tblArmy:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)
local count = table.getn(MassExtractorUnitList)
local currentCap = GetArmyUnitCap(army)
SetArmyUnitCap(army, count/0.5)
end

Unfortunately, it doesn't work in my skirmish map. :roll: Can you check it? I'm sure there is something wrong with it 'causeI am not professional in scripting.
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: How to make the unitcap related to the number of mass-ex

Postby Uveso » 23 Feb 2018, 22:13

Hello Ghoustaq,

well it's hard to see whats going wrong if i can't see the whole script.
Here an working example, ready for copy + paste to your MAP_script.lua:
Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
    ScenarioUtils.InitializeArmies()
    ScenarioFramework.SetPlayableArea('AREA_1', false)
end

function OnStart(self)
    ForkThread(MexWatcher)
end

function MexWatcher(self)
    -- Set variables to local use only.
    local MassExtractorUnitList
    local MexCount
    local CurrentCap
    local NewCap
    while true do
        -- Loop over every Army in this game
        for INDEX,BRAIN in ArmyBrains do
            -- Get all Extractors owned by this Brain (Army)
            MassExtractorUnitList = BRAIN:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)
            -- Reset the MexCounter
            MexCount = 0
            -- Loop over all extractors inside MassExtractorUnitList
            for index, extractor in MassExtractorUnitList do
                -- Is the extractor completed (or still in build)
                if extractor:GetFractionComplete() == 1 then
                    -- OK this extractor is finished and will count for the armycap
                    MexCount = MexCount + 1
                end
            end
            -- Get the current UnitCap (We don't need the value (CurrentCap) except for debugmessages)
            CurrentCap = GetArmyUnitCap(INDEX)
            -- Calculate the new unitcap. (extractors * 10)
            NewCap = MexCount * 10
            -- We need at least a unitcap of 2 to build the first massextractor. (Commander + new Massextractor = 2)
            if NewCap < 2 then
                NewCap = 2
            end
            -- Set the new unitcap
            SetArmyUnitCap(INDEX, NewCap)
            -- DebugMessage to the [F9] DebugWindow
            LOG('* MexWatcher: Army('..INDEX..') has ('..MexCount..') Extractors. - Set unitcap from ('..CurrentCap..') to ('..NewCap..')')
        end
        -- Wait a second before we loop again. (if you don't wait here, the game will freeze in a deadloop)
        WaitSeconds(1)
    end
end


The script will print debugmessages to the logwindow (open with [F9]) like this:
Code: Select all
INFO: * MexWatcher: Army(1) has (0) Extractors. - Set unitcap from (625) to (2)
INFO: * MexWatcher: Army(2) has (0) Extractors. - Set unitcap from (625) to (2)
INFO: * MexWatcher: Army(3) has (0) Extractors. - Set unitcap from (625) to (2)


This should push you in the right direction :)
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: How to make the unitcap related to the number of mass-ex

Postby Ghoustaq » 24 Feb 2018, 16:52

Uveso wrote:Hello Ghoustaq,

well it's hard to see whats going wrong if i can't see the whole script.
Here an working example, ready for copy + paste to your MAP_script.lua:
Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
    ScenarioUtils.InitializeArmies()
    ScenarioFramework.SetPlayableArea('AREA_1', false)
end

function OnStart(self)
    ForkThread(MexWatcher)
end

function MexWatcher(self)
    -- Set variables to local use only.
    local MassExtractorUnitList
    local MexCount
    local CurrentCap
    local NewCap
    while true do
        -- Loop over every Army in this game
        for INDEX,BRAIN in ArmyBrains do
            -- Get all Extractors owned by this Brain (Army)
            MassExtractorUnitList = BRAIN:GetListOfUnits(categories.MASSEXTRACTION * (categories.TECH1 + categories.TECH2 + categories.TECH3), false, false)
            -- Reset the MexCounter
            MexCount = 0
            -- Loop over all extractors inside MassExtractorUnitList
            for index, extractor in MassExtractorUnitList do
                -- Is the extractor completed (or still in build)
                if extractor:GetFractionComplete() == 1 then
                    -- OK this extractor is finished and will count for the armycap
                    MexCount = MexCount + 1
                end
            end
            -- Get the current UnitCap (We don't need the value (CurrentCap) except for debugmessages)
            CurrentCap = GetArmyUnitCap(INDEX)
            -- Calculate the new unitcap. (extractors * 10)
            NewCap = MexCount * 10
            -- We need at least a unitcap of 2 to build the first massextractor. (Commander + new Massextractor = 2)
            if NewCap < 2 then
                NewCap = 2
            end
            -- Set the new unitcap
            SetArmyUnitCap(INDEX, NewCap)
            -- DebugMessage to the [F9] DebugWindow
            LOG('* MexWatcher: Army('..INDEX..') has ('..MexCount..') Extractors. - Set unitcap from ('..CurrentCap..') to ('..NewCap..')')
        end
        -- Wait a second before we loop again. (if you don't wait here, the game will freeze in a deadloop)
        WaitSeconds(1)
    end
end


The script will print debugmessages to the logwindow (open with [F9]) like this:
Code: Select all
INFO: * MexWatcher: Army(1) has (0) Extractors. - Set unitcap from (625) to (2)
INFO: * MexWatcher: Army(2) has (0) Extractors. - Set unitcap from (625) to (2)
INFO: * MexWatcher: Army(3) has (0) Extractors. - Set unitcap from (625) to (2)


This should push you in the right direction :)

WOW! It's really working! Thanks very much for directing me. :D By the way, I suggest modifying "NewCap = MexCount * 2" to "NewCap = MexCount* 2 + 2" so that the NewCap will not be less than 2.
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: How to make the unitcap related to the number of mass-ex

Postby Uveso » 25 Feb 2018, 20:39

You can also use the LUA function math.max.

Math max is using the highest number from all given args:
Code: Select all
math.max( 2, 0 ) = 2
math.max( 2, 1 ) = 2
math.max( 2, 2 ) = 2
math.max( 2, 3 ) = 3
math.max( 2, 4 ) = 4

So we can use this to get the unitcap:
Code: Select all
math.max( 2, NewCap )

Now we can build the following optimized code:
Code: Select all
SetArmyUnitCap(INDEX, math.max( 2, NewCap ) )

This way you will have always a minimum cap of 2.
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


Return to Mapping

Who is online

Users browsing this forum: No registered users and 1 guest