Advertisement
Guest User

from the depths harpoon-ish guidance

a guest
Jan 6th, 2025
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. -- guidance parameters --
  2.  
  3. local skimAlt = 10  -- ASL alt (m) for seaskimming portion
  4. local cruiseThrust = 8500 -- thrust for sea skimming phase
  5.  
  6. local beginClimbPhase = 950 -- distance (m) to begin the pop-up for the top down attack
  7. local climbAimHeight = 125 -- ASL alt (m) to aim during climb phase
  8. local climbThrust = 11500 -- thrust for pop-up phase
  9. local maxSpeedForClimb = 40 -- max target speed (m/s) where the missile will do a pop-up attack
  10.  
  11. local beginAttackPhase = 400 -- distance (m) to begin the down part of the top down attack
  12. local attackThrust = 30000 -- thrust for attack phase
  13.  
  14.  
  15. function Update(I)
  16.     -- will sea skim before rising up for a more top down attack
  17.  
  18.     -- if no target already set or target can no longer be found set a target for the missile
  19.    
  20.     local targetInfo = FindTarget(I)
  21.     if (targetInfo ~= nil) then
  22.         for t = 0, I:GetLuaTransceiverCount() do
  23.             for m = 0, I:GetLuaControlledMissileCount(t) - 1 do
  24.  
  25.                 local targetPosition = targetInfo.AimPointPosition
  26.                 local targetVelocity = targetInfo.Velocity
  27.  
  28.  
  29.                 local missilePosition = I:GetLuaControlledMissileInfo(t, m).Position
  30.                 local missileSpeed = Vector3.Magnitude(I:GetLuaControlledMissileInfo(t, m).Velocity)
  31.                
  32.                 local targetDistance = Vector3.Distance(targetPosition, missilePosition)
  33.                 local targetVector = Vector3.Normalize(targetPosition - missilePosition)
  34.            
  35.                 local relativeSpeed = missileSpeed - Vector3.Dot(targetVector, targetVelocity)
  36.                 local prediction = targetVelocity*targetDistance/relativeSpeed
  37.                 local intercept = targetPosition + prediction
  38.                 local interceptVector = Vector3.Normalize(intercept - missilePosition)
  39.                
  40.                 local point = missilePosition + interceptVector*missileSpeed
  41.                 local interceptDistance = InterceptDistance2D(missilePosition,intercept)
  42.                 local parts = I:GetMissileInfo(t, m).Parts
  43.                 if (interceptDistance > beginClimbPhase) then
  44.                     for i=1, #parts do
  45.                         if parts[i].Name:find("thruster") then
  46.                             parts[i]:SendRegister(2, cruiseThrust)
  47.                             break
  48.                         end
  49.                     end
  50.                     I:LogToHud("sea skimming")
  51.                     point.y = skimAlt
  52.                 elseif (interceptDistance <= beginClimbPhase and interceptDistance > beginAttackPhase) then
  53.                     for i=1, #parts do
  54.                         if parts[i].Name:find("thruster") then
  55.                             parts[i]:SendRegister(2, climbThrust)
  56.                             break
  57.                         end
  58.                     end
  59.                    
  60.                     if (Vector3.Magnitude(targetVelocity) <= maxSpeedForClimb) then
  61.                         I:LogToHud("performing pop-up attack")
  62.                         point.y = climbAimHeight
  63.                     else
  64.                         I:LogToHud("target too fast for pop-up, going for direct attack")
  65.                     end    
  66.                 elseif (interceptDistance <= beginAttackPhase) then
  67.                     for i=1, #parts do
  68.                         if parts[i].Name:find("thruster") then
  69.                             parts[i]:SendRegister(2, attackThrust)
  70.                             break
  71.                         end
  72.                     end
  73.                 end
  74.                 I:SetLuaControlledMissileAimPoint(t, m, point.x, point.y, point.z)
  75.             end
  76.         end
  77.     end
  78. end
  79.  
  80. function InterceptDistance2D(missile,intercept)
  81.     -- returns distance to target ignoring difference in altitude (m)
  82.     return math.sqrt((missile.x - intercept.x)^2 + (missile.z - intercept.z)^2)
  83. end
  84.  
  85. function FindTarget(I)
  86.     -- Finds and sets a target
  87.     hasTarget = false  
  88.  
  89.     for i = 0, I:GetNumberOfMainframes() do
  90.         for e = 0, I:GetNumberOfTargets(i) do
  91.             TargetInfo = I:GetTargetInfo(i, e)
  92.             local targetPosition = TargetInfo.AimPointPosition
  93.             if (targetPosition.y < 20 and targetPosition.y > -10) then
  94.                 targetIDs = {i, TargetInfo.Id}  -- Update global targetIDs
  95.                 hasTarget = true
  96.                 break
  97.             end
  98.         end
  99.         if hasTarget then break end  -- Exit loop if target is found
  100.     end
  101.     if (hasTarget) then
  102.         return TargetInfo
  103.     else
  104.         return nil
  105.     end
  106. end
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement