MegiRicky

redstonePL-01.lua

Aug 13th, 2024 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. -- redstonePL-01.lua
  2. local modem = peripheral.find("modem") or error("No modem attached", 0)
  3. print("Input the controlChannel, default = 500")
  4. local controlChannel = io.read()
  5. if controlChannel == "" then
  6. controlChannel = 500
  7. end
  8. controlChannel = tonumber(controlChannel)
  9.  
  10. -- Redstone sides for cannon, autocannon, and smoke grenades
  11. local cannonSide = "back"
  12. local autocannonSide = "right"
  13. local smokeGrenadeSide = "front"
  14.  
  15. modem.open(controlChannel)
  16.  
  17. print("Redstone controller started. Listening for fire controls.")
  18.  
  19. local controls = {
  20. fireCannon = false,
  21. fireAutocannon = false,
  22. launchSmokeGrenade = false
  23. }
  24.  
  25. local function executeControls()
  26. while true do
  27. -- Fire controls
  28. if controls.fireCannon then
  29. redstone.setOutput(cannonSide, true)
  30. else
  31. redstone.setOutput(cannonSide, false)
  32. end
  33.  
  34. if controls.fireAutocannon then
  35. redstone.setOutput(autocannonSide, true)
  36. else
  37. redstone.setOutput(autocannonSide, false)
  38. end
  39.  
  40. if controls.launchSmokeGrenade then
  41. redstone.setOutput(smokeGrenadeSide, true)
  42. sleep(0.1) -- Short delay to ensure activation
  43. redstone.setOutput(smokeGrenadeSide, false)
  44. controls.launchSmokeGrenade = false -- Reset the control after activation
  45. end
  46.  
  47. sleep(0.05) -- Adjust the delay for control responsiveness
  48. end
  49. end
  50.  
  51. -- Function to listen for modem messages and update controls
  52. local function modem_message()
  53. while true do
  54. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  55. if channel == controlChannel then
  56. controls = message
  57. print("Received controls:", textutils.serialize(controls))
  58. end
  59. end
  60. end
  61.  
  62. -- Run the functions in parallel
  63. parallel.waitForAll(
  64. modem_message,
  65. executeControls
  66. )
  67.  
Advertisement
Add Comment
Please, Sign In to add comment