SA: Circle on Contact + NoNav

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

SA: Circle on Contact + NoNav

Post by Parel803 »

Good evening,
Since I'm not very smart with the Lua first question:
I made a initiate of a circle on a selected unit with input box. The question is if it is possible to do this on a contact. I think I need the Lat/Long of the selected contact and with that the GuiD of it's unit version?
I thought I had help on soething like this but could not found the correct one in the forum.

Code: Select all

-- Function to get the selected unit's GUID and latitude/longitude
local function getSelectedUnitInfo()
    local selectedUnits = ScenEdit_SelectedUnits()
    if selectedUnits ~= nil then
        if selectedUnits.units ~= nil and #selectedUnits.units == 1 then
            local unitGuid = selectedUnits.units[1].guid
            local unitInfo = ScenEdit_GetUnit({ guid = unitGuid })
            if unitInfo and unitInfo.latitude and unitInfo.longitude then
                return unitGuid, unitInfo.latitude, unitInfo.longitude
            else
                print("Selected unit does not have latitude and longitude information.")
            end
        else
            print("Please select a single unit.")
        end
    else
        print("No units selected.")
    end
    return nil, nil, nil
end

Code: Select all

-- Fixed number of reference points
local numReferencePoints = 4  -- temp 4 to ease the next step

-- Function to create a circle of reference points around a specified location
local function createCircleReferencePoints(numReferencePoints, latitude, longitude, range)
    if latitude and longitude then
        local circle = World_GetCircleFromPoint({
            latitude = latitude,
            longitude = longitude,
            numpoints = numReferencePoints,
            radius = range
        })

        for index, point in ipairs(circle) do
            local rp = ScenEdit_AddReferencePoint({
                side = 'Blue',
                name = 'tst ' .. index,
                latitude = point.latitude,
                longitude = point.longitude
            })
        end
    end
end

-- Modify this value to set the desired range (in nautical miles)
local range = 25

-- Call the function to get the selected unit's info
local unitGuid, latitude, longitude = getSelectedUnitInfo()

-- Call the function to create a circle of reference points around the selected unit's location with the fixed number of reference points
createCircleReferencePoints(numReferencePoints, latitude, longitude, range)
Does anyone knows of and how to get this on a selected contact (instead of the unit).

best regards GJ
User avatar
blu3s
Posts: 663
Joined: Fri Jul 08, 2022 9:45 am

Re: SA: Circle on Contact + NoNav

Post by blu3s »

You have latitude and longitude in your contact wrapper

https://commandlua.github.io/assets/Wra ... er_Contact

Edit to say that you can iterate over the list of contacts on your side and compare unit.guid to contact.actualunitguid to match the selected contact with the selected unit.
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: SA: Circle on Contact + NoNav

Post by Parel803 »

Good evening,

I'll try again.

regards GJ
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: SA: Circle on Contact + NoNav

Post by Parel803 »

I do apologize for giving up but I cannot figure out to get the lat/long of the selected contact to be used to initiate a cicle around it.
My lua still in embryotic state so if someone has the time to help me than appriciated if not this is a dead end for me.
regards GJ
User avatar
blu3s
Posts: 663
Joined: Fri Jul 08, 2022 9:45 am

Re: SA: Circle on Contact + NoNav

Post by blu3s »

The contact wrapper has .latitude and .longitude to acces their latitude and longitude as said earlier.

You can create an event trigger based on unit detection/classification for SAM units and obtain the contact via

local contact= ScenEdit_UnitC()

print(contact.latitude)
print(contact.longitude)


If you want to click on a contact and perform a Special Action you can use:

Code: Select all

local selected = ScenEdit_SelectedUnits( )
local guid_contact = selected.contacts[1].guid
local contact = ScenEdit_GetContact({guid=guid_contact, side='playerside'})

if contact ~= nil then
print(contact.latitude)
print(contact.longitude)
end
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: SA: Circle on Contact + NoNav

Post by Parel803 »

Blu3s,
Thank you for your time and answer.
I'm gonna try this weekend. See if
I can get the lat/long of a contact (instead of an unit) to use in my special Action.
thx again, best regards GJ
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: SA: Circle on Contact + NoNav

Post by Parel803 »

Good Evening,
I think I have, with help, found a way to make a circle on a contact.

Code: Select all

-- Function to retrieve information about the selected unit
local function getSelectedUnitInfo()
    local IDselect = ScenEdit_SelectedUnits()
    if IDselect.contacts[1] then
      local guid_contact = IDselect.contacts[1].guid
      local myC = ScenEdit_GetContact({guid=guid_contact, side='playerside'})
      local myU = ScenEdit_GetUnit({guid = myC.actualunitid})
      return myU
    else
      -- Handle the case where no contact is selected
      print("No unit selected. Please select a unit and try again.")
      return nil
    end
  end
  
  -- Function to get a number from the user within a specified range
  local function GetNumberFromUser(displaytext, minnum, maxnum)
    local retval
    repeat
      retval = ScenEdit_InputBox(displaytext)
      if retval == nil or retval == "" then
        ScenEdit_MsgBox('Invalid input. Please enter a valid number.', 0)
      elseif tonumber(retval) == nil or tonumber(retval) < minnum or tonumber(retval) > maxnum then
        ScenEdit_MsgBox('Invalid input. Please enter a number within the specified range.', 0)
      end
    until tonumber(retval) ~= nil and tonumber(retval) >= minnum and tonumber(retval) <= maxnum
    return tonumber(retval)
  end
  
  -- Function to create a circle of reference points around a specified location
  local function createCircleReferencePoints(numReferencePoints, latitude, longitude, range)
    if latitude and longitude then
      local circle = World_GetCircleFromPoint({ latitude = latitude, longitude = longitude, numpoints = numReferencePoints, radius = range })
      for index, point in ipairs(circle) do
        local rp = ScenEdit_AddReferencePoint({ side = 'playerside', name = 't#' .. index, latitude = point.latitude, longitude = point.longitude })
      end
    end
  end
  

Code: Select all

  
  -- Get information about the selected unit
  local selectedUnit = getSelectedUnitInfo()
  
  -- Check if a unit was found
  if selectedUnit then
    -- Get user input for range
    local range = GetNumberFromUser("Enter the desired range (1-200 nautical miles):", 1, 200)
  
    -- Get the unit's latitude and longitude
    local latitude = selectedUnit.latitude
    local longitude = selectedUnit.longitude
  
    -- Call the function to create the reference points
    createCircleReferencePoints(8, latitude, longitude, range) -- Use the fixed number of reference points
  else
    -- No unit was found, handle the error as needed
  end
Please say if it can be done easier or less errors etc.
Next to try making a NoNav with them.

Is it possible to adjust the "No Fire Zone" feature with Lua?

regards GJ
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: SA: Circle on Contact + NoNav

Post by Parel803 »

Good evening,

Hope someone can help me.
I added a NoNav zone and like to use the just initiated RP's.

Code: Select all

ScenEdit_AddZone('Blue', 0, {description='test3',  affects={'aircraft'}})
Then with

Code: Select all

local rps = ScenEdit_GetReferencePoints({side='Blue', area={'t#1','t#2','t#3','t#4','t#5','t#6','t#7','t#8'}})
ScenEdit_SetZone ('Blue', 0, {description='test3', area=rps})
It initiates new RP's on whole degrees crosses mostly around my circle

Looks like this I guess: https://www.matrixgames.com/forums/view ... v#p4777794

Any ideas how to add a NoNav zone using existing RP's?

best regards GJ

To add: in the RP manager the RP initiated for the circle has its name visible. The extra initated with the GetRP have blank name in manager but have name on screen.
NoNav3.png
NoNav3.png (1.36 MiB) Viewed 222 times
Post Reply

Return to “Lua Legion”