Advanced Scripts

From Weapons Release Wiki
Jump to: navigation, search

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.

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