|
KnightHawk75 -> RE: Defining closest contact to unit (7/17/2021 4:43:46 PM)
|
quote:
Thanks KnightHawk, that works really nicely. I appreciate the way you wrapped up the initial code using pcall, that's really clean. If I then wanted to use the function to direct a unit to engage the closest contact (and closest contact only), would it be as simple as using ScenEdit_AssignUnitAsTarget with, for example an associated counter-air mission, instead of printing to console? It's a bit unclear in the Command Lua docs how to call that function, ie does it only require the contact guid as a parameter? Glad it works for you. As for adding things, yup you can just add what you want to the return table, actually if you want you could just return the whole contact wrapper for each entry if you needed that data later in the process and wanted to save yourself gets later. Trading some memory for a little perf and potentially less code. For example if changed part of the table building code to look like:
local range=99999;
local ct;
for _,v in pairs(contacts) do
retval,ct = pcall(ScenEdit_GetContact,{side=side.name,guid=v.guid}); --get actual contact wrapper.
if (retval ==true) and ct ~=nil then
range = getRangeToContact(unit.guid,v.guid);
if (range ~=nil) and range < 99999 then -- if valid range then add otherwise skip.
--print(string.format('%sRange from %s to unit %s is %.2f nm', fn,tostring(v.name), tostring(unit.name), range)); --test by printing to console
table.insert(myTempTable, {contactname=v.name, contactguid=v.guid,range=range,contactwrap=ct});
end;
end
retval,ct,range=nil,nil,99999;
end
Then you'd have all the contact details later if you needed them. re: Attacking - Well you could do that sure. Also if you wanted a contact attacked immediately by a specific unit like in the above example you could just use ScenEdit_AttackContact(), as ScenEdit_AssignUnitAsTarget is I think more specific to Strike|AirIntercept missions. So say we have changed the sample such that it now returns the .contactwrap field. Now let's change the sample usage to either assign the contact to a strike or air intercept mission depending on what the type of the contact is....I'm just going to use the first sample case since it returns "all" types of contacts where as the second only return aircraft where type is a non-issue.
--Sample usage return only closest.
local u = SE_GetUnit({name="testunit",side="a"}) --just a sample.
local sometable = closest_contact_to(u.name) -- just a sample...I'd actually change the func just to take-in a unitguid but i digress.
local retval; --holds return val from SE commands in case you want to check it.
--these are just sample attackcontact option tables to give idea of syntax\options.
local sampleAttackTypes = {
[1]={mode=0},
[2]={mode=1,weapon=3567,qty=1},
[3]={mode=1,weapon=2442,qty=1,latitude='N 41.45.0',longitude='W 87.37.0'},
[4]={mode=1,mount=3017,weapon=2442,qty=1,latitude='N 41.45.00',longitude='W 87.37.0',course={{latitude='N 39.30.0',longitude='W 93.13.43'},
{latitude='N 39.51.0',longitude='W 92.34.17'}, {latitude='N 41.26.28',longitude='W 88.38.48'}}},
[5]={mode=1,mount=3017,weapon=2941,qty=2,course={{latitude='N 39.30.0',longitude='W 93.13.43'},{latitude='N 39.51.0',longitude='W 92.34.17'},
{latitude='N 41.26.28',longitude='W 88.38.48'}}}
}
if sometable ~=nil then
print(string.format('Closest contact was %s @ %.2f nm known altitude(m): %.2f', tostring(sometable.contactname), sometable.range, tostring(sometable.contactwrap.altitude)));
if sometable.contactwrap.typed==0 or sometable.contactwrap.typed == 1 then --"Air" or "Missile" might want to have others here.
retval = ScenEdit_AssignUnitAsTarget(sometable.contactguid,"SomeAirInterceptMission")
--Samples to attack the contact right away with the unit doing the range checking.
--retval = ScenEdit_AttackContact(u.guid,sometable.contactguid,sampleAttackTypes[1]) --automatic [like f1] use system logic
--retval = ScenEdit_AttackContact(u.guid,sometable.contactguid,sampleAttackTypes[2]) --manual [like shift-f1] using loadout wpns
else --ship,mobile or fixed facility do something else?
retval = ScenEdit_AssignUnitAsTarget(sometable.contactguid,"SomeStrikeMission")
--Samples to attack the contact right away with the unit doing the range checking.
--retval = ScenEdit_AttackContact(u.guid,sometable.contactguid,sampleAttackTypes[5]) --manual [like shift-f1] launched from specific mount and with a course specified.
--retval = ScenEdit_AttackContact(u.guid,"BOL",sampleAttackTypes[3]) --BOL manual [like ctrl-f1] using loadout wpns.
--retval = ScenEdit_AttackContact(u.guid,"BOL",sampleAttackTypes[4]) --BOL manual [like ctrl-f1] launched from specific mount and with a course specified.
end
else
print("Something went very wrong.");
end;
Btw the weapons there in 2,3,4,5 are aim-260,mald-j,mald-j, and a tomahawk-i respective, just to give you an better idea of usage, and if no course= is given during attackcontact for munitions that can take them, you'll usually get the typical dogleg logic range permitting automatically'.
|
|
|
|