miniminater

b

Jun 8th, 2023 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. local global_run = true
  2. local modem = peripheral.find("modem")
  3.  
  4. local monitor = peripheral.find("monitor")
  5. if not monitor then
  6. print("No monitor found!")
  7. end
  8. term.redirect(monitor)
  9.  
  10. local branches = {}
  11.  
  12. local function find(id)
  13. if tonumber(id) then
  14. for _, v in pairs(branches) do
  15. if v.id == id then
  16. return v
  17. end
  18. end
  19. else
  20. for _, v in pairs(branches) do
  21. if v.name == id then
  22. return v
  23. end
  24. end
  25. end
  26. -- not found
  27. return nil
  28. end
  29.  
  30. local function checkSide(side)
  31. return side == "top" or side == "bottom" or side == "left" or side == "right" or side == "front" or side == "back"
  32. end
  33.  
  34. local wid, hig = term.getSize()
  35. local evqueue = {}
  36. local function pullCommand()
  37. term.redirect(term.native())
  38. local buf = ":"
  39. term.scroll(1)
  40. term.setCursorPos(1, hig)
  41. term.write(buf)
  42. local running = true
  43. while running do
  44. local ev = {os.pullEvent()}
  45. if ev[1] == "char" then
  46. buf = buf .. ev[2]
  47. elseif ev[1] == "key" then
  48. if ev[2] == keys.enter then
  49. running = false
  50. elseif ev[2] == keys.backspace then
  51. buf = buf:sub(1, #buf - 1)
  52. if #buf == 0 then
  53. running = false
  54. end
  55. end
  56. else
  57. table.insert(evqueue, ev)
  58. end
  59. term.setCursorPos(1, hig)
  60. term.clearLine(hig)
  61. term.write(buf)
  62. end
  63.  
  64. local words = {}
  65. for str in buf:gmatch("%S+") do
  66. table.insert(words, str)
  67. end
  68. if #words < 1 then
  69. return
  70. end
  71. words[1] = words[1]:sub(2, #words[1])
  72.  
  73. print()
  74.  
  75. if words[1] == "test" then
  76. print("aawagga")
  77. elseif words[1] == "q" then
  78. print("Exiting. Goodbye!")
  79. global_run = false
  80. elseif words[1] == "redstone" then
  81. if (words[2] == "on" or words[2] == "off") and words[3] and words[4] and checkSide(words[3]) then
  82. local receiver = find(words[4])
  83. if receiver then
  84. modem.transmit(15, 43, receiver.id .. " command redstone " .. words[3] .. " " .. words[2])
  85. else
  86. print("Computer name/id `" .. words[3] .. "` not found!")
  87. end
  88. else
  89. print("Usage: redstone <on/off> <side> <receiver name/id>")
  90. end
  91. elseif words[1] == "ping" then
  92. if words[2] and ((not words[3]) or tonumber(words[3])) then
  93. local receiver = find(words[2])
  94. if receiver then
  95. modem.transmit(15, 43, receiver.id .. " ping")
  96. local id = os.startTimer(words[3] and tonumber(words[3]) or 3)
  97. local ev
  98. local replied = false
  99. local running_2 = true
  100. while running_2 do
  101. ev = {os.pullEvent()}
  102. if ev[1] == "timer" and ev[2] == id then
  103. running_2 = false --timed out
  104. elseif ev[1] == "modem_message" then
  105. if ev[5] == "ping_reply " .. receiver.id then
  106. replied = true
  107. running_2 = false
  108. os.cancelTimer(id)
  109. end
  110. end
  111. end
  112. if replied then
  113. print("Received reply from " .. words[2])
  114. else
  115. print("Did not receive reply from " .. words[2])
  116. end
  117. else
  118. print("Computer name/id `" .. words[2] .. "` not found!")
  119. end
  120. else
  121. print("Usage: ping <receiver name/id> [timeout]")
  122. end
  123. elseif words[1] == "enable" then
  124. if words[2] then
  125. local receiver = find(words[2])
  126. if receiver then
  127. modem.transmit(15, 43, receiver.id .. " enable")
  128. else
  129. print("Computer name/id `" .. words[2] .. "` not found!")
  130. end
  131. else
  132. print("Usage: enable <receiver name/id>")
  133. end
  134. elseif words[1] == "disable" then
  135. if words[2] then
  136. local receiver = find(words[2])
  137. if receiver then
  138. modem.transmit(15, 43, receiver.id .. " disable")
  139. else
  140. print("Computer name/id `" .. words[2] .. "` not found!")
  141. end
  142. else
  143. print("Usage: disable <receiver name/id>")
  144. end
  145. elseif words[1] == "scan" then
  146. modem.transmit(15, 43, "hub_scan")
  147. elseif words[1] == "help" then
  148. if not words[2] then
  149. print("hubOS commands:")
  150. print("redstone, ping, <...no more yet>")
  151. print("Run :help <command_name> for more info on any command.")
  152. elseif words[2] == "redstone" then
  153. print("hubOS help: redstone")
  154. print("Usage: redstone <on/off> <side> <receiver name/id>")
  155. print("Enables or disables the redstone output on a given side for a computer.")
  156. elseif words[2] == "ping" then
  157. print("hubOS help: ping")
  158. print("ping <receiver name/id> [timeout]")
  159. print("Pings a given computer to check whether it is online. Times out after 3 seconds, or after the passed timeout in seconds.")
  160. elseif words[2] == "enable" or words[2] == "disable" then
  161. print("hubOS help: enable/disable")
  162. print("enable <receiver name/id>")
  163. print("disable <receiver name/id>")
  164. print("Enables or disables the redstone output as configured in the computer's branch_config.cfg file.")
  165. end
  166. else
  167. print("Unknown command: " .. words[1])
  168. end
  169.  
  170. term.redirect(monitor)
  171. end
  172.  
  173.  
  174.  
  175.  
  176.  
  177. term.clear()
  178.  
  179. term.setCursorPos(1, 1)
  180. print("Starting hubOS...")
  181.  
  182. --Set up the modem for transmit
  183. if not modem then
  184. term.setTextColor(colors.red)
  185. print("No modem attached! Halting.")
  186. do return end
  187. end
  188.  
  189. modem.open(43) --listen on 43
  190.  
  191. os.startTimer(0.05)
  192.  
  193. --Send a seeker message
  194. modem.transmit(15, 43, "hub_boot")
  195.  
  196. while global_run do
  197. local event, ev1, ev2, ev3, ev4, ev5 = os.pullEvent()
  198. if event == "timer" then
  199. os.startTimer(0.05)
  200. elseif event == "modem_message" then
  201. print("message " .. ev4)
  202. local words = {}
  203. for word in ev4:gmatch("%S+") do
  204. table.insert(words, word)
  205. end
  206. if words[1] == "feeder" then
  207. if not find(words[3]) then
  208. print("Registering branch " .. words[2] .. " as " .. words[3])
  209. branches[tonumber(words[2])] = {id = tonumber(words[2]), name = words[3]}
  210. else
  211. local holder = find(words[3])
  212. print("Already using ID " .. words[3] .. " for computer " .. holder.id)
  213. end
  214. end
  215. elseif event == "char" then
  216. if ev1 == ":" then
  217. pullCommand()
  218. end
  219. else
  220. --print(event)
  221. end
  222. end
Advertisement
Add Comment
Please, Sign In to add comment