Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --//-----------------------------------------------------------------------------------------//-------------------------------
- --//--------------------Simple nanomachines controller + nanomachines scaner-----------------//-------------------------------
- --//-Now comments translated to bad English, so more people can understand what I'm TYPENING-//-------------------------------
- --//-----------------Fixed a bug with max inputs count, now it actually works-------------//-------------------------------
- --//-----------------------------------------------------------------------------------------//-------------------------------
- local component = require("component")
- local modem = component.modem
- local event = require("event")
- local term = require("term")
- local shell = require("shell")
- local PORT = 443 -- Port for nanomachines' respondes
- local MAXINPUTS = 10 -- No reason to change that number since it's still will be changed in process
- local PATH = "/usr/nano.cfg" -- Configuration file path
- if not modem.isWireless() then -- Without wireless card you'll get error
- io.stderr:write("Your network card isn't wireless.\nPlease install proper one.")
- return
- end
- modem.setStrength(2) -- No reason in this line, except for non-tablet controller...
- if not modem.isOpen(PORT) then -- Checking port forwarding
- modem.open(PORT) -- And forward it if it doesn't
- end
- modem.broadcast(1, "nanomachines", "setResponsePort", PORT) -- Set response port(huh)
- event.pull("modem_message")
- local message = ""
- local function handleMessage(tcl) -- Thanking XMrVizzy for his code(Nanobots Scanner)
- local head = tcl[6] -- Okay... Lazy to comment this.
- local title = tcl[7]
- local msg = tcl[8]
- local msg2 = tcl[9]
- if head ~= "nanomachines" then return
- elseif title == "effects" then message = msg
- elseif title == "totalInputCount" then MAXINPUTS = tonumber(msg)
- elseif title == "input" then message = msg2 end
- end
- local function send(command, ...) -- Shorthand for sending commands
- modem.broadcast(PORT, "nanomachines", command, ...)
- end
- local _, ops = shell.parse(...) -- Shell options. Pretty easy to understand.
- if ops.h then
- term.clear()
- io.write("USAGE: nano [-h, -c] \nOPTIONS:\n -h : Shows this text\n\n -c : Configurate\nPress ANY Key to Continue")
- term.pull("key_down")
- term.clear()
- return
- end
- send("getTotalInputCount") -- Getting how many inputs can have one set of bots
- handleMessage({event.pull("modem_message")})
- if (ops.c or not io.open(PATH, "r")) then -- Open config for writing if option choosed or program did'n have config anyway
- term.clear()
- io.write("Configurating...\n")
- config = io.open(PATH, "w")
- for i = 1, MAXINPUTS do
- send("setInput", i, true)
- event.pull("modem_message")
- send("getActiveEffects")
- handleMessage({event.pull("modem_message")})
- send("setInput", i, false)
- event.pull("modem_message")
- io.write(i, message, " ")
- if message ~= "{}" then config:write("ID : "..i.." = "..message.."\n") end
- end
- config:close() -- Closing file.
- end
- local activeinputs = {} -- Table for active inputs(huh x 2)
- function setI() -- Function-lever for switching inputs. True --> False and vice versa
- local ID = tonumber(id)
- if not ID then -- Checking for invalid inputs
- return 0
- end
- send("getInput", ID)
- handleMessage({event.pull("modem_message")})
- if message == true then
- send("setInput", ID, false)
- handleMessage({event.pull("modem_message")})
- activeinputs[ID] = nil
- elseif message == false then
- send("setInput", ID, true)
- handleMessage({event.pull("modem_message")})
- activeinputs[ID] = ID
- end
- end
- function text() --For printing all information
- local config = io.open(PATH, "r")
- for lines in config:lines() do
- io.write(lines.."\n")
- end
- config:close()
- io.write("Activated: ")
- for _, val in pairs(activeinputs) do
- io.write(val.." ")
- end
- end
- term.clear()
- while true do -- Main Cycle
- text()
- io.write("\nTo quit enter 0\n")
- io.write("ID: ")
- id = term.read()
- if tonumber(id) == 0 then -- Exit point
- term.clear()
- break
- end
- check = setI()
- if check == 0 then
- io.write("Invalid input")
- os.sleep(1)
- end
- term.clear()
- end
- modem.close(PORT) --Closing port before exiting
Advertisement
Add Comment
Please, Sign In to add comment