PongWithBreakout

[OpenComputers] Simple Nanomachines Controller v.1.1

Mar 15th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.29 KB | None | 0 0
  1. --//-----------------------------------------------------------------------------------------//-------------------------------
  2. --//--------------------Simple nanomachines controller + nanomachines scaner-----------------//-------------------------------
  3. --//-Now comments translated to bad English, so more people can understand what I'm TYPENING-//-------------------------------
  4. --//-----------------Fixed a bug with max inputs count, now it actually works-------------//-------------------------------
  5. --//-----------------------------------------------------------------------------------------//-------------------------------
  6. local component = require("component")
  7. local modem = component.modem
  8. local event = require("event")
  9. local term = require("term")
  10. local shell = require("shell")
  11. local PORT = 443  -- Port for nanomachines' respondes
  12. local MAXINPUTS = 10  -- No reason to change that number since it's still will be changed in process
  13. local PATH = "/usr/nano.cfg"  -- Configuration file path
  14.  
  15.  
  16. if not modem.isWireless() then  -- Without wireless card you'll get error
  17.   io.stderr:write("Your network card isn't wireless.\nPlease install proper one.")
  18.   return
  19. end
  20. modem.setStrength(2)  -- No reason in this line, except for non-tablet controller...
  21. if not modem.isOpen(PORT) then  -- Checking port forwarding
  22.   modem.open(PORT)  -- And forward it if it doesn't
  23. end
  24. modem.broadcast(1, "nanomachines", "setResponsePort", PORT) -- Set response port(huh)
  25. event.pull("modem_message")
  26.  
  27. local message = ""
  28. local function handleMessage(tcl) -- Thanking XMrVizzy for his code(Nanobots Scanner)
  29.   local head = tcl[6]   -- Okay... Lazy to comment this.
  30.   local title = tcl[7]
  31.   local msg = tcl[8]
  32.   local msg2 = tcl[9]
  33.   if head ~= "nanomachines" then return
  34.   elseif title == "effects" then message = msg
  35.   elseif title == "totalInputCount" then MAXINPUTS = tonumber(msg)
  36.   elseif title == "input" then message = msg2 end
  37. end
  38.  
  39. local function send(command, ...)  -- Shorthand for sending commands
  40.   modem.broadcast(PORT, "nanomachines", command, ...)
  41. end
  42.  
  43. local _, ops = shell.parse(...)  -- Shell options. Pretty easy to understand.
  44. if ops.h then
  45.   term.clear()
  46.   io.write("USAGE: nano [-h, -c] \nOPTIONS:\n  -h : Shows this text\n\n  -c : Configurate\nPress ANY Key to Continue")
  47.   term.pull("key_down")
  48.   term.clear()
  49.   return
  50. end
  51.  
  52. send("getTotalInputCount")  -- Getting how many inputs can have one set of bots
  53. handleMessage({event.pull("modem_message")})  
  54.  
  55. if (ops.c or not io.open(PATH, "r")) then  --   Open config for writing if option choosed or program did'n have config anyway
  56.   term.clear()
  57.   io.write("Configurating...\n")
  58.   config = io.open(PATH, "w")  
  59.   for i = 1, MAXINPUTS do
  60.     send("setInput", i, true)
  61.     event.pull("modem_message")
  62.     send("getActiveEffects")
  63.     handleMessage({event.pull("modem_message")})
  64.     send("setInput", i, false)
  65.     event.pull("modem_message")
  66.     io.write(i, message, " ")
  67.     if message ~= "{}" then config:write("ID : "..i.." = "..message.."\n") end
  68.   end
  69.   config:close()  -- Closing file.
  70. end
  71.  
  72. local activeinputs = {}  -- Table for active inputs(huh x 2)
  73.  
  74. function setI()  --  Function-lever for switching inputs. True --> False and vice versa
  75.   local ID = tonumber(id)
  76.   if not ID then    -- Checking for invalid inputs
  77.     return 0
  78.   end
  79.   send("getInput", ID)
  80.   handleMessage({event.pull("modem_message")})
  81.   if message == true then
  82.     send("setInput", ID, false)
  83.     handleMessage({event.pull("modem_message")})
  84.     activeinputs[ID] = nil
  85.   elseif message == false then
  86.     send("setInput", ID, true)
  87.     handleMessage({event.pull("modem_message")})
  88.     activeinputs[ID] = ID
  89.   end
  90. end
  91.    
  92. function text()  --For printing all information
  93.   local config = io.open(PATH, "r")
  94.   for lines in config:lines() do
  95.     io.write(lines.."\n")
  96.   end
  97.   config:close()
  98.   io.write("Activated: ")
  99.   for _, val in pairs(activeinputs) do
  100.     io.write(val.." ")
  101.   end
  102. end
  103.  
  104. term.clear()
  105. while true do   -- Main Cycle
  106.   text()
  107.   io.write("\nTo quit enter 0\n")
  108.   io.write("ID: ")
  109.   id = term.read()
  110.   if tonumber(id) == 0 then   -- Exit point
  111.     term.clear()
  112.     break
  113.   end
  114.   check = setI()
  115.   if check == 0 then
  116.     io.write("Invalid input")
  117.     os.sleep(1)
  118.   end
  119.   term.clear()
  120. end
  121. modem.close(PORT)  --Closing port before exiting
Advertisement
Add Comment
Please, Sign In to add comment