Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.86 KB | None | 0 0
  1. -- Made by _pH34_ (Tyler H) for From the Depths
  2. -- First (that I know of) effective Lua remote control of ships by ships
  3. -- Step 1 of creating FTD Skynet
  4.  
  5.  
  6. -- Don't change these --
  7. LastSpinnerCount = 5
  8. LeftSponson = 0
  9. LeftThruster = 1
  10. RightThruster = 3
  11. RightSponson = 2
  12. PitchThruster = 4
  13.  
  14. RollDamper = 0
  15. LeftThrustAdjust = 0
  16. RightThrustAdjust = 0
  17. PitchAdjust = 0
  18. AltitudeAdjust = 0
  19.  
  20. CruisingAltitude = 400
  21.  
  22. DefaultThrust = -1.65
  23. Angle = 90
  24. LeftAngleAdjust = 0
  25. RightAngleAdjust = 0
  26. Mode = 0
  27.  
  28. SelfId = 0
  29. MothershipId = -1
  30.  
  31.  
  32. -- User Editable --
  33.  
  34. -- In degrees. Tweaking these may aid or totally screw stability
  35. RollTolerance = 2
  36. SevereRoll = 5
  37. PitchTolerance = 1
  38. AltitudeTolerance = 3
  39. TargetTolerance = 7.5
  40.  
  41. -- Applied as adjustments to spin blocks with a max speed of 30.
  42. -- Smaller steps may make smoother, slower reactions to roll/pitch errors.
  43. AltitudeSensitivity = -1
  44.  
  45. RollSensitivity = -0.02
  46. PitchSensitivity = -0.5
  47.  
  48. -- Min Alt + Max Alt = actual max altitude
  49. MinCruiseAlt = 50
  50. MaxCruiseAlt = 300
  51.  
  52. -- Used for turning log statements on/off
  53. -- sort-of, kind-of implemented
  54. Debug = 1
  55.  
  56. -- End Variables --
  57.  
  58. function Update(I)
  59.     -- Attempt at programmatically identifying spinners correctly
  60.     -- sort of works, but not entirely. currently building from blueprint
  61.     -- creates a crippled drone
  62.     if Mode == 0 or not I:GetSpinnerCount() == LastSpinnerCount then
  63.         LastSpinnerCount = I:GetSpinnerCount()
  64.         for ii=0, (I:GetSpinnerCount() - 1) do
  65.             Spinner = I:GetSpinnerInfo(ii)
  66.             if Spinner.LocalPosition.x > 0 then
  67.                 RightSponson = ii
  68.             elseif Spinner.LocalPosition.x < 0 then
  69.                 LeftSponson = ii
  70.             elseif Spinner.LocalPosition.z > 0 then
  71.                 PitchThruster = ii
  72.             end
  73.         end
  74.     end
  75.     if Mode == 0 then
  76.         SelfId = I:GetFriendlyCount()
  77.         I:LogToHud("Drone " .. SelfId .. " Active.")
  78.         SetThrusterAngle(I)
  79.         SetThrust(I, DefaultThrust)
  80.         -- Each drone gets a unique cruising altitude to reduce chances of crashing
  81.         CruisingAltitude = MaxCruiseAlt - ((SelfId * (AltitudeTolerance * 2)) % MaxCruiseAlt) + MinCruiseAlt
  82.         for j=0, I:GetFriendlyCount() do
  83.             if I:GetFriendlyInfo(j).BlueprintName == "Mothership" then
  84.                 MothershipId = I:GetFriendlyInfo(j).Id
  85.                 j = I:GetFriendlyCount() + 1
  86.                 I:LogToHud("Mothership Found. (" .. SelfId .. ")")
  87.                 break
  88.             end
  89.         end
  90.         if MothershipId == -1 then
  91.             I:LogToHud("Mothership Not Found! (" .. SelfId .. ")")
  92.         end
  93.         Mode = 1
  94.     elseif Mode == 1 then
  95.         Hover(I)
  96.     elseif Mode == 2 or Mode == 3 then
  97.         Fly(I)
  98.     elseif Mode == 4 then
  99.         Hibernate(I)
  100.     end
  101.     Mode = CheckForCommand(I)
  102.  
  103. end
  104.  
  105. function CheckForCommand(I)
  106.     if MothershipId == -1 then
  107.         if (I:GetConstructPosition().y > CruisingAltitude - AltitudeTolerance) or
  108.            (Mode == 2 and I:GetConstructPosition().y > CruisingAltitude - (AltitudeTolerance * 40)) then
  109.             return 2
  110.         else
  111.             TargetId = -1
  112.             if I:GetNumberOfTargets(0) > 0 then
  113.                 LastRange = 50000
  114.                 for ii = 0, I:GetNumberOfTargets(0) do
  115.                     TargetOption = I:GetTargetInfo(0, ii)
  116.                     if TargetOption.Valid then
  117.                         Range = I:GetTargetPositionInfo(0, ii).Range
  118.                         if Range < LastRange then
  119.                             LastRange = Range
  120.                             TargetId = ii
  121.                             break
  122.                         end
  123.                     end
  124.                 end
  125.             end
  126.             if TargetId == -1 then
  127.                 return 1
  128.             else
  129.                 return 2
  130.             end
  131.         end
  132.     else
  133.         Info = I:GetFriendlyInfoById(MothershipId)
  134.         -- This section is attempting to get the mothership heading in order to adjust the XVal and ZVal
  135.         -- so that the mothership doesn't have to remain pointed at 0 degrees.
  136.         -- Another solution involves keeping the selector arm pointed at 0 degrees rather than the motherships
  137.         -- front as a default position.
  138.         Rotation = math.deg(math.atan(Info.ForwardVector.x,Info.ForwardVector.z)) * 2
  139.         X = (Info.CenterOfMass - Info.ReferencePosition).x
  140.         Z = (Info.CenterOfMass - Info.ReferencePosition).z
  141.         XVal = (Z * math.sin(Rotation)) + (X * math.cos(Rotation))
  142.         ZVal = (X * math.sin(Rotation)) + (Z * math.cos(Rotation))
  143.  
  144.         -- This is a heavily simplified proof-of-concept implementation, because I could hardly find
  145.         -- a purpose for 4 modes, much less the nearly unlimited modes of using pure vectors.
  146.         if XVal > 0 and ZVal > 0 then
  147.             if not Mode == 1 then
  148.                 I:LogToHud("Switching to Hover Mode. (" .. SelfId .. ")")
  149.             end
  150.             return 1
  151.         elseif XVal < 0 and ZVal > 0 then
  152.             if not Mode == 2 then
  153.                 I:LogToHud("Switching to Fly Mode. (" .. SelfId .. ")")
  154.             end
  155.             return 2
  156.         elseif XVal < 0 and ZVal < 0 then
  157.             if not Mode == 3 then
  158.                 -- not implemented yet
  159.                 I:LogToHud("Switching to Return Mode. (" .. SelfId .. ")")
  160.             end
  161.             return 3
  162.         elseif XVal > 0 and ZVal < 0 then
  163.             if not Mode == 4 then
  164.                 I:LogToHud("Switching to Hibernate Mode. (" .. SelfId .. ")")
  165.             end
  166.             return 4
  167.         end
  168.     end
  169. end
  170.  
  171. function Hibernate(I)
  172.     Angle = 90
  173.     SetThrusterAngle(I)
  174.     SetThrust(I, 0)
  175. end
  176.  
  177. function Fly(I)
  178.     Roll = I:GetConstructRoll()
  179.     Pitch = I:GetConstructPitch()
  180.     Altitude = I:GetConstructPosition().y
  181.     Angle = 5
  182.     SteeringDifference = -0.2
  183.     DefaultThrust = -23
  184.     TargetId = -1
  185.  
  186.     if I:GetNumberOfTargets(0) > 0 then
  187.         LastRange = 50000
  188.         for ii = 0, I:GetNumberOfTargets(0) do
  189.             TargetOption = I:GetTargetInfo(0, ii)
  190.             if TargetOption.Valid then
  191.                 if TargetOption.PlayerTargetChoice then
  192.                     TargetId = ii
  193.                     break
  194.                 else
  195.                     Range = I:GetTargetPositionInfo(0, ii).Range
  196.                     if Range < LastRange then
  197.                         LastRange = Range
  198.                         TargetId = ii
  199.                     end
  200.                 end
  201.             end
  202.         end
  203.     end
  204.     TargetPInfo = I:GetTargetPositionInfo(0, TargetId)
  205.  
  206.     if Mode == 3 then
  207.         MInfo = I:GetFriendlyInfoById(MothershipId)
  208.         TargetPInfo = I:GetTargetPositionInfoForPosition(0,
  209.                                                         MInfo.ReferencePosition.x,
  210.                                                         MInfo.ReferencePosition.y,
  211.                                                         MInfo.ReferencePosition.z)
  212.     end
  213.  
  214.     Elevation = TargetPInfo.ElevationForAltitudeComponentOnly
  215.     Azimuth = math.abs(TargetPInfo.Azimuth)
  216.  
  217.     if not (TargetId == -1) or (Mode == 3) then
  218.        
  219.         if Azimuth > TargetTolerance and Azimuth < 180 then
  220.             LeftThrustAdjust = SteeringDifference * Azimuth
  221.             RightThrustAdjust = -SteeringDifference * Azimuth
  222.         elseif Azimuth >= 180 and Azimuth < (360 - TargetTolerance) then
  223.             LeftThrustAdjust = -SteeringDifference * (360 - Azimuth)
  224.             RightThrustAdjust = SteeringDifference * (360 - Azimuth)
  225.         else
  226.             LeftThrustAdjust = 0
  227.             RightThrustAdjust = 0
  228.         end
  229.         if (Pitch > 40 and Pitch < 180) or (Pitch < 320 and Pitch > 180)then
  230.             LeftThrustAdjust = 0
  231.             RightThrustAdjust = 0
  232.         end
  233.     end
  234.    
  235.     if Pitch > (PitchTolerance * 2) and Pitch < 180 then
  236.         PitchAdjust = -((Pitch / PitchTolerance) * PitchSensitivity)
  237.     elseif Pitch < (360 - (PitchTolerance * 2)) and Pitch > 180 then
  238.         PitchAdjust = (((360 - Pitch) / PitchTolerance) * PitchSensitivity)
  239.     else
  240.         PitchAdjust = 1
  241.     end
  242.  
  243.     if (Pitch > PitchTolerance * 3 and Pitch < 180) or
  244.        (Pitch < (360 - (PitchTolerance * 3)) and Pitch > 180) then
  245.         PitchAdjust = 10
  246.     end
  247.  
  248.     if (Altitude < CruisingAltitude - (AltitudeTolerance * 20)) then
  249.         PitchAdjust = PitchAdjust + 1
  250.     elseif Altitude > 450 then
  251.         LeftThrustAdjust = 50
  252.         RightThrustAdjust = 50
  253.         PitchAdjust = 25
  254.     elseif (Altitude > CruisingAltitude + AltitudeTolerance) then
  255.         PitchAdjust = PitchAdjust - 1
  256.     end
  257.  
  258.     if Elevation < -TargetTolerance and Altitude > 5 then
  259.         PitchAdjust = PitchAdjust + (Elevation * -0.5)
  260.     elseif Elevation > TargetTolerance then
  261.         PitchAdjust = PitchAdjust - (Elevation * 0.5)
  262.     end
  263.  
  264.     if TargetPInfo.Range < 300 and math.abs(Elevation) < TargetTolerance and
  265.           (Azimuth > (360 - TargetTolerance) or Azimuth < TargetTolerance) then
  266.         I:FireWeapon(0,0)
  267.     end
  268.  
  269.     SetThrust(I, DefaultThrust)
  270.     SetThrusterAngle(I)
  271. end
  272.  
  273. function Hover(I)
  274.     Roll = I:GetConstructRoll()
  275.     Pitch = I:GetConstructPitch()
  276.     Altitude = I:GetConstructPosition().y
  277.     Angle = 90
  278.     DefaultThrust = -1.65
  279.  
  280.     if Altitude > CruisingAltitude + AltitudeTolerance then
  281.         AltitudeAdjust = -AltitudeSensitivity
  282.     elseif Altitude < CruisingAltitude - AltitudeTolerance then
  283.         AltitudeAdjust = AltitudeSensitivity
  284.     else
  285.         AltitudeAdjust = 0
  286.     end
  287. --[[ Removed after adding jet stabilisers -- code kept in case I want to make an Osprey
  288.     if Roll > RollTolerance and Roll < (RollTolerance * SevereRoll) then
  289.         RollDamper = 0
  290.         LeftThrustAdjust = ((Roll / RollTolerance) * RollSensitivity)
  291.         RightThrustAdjust = -((Roll / RollTolerance) * RollSensitivity)
  292.     elseif Roll < 360 - RollTolerance and Roll > 360 - (RollTolerance * SevereRoll) then
  293.         RollDamper = 0
  294.         LeftThrustAdjust = -((-(360-Roll) / RollTolerance) * RollSensitivity)
  295.         RightThrustAdjust = ((-(360-Roll) / RollTolerance) * RollSensitivity)
  296.     elseif Roll > (RollTolerance * SevereRoll) and Roll < 180 then
  297.         RollDamper = RollDamper + 1
  298.         LeftThrustAdjust = RollSensitivity
  299.         RightThrustAdjust = -((DefaultThrust * math.max(0.07, ((7.5 - RollDamper) / 10)) * ((RollTolerance * SevereRoll) / Roll))
  300.  - math.abs(AltitudeAdjust))
  301.     elseif Roll > 180 and Roll < 360 - (RollTolerance * SevereRoll) then
  302.         RollDamper = RollDamper + 1
  303.         LeftThrustAdjust = -((DefaultThrust * math.max(0.07, ((7.5 - RollDamper) / 10)) * (Roll / (360 -(RollTolerance * SevereRoll)))) - math.abs(AltitudeAdjust))
  304.         RightThrustAdjust = RollSensitivity
  305.     else
  306.         RollDamper = 0
  307.         LeftThrustAdjust = 0
  308.         RightThrustAdjust = 0
  309.     end
  310.  
  311. ]]--
  312.     if Pitch > PitchTolerance and Pitch < 180 then
  313.         PitchAdjust = -((Pitch / PitchTolerance) * PitchSensitivity)
  314.     elseif Pitch < (360 - PitchTolerance) and Pitch > 180 then
  315.         PitchAdjust = ((Pitch / PitchTolerance) * PitchSensitivity)
  316.     else
  317.         PitchAdjust = 0
  318.     end
  319.  
  320.     SetThrusterAngle(I)
  321.     SetThrust(I, DefaultThrust)
  322. end
  323.  
  324. function SetThrust(I, speed)
  325.     if I:IsSpinnerDedicatedHelispinner(RightThruster) then
  326.         I:SetSpinnerContinuousSpeed(RightThruster, (speed + RightThrustAdjust + AltitudeAdjust))
  327.     end
  328.     if I:IsSpinnerDedicatedHelispinner(LeftThruster) then
  329.         I:SetSpinnerContinuousSpeed(LeftThruster, (speed + LeftThrustAdjust + AltitudeAdjust))
  330.     end
  331.     if I:IsSpinnerDedicatedHelispinner(PitchThruster) then
  332.         I:SetSpinnerContinuousSpeed(PitchThruster, PitchAdjust)
  333.     end
  334. end
  335.  
  336. function SetThrusterAngle(I)
  337.     I:SetSpinnerRotationAngle(LeftSponson, Angle + LeftAngleAdjust)
  338.     I:SetSpinnerRotationAngle(RightSponson, (Angle + RightAngleAdjust)* -1)    
  339. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement