miniminater

bb

Jun 8th, 2023 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local enable_sides
  4.  
  5. local config_file = fs.open("/branch_config.cfg", "r")
  6. if config_file then
  7. local sides = textutils.unserialise(config_file.readAll())
  8. if sides then
  9. enable_sides = sides
  10. else
  11. print("Malformed config file!")
  12. end
  13. else
  14. print("No config file!")
  15. end
  16.  
  17. if #args < 1 then
  18. error("Usage: branch <station_name>")
  19. end
  20.  
  21. local station_name = args[1]
  22. print("Starting branch as station "..station_name)
  23.  
  24. local modem = peripheral.find("modem")
  25.  
  26. if not modem then
  27. error("No modem found!")
  28. end
  29.  
  30. modem.open(15) --listen for feeders on channel 15
  31.  
  32. os.startTimer(0.05)
  33.  
  34. modem.transmit(43, 15, "feeder " .. os.getComputerID() .. " " .. station_name)
  35.  
  36. while true do
  37. local event, ev1, ev2, ev3, ev4, ev5 = os.pullEvent()
  38. if event == "modem_message" then
  39. if ev4 == "hub_boot" or ev4 == "hub_scan" then
  40. modem.transmit(43, 15, "feeder " .. os.getComputerID() .. " " .. station_name)
  41. else
  42. local words = {}
  43. for word in ev4:gmatch("%S+") do
  44. table.insert(words, word)
  45. end
  46. if tonumber(words[1]) == os.getComputerID() then
  47. if words[2] == "command" then
  48. --handle a command
  49. if words[3] == "redstone" then
  50. redstone.setOutput(words[4], words[5] == "on")
  51. end
  52. elseif words[2] == "ping" then
  53. --return a ping
  54. modem.transmit(43, 15, "ping_reply " .. os.getComputerID())
  55. elseif words[2] == "enable" then
  56. for k, v in pairs(enable_sides) do
  57. redstone.setOutput(k, v == "true")
  58. end
  59. elseif words[2] == "disable" then
  60. for k, v in pairs(enable_sides) do
  61. redstone.setOutput(k, v ~= "true")
  62. end
  63. end
  64. end
  65. end
  66. elseif event == "timer" then
  67. os.startTimer(0.05)
  68. else
  69. print(event)
  70. end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment