Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. cruiseAltitude = 250
  2. diveDistance = 100
  3. -- Gain from 1 to 10
  4. rollGain = 5
  5. yawGain = 5
  6. pitchGain = 5
  7. minTargetAltitude = -5
  8. blueprintName = "SNUKEB"
  9.  
  10. -- internal variables (do not touch)
  11. runOnce = true
  12. swarmId = 0
  13.  
  14.  
  15. function GetIntercept(I, Pos, Info)
  16.  
  17. -- get my vehicle info
  18. local mySpeed = I:GetForwardsVelocityMagnitude()
  19. local myAltitude = I:GetConstructCenterOfMass().y
  20.  
  21. --I:LogToHud(mySpeed .. " " Pos.Range)
  22.  
  23. -- My attempt to make very basic prediction
  24. local Prediction = Info.AimPointPosition + Info.Velocity * ((Pos.Range / mySpeed) / 4)
  25.  
  26. -- Aim straight after we reach "diveDistance"
  27. local adjustedDistance = math.max(0,Pos.GroundDistance - diveDistance)
  28.  
  29. -- if we get above cruiseAltitude, lower the height aimpoint
  30. local height = Prediction.y + adjustedDistance / math.max(1, (myAltitude / cruiseAltitude) * 4)
  31.  
  32. return I:GetTargetPositionInfoForPosition(0, Prediction.x, height, Prediction.z)
  33.  
  34. end
  35.  
  36. function NavigateToPoint(I, Pos)
  37.  
  38. --Roll
  39. local roll = I:GetConstructRoll()
  40. if( roll >180 ) then roll = roll-360 end
  41. if(roll>0) then I:RequestControl(2,3,math.min(1,math.abs(roll)/(180/rollGain))) end -- roll right
  42. if(roll<0) then I:RequestControl(2,2,math.min(1,math.abs(roll)/(180/rollGain))) end -- roll left
  43.  
  44.  
  45. --Yaw
  46. if(Pos.Azimuth>0) then I:RequestControl(2,0,math.min(1,math.abs(Pos.Azimuth)/(180/yawGain))) end --yaw left
  47. if(Pos.Azimuth<0) then I:RequestControl(2,1,math.min(1,math.abs(Pos.Azimuth)/(180/yawGain))) end --yaw right
  48.  
  49. --Pitch
  50. if(Pos.Elevation>0) then I:RequestControl(2,4,math.min(1,math.abs(Pos.Elevation)/(180/pitchGain))) end --elevate up
  51. if(Pos.Elevation<0) then I:RequestControl(2,5,math.min(1,math.abs(Pos.Elevation)/(180/pitchGain))) end --elevate down
  52.  
  53. --I:LogToHud(Pos.Azimuth)
  54.  
  55. end
  56.  
  57. function SmartTargeting(I)
  58.  
  59. -- swarmId is number of this craft in a swarm of similar crafts
  60.  
  61. if(runOnce) then
  62. -- runOnce to false, so we dont run this again
  63. runOnce = false
  64.  
  65. --Count how many similar units are in a 1km radius
  66. -- and assign myself a flight number
  67. local numFriendlies = I:GetFriendlyCount()
  68. for i = 0, numFriendlies-1, 1 do
  69. local friendlyInfo = I:GetFriendlyInfo(i)
  70. if(friendlyInfo.BlueprintName == blueprintName) then
  71. swarmId = swarmId+1
  72. end
  73. end
  74. I:Log("Set swarmId: " .. swarmId)
  75. end
  76.  
  77. --Count number of possilbe targets we could engage
  78. local numTargets= I:GetNumberOfTargets(0)
  79. local numValidTargets= 0
  80.  
  81. for i = 0, numTargets-1, 1 do
  82. local TargetInfo = I:GetTargetInfo(0,i)
  83. if(TargetInfo.Valid and (TargetInfo.Position.y > minTargetAltitude)) then
  84. numValidTargets= numValidTargets+1
  85. end
  86. end
  87.  
  88.  
  89. --return a target number
  90. local myTarget = 0
  91. if( numValidTargets > 0)then
  92. myTarget= swarmId%numValidTargets
  93. if (myTarget == 0 ) then
  94. myTarget = numValidTargets
  95. end
  96. myTarget = myTarget-1
  97. end
  98. I:LogToHud(blueprintName .. " swarmId: " .. swarmId .. " targeting: " .. myTarget)
  99. return myTarget
  100. end
  101.  
  102.  
  103. function Update(I)
  104. local myTarget = SmartTargeting(I)
  105. local TargetPos = I:GetTargetPositionInfo(0, myTarget )
  106. local TargetInfo = I:GetTargetInfo(0, myTarget)
  107.  
  108. if (I:GetNumberOfMainframes() > 0 and I:GetNumberOfTargets(0) > 0) then
  109. if TargetPos.Valid then
  110. local intercept = GetIntercept(I, TargetPos, TargetInfo )
  111. NavigateToPoint(I, intercept)
  112. --thrust full speed
  113. I:RequestThrustControl(0)
  114. end
  115. else
  116. local myPos = I:GetConstructCenterOfMass()
  117. local intercept =I:GetTargetPositionInfoForPosition(0,
  118. myPos.x+I:GetConstructRightVector().x*50,
  119. cruiseAltitude,
  120. myPos.z+I:GetConstructRightVector().z*50)
  121. NavigateToPoint(I, intercept)
  122. --thrust low speed
  123. I:RequestThrustControl(0,0.25)
  124. end
  125.  
  126. -- self destruct NOTE: does not work as of 1.966 -- commented out
  127. -- since NUKE is not a weapon-type and I:GetWeaponCount() returns 0
  128. -- if((I:GetVelocityMagnitude()<25 and TargetPos.Valid and TargetPos.Range<100)) then
  129. -- local aim = I:GetConstructForwardVector()
  130. -- I:AimWeaponInDirection(0, aim.x,aim.y,aim.z, 0)
  131. -- I:FireWeapon(0,0)
  132. -- end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement