Help with limiting turret numbers.

Everything about mods can be found here.

Moderator: Morax

Help with limiting turret numbers.

Postby DDDX » 20 Oct 2018, 01:21

Hi guys.
The problem is as follows - I need a code that will restrict the number of a specific turrets available for build, and then unrestrict them again when they get killed.

Now the restricting i have, and it goes as follows:

Code: Select all
    OnStopBuild = function(self, sbrot1expd, order)

        AAirUnit.OnStopBuild(self, sbrot1expd)       
        --restrict the total number of things we can build
            local army = self:GetArmy()

                List = GetArmyBrain(army):GetListOfUnits(categories.ALLUNITS, false, true)                   
                    for k, unit in List do

                        if (unit:GetUnitId() == 'sbrot1expd') then                                        --my turret that I only need 2 of at any given time
                            table.insert(num_Mizura, unit); -- add unit to special unit list     
                             if (table.getn(num_Mizura) >= 2) then
                                self:AddBuildRestriction( categories.sbrot1expd )                    -- works like a charm
                              end
                        end
                    end   
    end,


Now comes the tricky part (for noob me, at least).
unrestricting the turret when it gets killed.
Now, I tried it like this
Code: Select all
OnStartReclaim = function(self, target)                     -- the unit is gonna do a ton of reclaiming, that's why
        AAirUnit.OnStartReclaim(self, target)
        local army = self:GetArmy()
     
           List = GetArmyBrain(army):GetListOfUnits(categories.sbrot1expd, false, true)                   
                    for k, unit in List do
                       if (table.getn(num_Mizura) <= 1) then
                            self:RemoveBuildRestriction( categories.sbrot1expd )
                             table.remove(num_Mizura, unit)                         
                        end
                    end
    end,


but what I guess is happening is that the list gets filled more and more with every OnStopBuild, instead of having only 2 units in there, for my 2 turrets...and is not getting lesser when the turrets die.
So I figured - well it should then be in OnKilled script of the actual turret...tried to make that, but failed

Code: Select all
OnKilled = function(self, instigator, damagetype, overkillRatio)
        TStructureUnit.OnKilled(self, instigator, damagetype, overkillRatio)

        local army = self:GetArmy()

                List = GetArmyBrain(army):GetListOfUnits(categories.sbrot1expd, false, true)                   
                    for k, unit in List do

                       -- if (unit:GetUnitId() == 'sbrot1expd') then
                            table.remove(num_Mizura, 1);   -- was table.remove(num_Mizura, unit), that's why I got the whole GetArmyBrain etc... but that gave me an error, asked for numerical value...what does the num value do here?
                    end
        self:CreatTheEffectsDeath()
    end,


So now...no idea how to pull that off. (Yes i have the local num_Mizura = {}; defined at start in both script files)
Any thoughts, or at least maybe point me in a direction of a mod/map that did this with another unitand i will be able to figure it out for myself...
Thanx.
Check out my 2 maps: "Survival_Mayhem&BO_3d_v1" "Survival_Mayhem&BO_3d_RPG"
as well as my mod: "Survival Mayhem&BO balance"
-- let me know of any bugs or issues regarding those 3.
DDDX
Avatar-of-War
 
Posts: 170
Joined: 21 Mar 2016, 16:13
Has liked: 18 times
Been liked: 16 times
FAF User Name: DDDX

Re: Help with limiting turret numbers.

Postby Franck83 » 20 Oct 2018, 12:40

HI DDDX,

If you just count turret number and if you put it in your turret scripts, you don't need an array but a simple variable.
Put it at the top.

I would suggest this kind of code in your units turrets script :

Code: Select all
local num_Turrets = 0

local OldXEB2306 = XEB2306
XEB2306 = Class(OldXEB2306) {

   OldOnStopBeingBuilt = OldXEB2306.OnStopBeingBuilt,
   OnStopBeingBuilt = function(self, builder, layer)
      num_Turrets = num_Turrets + 1
      if num_Turrets > 5 then
         -- Your building restriction code
      end
   end,

   OldDestroyUnit = OldXEB2306.DestroyUnit,
   DestroyUnit = function(self, overkillRatio)
      num_Turrets = num_Turrets - 1
      if num_Turrets <= 5 then
         -- Your building unlocking code
      end
   
   end,
}
TypeClass = XEB2306
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Help with limiting turret numbers.

Postby DDDX » 24 Oct 2018, 13:27

Ty for the reply
I did consider that at first, but seeing how I need to restrict other unit(s) to be able to build THIS turret, and I only know of this command to edit buildable categories self:AddBuildRestriction(categories.sbrot1expd), you can see how I would have a problem.

I know how to restrict building stuff from a builder's .script file, and I know how to count turret numbers and put them in my turret's script, but not how to combine the 2

I took a look at your T1ComRadar mod, however the Blueprints.lua file does not help me considering the OnStopBeingBuilt and OnDestroyed changes in the turrets .script file, pertaining to the numbers of turrets in existance because it only makes a general change to the blueprint and that is it. And I need it to be dinamic, if there's too many turrets, lose it from the build queue, if turrets die, be able to make em again.

Is there a way to add build restrictions of another unit and not just "self", from a .script file of my turret (namely, to edit the blueprint of that turret unit in general - (bp.categories) so that no further turrets can be made)? Or will I have to go another way around?

Worst case scenario I can just make the extra turret over the max number simply blow up when finished, but that is not an elegant way to do this...
Check out my 2 maps: "Survival_Mayhem&BO_3d_v1" "Survival_Mayhem&BO_3d_RPG"
as well as my mod: "Survival Mayhem&BO balance"
-- let me know of any bugs or issues regarding those 3.
DDDX
Avatar-of-War
 
Posts: 170
Joined: 21 Mar 2016, 16:13
Has liked: 18 times
Been liked: 16 times
FAF User Name: DDDX

Re: Help with limiting turret numbers.

Postby Franck83 » 24 Oct 2018, 16:16

If i understand well you need to edit all your engineers build restrictions at the same time ?

If yes, you can use the GetListOfUnits AI function to recover your engineer list. Then you can set the AddBuildRestriction method to all your engineers.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Help with limiting turret numbers.

Postby DDDX » 24 Oct 2018, 18:24

god...I'm a moron. I assumed that
Code: Select all
                List = GetArmyBrain(army):GetListOfUnits(categories.ALLUNITS, false, true)                   
                    for k, unit in List do
                          if (unit:GetUnitId() == 'alphaa1') then
                                self:AddBuildRestriction( categories.sbrot1expd )


needs to be SELF and nothing else. Just suplemented it for unit:AddBuildRestriction( categories.sbrot1expd ) and it worked perfectly....
I had the solution 2 days ago, in my 1.st post, if only I knew that...god damnit.
Ty for suggesting I retrace my steps, it helped me solve this :)
Check out my 2 maps: "Survival_Mayhem&BO_3d_v1" "Survival_Mayhem&BO_3d_RPG"
as well as my mod: "Survival Mayhem&BO balance"
-- let me know of any bugs or issues regarding those 3.
DDDX
Avatar-of-War
 
Posts: 170
Joined: 21 Mar 2016, 16:13
Has liked: 18 times
Been liked: 16 times
FAF User Name: DDDX


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest