Advertisement
Marlingaming

Hovercraft Automated Control

Aug 22nd, 2022 (edited)
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1. Active = false
  2. ObjDetails = {}
  3. TargetHeight = -1 -- -1 results in using current Height--
  4. Mode = 0 --0 = Nav, 1 = Combat--
  5. TargetData = {}
  6. Friendlies = {}
  7.  
  8. TX = 0
  9. TY = 0
  10.  
  11. CX = 0
  12. CY = 0
  13. CH = 0
  14.  
  15. MissionGiver()
  16. --Inputs, 1 CX, 2 CY, 3 CHeight, 4 CHeading, 5 Obj X, 6 Obj Y, 7 THeight, 8 9 10 Target/friendly updates,
  17. -- Bools, 1 Active, 2 Recieving Obj, 3 ,4 ,5 Receiving Target, 6 Receiving Friendly
  18. --outputs, 1 X, 2 Y, 3 Mission, 4 Up/Down, 6 Target X, 7 Target Y,
  19. --Bools, 1, 2 Return after Obj, 3 Land at Obj
  20. function onTick()
  21.     Active = input.getBool(1)
  22.     if input.getBool(5) == true then
  23.         UpdateTargets(input.getNumber(8),input.getNumber(9))
  24.     end
  25.     if input.getBool(6) == true then
  26.         UpdateFriendlies(input.getNumber(8),input.getNumber(9),input.getNumber(10))
  27.     end
  28.     if Active == true then
  29.         Decision()
  30.     end
  31.     HeightMaintainer()
  32.     TurretManager()
  33. end
  34.  
  35. function UpdateTargets(X,Y)--checks list of targets and see if the target is a new one or is a existing one that moved
  36.     Pass = false
  37.     if #TargetData > 0 then
  38.         for i = 1, #TargetData do
  39.             Dis = math.sqrt((X-TargetData[i][2])^2 + (Y - TargetData[i][3])^2)
  40.             if Pass == false and Dis < 20 then
  41.                 Pass = true
  42.                 TargetData[i][2] = X
  43.                 TargetData[i][3] = Y
  44.             end
  45.         end
  46.     end
  47.     if Pass == false then
  48.         TargetData[#TargetData + 1] = {true, X, Y}
  49.     end
  50. end
  51.  
  52. function UpdateFriendlies(X,Y,ID)--updates list of friendlies received from Friend or Foe Component
  53.     Pass = false
  54.     if #Friendlies > 0 then
  55.         for i = 1, #Friendlies do
  56.             if ID == Friendlies[i][1] then
  57.                 Pass = true
  58.                 Friendlies[i][2] = X
  59.                 Friendlies[i][3] = Y
  60.             end
  61.         end
  62.     end
  63.     if Pass == false then
  64.         Friendlies[#Friendlies + 1] = {ID, X, Y}
  65.     end
  66. end
  67.    
  68. function Decision()
  69.     Combat = false
  70.     Dis = 1000
  71.     if #TargetData > 0 then
  72.         for i = 1, #TargetData do
  73.             for I = 1, #Friendlies do
  74.                 Dis = math.sqrt((Friendlies[I][2]-TargetData[i][2])^2 + (Friendlies[I][3] - TargetData[i][3])^2)
  75.             end
  76.             if Dis > 20 then
  77.                 TX = TargetData[i][2]
  78.                 TY = TargetData[i][3]
  79.                 Combat = true
  80.             end
  81.         end
  82.     end
  83.     if Combat == true then
  84.  
  85.     else
  86.         TX = 0
  87.         TY = 0
  88.     end
  89. end
  90.  
  91. function TurretManager()--this gives Turret script Cords to work with, the turret script converts this and given ground alt to target ground targets
  92.     if TX == 0 and TY == 0 then
  93.         output.setNumber(6,0)--X
  94.         output.setNumber(7,0)--Y
  95.     else
  96.         output.setNumber(6,TX)
  97.         output.setNumber(7,TY)
  98.     end
  99. end
  100.  
  101. function HeightMaintainer()
  102.     Diff = 0
  103.     CHeight = input.getNumber(3)
  104.     UpDown = 0
  105.     if TargetHeight == -1 then
  106.         Diff = 0
  107.     else
  108.         Diff = TargetHeight - CHeight
  109.     end
  110.     if Diff == 0 then
  111.         UpDown = 0
  112.     elseif CHeight > TargetHeight then
  113.         UpDown = -1
  114.     elseif CHeight < TargetHeight then
  115.         UpDown = 1
  116.     end
  117.     output.setNumber(4,UpDown)
  118. end
  119.  
  120. function MissionGiver()
  121.     MisType = property.getNumber("MissionType")
  122.     Land = false
  123.     RetAf = false
  124.     if MisType == 1 then
  125.         RetAf = true
  126.     elseif MisType == 2 then
  127.         RetAf = true
  128.         Land = true
  129.     end
  130.     output.setBool(1,true)
  131.     output.setNumber(3,MisType)
  132.     output.setBool(2,RetAf)
  133.     output.setBool(3,Land)
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement