Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Share your best strategies and tactics with other players by posting them here.

Moderator: MOD_Command

Post Reply
Kitchens Sink
Posts: 402
Joined: Sun May 04, 2014 8:55 pm

Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by Kitchens Sink »

Hope some Lua gurus can help me.

I'm trying to change the Weapons Doctrine of a friendly Task Force from "Surface Weapons Hold" to "Surface Weapons Free", but only when a particular enemy surface vessel (part of an enemy Task Force) is within a certain distance from my TF (or a unit of my TF). I know that if the enemy unit is within 100nm, then his whole TF is within range and I can unload a large Anti-ship missile salvo at all the ships at one time, not shoot piecemeal as they come into range.

I know how to do the Weapons Doctrine change, but I am not sure how to set up the trigger or condition. What I have come up with so far is:

Trigger: Enemy "Unit A" is detected
Condition: ??
Action: ScenEdit_SetDoctrine({side="Me", mission="My TF Sea Control"}, {weapon_control_status_surface="0"})

So, how do I set up the Condition that enemy Unit A has to be within 100nm before the Doctrine is changed? Both TF's are moving, so I don't want to use Unit Enters Area because that could vary. Also changing the WRA missile firing distance wouldn't do what I want.

Thanks for any help!
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by michaelm75au »

In SR7, you can now get a 'range to contact'. The condition would be something like this:
....
local answer = false
local con = ScenEdit_UnitX().ascontact[1].name
local u = ScenEdit_GetUnit({name='LST 4001 Osumi', guid='8269b881-20ce-4f2e-baa0-6823e46d55a4'})
local dist = u:rangetotarget( con)
if dist < 100 then
print('target in range (' .. dist ..'NM)')
answer=true
else
print('target not in range (' .. dist ..'NM)')
end
return answer
....
Michael
Kitchens Sink
Posts: 402
Joined: Sun May 04, 2014 8:55 pm

RE: Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by Kitchens Sink »

Great! Thanks for the help. I'll give it a shot.
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by michaelm75au »

I have tuned this some to be more useful as a condition. Note that I am using the contact generated by detecting this unit. You could 'cheat' and just use the UnitX (which is the actual real unit) to get the precise information about the triggering unit.

---
local x,y
local answer = false
-- unit base range on
local u = ScenEdit_GetUnit({name='LST 4001 Osumi', guid='8269b881-20ce-4f2e-baa0-6823e46d55a4'})
-- unit that triggered event
local contact = ScenEdit_UnitX()
-- this unit seen as a contact by others
local con = contact.ascontact
-- not generated as a contact yet
if #con ~= 0 then
-- listed as a contact by some side
-- what is the side id for the base unit
local side = ScenEdit_GetSideOptions({side=u.side}).guid
-- find the side's entry in the triggering unit's contact list
for x = 1, #con do
if con[x].side == side then
-- contact on my side
local mycontact = con[x].guid
-- distance between my base unit and the contact
local dist = u:rangetotarget( mycontact)
if dist < 100 then
print('target in range (' .. dist ..'NM)')
answer=true
else
print('target not in range (' .. dist ..'NM)')
end
end
end
end
return answer
Michael
Kitchens Sink
Posts: 402
Joined: Sun May 04, 2014 8:55 pm

RE: Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by Kitchens Sink »

Thanks for your work on this Michael, much appreciated.
Kitchens Sink
Posts: 402
Joined: Sun May 04, 2014 8:55 pm

RE: Lua Script Question: Changing Weapons Doctrine based on Distance to enemy unit

Post by Kitchens Sink »

Just for general info, I thought up another way to do this:

I set up an semicircle-shaped area about 100nm out from my task force, and made the reference points comprising the area Fixed Bearing to the Task force Lead, so the area moves as my task force moves. Then I used "Unit Enters Area" as the Trigger, with no condition set.

Just another way to do the same thing I guess.
Post Reply

Return to “The War Room”