Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

LUA - Actions based on Emitter type?

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> LUA - Actions based on Emitter type? Page: [1]
Login
Message << Older Topic   Newer Topic >>
LUA - Actions based on Emitter type? - 2/16/2018 3:11:09 PM   
AlGrant


Posts: 833
Joined: 8/18/2015
Status: offline

Can anybody think if a way to use LUA to trigger certain actions based on the type of emitter detected?

The sort of thing I'd like to do is.
If a SKUNK is transmitting on a military type of radar (FC/Air Search etc) then mark it as Hostile or Unfriendly.

When ships are on a Patrol mission they tend to go and investigate the closest contact.
If one ship is transmitting on a Nav radar and another has all sorts of military stuff burning and turning then that's the one I'd want them to go and check first ..... In most scenarios, warships are more of a threat than fishing boats!

It would, of course, allow a player to try sneak in close mimicking commercial traffic.

I've looked at this but can't see an easy way to do it. It would need to be easy (or at least a working example) due to my level of LUA

_____________________________

GOD'S EYE DISABLED.
Post #: 1
RE: LUA - Actions based on Emitter type? - 2/16/2018 7:24:45 PM   
michaelm75au


Posts: 12471
Joined: 5/5/2001
From: Sydney, Australia
Status: offline
In the Detection event, SE_UnitC returns the contact unit information. One of the attributes of that is UnitC.emissions which is a table of detected emissions; however it is limited to name and age rather type.
Not sure if this would be a starting point.

_____________________________

Michael

(in reply to AlGrant)
Post #: 2
RE: LUA - Actions based on Emitter type? - 2/16/2018 7:45:39 PM   
apache85

 

Posts: 1464
Joined: 12/18/2014
From: Melbourne, Australia
Status: offline
Ok, while I wouldn't call it 'easy', here is some code that will do what you asked. A description of how it works follows in the next post.

As michaelm75au says, UnitC is a shortcut for what I used below, but I had trouble getting UnitC working so went the long way around:

quote:

scenarioEmitters = {
{ name = 'DA.05 [RAWS-3] (Radar, Target Indicator, 2D Surface-to-Air & Surface-to-Surface)',
emitterType = 'Military' },
{ name = 'Decca 1229 (Radar, Surface Search & Navigation)',
emitterType = 'Commercial' },
{ name = 'LW.03 [SGR.114] (Radar, Air Search, 2D Medium-Range)',
emitterType = 'Military' },
{ name = 'M.45 (Radar, FCR, Weapon Director)',
emitterType = 'Military' },
}

local result = false
local contact = ScenEdit_UnitX()
local receiver = ScenEdit_UnitY()
local sideContacts = ScenEdit_GetContacts(receiver.unit.side)

for k,v in ipairs (sideContacts) do
contactData = ScenEdit_GetContact({side=receiver.unit.side, guid=v.guid})
if contactData.actualunitid == contact.guid then
emissions = contactData.emissions
print (emissions)
break
end
end

if emissions ~= nil then
for key,value in ipairs (emissions) do
for list, emission in ipairs (scenarioEmitters) do
if emission.name == value.Name and emission.emitterType == 'Military' then
print (contact.name .. ' determined to be using military type emitter: '..value.Name)
result = true
end
end
end
end

return result


Example file attached and explanation to follow.

Attachment (1)

< Message edited by apache85 -- 2/16/2018 7:46:06 PM >


_____________________________


(in reply to michaelm75au)
Post #: 3
RE: LUA - Actions based on Emitter type? - 2/16/2018 8:35:46 PM   
apache85

 

Posts: 1464
Joined: 12/18/2014
From: Melbourne, Australia
Status: offline
quote:


scenarioEmitters = { --A table including the names of emitters and whether they're military or civilian
{ name = 'DA.05 [RAWS-3] (Radar, Target Indicator, 2D Surface-to-Air & Surface-to-Surface)',
emitterType = 'Military' },
{ name = 'Decca 1229 (Radar, Surface Search & Navigation)',
emitterType = 'Commercial' }, --In this example this is the only non-military emitter
{ name = 'LW.03 [SGR.114] (Radar, Air Search, 2D Medium-Range)',
emitterType = 'Military' },
{ name = 'M.45 (Radar, FCR, Weapon Director)',
emitterType = 'Military' },
}

local result = false --Set the initial/default result to false
local contact = ScenEdit_UnitX() --Get the details of the detected unit
local receiver = ScenEdit_UnitY() --Get the details of the detecting unit
local sideContacts = ScenEdit_GetContacts(receiver.unit.side) --Return a list of all contacts for the detecting units side

for k,v in ipairs (sideContacts) do --Go through that list and..
contactData = ScenEdit_GetContact({side=receiver.unit.side, guid=v.guid}) --Get the full data for each contact
if contactData.actualunitid == contact.guid then --See if this unit is the one that was detected, if it is then...
emissions = contactData.emissions --Store a list of the emissions detected
break --And break out of the loop (more useful in larger scenarios)
end
end

if emissions ~= nil then --If the list of emissions isn't empty
for key,value in ipairs (emissions) do --For each entry in that list
for list, emission in ipairs (scenarioEmitters) do --Compare it to each entry in the list of emitters in the scenario
if emission.name == value.Name and emission.emitterType == 'Military' then --If the names match, and the type is military emitter then...
print (contact.name .. ' determined to be using military type emitter: '..value.Name) --Print out a message to the console
result = true --Change the result to true
end
end
end
end

return result --Return the result (be it true or false)


Above is a commented version of the code which steps you through what each line means and does.

