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.