MegiRicky

Untitled

Jul 19th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.43 KB | None | 0 0
  1. local monitorSide = "top"
  2. local modemSide = "back"
  3. local Xchannel = 20
  4. local Ychannel = 21
  5. local Zchannel = 22
  6. local pitchChannel = 23
  7. local yawChannel = 24
  8.  
  9. local radar = peripheral.find("sp_radar")
  10. local modem = peripheral.find("modem")
  11. local monitor = peripheral.find("monitor")
  12. local router = peripheral.find("redrouter")
  13. local controller = peripheral.find("tweaked_controller")
  14. monitor.setTextScale(0.5)
  15.  
  16. modem.open(yawChannel)
  17. modem.open(Xchannel)
  18. modem.open(Ychannel)
  19. modem.open(Zchannel)
  20. modem.open(pitchChannel)
  21.  
  22. local currentPitch = 0
  23. local currentYaw = 0
  24. local sourceX = 0
  25. local sourceY = 0
  26. local sourceZ = 0
  27. local speed = 0
  28. local hostileIDs = {}
  29. -- Helper function to check if an element exists in a table
  30. function table.contains(table, element)
  31.     for _, value in ipairs(table) do
  32.         if value == element then
  33.             return true
  34.         end
  35.     end
  36.     return false
  37. end
  38.  
  39. local function round(num)
  40.     return math.floor(num * 100 + 0.5) / 100
  41. end
  42.  
  43. -- Function to convert a table of numbers into a comma-separated string
  44. local function tableToString(t)
  45.     local list = {}
  46.     for _, v in ipairs(t) do
  47.         table.insert(list, tostring(v))
  48.     end
  49.     return table.concat(list, ", ")
  50. end
  51.  
  52. -- Function to listen for modem messages and update coordinates
  53. local function listenForCoordinates()
  54.     while true do
  55.         local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  56.         if senderChannel == Xchannel then
  57.             sourceX = tonumber(message)
  58.         elseif senderChannel == Ychannel then
  59.             sourceY = tonumber(message)
  60.         elseif senderChannel == Zchannel then
  61.             sourceZ = tonumber(message)
  62.         elseif senderChannel == pitchChannel then
  63.             currentPitch = tonumber(message)
  64.         elseif senderChannel == yawChannel then
  65.             currentYaw = tonumber(message)
  66.         end
  67.     end
  68. end
  69.  
  70. local lockedTarget = {}
  71. local selectedIndex = 1
  72. local function selectTarget()
  73.     local results = radar.scanForShips(1000)
  74.     local possibleTargets = {}
  75.  
  76.     if not results or #results == 0 then
  77.         table.insert(possibleTargets, "No objects detected.")
  78.     else
  79.         for i, object in ipairs(results) do
  80.             local x = object.pos.x - sourceX
  81.             local y = object.pos.y - sourceY
  82.             local z = object.pos.z - sourceZ
  83.             local distance = math.sqrt(x^2 + y^2 + z^2)
  84.  
  85.             -- Check if the object's mass is greater than 10,000 before adding it to the display
  86.             if object.mass > 20000 and distance > 50 then
  87.                
  88.                 local objectInfo = {
  89.                     id = object.id,
  90.                     mass = object.mass,
  91.                     pos = object.pos,
  92.                     velocity = object.velocity,
  93.                     distance = distance
  94.                 }
  95.                 table.insert(possibleTargets, objectInfo)
  96.             end
  97.         end
  98.         -- Check if any objects passed the mass filter, if not, modify the display message
  99.     end
  100.  
  101.     local maxIndex = #possibleTargets
  102.    
  103.     local yAxis = controller.getAxis(2)
  104.     if yAxis < 0 then
  105.         selectedIndex = selectedIndex - 1
  106.        
  107.     elseif yAxis > 0 then
  108.         selectedIndex = selectedIndex + 1
  109.        
  110.     elseif controller.getButton(11) then
  111.  
  112.     end
  113.     if selectedIndex < 1 then
  114.         selectedIndex = 1
  115.     elseif selectedIndex > maxIndex then
  116.         selectedIndex = maxIndex
  117.     end
  118.     lockedTarget = possibleTargets[selectedIndex]
  119.    
  120.  
  121.     -- Update the monitor to highlight the selected target
  122.  
  123.     monitor.clear()
  124.     monitor.setCursorPos(1, 1)
  125.     monitor.write("Position (X, Y, Z): " .. tostring(round(sourceX)) .. ", " .. tostring(round(sourceY)) .. ", " .. tostring(round(sourceZ)))
  126.     monitor.setCursorPos(1, 2)
  127.     monitor.write("Yaw: " .. tostring(round(currentYaw)) .. " Pitch: " .. tostring(round(currentPitch)))
  128.  
  129.  
  130.  
  131.     for i, line in ipairs(displayData) do
  132.         monitor.setCursorPos(1, i + 3)
  133.         if i == selectedIndex + 1 then
  134.             monitor.setBackgroundColor(colors.blue)
  135.         end
  136.         monitor.write(line)
  137.         monitor.setBackgroundColor(colors.black)
  138.     end
  139. end
  140.  
  141. parallel.waitForAny(listenForCoordinates, function()
  142.     while true do
  143.  
  144.         modem.open(yawChannel)
  145.         modem.open(Xchannel)
  146.         modem.open(Zchannel)
  147.         modem.open(pitchChannel)
  148.         -- radar scan
  149.         results = radar.scanForShips(1000)  -- Scan within a range of 1000; adjust as needed
  150.         displayData = {}  -- Initialize an empty table to store display information
  151.        
  152.         if not results or #results == 0 then
  153.             table.insert(displayData, "No objects detected.")
  154.         else
  155.             table.insert(displayData, "Detected Objects:")
  156.             for i, object in ipairs(results) do
  157.                 local position = ship.getWorldspacePosition()
  158.                 local x = object.pos.x - sourceX
  159.                 local y = object.pos.y - sourceY
  160.                 local z = object.pos.z - sourceZ
  161.                 local distance = math.sqrt(x^2 + y^2 + z^2)
  162.  
  163.                 local X = object.pos.x - position.x
  164.                 local Y = object.pos.y - position.y
  165.                 local Z = object.pos.z - position.z
  166.                 local radarDistance = math.sqrt(X^2 + Y^2 + Z^2 )
  167.                 -- Check if the object's mass is greater than 10,000 before adding it to the display
  168.                 if object.mass > 20000 and distance > 5 and radarDistance > 50 then
  169.                     local objectInfo = string.format("ID:%d M:%d X:%.1f Y:%.2f Z:%.1f D:%.1f DR:%.1f",
  170.                     object.id, object.mass, object.pos.x, object.pos.y, object.pos.z, distance, radarDistance)
  171.                     table.insert(displayData, objectInfo)
  172.                 end
  173.  
  174.             end
  175.             -- Check if any objects passed the mass filter, if not, modify the display message
  176.             if #displayData == 1 then  -- Only the title has been added
  177.                 displayData[2] = "No objects above mass threshold detected."
  178.             end
  179.         end
  180.         selectTarget()
  181.         if lockedTarget.id and redstone.getInput("bottom") then
  182.             local results = radar.scanForShips(1000)
  183.             local locked = {}
  184.             print()
  185.             print("locked id: ",lockedTarget.id)
  186.             print("target position: ",lockedTarget.pos.x, lockedTarget.pos.y, lockedTarget.pos.z)
  187.             print("cannon position: ",sourceX, sourceY, sourceZ)
  188.             print("distance: ",lockedTarget.distance)
  189.  
  190.             local estimateTime = lockedTarget.distance / (6*20)
  191.             local estimateX = lockedTarget.pos.x + lockedTarget.velocity.x * estimateTime
  192.             local estimateY = lockedTarget.pos.y + lockedTarget.velocity.y * estimateTime
  193.             local estimateZ = lockedTarget.pos.z + lockedTarget.velocity.z * estimateTime
  194.  
  195.             local dx = estimateX - sourceX
  196.             local dy = estimateY - sourceY
  197.             local dz = estimateZ - sourceZ
  198.  
  199.             print("dx: ",dx," dy: ",dy," dz: ",dz)
  200.  
  201.             local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  202.             local pitch = math.deg(math.atan2(dy, horizontalDistance))
  203.  
  204.             local yaw = math.deg(math.atan2(-dx, dz))
  205.             yaw = yaw + 180
  206.    
  207.             print("non compesated pitch: ",pitch)
  208.             local pitchAdjustments = {
  209.                 {min = 50, max = 100, increment = 0.5},
  210.                 {min = 180, max = 250, increment = 1},
  211.                 {min = 250, max = 300, increment = 2},
  212.                 {min = 300, max = 350, increment = 3},
  213.                 {min = 350, max = 400, increment = 3.7},
  214.                 {min = 400, max = 450, increment = 4.3},
  215.                 {min = 450, max = 500, increment = 5},
  216.                 {min = 500, max = 550, increment = 5.9},
  217.                 {min = 550, max = 600, increment = 6.6},
  218.                 {min = 600, max = 650, increment = 7.5}
  219.             }
  220.  
  221.             for _, adjustment in ipairs(pitchAdjustments) do
  222.                 if lockedTarget.distance > adjustment.min and lockedTarget.distance <= adjustment.max then
  223.                     pitch = pitch + adjustment.increment
  224.                     break  -- Exit the loop once the correct range is found
  225.                 end
  226.             end
  227.  
  228.             print("current",currentPitch,currentYaw)
  229.             print("pitch,yaw",pitch,yaw)
  230.  
  231.             local deltaYaw = yaw - currentYaw
  232.             local deltaYaw = (deltaYaw + 180) % 360 - 180
  233.             local deltaPitch = pitch - currentPitch
  234.             --turning
  235.             local yaw_tolerance = 1
  236.             local pitch_tolerance = 1
  237.             if math.abs(deltaYaw) > yaw_tolerance then
  238.                 if deltaYaw < 0 then
  239.                     print("Turning left")
  240.  
  241.                     router.setOutput("left", false)
  242.                     if math.abs(deltaYaw) < 5 then
  243.                         local turnTime = math.abs(deltaYaw) / (4*360/60)* 0.8
  244.                         router.setOutput("top", true)
  245.                         sleep(turnTime)
  246.                         router.setOutput("top",false)
  247.                     else
  248.                         router.setOutput("top", true)
  249.                     end
  250.                 elseif deltaYaw > 0 then
  251.                     print("Turning right")
  252.                     router.setOutput("left", true)
  253.  
  254.                     router.setOutput("top", false)
  255.                     if math.abs(deltaYaw) < 5 then
  256.                         local turnTime = math.abs(deltaYaw) / (4*360/60) * 0.8
  257.                         router.setOutput("left", true)
  258.                         sleep(turnTime)
  259.                         router.setOutput("left",false)
  260.                     else
  261.                         router.setOutput("left", true)
  262.                     end
  263.                 end
  264.             else
  265.                 router.setOutput("top", false)
  266.                 router.setOutput("left", false)
  267.             end
  268.  
  269.             if math.abs(deltaPitch) > pitch_tolerance then
  270.                 if deltaPitch > 0 then
  271.                     print("Adjusting pitch up")
  272.                     router.setOutput("back", false)
  273.                     if math.abs(deltaPitch) < 5 then
  274.                         local turnTime = math.abs(deltaPitch) / (4 * 360 / 60) * 0.8
  275.                         router.setOutput("front", true)
  276.                         sleep(turnTime)
  277.                         router.setOutput("front", false)
  278.                     else
  279.                         router.setOutput("front", true)
  280.                     end
  281.                 else
  282.                     print("Adjusting pitch down")
  283.                     router.setOutput("front", false)
  284.                     if math.abs(deltaPitch) < 5 then
  285.                         local turnTime = math.abs(deltaPitch) / (4 * 360 / 60) * 0.8
  286.                         router.setOutput("back", true)
  287.                         sleep(turnTime)
  288.                         router.setOutput("back", false)
  289.                     else
  290.                         router.setOutput("back", true)
  291.                     end
  292.                 end
  293.             else
  294.                 router.setOutput("front", false)
  295.                 router.setOutput("back", false)
  296.             end
  297.             sleep(0.01)
  298.         else
  299.             router.setOutput("front", false)
  300.             router.setOutput("back", false)
  301.             router.setOutput("top", false)
  302.             router.setOutput("left", false)
  303.         end
  304.        
  305.     end
  306. end)
Advertisement
Add Comment
Please, Sign In to add comment