Advertisement
rjtwins

Untitled

Dec 22nd, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | Gaming | 0 0
  1. LAUNCHER_NAME = nil -- "<NAME>"
  2. MISSILE_CRUISE_ALTITUDE = 500
  3. MISSILE_TERMINAL_DISTANCE = 500
  4. MISSILE_TERMINAL_THRUST = nil -- 1000
  5. missiles = {}
  6. targets = {}
  7.  
  8. function Update(I)
  9.     I:ClearLogs()
  10.     UpdateTargets(I)
  11.  
  12.     if(HasTarget()) then
  13.         I:Log("Has Target")
  14.     else
  15.         I:Log("No Target")
  16.         return
  17.     end
  18.  
  19.     local count = I:GetLuaTransceiverCount()
  20.     for i = 0, count -1 do
  21.         UpdateTransceiver(I, i)
  22.     end
  23. end
  24.  
  25.  
  26. function UpdateTransceiver(I, TI)
  27.     local t = I:GetLuaTransceiverInfo(TI)
  28.  
  29.     if LAUNCHER_NAME and t.CustomName ~= LAUNCHER_NAME then
  30.         return
  31.     end
  32.  
  33.     local missile_count = I:GetLuaControlledMissileCount(TI)
  34.  
  35.     for m = 0, missile_count - 1 do
  36.         local missile_id = UpdateMissile(I, TI, m)
  37.         SetMissileMode(I, missile_id)
  38.         SetAimpoint(I, missile_id)
  39.     end
  40. end
  41.  
  42.  
  43. function SetMissileMode(I, missile_id)
  44.     local pos = missiles[missile_id].pos
  45.     local aimpoint  = I:GetTargetInfo(0, targets[1]).AimPointPosition
  46.  
  47.     if not missiles[missile_id] then
  48.         missiles[missile_id] = "climb"
  49.         return
  50.     end
  51.  
  52.     if missiles[missile_id].mode == "climb" and missiles[missile_id].pos.y > MISSILE_CRUISE_ALTITUDE then
  53.         missiles[missile_id].mode = "cruise"
  54.         return
  55.     end
  56.  
  57.     local surface_distance = XZDistance(pos, aimpoint)
  58.     if missiles[missile_id].mode == "cruise" and surface_distance < MISSILE_TERMINAL_DISTANCE then
  59.         missiles[missile_id].mode = "terminal"
  60.         return
  61.     end
  62. end
  63.  
  64. function SetAimpoint(I, missile_id)
  65.     local target = I:GetTargetInfo(0, targets[1])
  66.     local aimpoint = target.AimPointPosition
  67.     local pos = missiles[missile_id].pos
  68.     local vel = missiles[missile_id].vel
  69.  
  70.     if missiles[missile_id].mode == "climb" then
  71.         aimpoint = pos + (vel.normalized * 30)
  72.         aimpoint.y = aimpoint.y + 30
  73.     end
  74.  
  75.     if missiles[missile_id].mode == "cruise" then
  76.         aimpoint = CalcPredictiveAimpoint(target.AimPointPosition, target.Velocity, pos, vel)
  77.         aimpoint.y = pos.y
  78.     end
  79.  
  80.     if missiles[missile_id].mode == "terminal" then
  81.         aimpoint = CalcPredictiveAimpoint(target.AimPointPosition, target.Velocity, pos, vel)
  82.         if HasVariableThruster(I, missile_id) and MISSILE_TERMINAL_THRUST then
  83.             SetThrust(I, missile_id, MISSILE_TERMINAL_THRUST)
  84.         end
  85.     end
  86.  
  87.     I:SetLuaControlledMissileAimPoint(missiles[missile_id].transceiver_index, missiles[missile_id].index, aimpoint.x, aimpoint.y, aimpoint.z)
  88. end
  89.  
  90. --TI: Transceiver Index
  91. --MI: Missile Index
  92. function UpdateMissile(I, TI, MI)
  93.     I:Log("UpdateMissile")
  94.     local missile = I:GetLuaControlledMissileInfo(TI,MI)
  95.     local missile_id = missile.Id
  96.  
  97.     if missile.Valid == false then
  98.         missiles[missile_id] = nil
  99.         return
  100.     end
  101.  
  102.     if not contains(missiles, missile_id) then
  103.         missiles[missile_id] = {}
  104.         missiles[missile_id].mode = "climb"
  105.         I:Log("New Missile")
  106.     end
  107.  
  108.     missiles[missile_id].transceiver_index = TI
  109.     missiles[missile_id].index = MI
  110.  
  111.     missiles[missile_id].pos = missile.Position
  112.     missiles[missile_id].vel = missile.Velocity
  113.  
  114.     local message = string.format("Missile %d %s", missile_id, missiles[missile_id].mode)
  115.  
  116.     I:LogToHud(message)
  117.     I:Log(message)
  118.  
  119.     return missile_id
  120. end
  121.  
  122. function UpdateTargets(I)
  123.     targets = {} -- Clear the array
  124.     local nr_targets = I:GetNumberOfTargets(0)
  125.     if nr_targets == 0 then
  126.         return
  127.     end
  128.  
  129.     for i = 0, nr_targets - 1 do
  130.         local target = I:GetTargetInfo(0, i)
  131.         if target.Valid then
  132.             table.insert(targets, i) -- Use table.insert to add to the array
  133.         end
  134.     end
  135.  
  136.     table.sort(targets, function(a, b)
  137.         return a.Priority < b.Priority
  138.     end)
  139. end
  140.  
  141. function HasTarget()
  142.     return #targets > 0
  143. end
  144.  
  145. function CalcPredictiveAimpoint(target_position, target_velocity, missile_position, missile_velocity)
  146.     local missile_speed = Vector3.Magnitude(missile_velocity)
  147.     local target_distance = Vector3.Distance(target_position, missile_position)
  148.     local target_vector = Vector3.Normalize(target_position - missile_position)
  149.     local relative_speed = missile_speed - Vector3.Dot(target_vector, target_velocity)
  150.     local prediction = target_velocity*target_distance/relative_speed
  151.     local intercept = target_position + prediction
  152.     local intercept_vector = Vector3.Normalize(intercept - missile_position)
  153.     local aimpoint = missile_position + intercept_vector*missile_speed
  154.  
  155.     return aimpoint
  156. end
  157.  
  158. function HasVariableThruster(I, missile_id)
  159.     local parts = I:GetMissileInfo(missiles[missile_id].transceiver_index, missiles[missile_id].index).Parts
  160.  
  161.     if string.find(parts[1].Name,'Variable thruster') then
  162.         return true
  163.     end
  164.  
  165.     return false
  166. end
  167.  
  168. function SetThrust(I, missile_id, thrust)
  169.     local parts = I:GetMissileInfo(missiles[missile_id].transceiver_index, missiles[missile_id].index).Parts
  170.     local thruster = parts[1]
  171.     thruster:SendRegister(2, thrust)
  172. end
  173.  
  174. function GetFuel()
  175.     local parts = I:GetMissileInfo(missiles[missile_id].transceiver_index, missiles[missile_id].index).Parts
  176.     local fuel = 0
  177.  
  178.     for k=1, #parts do
  179.         if string.find(parts[k].Name,'Fuel tank') then
  180.             fuel = reservefuel + 5000
  181.         end
  182.     end
  183. end
  184.  
  185. function contains(table, item)
  186.     for key, value in pairs(table) do
  187.         if key == item then
  188.             return true
  189.         end
  190.     end
  191.     return false
  192. end
  193.  
  194. function XZDistance(p1, p2)
  195.     return math.sqrt((p1.x - p2.x)^2 + (p1.z - p2.z)^2)
  196. end
Tags: FTD Missile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement