MegiRicky

NGDW_Gen4_Drone

May 19th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. local helm = peripheral.find("eureka_ship_helm")
  2. local camera = peripheral.find("camera")
  3. local modem = peripheral.find("modem") or error("No modem attached")
  4.  
  5. local function getChannelInput(prompt, default)
  6. print(prompt .. ", default: " .. default)
  7. local input = io.read()
  8. if input == "" then
  9. input = default
  10. end
  11. return tonumber(input)
  12. end
  13. local waypointChannel = getChannelInput("Input the waypoint channel",1421)
  14. local droneControlChannel = getChannelInput("Input the droneControlChannel",2020)
  15. modem.open(waypointChannel)
  16. modem.open(droneControlChannel)
  17.  
  18. local droneControls = {}
  19. local function designation()
  20. -- Check if camera exists
  21. if not camera then
  22. print("Error: Camera peripheral not found")
  23. return
  24. end
  25.  
  26. print("Designating target...")
  27. local result = camera.clipBlockDetail()
  28.  
  29. if result and result.hit then
  30. local newWaypoint = {
  31. x = result.hit.x,
  32. y = result.hit.y,
  33. z = result.hit.z,
  34. color = colors.white -- White color in hexadecimal
  35. }
  36.  
  37. print(string.format("Adding waypoint at x:%.1f y:%.1f z:%.1f",
  38. result.hit.x, result.hit.y, result.hit.z))
  39.  
  40. -- Optional: Draw outline at target
  41. camera.outlineToUser(
  42. result.hit.x, result.hit.y, result.hit.z,
  43. "UP", -- Default direction
  44. 0xFFFFFF, -- White
  45. "designation" -- Slot ID
  46. )
  47.  
  48. -- Transmit waypoint
  49. if modem then
  50. modem.transmit(waypointChannel, waypointChannel, newWaypoint)
  51. else
  52. print("Error: Modem not available")
  53. end
  54. else
  55. print("No valid target detected")
  56. end
  57. end
  58.  
  59. -- Map controls to redstone outputs
  60. local function applyControls()
  61. -- Movement controls
  62. --print(textutils.serialize(droneControls))
  63. local forward = 0
  64. if droneControls.forward then
  65. print("moving forward")
  66. redstone.setAnalogOutput("top", 1)
  67. forward = 1
  68. else
  69. redstone.setOutput("top",false)
  70. end
  71.  
  72. if droneControls.backward then
  73. redstone.setAnalogOutput("back", 1)
  74. forward = -1
  75. else
  76. redstone.setOutput("back",false)
  77. end
  78.  
  79. if droneControls.strafeLeft then
  80. redstone.setAnalogOutput("right", 1)
  81. else
  82. redstone.setOutput("right",false)
  83. end
  84.  
  85. if droneControls.strafeRight then
  86. redstone.setAnalogOutput("left", 1)
  87. else
  88. redstone.setOutput("left",false)
  89. end
  90. local turning = 0
  91. local vertical = 0
  92. if droneControls.turnLeft then
  93. turning = 1
  94. elseif droneControls.turnRight then
  95. turning = -1
  96. end
  97.  
  98. if droneControls.goUp then
  99. vertical = 1
  100. elseif droneControls.goDown then
  101. vertical = -1
  102. end
  103.  
  104. helm.move(turning,vertical,forward)
  105.  
  106. if droneControls.designate then
  107. designation()
  108. end
  109. end
  110.  
  111. local function controlLoop()
  112. while true do
  113. applyControls()
  114. sleep() -- 20Hz update rate
  115. end
  116. end
  117.  
  118. local function modemHandler()
  119. while true do
  120. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  121. if channel == droneControlChannel then
  122. --print("recieved")
  123. droneControls = message
  124. end
  125. end
  126. end
  127.  
  128. -- Start all threads
  129. parallel.waitForAll(
  130. controlLoop,
  131. modemHandler
  132. )
Advertisement
Add Comment
Please, Sign In to add comment