Skip to content

Instantly share code, notes, and snippets.

@albertveldman96
Created March 3, 2017 23:09
Show Gist options
  • Save albertveldman96/3cbd1499ce332d9b895676a0a0e60dd0 to your computer and use it in GitHub Desktop.
Save albertveldman96/3cbd1499ce332d9b895676a0a0e60dd0 to your computer and use it in GitHub Desktop.
function Update(I)
--Get target info
target = I:GetTargetInfo(0,0)
targetPosition = target.AimPointPosition
targetVelocity = target.Velocity
if I:GetNumberOfTargets(0) > 0 then
for trans = 0, I:GetLuaTransceiverCount()-1 do
for missile = 0, I:GetLuaControlledMissileCount(trans)-1 do
--Get current missile info
currentMissile = I:GetLuaControlledMissileInfo(trans, missile)
missilePosition = currentMissile.Position
distanceToTarget = Vector3.Distance(missilePosition, targetPosition)
--if distanceToTarget == math.huge then
--distanceToTarget = 10
--end
--Guess time until impact
missileSpeed = Vector3.Magnitude(currentMissile.Velocity)
if missileSpeed < 1 then missileSpeed = 1 end
timeGuess = distanceToTarget/missileSpeed
--Setup loop variables
i = 0
relativeChange = 1
timeUntilImpact = timeGuess
allowedChange = 0.005
--Calculate timeUntilImpact until the relative change in 2 consecutively
--calculated times is lower than the minimum value
while relativeChange > allowedChange do
--Stop after 25 tries
if i > 25 then
timeUntilImpact = timeGuess
break
end
oldTime = time
--Update expected position with new time
updatedPosition = targetPosition + (targetVelocity * timeUntilImpact)
--Update distance to target with new target position
distanceToTarget = Vector3.Distance(missilePosition, updatedPosition)
--Update time with new distanceToTarget.
timeUntilImpact = distanceToTarget/missileSpeed
--Calculate relative change
relativeChange = math.abs(1-(timeUntilImpact/oldTime))
i = i+1
end
finalPos = targetPosition + (targetVelocity * timeUntilImpact)
I:SetLuaControlledMissileAimPoint(trans, missile, finalPos.x, finalPos.y, finalPos.z)
if distanceToTarget < 7.5 then
I:DetonateLuaControlledMissile(trans, missile)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment