MegiRicky

torpedo

Aug 23rd, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2.  
  3. local function toDegrees(radians)
  4. return radians * (180 / math.pi)
  5. end
  6.  
  7. -- Function to convert degrees to a bearing
  8. local function toBearing(degrees)
  9. local bearing = degrees % 360
  10. if bearing < 0 then
  11. bearing = bearing + 360
  12. end
  13. return bearing
  14. end
  15.  
  16. print("Input the water level, default : 0")
  17. local waterLevel = io.read()
  18. if waterLevel == "" then
  19. waterLevel = 0
  20. end
  21. waterLevel = tonumber(waterLevel)
  22. redstone.setOutput("front",false)
  23. redstone.setOutput("left",false)
  24. redstone.setOutput("right",false)
  25. redstone.setOutput("back",false)
  26. redstone.setOutput("top",false)
  27. while true do
  28. print("Waiting for redstone")
  29. if redstone.getInput("bottom") then
  30. print("armmed, launching")
  31. break
  32. end
  33. sleep()
  34. end
  35.  
  36. print("armmed")
  37.  
  38. while true do
  39. local pos = ship.getWorldspacePosition()
  40. sleep(0.01)
  41. local velocity = ship.getVelocity()
  42. pos.x = pos.x + velocity.x * 2
  43. pos.z = pos.z + velocity.y * 2
  44.  
  45. local yaw = math.deg(ship.getYaw())
  46. local pitch = math.deg(ship.getPitch())
  47.  
  48.  
  49. -- Guidance control
  50. if pos.y > waterLevel - 4 then
  51. redstone.setOutput("back", true)
  52. else
  53. redstone.setOutput("back", false)
  54. end
  55.  
  56. --radar
  57. local results = radar.scanForShips(1000)
  58. local displayData = {}
  59. print(results)
  60. if not results or #results == 0 then
  61. table.insert(displayData, "No objects detected.")
  62. else
  63. table.insert(displayData, "Detected Objects:")
  64. local smallestYawDiff = 360
  65. local finalTargetYaw = nil
  66. local finalDistance = 1000
  67.  
  68. for i, object in ipairs(results) do
  69.  
  70. if object.mass > 100000 then
  71. print("id:", object.id)
  72. local dx = object.pos.x - pos.x
  73. local dz = object.pos.z - pos.z
  74. local angleFromSouth = math.atan2(dx, dz)
  75. local bearingFromSouth = math.deg(angleFromSouth)
  76. local targetYaw = (180 - bearingFromSouth) % 360
  77. local yawDiff = (targetYaw - yaw + 180) % 360 - 180
  78. local distance = math.sqrt(dx*dx+dz*dz)
  79.  
  80. -- Compare and store the smallest yaw difference
  81. if math.abs(yawDiff) < math.abs(smallestYawDiff) then
  82. smallestYawDiff = yawDiff
  83. finalTargetYaw = targetYaw
  84. finalDistance = distance
  85. print("Yaw Difference:", smallestYawDiff)
  86. end
  87. end
  88. end
  89.  
  90. if finalTargetYaw then
  91. if smallestYawDiff > 0 and math.abs(smallestYawDiff) > 8 then
  92. redstone.setOutput("right", true)
  93. redstone.setOutput("left", false)
  94. sleep(math.abs(smallestYawDiff)/40)
  95. redstone.setOutput("right", false)
  96. print("Turning left")
  97. elseif smallestYawDiff < 0 and math.abs(smallestYawDiff) > 8 then
  98. redstone.setOutput("left", true)
  99. redstone.setOutput("right", false)
  100. sleep(math.abs(smallestYawDiff)/40)
  101. redstone.setOutput("left", false)
  102. print("Turning right")
  103. else
  104. redstone.setOutput("left", false)
  105. redstone.setOutput("right", false)
  106. end
  107. end
  108. print("distance:",finalDistance)
  109. if finalDistance < 40 and redstone.getInput("front") then
  110. print("detonating")
  111.  
  112. sleep(0.5)
  113. redstone.setOutput("top",true)
  114. sleep(2)
  115. redstone.setOutput("top",false)
  116.  
  117. end
  118.  
  119. if #displayData == 1 then
  120. displayData[2] = "No objects above mass threshold detected."
  121. end
  122. end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment