MegiRicky

fireControlRadar

Aug 11th, 2024 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. local reader = peripheral.find("blockReader")
  2. local modem = peripheral.find("modem")
  3.  
  4. local g = 0.04
  5. local cd = 0.99
  6.  
  7. local c_est = 0.0028
  8. local vm = 340 --muzzle velocity, medium cannon = 60 + 20*barrel length
  9. local u = vm/20 --speed per tick maybe, idk
  10.  
  11. print("Input the y height compensation (how far the center of gravity of this ship is to the cannon), default = -10")
  12. local yLevelCompensation = io.read()
  13. if yLevelCompensation == "" then
  14. yLevelCompensation = -10
  15. end
  16. yLevelCompensation = tonumber(yLevelCompensation)
  17.  
  18. print("Input the yaw compensation. (test 0,90,180,270 until the yaw is correct), default = 90")
  19. local yawCompensate = io.read()
  20. if yawCompensate == "" then
  21. yawCompensate = 90
  22. end
  23. yawCompensate = tonumber(yawCompensate)
  24.  
  25. print("Input the muzzle velocity (default: 340)")
  26. local vm = io.read()
  27. if vm == "" then
  28. vm = 340
  29. end
  30. vm = tonumber(vm)
  31.  
  32. print("Input ship pitch cannon pitch compensation, type(-pitch,+pitch,-roll,+roll,none), default: -pitch")
  33. local cannonPitchCom = io.read()
  34. if cannonPitchCom == "" then
  35. cannonPitchCom = "-pitch"
  36. end
  37.  
  38. print("Input the cannon data transmition channel (default: 900)")
  39. local cannonDataChannel = io.read()
  40. if cannonDataChannel == "" then
  41. cannonDataChannel = 900
  42. end
  43. cannonDataChannel = tonumber(cannonDataChannel)
  44.  
  45.  
  46. if modem then
  47. modem.open(cannonDataChannel)
  48. end
  49.  
  50. function calculateRange(a)
  51. local radians = math.rad(a)
  52.  
  53. local part1 = u * math.cos(radians) / math.log(cd)
  54. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * vm * math.sin(radians)) - 1
  55. local XR = part1 * part2
  56.  
  57. return XR
  58. end
  59.  
  60. function calculateHitPosition(shipPosition, cannonYaw, cannonPitch)
  61. local range = calculateRange(cannonPitch)
  62. local yawRadians = math.rad(cannonYaw)
  63. local deltaX = range * math.sin(yawRadians) -- Change in the x-direction
  64. local deltaZ = -range * math.cos(yawRadians) -- Change in the z-direction
  65.  
  66. local targetX = shipPosition.x + deltaX
  67. local targetZ = shipPosition.z + deltaZ
  68.  
  69. return {x = targetX, z = targetZ}
  70. end
  71.  
  72. local pitch = 40
  73. local range = calculateRange(pitch)
  74. print("The calculated range for a pitch of " .. pitch .. " degrees is: " .. range)
  75.  
  76. while true do
  77. cannonData = reader.getBlockData()
  78. cannonPitch = cannonData.CannonPitch
  79. if cannonPitchCom == "-pitch" then
  80. cannonPitch = cannonPitch - math.deg(ship.getPitch())
  81. print("adjusted pitch with -pitch: "..cannonPitch)
  82. elseif cannonPitchCom == "+pitch" then
  83. cannonPitch = cannonPitch + math.deg(ship.getPitch())
  84. print("adjusted pitch with +pitch: "..cannonPitch)
  85. elseif cannonPitchCom == "-roll" then
  86. cannonPitch = cannonPitch - math.deg(ship.getRoll())
  87. print("adjusted pitch with -roll: "..cannonPitch)
  88. elseif cannonPitchCom == "+roll" then
  89. cannonPitch = cannonPitch + math.deg(ship.getRoll())
  90. print("adjusted pitch with +roll: "..cannonPitch)
  91. else
  92. print("Not adjusting pitch with ship pitch")
  93. end
  94. cannonYaw = math.deg(ship.getYaw()) + yawCompensate
  95. data = {}
  96. if cannonYaw<0 then
  97. cannonYaw = cannonYaw + 360
  98. end
  99. print(cannonYaw)
  100. pos = ship.getWorldspacePosition()
  101. pos.y = pos.y + yLevelCompensation --fire control radar posistion compensation
  102. if cannonPitch > 0 then
  103. distance = calculateRange(cannonPitch)
  104. hitPos = calculateHitPosition(pos,cannonYaw,cannonPitch)
  105. print("hit posisiton: "..hitPos.x.." Y: "..pos.y.." ,Z: "..hitPos.z)
  106. data = {
  107. x = hitPos.x,
  108. y = hitPos.y,
  109. z = hitPos.z,
  110. cannonPitch = cannonPitch,
  111. cannonYaw = cannonYaw,
  112. source = pos
  113. }
  114. modem.transmit(cannonDataChannel,0,data)
  115. else
  116. data = {
  117. cannonPitch = cannonPitch,
  118. cannonYaw = cannonYaw,
  119. source = pos
  120. }
  121. modem.transmit(cannonDataChannel,0,data)
  122. end
  123. print("cannonPitch: "..cannonPitch)
  124. print("cannonYaw: "..cannonYaw)
  125.  
  126. sleep()
  127. end
Advertisement
Add Comment
Please, Sign In to add comment