Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- guidance parameters --
- local skimAlt = 10 -- ASL alt (m) for seaskimming portion
- local cruiseThrust = 8500 -- thrust for sea skimming phase
- local beginClimbPhase = 950 -- distance (m) to begin the pop-up for the top down attack
- local climbAimHeight = 125 -- ASL alt (m) to aim during climb phase
- local climbThrust = 11500 -- thrust for pop-up phase
- local maxSpeedForClimb = 40 -- max target speed (m/s) where the missile will do a pop-up attack
- local beginAttackPhase = 400 -- distance (m) to begin the down part of the top down attack
- local attackThrust = 30000 -- thrust for attack phase
- function Update(I)
- -- will sea skim before rising up for a more top down attack
- -- if no target already set or target can no longer be found set a target for the missile
- local targetInfo = FindTarget(I)
- if (targetInfo ~= nil) then
- for t = 0, I:GetLuaTransceiverCount() do
- for m = 0, I:GetLuaControlledMissileCount(t) - 1 do
- local targetPosition = targetInfo.AimPointPosition
- local targetVelocity = targetInfo.Velocity
- local missilePosition = I:GetLuaControlledMissileInfo(t, m).Position
- local missileSpeed = Vector3.Magnitude(I:GetLuaControlledMissileInfo(t, m).Velocity)
- local targetDistance = Vector3.Distance(targetPosition, missilePosition)
- local targetVector = Vector3.Normalize(targetPosition - missilePosition)
- local relativeSpeed = missileSpeed - Vector3.Dot(targetVector, targetVelocity)
- local prediction = targetVelocity*targetDistance/relativeSpeed
- local intercept = targetPosition + prediction
- local interceptVector = Vector3.Normalize(intercept - missilePosition)
- local point = missilePosition + interceptVector*missileSpeed
- local interceptDistance = InterceptDistance2D(missilePosition,intercept)
- local parts = I:GetMissileInfo(t, m).Parts
- if (interceptDistance > beginClimbPhase) then
- for i=1, #parts do
- if parts[i].Name:find("thruster") then
- parts[i]:SendRegister(2, cruiseThrust)
- break
- end
- end
- I:LogToHud("sea skimming")
- point.y = skimAlt
- elseif (interceptDistance <= beginClimbPhase and interceptDistance > beginAttackPhase) then
- for i=1, #parts do
- if parts[i].Name:find("thruster") then
- parts[i]:SendRegister(2, climbThrust)
- break
- end
- end
- if (Vector3.Magnitude(targetVelocity) <= maxSpeedForClimb) then
- I:LogToHud("performing pop-up attack")
- point.y = climbAimHeight
- else
- I:LogToHud("target too fast for pop-up, going for direct attack")
- end
- elseif (interceptDistance <= beginAttackPhase) then
- for i=1, #parts do
- if parts[i].Name:find("thruster") then
- parts[i]:SendRegister(2, attackThrust)
- break
- end
- end
- end
- I:SetLuaControlledMissileAimPoint(t, m, point.x, point.y, point.z)
- end
- end
- end
- end
- function InterceptDistance2D(missile,intercept)
- -- returns distance to target ignoring difference in altitude (m)
- return math.sqrt((missile.x - intercept.x)^2 + (missile.z - intercept.z)^2)
- end
- function FindTarget(I)
- -- Finds and sets a target
- hasTarget = false
- for i = 0, I:GetNumberOfMainframes() do
- for e = 0, I:GetNumberOfTargets(i) do
- TargetInfo = I:GetTargetInfo(i, e)
- local targetPosition = TargetInfo.AimPointPosition
- if (targetPosition.y < 20 and targetPosition.y > -10) then
- targetIDs = {i, TargetInfo.Id} -- Update global targetIDs
- hasTarget = true
- break
- end
- end
- if hasTarget then break end -- Exit loop if target is found
- end
- if (hasTarget) then
- return TargetInfo
- else
- return nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement