OutOfComms within radius of multiple units

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
texag11
Posts: 12
Joined: Sun Jan 19, 2014 1:08 pm

OutOfComms within radius of multiple units

Post by texag11 »

I'm working on a large SCS scenario were the Chinese have advanced Trawlers acting as pickets (and appear as a neutral side) and some have Comms jammers on board. My idea was that if a players unit gets within a certain distance of these pickets they go "nocomms" until the trawler is delt with. I was recently watching P. Gatcombs "Loss of communications at a certain distance" video and got me thinking of a way to use this to simulate comms jamming in the commercial version.

I can get the below LUA Script to work with 1 unit but have been unable to find a way to do this with multiple units (8 total). I've tried just replicating the script for each of the jammer boats but it only does the last one.


units = VP_GetSide({side='INDOPACOM'}).units
local center = VP_GetUnit({name='Jammer Picket Boat 1',side='Merchants 1'})

for k,v in ipairs(units) do
local dist = Tool_Range(v.guid,center.guid)
if dist < 35 then
ScenEdit_SetUnit({guid=v.guid,OutOfComms="True"})
else
ScenEdit_SetUnit({guid=v.guid,OutOfComms="False"})
end
end
User avatar
blu3s
Posts: 1222
Joined: Fri Jul 08, 2022 9:45 am

Re: OutOfComms within radius of multiple units

Post by blu3s »

You can store the jam units in a table with their GUID

Code: Select all

jam_units ={
    {name='Jammer Picket Boat 1', guid ="XXAA-VVAA"},
    {name='Jammer Picket Boat 2', guid ="XXAA-VVZZ"},
    {name='Jammer Picket Boat 3', guid ="XXAA-VVFF"}
    
}
---
local units = VP_GetSide({side='INDOPACOM'}).units
for _,v in ipairs(units) do
    local unit = SE_GetUnit({guid = v.guid})
    for _, jammer in ipairs(jam_units) do
        local jam = SE_GetUnit({guid = jammer.guid})
        local dist = Tool_Range(jam.guid,unit.guid)
        if dist < 35 then 
            ScenEdit_SetUnit({guid=unit.guid,OutOfComms="True"})
        else
            ScenEdit_SetUnit({guid=unit.guid,OutOfComms="False"})
        end
end


But if it is a scenario with many units it will be a bit hard to always go through those arrays... you can create an area around the ships and get the units that are inside that area and make them outofcomms, and to return the units that are outofcoms, you can save them in a table and as they will be few units you can go through it to check if they are inside the area with the methods that the Lua API provides.
texag11
Posts: 12
Joined: Sun Jan 19, 2014 1:08 pm

Re: OutOfComms within radius of multiple units

Post by texag11 »

I gave that a try and the script works but only for whatever unit is last in the table. Whichever unit is last in the list is the only one that triggers a "Indopacom" unit to go out of comms. I test it by moving other units to the last row to confirm.

jam_units ={
{name='Jammer Picket Boat 1', guid ="0FZNQD-0HMGVQLH5KH6S"},
{name='Jammer Picket Boat 2', guid ="0FZNQD-0HMGVQLH5KO2S"},
{name='Jammer Picket Boat 3', guid ="0FZNQD-0HMGVQLH5L5N4"},
{name='Jammer Picket Boat 4', guid ="0FZNQD-0HMGVQLH5L3PT"},
{name='Jammer Picket Boat 5', guid ="0FZNQD-0HMGVQLH5KL0B"},
{name='Jammer Picket Boat 6', guid ="0FZNQD-0HMGVQLH5MDGS"},
{name='Jammer Picket Boat 7', guid ="0FZNQD-0HMGVQLH5MJNU"},
{name='Jammer Picket Boat 8', guid ="0FZNQD-0HMOV20EV7Q19"}
}
units = VP_GetSide({side='INDOPACOM'}).units
for _,v in ipairs(units) do
local unit = SE_GetUnit({guid = v.guid})
for _, jammer in ipairs(jam_units) do
local jam = SE_GetUnit({guid = jammer.guid})
local dist = Tool_Range(jam.guid,unit.guid)
if dist < 35 then
ScenEdit_SetUnit({guid=unit.guid,OutOfComms="True"})
else
ScenEdit_SetUnit({guid=unit.guid,OutOfComms="False"})
end
end
end
User avatar
blu3s
Posts: 1222
Joined: Fri Jul 08, 2022 9:45 am

Re: OutOfComms within radius of multiple units

Post by blu3s »

I edit my last msg bc is a better way to achieve that you want.

You must implement a function that passing a unit and a list of jammers, return true if the unit is in range with any of the jammers, and false otherwise. And based in this true or false value, set the unit out of comms.

Something like

Function IsJammed(unit, t_jammers)
For _, Jam in ipairs(t_jammers) do
If distance btw Jam and unit < 30 return true
End if
End for
Return false

With that way assure you that the unit is set out of comms if at least one jammer is near.

Sorry but until tomorrow I cant provide you better code
texag11
Posts: 12
Joined: Sun Jan 19, 2014 1:08 pm

Re: OutOfComms within radius of multiple units

Post by texag11 »

Not a problem, you've been very helpful! I'm not a complete rookie but my knowledge so far of LUA is still pretty beginner.
User avatar
blu3s
Posts: 1222
Joined: Fri Jul 08, 2022 9:45 am

Re: OutOfComms within radius of multiple units

Post by blu3s »

Here it is

Code: Select all

function isJammed(unit)
  local jam_units ={
  {name='Jammer Picket Boat 1', guid ="0FZNQD-0HMGVQLH5KH6S"},
  {name='Jammer Picket Boat 2', guid ="0FZNQD-0HMGVQLH5KO2S"},
  {name='Jammer Picket Boat 3', guid ="0FZNQD-0HMGVQLH5L5N4"},
  {name='Jammer Picket Boat 4', guid ="0FZNQD-0HMGVQLH5L3PT"},
  {name='Jammer Picket Boat 5', guid ="0FZNQD-0HMGVQLH5KL0B"},
  {name='Jammer Picket Boat 6', guid ="0FZNQD-0HMGVQLH5MDGS"},
  {name='Jammer Picket Boat 7', guid ="0FZNQD-0HMGVQLH5MJNU"},
  {name='Jammer Picket Boat 8', guid ="0FZNQD-0HMOV20EV7Q19"}
  }
  for _,jam in ipairs(jam_units) do
    if Tool_Range(unit.guid,jam.guid) <= 35 then
      return true
    end
  end
  return false
end

local units = VP_GetSide({side='INDOPACOM'}).units
for _,v in ipairs(units) do
  local unit = SE_GetUnit({guid = v.guid})
  if isJammed(unit) then
    ScenEdit_SetUnit({guid=unit.guid,outofcomms=true})
  else
    ScenEdit_SetUnit({guid=unit.guid,outofcomms=false})
  end
end
I have not tested it but it should work
Post Reply

Return to “Lua Legion”