Advertisement
PongWithBreakout

[OpenComputers]Simple nanomachines controller + scanner v1.3

May 29th, 2017
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. if not require("component").isAvailable("modem") then
  2.   io.stderr:write("This program requires a wireless network card!")
  3.   os.exit(1)
  4. end
  5.  
  6. local modem = require("component").modem
  7. local event = require("event")
  8. local term = require("term")
  9. local ser = require("serialization")
  10. local shell = require("shell")
  11.  
  12. local INP_PATH = "/etc/nano.cfg"
  13. local AINP_PATH = "/etc/nano_state.cfg"
  14. local MAXINPUTS = 10
  15. local PORT = 443
  16.  
  17.  
  18. local inputs = {}
  19. local activeInputs = {}
  20.  
  21. if not modem.isWireless() then
  22.   io.stderr:write("Your network card isn't wireless!")
  23.   os.exit(1)
  24. end
  25.  
  26. if not modem.isOpen(PORT) then
  27.   modem.open(PORT)
  28. end
  29.  
  30. function send(command, ...)
  31.   modem.broadcast(PORT, "nanomachines", command, ...)
  32. end
  33.  
  34. local _, ops = shell.parse(...)
  35.  
  36. if ops.h then
  37.   io.write("-h shows this and -c rescan your nanomachines")
  38.   os.exit(1)
  39. end
  40.  
  41. send("setResponsePort", PORT)
  42. event.pull("modem_message")
  43.  
  44. -------------------------------------------------------------------------------
  45. message = ""
  46. function handleMessage(tbl)
  47.   local head = tbl[6]
  48.   if head ~= "nanomachines" then return end
  49.   local title = tbl[7]
  50.   local msg1 = tbl[8]
  51.   if title == "effects" then message = msg1
  52.   elseif title == "totalInputCount" then MAXINPUTS = tonumber(msg1)
  53.   end
  54. end
  55.  
  56. send("getTotalInputCount")
  57. handleMessage({event.pull("modem_message")})
  58.  
  59. function tableElementCheck(tbl, element)
  60.   for key, val in pairs(tbl) do
  61.     if val == element then
  62.       return true, key
  63.     end
  64.   end
  65.   return false
  66. end
  67.  
  68. function getEffects()
  69.   io.write("Getting your nanomachines' config.\n")
  70.   for i = 1, MAXINPUTS do
  71.     send("setInput", i, true)
  72.     event.pull("modem_message")
  73.     send("getActiveEffects")
  74.     handleMessage({event.pull("modem_message")})
  75.     send("setInput", i, false)
  76.     event.pull("modem_message")
  77.     io.write("ID: "..i.." = "..message.."\n")
  78.     if message ~= "{}" then
  79.       inputs[i] = message
  80.     end
  81.   end
  82.   local file = io.open(INP_PATH, "w")
  83.   file:write(ser.serialize(inputs))
  84.   file:close()
  85. end
  86.  
  87. function turnEffect(inp)
  88.   if not tableElementCheck(activeInputs, inp) then
  89.     send("setInput", inp, true)
  90.     event.pull("modem_message")
  91.     activeInputs[inp] = inp
  92.   else
  93.     send("setInput", inp, false)
  94.     event.pull("modem_message")
  95.     activeInputs[inp] = nil
  96.   end
  97. end
  98.  
  99. function saveAndQuit()
  100.   if activeInputs == {} and io.open(AINP_PATH, "r") then
  101.     os.execute("rm "..AINP_PATH)
  102.     return
  103.   elseif activeInputs == {} then
  104.     return
  105.   end
  106.   local file = io.open(AINP_PATH, "w")
  107.   file:write(ser.serialize(activeInputs))
  108.   file:close()
  109.   modem.close(PORT)
  110. end
  111.  
  112. function display(inp, ainp)
  113.   term.clear()
  114.   io.write("Inputs: \n")
  115.   for key, val in pairs(inp) do
  116.     io.write("ID: "..key.." = "..val.."\n")
  117.   end
  118.   io.write("Active inputs: ")
  119.   for _, val in pairs(ainp) do
  120.     io.write(val.." ")
  121.   end
  122.   io.write("\n")
  123. end
  124.  
  125. if ops.c or not io.open(INP_PATH, "r") then
  126.   getEffects()
  127. else
  128.   file = io.open(INP_PATH, "r")
  129.   inputs = ser.unserialize(file:read("*a"))
  130.   file:close()
  131.   if io.open(AINP_PATH, "r") then
  132.     file = io.open(AINP_PATH, "r")
  133.     activeInputs = ser.unserialize(file:read("*a"))
  134.     file:close()
  135.   end
  136. end
  137. --------------------------------------------------------------------------------
  138. while true do
  139.   display(inputs, activeInputs)
  140.   io.write("Print 0 to exit\nID: ")
  141.   id = tonumber(term.read())
  142.   if id == 0 then
  143.     saveAndQuit()
  144.     break
  145.   elseif not id then
  146.     io.stderr:write("\nInvalid input")
  147.     os.sleep(1)
  148.   else
  149.     turnEffect(id)
  150.   end
  151. end
  152.  
  153. modem.close(PORT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement