Difference between revisions of "Advanced Scripts"

From Weapons Release Wiki
Jump to: navigation, search
 
Line 110: Line 110:
 
end
 
end
  
 +
</syntaxhighlight>
 +
 +
===Comm Jamming Simulation===
 +
 +
This code comes from Rockpaperscissors, Whicker, Knighthawk75 and MichaelM75au.
 +
 +
To use you must get the guid of your "jammer" unit. If you are using the reference point method then set the reference point area as needed. Otherwise the distance method does not require it.
 +
 +
This is the distance method.
 +
<syntaxhighlight lang="lua" line='line'>
 +
local side = VP_GetSide({side='NATO Allied Air Command'}) --Set your side here
 +
 +
local jammerArea = {'Jammer 1', 'Jammer 2', 'Jammer 3', 'Jammer 4', 'Jammer 5', 'Jammer 6', 'Jammer 7', 'Jammer 8'} --Not needed for the distance method.
 +
 +
math.randomseed(os.time())
 +
local jammerGuid ='DEMUU8-0HLVBNMSNGQDO'
 +
local jammer = ScenEdit_GetUnit({guid=jammerGuid})
 +
local comp2 = 1
 +
 +
for k,v in ipairs(side.units) do
 +
local x = ScenEdit_GetUnit({ guid= v.guid })
 +
print("Unit name "..x.name)
 +
local distance = Tool_Range(v.guid, jammerGuid)
 +
print("Distance to target is "..distance)
 +
 +
if distance <= 100 then
 +
    local comp = -0.231*math.log(distance)+1.248
 +
    print("Comparison is "..comp)
 +
    local comp2 = 1
 +
    if comp > 0 then
 +
    comp2 = (comp2*(1-comp))
 +
    end
 +
    print('Comp 2 is: ' ..comp2)
 +
    local random = math.random()
 +
    print('....Calculating....')
 +
    print('random: '..random)
 +
    print(x.name..' has '..((1-comp2)*100)..'% chance of being jammed.')
 +
    if comp2 < random then
 +
    ScenEdit_SetUnit({guid=v.guid, OutOfComms="True"})
 +
    print(x.name.. " is out of comms")
 +
    else
 +
    ScenEdit_SetUnit({guid=v.guid, OutOfComms="False"})   
 +
    print(x.name.. " is NOT out of comms")
 +
    end
 +
else
 +
print(x.name.." is not in jamming range.")
 +
 +
end
 +
if x:inArea(jammerArea) == true then print("In!") else print("Out!") end
 +
end
 +
</syntaxhighlight>
 +
 +
This is the inArea method.
 +
 +
<syntaxhighlight lang="lua" line='line'>
 +
local side = VP_GetSide({side='NATO Allied Air Command'})
 +
 +
local jammerArea = {'Jammer 1', 'Jammer 2', 'Jammer 3', 'Jammer 4', 'Jammer 5', 'Jammer 6', 'Jammer 7', 'Jammer 8'}
 +
 +
math.randomseed(os.time())
 +
local jammerGuid ='DEMUU8-0HLVBNMSNGQDO'
 +
local jammer = ScenEdit_GetUnit({guid=jammerGuid})
 +
local comp2 = 1
 +
 +
for k,v in ipairs(side.units) do
 +
local x = ScenEdit_GetUnit({ guid= v.guid })
 +
print("Unit name "..x.name)
 +
local distance = Tool_Range(v.guid, jammerGuid)
 +
print("Distance to target is "..distance)
 +
 +
--if distance <= 100 then
 +
if x:inArea(jammerArea) == true then
 +
    local comp = -0.231*math.log(distance)+1.248
 +
    print("Comparison is "..comp)
 +
    local comp2 = 1
 +
    if comp > 0 then
 +
    comp2 = (comp2*(1-comp))
 +
    end
 +
    print('Comp 2 is: ' ..comp2)
 +
    local random = math.random()
 +
    print('....Calculating....')
 +
    print('random: '..random)
 +
    print(x.name..' has '..((1-comp2)*100)..'% chance of being jammed.')
 +
    if comp2 < random then
 +
    ScenEdit_SetUnit({guid=v.guid, OutOfComms="True"})
 +
    print(x.name.. " is out of comms")
 +
    else
 +
    ScenEdit_SetUnit({guid=v.guid, OutOfComms="False"})   
 +
    print(x.name.. " is NOT out of comms")
 +
    end
 +
else
 +
print(x.name.." is not in jamming range.")
 +
 +
end
 +
if x:inArea(jammerArea) == true then print("In!") else print("Out!") end
 +
end
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 23:28, 28 April 2020

Put your Advanced Lua scripts here. This is less for learning how the scripts work and instead giving working examples and showing the results. If this is not your script please attribute the author.

Please add a description of functionality and use the <syntaxhighlight lang="lua" line='line'>Lua code here</syntaxhighlight> tags.

Event to Add-Delete Trigger, Action, Event[edit]

  • This script simulates command degradation where it is turning radars back on after being attacked. You will also need a separate action to turn the radar OFF. This script turns them back on after a set amount of time.
 1 local detectingunit = ScenEdit_UnitY()
 2 print(detectingunit.unit.guid)
 3 
 4 local delayScore = ScenEdit_GetScore(detectingunit.unit.side)
 5 
 6 local currtime = ScenEdit_CurrentTime()
 7 print(currtime)
 8 local futuretime = currtime + 300 + delayScore
 9 print(futuretime)
10 local convtime = (os.date('%m-%d-%Y %H:%M:%S %p', futuretime))
11 print(convtime)
12 
13 local triggerunit = detectingunit.unit.guid .. convtime
14 
15 local triggergroup = detectingunit.unit.group.name
16 print(triggergroup)
17 
18 
19 ScenEdit_SetTrigger({mode='add',type='Time', name='trigger'.. triggerunit, time = convtime})
20 
21 local SetEMCON = "ScenEdit_SetEMCON('Group', '"..triggergroup.."', 'Inherit;Radar=Active')"
22 local RemoveT = "ScenEdit_SetTrigger({mode='remove', type = 'Time', name='trigger"..triggerunit.."' })"
23 local RemoveA = "ScenEdit_SetAction({mode='remove', type = 'Luascript', name= 'triggerunitAction"..triggerunit.."'})"
24 local RemoveE = "ScenEdit_SetEvent('TimeTest Event"..triggerunit.."', {mode='remove'})"
25 
26 local ScriptTextComp = SetEMCON.. "\r\n" ..RemoveE.. "\r\n" ..RemoveA.. "\r\n" ..RemoveT.. "\r\n" 
27 print(ScriptTextComp)
28 
29 ScenEdit_SetAction({mode='add', type='LuaScript', name= 'triggerunitAction'..triggerunit, ScriptText = ScriptTextComp})
30 
31 ScenEdit_SetEvent('TimeTest Event'..triggerunit, {mode='add', isActive=True})
32 
33 ScenEdit_SetEventTrigger('TimeTest Event'..triggerunit, {mode='add', name='trigger'.. triggerunit})
34 ScenEdit_SetEventAction('TimeTest Event'..triggerunit, {mode='add', name='triggerunitAction'..triggerunit})


No GPS or guided Weapons[edit]

  • This script simulates jamming on GPS or guided weapons. A huge cornerstone of modern weapons systems is being able to stand off and launch from extreme range. But what if you can't? Weapons like AAMRAM's, SDB's and Tomhawaks are affected while EO, IR, or SARH are not. (Second fuel section applies to even SARH etc. so comment it out if not needed.)
 1 math.randomseed(os.time()); 
 2 
 3 --Code courtesy of KnightHawk75, 2/1/19, http://www.matrixgames.com/forums/tm.asp?m=4587484&mpage=1&key=&#4588137
 4 
 5 
 6 --Ok so now for each entry in the table do our stuff. 
 7 
 8 myunit = UnitX() --get real object from name and guid listed in selectedUnits table. 
 9 if (myunit ~= nil) and (myunit.type == 'Weapon') and (myunit.course[1] ~= nil) then 
10 --Lets do the waypoint stuff first. 
11 local diceroll = math.random(1,20); 
12 local mywaypoint = {}; --fresh empty table 
13 if diceroll > 10 then --use that random value to 50% of time Add lon\lat & 50% of time Subtract lon\lat; this could probably be better. but I'm lazy 
14 mywaypoint = {{longitude=(myunit.course[1].longitude + (.0125 * math.random(1,100))),latitude=(myunit.course[1].latitude + (.0125 * math.random(1,100))),description='NotAWeaponTerminalPoint',presetAltitude=min,desiredAltitude=0,desiredSpeed=100}} 
15 else 
16 mywaypoint = {{longitude=(myunit.course[1].longitude - (.0125 * math.random(1,100))),latitude=(myunit.course[1].latitude - (.0125 * math.random(1,100))),description='NotAWeaponTerminalPoint',presetAltitude=min,desiredAltitude=0,desiredSpeed=100}} 
17 end 
18 print(mywaypoint) 
19 logstring = logstring .. string.format('Unit: %s diceroll=%s :\r\nGPS guidence corrupted new waypoint set for lon: %s lat: %s oldLon: %s oldLat: %s \r\n',tostring(myunit.name),tostring(diceroll),tostring(mywaypoint[1].longitude),tostring(mywaypoint[1].latitude),tostring(myunit.course[1].longitude),tostring(myunit.course[1].latitude)) 
20 myunit.course = mywaypoint; -- make the assigment replacing the waypoint\course table. 
21 
22 --now on to the unit's fuel 
23 local mynewfuel = myunit.fuel; -- make new a local copy of the fuel table. 
24 if mynewfuel ~= nil then --if not empty\nil then proceed. 
25 --for each item or key\value pair in myfuel do whats inside the for loop. 
26 -- helpful note 'ipairs()' is used when sequenced items like if the containing table entries are '1' '2' '3' etc. 
27 -- pairs() is similar but will just go though everything not stopping upon finding a nil or out of order sequence. 
28 for k,v in pairs(mynewfuel) do --k is the index ie [5003] v is the value, in this case another table {name=''..etc}. 
29 mynewfuel[k].current = 1.0 --set the value in our copy for this entry to 1 units 
30 end 
31 
32 myunit.fuel = mynewfuel; -- now that we're done replace the entire original fuel table with our modded copy. 
33 
34 myunit.fuel=1.0
35 
36 logstring = logstring .. '\r\n'; --add blank line before next unit itertation 
37 else 
38 print('Unit named ' .. tostring(u.name) .. ' fuel table was nil so it was not touched.') 
39 end 
40 else 
41 print('Could not obtain unit object for ' .. tostring(unit.name) .. ' or it was not of type: Weapon.'); 
42 end 
43 
44 --For SAM's, SARH, etc. Comment out if not needed. Use this in tandem but tie in to an altitude condition.
45 
46 if (myunit ~= nil) and (myunit.type == 'Weapon') then
47 local mynewfuel = myunit.fuel; -- make new a local copy of the  fuel table. 
48 --if mynewfuel ~= nil then --if not empty\nil then proceed. 
49 --for each item or key\value pair in myfuel do whats inside the for loop. 
50 -- helpful note 'ipairs()' is used when sequenced items like if the containing table entries are '1' '2' '3' etc. 
51 -- pairs() is similar but will just go though everything not stopping upon finding a nil or out of order sequence. 
52 for k,v in pairs(mynewfuel) do --k is the index ie [5003] v is the value, in this case another table {name=''..etc}. 
53 mynewfuel[k].current = (math.random(1,5) + 1.0) --set the value in our copy for this entry to 1 units 
54 myunit.fuel = mynewfuel
55 print(myunit.fuel)
56 end 
57 
58 myunit.fuel = mynewfuel;
59 end

Comm Jamming Simulation[edit]

This code comes from Rockpaperscissors, Whicker, Knighthawk75 and MichaelM75au.

To use you must get the guid of your "jammer" unit. If you are using the reference point method then set the reference point area as needed. Otherwise the distance method does not require it.

This is the distance method.

 1 local side = VP_GetSide({side='NATO Allied Air Command'}) --Set your side here
 2 
 3 local jammerArea = {'Jammer 1', 'Jammer 2', 'Jammer 3', 'Jammer 4', 'Jammer 5', 'Jammer 6', 'Jammer 7', 'Jammer 8'} --Not needed for the distance method.
 4 
 5 math.randomseed(os.time())
 6 local jammerGuid ='DEMUU8-0HLVBNMSNGQDO'
 7 local jammer = ScenEdit_GetUnit({guid=jammerGuid})
 8 local comp2 = 1
 9 
10 for k,v in ipairs(side.units) do
11 local x = ScenEdit_GetUnit({ guid= v.guid })
12 print("Unit name "..x.name)
13 local distance = Tool_Range(v.guid, jammerGuid)
14 print("Distance to target is "..distance)
15 
16 if distance <= 100 then
17     local comp = -0.231*math.log(distance)+1.248
18     print("Comparison is "..comp)
19     local comp2 = 1
20     if comp > 0 then
21     comp2 = (comp2*(1-comp))
22     end
23     print('Comp 2 is: ' ..comp2)
24     local random = math.random()
25     print('....Calculating....')
26     print('random: '..random)
27     print(x.name..' has '..((1-comp2)*100)..'% chance of being jammed.')
28     if comp2 < random then
29     ScenEdit_SetUnit({guid=v.guid, OutOfComms="True"})
30     print(x.name.. " is out of comms")
31     else
32     ScenEdit_SetUnit({guid=v.guid, OutOfComms="False"})    
33     print(x.name.. " is NOT out of comms")
34     end
35 else 
36 print(x.name.." is not in jamming range.")
37 
38 end
39 if x:inArea(jammerArea) == true then print("In!") else print("Out!") end
40 end

This is the inArea method.

 1 local side = VP_GetSide({side='NATO Allied Air Command'})
 2 
 3 local jammerArea = {'Jammer 1', 'Jammer 2', 'Jammer 3', 'Jammer 4', 'Jammer 5', 'Jammer 6', 'Jammer 7', 'Jammer 8'}
 4 
 5 math.randomseed(os.time())
 6 local jammerGuid ='DEMUU8-0HLVBNMSNGQDO'
 7 local jammer = ScenEdit_GetUnit({guid=jammerGuid})
 8 local comp2 = 1
 9 
10 for k,v in ipairs(side.units) do
11 local x = ScenEdit_GetUnit({ guid= v.guid })
12 print("Unit name "..x.name)
13 local distance = Tool_Range(v.guid, jammerGuid)
14 print("Distance to target is "..distance)
15 
16 --if distance <= 100 then
17 if x:inArea(jammerArea) == true then
18     local comp = -0.231*math.log(distance)+1.248
19     print("Comparison is "..comp)
20     local comp2 = 1
21     if comp > 0 then
22     comp2 = (comp2*(1-comp))
23     end
24     print('Comp 2 is: ' ..comp2)
25     local random = math.random()
26     print('....Calculating....')
27     print('random: '..random)
28     print(x.name..' has '..((1-comp2)*100)..'% chance of being jammed.')
29     if comp2 < random then
30     ScenEdit_SetUnit({guid=v.guid, OutOfComms="True"})
31     print(x.name.. " is out of comms")
32     else
33     ScenEdit_SetUnit({guid=v.guid, OutOfComms="False"})    
34     print(x.name.. " is NOT out of comms")
35     end
36 else 
37 print(x.name.." is not in jamming range.")
38 
39 end
40 if x:inArea(jammerArea) == true then print("In!") else print("Out!") end
41 end