The first step would be to get a list of the names of the emitters you want to classify as military type emitters; you could do this manually (which risks data entry errors, remember Lua is a case sensitive language) or do what I did and use GetContact.emissions to list emitters and then edit the table to suit your needs (have a look here for an introduction to tables).

As mentioned above, the use of UnitX, UnitY and GetContacts was a work-around because I had trouble getting UnitC to work (I'm sure this is user error on my part, but this goes to show that there's nearly always more than one way to approach a problem in Lua).

the 'for k,v in ipairs (sideContacts) do' command really confused me when I first started using Lua. In a basic sense, the k and v represent keys and values (refer to the link above for a description of what this means) in a table. The function ipairs goes through the table entered in the brackets (sideContacts here) and performs the relevant code on each entry. As a simple demonstration, type this into your console:
quote:


list = {
'A','B','C','D'
}

for k,v in ipairs(list) do
print (k)
print (v)
end

Running this code will get you an output of:
quote:

1
A
2
B
3
C
4
D

The numbers are the keys, and the letters are the values. You can substitute 'k' and 'v' for whatever you like, and if nesting these functions you absolutely must substitute them for something--having multiple nested functions trying to reference the same variables simply won't work. As always try to use something that is meaningful; a few extra keystrokes here now will save you a lot of confusion when you revisit the code weeks or months down the track.

Once we're going through the list, we then compare some values from the tables. Note that the if comparative statement checks to see whether the table values contactData.actualunitid and contact.guid are equal to each other using double equals signs ==. A common mistake (I do this when I'm tired) is to use one equals sign, which will return an error. Essentially == is comparative, and = is declarative, so we use == to see if two things are equal, while we use = to say this variable is 'whatever'. Note also that if the criteria for the if comparative statement are not met, nothing happens.

If the if criteria are met, we then store a list of emissions from that contact as the variable emissions and break out of the for loop. This is of little consequence in the demonstration .scen file with only one possible contact, but in large scenarios this will cut down on unnecessary evaluation (it's only possible to have one match for GUID as GUIDs are unique; any further evaluation is pointless after there's a match).

Next we check to see if there's anything in the emissions variable. If it returns nil nothing happens and the script ends with the default result of false. Situations where this might occur include the contact being spotted visually or detected via radar where there's no emissions, in which case this script is not relevant. If there's data stored in the emissions variable, it is then evaluated using nested ipairs functions. What this does is take each entry in the emissions table, and check it against each entry in the list of scenarioEmitters. If the names match, and the listed type for that name is 'Military' then we print out a message for the console user (generally invisible to a player in a finished scenario, useful for debugging) and change the result to true.

Finally, the result is returned. This will simply state 'Yes' if true in the console, or if used in a condition will allow the event action to be performed. If the result is false, it will state 'No' in the console and block the action from being performed if used in a condition.

I hope that explains everything, please feel free to ask questions if there's something I didn't explain clearly

I've also attached the .lua file in a zip file, the forum doesn't handle code formatting well so opening this in notepad++ or notepad will allow you to see the indents etc that make reading code much easier.

Attachment (1)

_____________________________


(in reply to apache85)
Post #: 4
RE: LUA - Actions based on Emitter type? - 2/17/2018 1:19:37 AM   
michaelm75au


Posts: 12471
Joined: 5/5/2001
From: Sydney, Australia
Status: offline
Some past change has broken the UnitC as contact in some cases of the event 'Detected'. I will look into it.

_____________________________

Michael

(in reply to apache85)
Post #: 5
RE: LUA - Actions based on Emitter type? - 2/17/2018 2:23:25 AM   
michaelm75au


Posts: 12471
Joined: 5/5/2001
From: Sydney, Australia
Status: offline
The contact.emissions will be expand to include the type, role and max range of the detected sensors. Similar to the sensor information available in the unit wrapper.
This may help.

< Message edited by michaelm75au -- 2/17/2018 2:27:38 AM >


_____________________________

Michael

(in reply to michaelm75au)
Post #: 6
RE: LUA - Actions based on Emitter type? - 2/17/2018 10:09:14 AM   
AlGrant


Posts: 833
Joined: 8/18/2015
Status: offline


Gentlemen, this is very much appreciated - thanks!

Having ships or MPA's mostly ignoring a unit transmitting on Air Search or Fire Control radars just because the generic nav radar was a little closer is something that bugs me from time to time, but I couldn't find a way to get around it. I'll certainly give this a go - the example scenario means I might even get it working Will also look forward to the updates contact.emissions

In an ideal world, I'd like to see some options that can be set via the doctrine, where units will prioritise 'threat' emitters within a certain area/range.
Allowing a player to only transmit on Nav radars to try imitate commercial shipping whilst on a transit ..... Which is certainly something that has happened on ships I've been on!
Transmit on an off the shelf commercial nav radar and rig lighting at night to look like something you're not


Once again - THANKS

_____________________________

GOD'S EYE DISABLED.

(in reply to michaelm75au)
Post #: 7
RE: LUA - Actions based on Emitter type? - 2/19/2018 9:23:27 AM   
AlGrant


Posts: 833
Joined: 8/18/2015
Status: offline
apache85,

Thanks very much for the LUA and test scenario.
I've had a look and switched various emitters on/off and see the correct Yes/No result.

I need to have a closer look and see if I can get it working in a larger scenario, but it certainly does the job.
I can see it would be really useful for something like an MPA mission which required searching for a specific unit with known emitters and triggering a mission based on that.

I should get some time to play around with it in a couple of days - but your script is very much appreciated.

Al

_____________________________

GOD'S EYE DISABLED.

(in reply to AlGrant)
Post #: 8
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> LUA - Actions based on Emitter type? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.145