Advertisement
Guest User

Refined from the depths missile lua

a guest
Mar 26th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.95 KB | Source Code | 0 0
  1. --[[
  2. Improved Missile Guidance Script v57 ("v50" + AotE Averaging Option)
  3. - Added optional weighted averaging between AimPointPosition and Position (CoM proxy)
  4.   specifically for the *predictive* guidance phase. Controlled by AimPointWeight.
  5. - Terminal and Overshoot phases still target AimPointPosition directly for precision.
  6. - Added Bias Tuning.
  7. - Previous Features: Fixed Logic Flow, Hard G Pull, Per-Transceiver Prox, Min TTI Clamp, EWMA Accel, Iterative Pred, etc.
  8. ]]
  9.  
  10. -- --- Configuration ---
  11. -- Core Settings
  12. local MainframeIndex = 0; local DetonationRadius = 8.0; local MinClosingSpeed = 1.0;
  13. -- Per-Transceiver Proximity Fuse Control
  14. local ProximityFuseEnabledPerTransceiver = {}; local DefaultProximityEnabled = true;
  15.  
  16. -- Averaging Factor (Prediction Phase Only)
  17. -- Weight given to AimPointPosition vs Position (CoM proxy). 1.0 = Only AimPoint, 0.0 = Only Position, 0.5 = Halfway.
  18. local AimPointWeight = 1.0 -- <<< SET BETWEEN 0.0 and 1.0. Default 1.0 (current behavior).
  19.  
  20. -- Prediction Bias Tuning -- TUNE THESE!
  21. local PredictionVerticalBias = 1.0   -- Subtracts Y value. Positive values counteract flying HIGH.
  22. local PredictionHorizontalBias = 1.0 -- Subtracts X value. Positive=West bias, Negative=East bias.
  23.  
  24. -- Prediction Timing & Limits
  25. local MaxInterceptTime = 8.0; local MinInterceptTime = 0.05;
  26. -- Prediction Method Configuration
  27. local EnableAPNPrediction = true; local AccelFactor = 0.4; local MaxEstimatedAccel = 50.0;
  28. local EnableAccelSmoothing = true; local AccelSmoothingAlpha = 0.1;
  29. local EnableIterativePrediction = true;
  30. -- Overshoot Correction Config
  31. local EnableOvershootCorrection = true; local HardGPullOvershootDistance = 5.0;
  32. local OvershootMinRange = 20.0; local OvershootMinTargetSpeed = 5.0;
  33. local GentleOvershootDistance = 65.0;
  34. -- Terminal Guidance Config
  35. local EnableTerminalGuidance = true; local TerminalGuidanceRange = 35.0;
  36. -- --- End Configuration ---
  37.  
  38. -- --- Global State ---
  39. if prevTime == nil then prevTime = 0 end; if prevTargetVel == nil then prevTargetVel = Vector3(0,0,0) end
  40. if prevUpdateTime == nil then prevUpdateTime = 0 end; if smoothedEstimatedTargetAccel == nil then smoothedEstimatedTargetAccel = Vector3(0,0,0) end
  41. -- --- End Global State ---
  42.  
  43. --- Main Update Function ---
  44. function Update(I)
  45.     local currentTime = I:GetTime(); if currentTime <= prevTime + 0.001 then return end
  46.     local scriptDeltaTime = currentTime - prevTime; prevTime = currentTime
  47.  
  48.     local numTargets = I:GetNumberOfTargets(MainframeIndex)
  49.     if numTargets == 0 then prevUpdateTime=0; smoothedEstimatedTargetAccel=Vector3(0,0,0); return end
  50.     local targetInfo = I:GetTargetInfo(MainframeIndex, 0)
  51.     if not targetInfo.Valid then prevUpdateTime=0; smoothedEstimatedTargetAccel=Vector3(0,0,0); return end
  52.  
  53.     -- Define both potential target points
  54.     local targetAimPointPos = targetInfo.AimPointPosition
  55.     local targetCenterPosProxy = targetInfo.Position -- Use Position as proxy for CoM/Center
  56.  
  57.     local targetVel = targetInfo.Velocity
  58.     local targetSpeed = targetVel.magnitude
  59.  
  60.     -- --- Target Acceleration Estimation ---
  61.     local rawEstimatedTargetAccel = Vector3(0,0,0); local actualDeltaTime = currentTime - prevUpdateTime
  62.     if EnableAPNPrediction and prevUpdateTime > 0 and actualDeltaTime > 0.01 then
  63.         rawEstimatedTargetAccel = (targetVel - prevTargetVel) / actualDeltaTime
  64.         if rawEstimatedTargetAccel.magnitude > MaxEstimatedAccel then rawEstimatedTargetAccel = rawEstimatedTargetAccel.normalized * MaxEstimatedAccel end
  65.         if EnableAccelSmoothing then smoothedEstimatedTargetAccel = rawEstimatedTargetAccel * AccelSmoothingAlpha + smoothedEstimatedTargetAccel * (1.0 - AccelSmoothingAlpha)
  66.         else smoothedEstimatedTargetAccel = rawEstimatedTargetAccel end
  67.     else smoothedEstimatedTargetAccel = Vector3(0,0,0) end
  68.     prevTargetVel = targetVel; prevUpdateTime = currentTime
  69.     -- --- End Acceleration Estimation ---
  70.  
  71.     local transceiverCount = I:GetLuaTransceiverCount()
  72.     for trIdx = 0, transceiverCount - 1 do
  73.         local missileCount = I:GetLuaControlledMissileCount(trIdx)
  74.         for mIdx = 0, missileCount - 1 do
  75.             local missileInfo = I:GetLuaControlledMissileInfo(trIdx, mIdx)
  76.             if not missileInfo.Valid then goto NextMissile end
  77.  
  78.             local missilePos = missileInfo.Position; local missileVel = missileInfo.Velocity
  79.             local missileSpeed = missileVel.magnitude
  80.  
  81.             -- Calculate relative kinematics based on the AimPoint primarily
  82.             local vectorTargetToMissile = missilePos - targetAimPointPos -- Vector relative to AimPoint
  83.             local relativeVel = missileVel - targetVel
  84.             local range = vectorTargetToMissile.magnitude -- Range to AimPoint
  85.  
  86.             local aimPoint = targetAimPointPos -- Default aim point is target's AimPointPosition
  87.             local guidanceModeSet = false
  88.  
  89.             local useProximity = ProximityFuseEnabledPerTransceiver[trIdx]; if useProximity == nil then useProximity = DefaultProximityEnabled end
  90.  
  91.             -- *** Guidance Logic Flow ***
  92.             if useProximity and range <= DetonationRadius then I:DetonateLuaControlledMissile(trIdx, mIdx); guidanceModeSet = true; goto NextMissile end
  93.  
  94.             local closingSpeed = 0; local timeToIntercept = MaxInterceptTime; local canIntercept = false
  95.             if range >= 0.01 then
  96.                 closingSpeed = Vector3.Dot(relativeVel, -vectorTargetToMissile.normalized) -- Closing speed towards AimPoint
  97.                 canIntercept = closingSpeed >= MinClosingSpeed
  98.                 if closingSpeed > 0.001 then timeToIntercept = range / closingSpeed; timeToIntercept = Mathf.Max(MinInterceptTime, Mathf.Min(timeToIntercept, MaxInterceptTime))
  99.                 else canIntercept = false; timeToIntercept = MaxInterceptTime end
  100.             else canIntercept = false; timeToIntercept = MinInterceptTime end
  101.  
  102.             -- 2. Check Terminal Guidance (Aims DIRECTLY at AimPoint)
  103.             if not guidanceModeSet and EnableTerminalGuidance and range <= TerminalGuidanceRange then
  104.                 aimPoint = targetAimPointPos
  105.                 guidanceModeSet = true
  106.             end
  107.  
  108.             -- 3. Check Overshoot Correction (Aims DIRECTLY at AimPoint)
  109.             if not guidanceModeSet and EnableOvershootCorrection and range > OvershootMinRange and targetSpeed > OvershootMinTargetSpeed then
  110.                 local targetVelNorm = targetVel.normalized
  111.                 if targetVelNorm.magnitude > 0.01 then
  112.                     local distanceAhead = Vector3.Dot(vectorTargetToMissile, targetVelNorm) -- Distance ahead relative to AimPoint
  113.                     if distanceAhead > HardGPullOvershootDistance then aimPoint = targetAimPointPos; guidanceModeSet = true
  114.                     elseif distanceAhead > GentleOvershootDistance then aimPoint = targetAimPointPos; guidanceModeSet = true end
  115.                 end
  116.             end
  117.  
  118.             -- 4. Perform Predictive Guidance (Only if no other mode was set)
  119.             if not guidanceModeSet then
  120.                 if not canIntercept then
  121.                     aimPoint = targetAimPointPos -- Fallback aims at AimPoint
  122.                 else
  123.                     -- *** NEW: Calculate weighted base position for prediction ***
  124.                     local baseTargetPosForPred = targetAimPointPos * AimPointWeight + targetCenterPosProxy * (1.0 - AimPointWeight)
  125.  
  126.                     -- Perform prediction using baseTargetPosForPred as the reference
  127.                     local timeToIntercept1 = timeToIntercept
  128.                     local accelTerm1 = Vector3(0,0,0)
  129.                     if EnableAPNPrediction and smoothedEstimatedTargetAccel.magnitude > 0.01 then accelTerm1 = smoothedEstimatedTargetAccel * (timeToIntercept1 * timeToIntercept1 * AccelFactor) end
  130.                     local predictedInterceptPoint1 = baseTargetPosForPred + targetVel * timeToIntercept1 + accelTerm1 -- Predict from base pos
  131.                     local predictedInterceptPoint = predictedInterceptPoint1
  132.  
  133.                     if EnableIterativePrediction then
  134.                         -- Recalculate range/TTI relative to the *first predicted point* (not the base avg point) for iteration 2
  135.                         local vectorToPIP1 = predictedInterceptPoint1 - missilePos
  136.                         local rangeToPIP1 = vectorToPIP1.magnitude
  137.                         local closingSpeedToPIP1 = Vector3.Dot(missileVel, vectorToPIP1.normalized) -- Closing speed towards PIP1
  138.  
  139.                         local timeToIntercept2 = MaxInterceptTime
  140.                         -- Only recalculate if closing speed to PIP1 is reasonable
  141.                         if closingSpeedToPIP1 > MinClosingSpeed * 0.5 then -- Use slightly lower threshold?
  142.                             timeToIntercept2 = rangeToPIP1 / closingSpeedToPIP1
  143.                             timeToIntercept2 = Mathf.Max(MinInterceptTime, Mathf.Min(timeToIntercept2, MaxInterceptTime))
  144.                         end
  145.  
  146.                         local accelTerm2 = Vector3(0,0,0)
  147.                         if EnableAPNPrediction and smoothedEstimatedTargetAccel.magnitude > 0.01 then accelTerm2 = smoothedEstimatedTargetAccel * (timeToIntercept2 * timeToIntercept2 * AccelFactor) end
  148.                         predictedInterceptPoint = baseTargetPosForPred + targetVel * timeToIntercept2 + accelTerm2 -- Predict again from base pos using new TTI
  149.                     end
  150.  
  151.                     -- Apply Biases to the final predicted point
  152.                     local biasVector = Vector3(PredictionHorizontalBias, PredictionVerticalBias, 0)
  153.                     if biasVector.magnitude > 0 then aimPoint = predictedInterceptPoint - biasVector
  154.                     else aimPoint = predictedInterceptPoint end
  155.                 end
  156.                 guidanceModeSet = true
  157.             end
  158.  
  159.             if guidanceModeSet then I:SetLuaControlledMissileAimPoint(trIdx, mIdx, aimPoint.x, aimPoint.y, aimPoint.z) end
  160.  
  161.             ::NextMissile::
  162.         end -- End missile loop
  163.     end -- End transceiver loop
  164. end -- End Update function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement