Random Aircraft Maintenance Function - not working

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
vettim89
Posts: 3668
Joined: Fri Jul 13, 2007 11:38 pm
Location: Toledo, Ohio

Random Aircraft Maintenance Function - not working

Post by vettim89 »

I created the following function to be able to cycle through squadrons once every 24 hours. The idea being that a certain percentage of aircraft will develop some glitch every day. Rather than just set the game up with certain planes preselected as 'maintenance unavailable' I wanted to make it more of an unknown for the player. As usual, for me, the function runs but nothing happens to the aircraft readiness. As I have some aircraft starting in maintenance, that should not be the case.

Code: Select all

function sdn_check (sdn, count)
 local defect, plane
 for i=1,count do
  plane=ScenEdit_GetUnit({side='NATO', type='aircraft', name=sdn..i})
  if plane~=nil then
    if plane.loadoutdbid=='4' then
       plane.loadoutdbid='3'
    else
      defect=math.random(1,10)
      print (defect)
      if defect>>8 then
      plane.loadoutdbid='4'
    end
  end
 end
end
end
PS: look at me trying to write functions. Never would have even tried without the encouragement of Knighthawk
"We have met the enemy and they are ours" - Commodore O.H. Perry
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Random Aircraft Maintenance Function - not working

Post by KnightHawk75 »

Your original with modifications that produce the effect you desire with the given function.
Note the ScenEdit_SetLoadout changes as well as added features.
Also examples here are converted to local for demonstration purposes.

Code: Select all

-- Function to loop through a given series of unit names, and reset or swap loadouts to maintenance.
--@param sd string - required The side name
--@param sdn string -required the base name of the unit.
--@param the number of names to process.
--@requirements: If count is provided then units must be named accordingly.
local function sdn_check(sd,sdn, count, pctchance,maxswitch)
  if pctchance == nil then pctchance = 10; end
  if maxswitch == nil then maxswitch = 100000; end
  math.randomseed(os.clock());
  local retval,defect, plane,switchcount = nil,nil,nil,0;
  for i=1,count do
    retval,plane = pcall(ScenEdit_GetUnit,{side=sd, name=sdn .. ' #' ..i})
    if retval and plane~=nil then
      print(plane.loadoutdbid)
      if plane.loadoutdbid == 4 or plane.loadoutdbid == 0 then --maintenance | or 0 = no loadout.
        print(ScenEdit_SetLoadout({UnitName=plane.name,LoadoutId=3,TimeToReady_Minutes=0})); --reserved avil.
        ScenEdit_SpecialMessage('playerside',"Aircraft " .. tostring(sdn) .. ' #' .. i .. ' is now available.')
      else
        defect=math.random(1,100);
        print(defect .. ' g:' .. plane.guid);
        if (switchcount < maxswitch) and defect <= pctchance then --10 = 10% chance per-plane.
          ScenEdit_SetLoadout({UnitName=plane.guid,LoadoutId=4}); --maint unavil.
          switchcount = switchcount + 1;
          ScenEdit_SpecialMessage('playerside',"Aircraft " .. tostring(sdn) .. ' #' .. i .. ' developed a problem and is now down for maintenance.')
        end
      end
    else
      print('could not obtain unit ' .. tostring(sdn) .. ' #' .. i);
    end
  end
end
--test call
sdn_check('sideA','Valiant',24,20,10) --sidename,unitcorename,unit name increment count,pct-perunit, max-touched
The follow may be a better way of doing it.. keep a table of facilities you want checked\included and feed them to this version.

Code: Select all

--Function to process all aircraft units currently hosted inside another unit.
--Sets all loadouts if maintenance to reserved, else % chance of being switched to maintenance 
--@param fguid string - The guid of the unit or facility
--@param pctchance? number - [optional] the integer/whole number random percentage chance per aircraft to be switched to maint. 10% by default.
--@param maxswitch? number - [optional] the maxiumum number of total aircraft to switch. practically unlimited by default.
local function hostingFacilityCheck(fguid,pctchance,maxswitch)
  if pctchance == nil then pctchance = 10; end
  if maxswitch == nil then maxswitch = 100000; end
  if fguid ==nil then print('no unit guid specified, aborting.'); return; end
  math.randomseed(os.clock());
  local retval,defect, host,u,switchcount = nil,nil,nil,nil,0;
  retval,host = pcall(ScenEdit_GetUnit,{guid=fguid});
  if retval == false then print('could not obtain the hosting unit with guid: '.. tostring(fguid) .. ' aborting.'); return; end 
  if host.embarkedUnits ~= nil and #host.embarkedUnits.Aircraft > 0 then
    for i=1,#host.embarkedUnits.Aircraft do
      retval,u = pcall(ScenEdit_GetUnit,{guid=host.embarkedUnits.Aircraft[i]});
      if retval and u~=nil then
        if u.loadoutdbid == 4 or u.loadoutdbid == 0 then --maintenance | or 0 = no loadout.
          ScenEdit_SetLoadout({UnitName=u.guid,LoadoutId=3,TimeToReady_Minutes=0}); --reserved avil.
          ScenEdit_SpecialMessage('playerside',"Aircraft " .. tostring(u.name) .. ' is now available at ' .. tostring(host.name));
        else
          defect=math.random(1,100);
          if (switchcount < maxswitch) and defect <= pctchance then --10 = 10% chance per-plane.
            ScenEdit_SetLoadout({UnitName=u.guid,LoadoutId=4}); --maint unavil.
            switchcount = switchcount + 1;
            ScenEdit_SpecialMessage('playerside',string.format("Aircraft %s at %s developed a problem and is now down for maintenance.",tostring(u.name),tostring(host.name)));
          end
        end
      else
        print('could not obtain embarked unit with guid ' .. tostring(host.embarkedUnits.Aircraft[i]));
      end
    end
  end
  return switchcount;
end

local somelist = {
{name='My SUA Airbase', guid='4FH7PU-0HMGDK8BF0BB6'},
{name='SomeOtherGroupedAirfield', guid='4FH7PU-0HMGDK8BF0EE4'},
}
--test call
for _,v in ipairs(somelist) do
  hostingFacilityCheck(v.guid,9,4) --9% chance per aircraft, 4 max switched to maint per fac.
end
The above avoids processing aircraft that are flying, and allows to set the chance and max per-host/facility, return value from hostFacilityCheck can be used for counting an overall limit among all facilities if needed. Use whatever works best for you.
pboiler
Posts: 21
Joined: Fri Feb 15, 2008 11:08 pm

Re: Random Aircraft Maintenance Function - not working

Post by pboiler »

I am hoping to alter this to make it a random aircraft “destroyed” script. I intend to use this to destroy aircraft at a single unit airfield when a missile hits the airfield.

In a scenario I'm creating an airbase group composed of just a single unit airfield (with runways removed) and a runway. Then either aircraft at the base are potentially destroyed or the runway is damaged depending on which of the 2 units at the airbase are hit.

The reason is 2 fold:
- reduce the units in a scenario to improve performance

- simulate but simplify attack/defense of the airbase because it is not the focus of the scenario


Would this be easy to do?
Thanks
User avatar
blu3s
Posts: 992
Joined: Fri Jul 08, 2022 9:45 am

Re: Random Aircraft Maintenance Function - not working

Post by blu3s »

The downside it's that single unit airfields IIRC can only be targeted by nuclear weapons. There were some scripts that allow you to switch between a single unit airfield and a complete airfield to allow you to attack it. My recomendation would be that if the airfield is meant to be targeted don't be afraid of using all of the airfield facilities.
Post Reply

Return to “Lua Legion”