KnightHawk75
Posts: 1450
Joined: 11/15/2018 Status: offline
|
Unchanged you'll run into it re-doing it on each loadup of a save, which may not be desirable once someone has started playing. Below I account for that, additionally added option to change the loadout or leave the default and just do the ready time change, and few tweaks for nil and error trapping, as well as allowing for any number of table entries without having to re-touch the rest of the code.
config={};
config.cvns = {
[1]={name='USS George W. Bush', guid='GWKW9V-0HMC21A9CO9FE'},
[2]={name='USS George Washington', guid='GWKW9V-0HMC21A9COA5M'},
[3]={name='USS Ronald Reagan', guid='GWKW9V-000SOME00GUID'}
} --table of carriers or SUAB's.
config.reserveLoadout = 3
config.oneDeckCycle = 12 * 60 -- 12 hours
math.randomseed(os.clock()); --use clock instead of time
--- This loops over the aircraft in on the selected inActiveCvn and sets them to a reserve loadout with a ready time of 12 hours.
--- coin flip choose which of the cvns gets to be active. The other will have the aircraft placed in a "reserve." state
---@param useReserveLoadout boolean - When set to true uses the config specified reserve loadoutid, otherwise does not touch loadout.
function ChooseActiveCarrier(useReserveLoadout)
local loadoutid = 0;
if (useReserveLoadout ~= nil) and useReserveLoadout == true then loadoutid= config.reserveLoadout; end
if #config.cvns > 0 then
roll = math.random(1,#config.cvns); --1 to max number in the table.
local retval, inActiveCvn = pcall(ScenEdit_GetUnit,{ guid = config.cvns[roll].guid } );
if (retval ==true) and inActiveCvn ~=nil then
if (inActiveCvn.assignedUnits.Aircraft ~=nil) and #inActiveCvn.assignedUnits.Aircraft > 0 then
for _, guid in ipairs( inActiveCvn.assignedUnits.Aircraft ) do
pcall(ScenEdit_SetLoadout,{UnitName=guid, LoadoutID=loadoutid, IgnoreMagazines=true, TimeToReady_Minutes=config.oneDeckCycle});
end
ScenEdit_SetKeyValue('RunOnceMarker','1'); --flag ourselves as having run.
else
print(string.format('Error obtaining assigned aircraft list for the chosen carrier (%s).', tostring(config.cvns[roll].name)));
end
else
print(string.format('Error obtaining carrier (%s) unit.', tostring(config.cvns[roll].name)));
end
else
print("cvns table is empty");
end
end
function RunOnceCheck()
local n = ScenEdit_GetKeyValue('RunOnceMarker');
if (n==nil) or n=="" then --flag doesn't exist.
ChooseActiveCarrier(true);
else
print("Carrier already choosen. Use ClearKeyValue('RunOnceMarker') followed by a save and reload to force re-execution of carrier selection.");
end
end
RunOnceCheck(); --kick of the check and the choose function call if needed.
< Message edited by KnightHawk75 -- 10/1/2021 2:42:19 AM >
|