Generate Patrol Mission & RPs @ Randomized airbase

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
spinnaker
Posts: 5
Joined: Sat Apr 15, 2017 7:07 pm

Generate Patrol Mission & RPs @ Randomized airbase

Post by spinnaker »

Hello!

I'm creating a small scenario that gives some solid replay value.

Goal is to have random opfor aircraft patrols start & launch from single unit airfields

I have code generating randomized single unit airbases - 4 different locations
I want to then take the location of that single unit base and generate a patrol for that given location

I'm going to share what I've managed to put together but I would appreciate thoughts on how to create a patrol mission around the farp without hardcoding the locations. Code is incomplete

Code: Select all

math.randomseed(os.time())
math.random(); math.random(); math.random()
a = math.random(1,3) -- random airbase locations and cap missions 
b = math.random(1,6) -- random additional A/C 

function CreateAirBase(basename,latlonTable, singleunitAF, sam, radar)
    local lat=latlonTable['latitude']
    local lon=latlonTable['longitude']
    for i=1,singleunitAF do
        local u = ScenEdit_AddUnit({side='PLA', type='Facility', name='Single Unit Airfield '..i, autodetectable="false", dbid=1593, Lat=lat, Lon=lon})
        u.group ='PLA AB (PLAAF)' 
        lat=lat +.004
    end
    lon=latlonTable['longitude'] + .008
    for i=1,sam do
    local u = ScenEdit_AddUnit({side='PLA', type='Facility', name='sam'..i, autodetectable="false", dbid=2991, Lat=lat, Lon=lon}) --HQ-16B
    u.group ='PLA AB (PLAAF)' 
    lat=lat +.002
    end
    lon=latlonTable['longitude'] + .008
    for i=1,radar do
    local u = ScenEdit_AddUnit({side='PLA', type='Facility', name='radar'..i, autodetectable="false", dbid=2537, Lat=lat, Lon=lon}) --JY-26
    u.group ='PLA AB (PLAAF)' 
    lat=lat +.002
    end
end

local function AddACToBase(side,baseName,aircraftDBID,aircraftQty,aircraftNamePrefix,loadoutDBID,missionName)
    local baseData = ScenEdit_GetUnit({side=side,name=baseName})
    for i = 1,aircraftQty do
        local unit = ScenEdit_AddUnit({
            side=side,
            name=aircraftNamePrefix.." #"..i,
            type='Aircraft',
            dbid=aircraftDBID,
            loadoutid=loadoutDBID,
            base=baseData.guid,

        })
        if missionName ~= nil then
            local missionData = ScenEdit_GetMission(side,missionName)
            ScenEdit_AssignUnitToMission(unit.guid,missionData.guid)
        end
    end
    print (baseName..' done..........')
end

if a == 1 then 
        CreateAirBase('PLA AB (PLAAF)',{},1,1,1) -- need to find suitiable locations 

       --[[ ScenEdit_AddReferencePoint( 
        {side='PLA', name='CAP1A', RelativeTo='PLA AB (PLAAF)', bearing=0 ,distance=35, clear=true },
        {side='PLA', name='CAP1B', RelativeTo='PLA AB (PLAAF)', bearing=45 ,distance=35, clear=true }, -- need to figure out how to add a table of RPs dynamic to a generated unit
        {side='PLA', name='CAP1C', RelativeTo='PLA AB (PLAAF)', bearing=90 ,distance=35, clear=true },
        {side='PLA', name='CAP1D', RelativeTo='PLA AB (PLAAF)', bearing=180 ,distance=35, clear=true })

        ScenEdit_AddMission('PLA','CAP1','patrol',{type='AAW'})
        ScenEdit_SetMission('PLA', 'CAP1', {isactive=true,{} }) -- need to add the table of RPs to this 
        ]]-- 
        AddACToBase('PLA AB (PLAAF)', 2463, 12, 'J-20B Mighty', 9606, 'CAP1')
        AddACToBase('PLA AB (PLAAF)', 5013, 24, 'J-20B-', 28006, 'CAP1')
        AddACToBase('PLA AB (PLAAF)', 5013, 12, 'J-20B-', 28006, 'CAP1')
        AddACToBase('PLA AB (PLAAF)', 5435, 16, 'J-6F-', 29946, 'Mock')
        AddACToBase('PLA AB (PLAAF)', 4547, 12, 'Su-35K Flanker E-', 23066, 'CAP1')
elseif a == 2 then 
 --- wip 
elseif a == 3 then 
--- wip



thanks in advance!
LettuceTurnipTheBeet
Posts: 10
Joined: Sat Dec 18, 2021 12:09 pm

Re: Generate Patrol Mission & RPs @ Randomized airbase

Post by LettuceTurnipTheBeet »

I'm note entirely sure what you mean by hardcoded - your code already adds 4 reference points relative to the base added. But I guess you could generate a circle of reference points and set them as the patrol area for your mission.

If you modified the CreateAirBase() function to return the base unit with lat, lon then it could look something like this.

Code: Select all

-- Creates a cirle of reference points around the provided coordinates
function createPatrolArea(base, missionName, numPoints, range)
  	local refPoints = {}
  	local pointsTable = World_GetCircleFromPoint({latitude=base.latitude,longitude=base.longitude, numpoints=numPoints,radius=range})
  	local counter = 1
  	for k,v in ipairs(pointsTable) do
    	table.insert(refPoints, ScenEdit_AddReferencePoint({side=theside,latitude=v.latitude,longitude=v.longitude, relativeto=base.guid, name=missionName .. "-" .. counter}).name)
    counter = counter + 1
  end
  return refPoints
end
and then set it as your patrol zone.

Code: Select all

patrolZone = createPatrolArea(base, 'CAP1', 6 --for example, 35)
ScenEdit_SetMission('PLA', 'CAP1', {patrolzone=patrolZone})
Post Reply

Return to “Lua Legion”