Skip to content

Instantly share code, notes, and snippets.

@Inari-Whitebear
Last active January 13, 2016 15:36
Show Gist options
  • Save Inari-Whitebear/50450aff3f782d630dbc to your computer and use it in GitHub Desktop.
Save Inari-Whitebear/50450aff3f782d630dbc to your computer and use it in GitHub Desktop.
Automated Nanomachines Effects List
local component = require("component")
local term = require("term")
local event = require("event")
local os = require("os")
local shell = require("shell")
local computer = require("computer")
term.setCursorBlink(false)
local modem = component.modem
if not modem then
error("Modem required")
end
local params, opts = shell.parse(...)
local port = 1
if opts.port then
port = tonumber(opts.port) or port
end
local totalInputCount = nil
local wX, wY, wS, wC, wW = 0, 0, 0, {"▖", "▘", "▝", "▗"}, 0.1
local function drawWait()
term.setCursor(wX, wY)
term.write(wC[wS + 1])
end
local function stepWait()
wS = (wS + 1) % #wC
end
local function send(e, ...)
modem.broadcast(port, "nanomachines", e, ...)
end
local timer
local function getData(e, r, ...)
send(e, ...)
wX, wY = term.getCursor()
drawWait()
timer = event.timer(wW, function() stepWait() drawWait() end, math.huge)
local data = {event.pull("nanodata_" .. r)}
event.cancel(timer)
table.remove(data, 1)
return table.unpack(data)
end
local processCmds = {}
processCmds.totalInputCount = true
processCmds.input = true
processCmds.effects = true
local function modemMsg(ev, receiver, sender, receivedPort, distance, ...)
local data = {...}
if ev == "modem_message" then
if receivedPort == port then
local proto = table.remove(data, 1)
if proto == "nanomachines" then
local dataCmd = table.remove(data, 1)
if processCmds[dataCmd] then
computer.pushSignal("nanodata_" .. dataCmd, table.unpack(data))
end
end
end
end
end
event.listen("modem_message", modemMsg)
print("Using port " .. port .. "...")
modem.open(port)
modem.broadcast(1, "nanomachines", "setResponsePort", port)
os.sleep(3)
local effects = {}
local function drawEffectInfo(i)
local effect = effects[i]
term.setCursor(1, i)
term.clearLine()
term.write("#" .. i .. " ")
if effect then
local s = ""
local first = true
for k, v in pairs(effect) do
if first then
first = false
else
s = s .. ", "
end
s = s .. v
end
term.write(s)
else
term.write(" - TBD - ")
end
end
local function drawEffectsInfo()
term.clear()
for i = 1, totalInputCount do
drawEffectInfo(i)
end
local cX, cY = term.getCursor()
term.setCursor(cX, cY + 1)
term.write("Press CTRL-ALT-C to kill program")
end
local function tryEffect(index)
drawEffectInfo(index)
term.write(" testing... ")
getData("setInput", "input", index, true)
term.setCursor(wX, wY)
effects[index] = {getData("getActiveEffects", "effects")}
term.setCursor(wX, wY)
getData("setInput", "input", index, false)
end
local function tryEffects()
if not totalInputCount then
totalInputCount = getData("getTotalInputCount", "totalInputCount")
end
for i = 1, totalInputCount do
tryEffect(i)
drawEffectInfo(i)
os.sleep(0.5)
end
end
local suc, res = pcall(function()
os.sleep(0.5)
term.write("Finding total input count... ")
totalInputCount = getData("getTotalInputCount", "totalInputCount")
term.write("\n")
term.write("Setting up inputs... ")
for i = 1, totalInputCount do
getData("setInput", "input", i, false)
term.setCursor(wX, wY)
term.write("." .. wC[wS + 1])
term.setCursor(wX + 1, wY)
end
end)
if suc then
drawEffectsInfo()
local suc, res = pcall(tryEffects)
if not suc then
if res ~= "interrupted" then
print("Error: " .. res)
end
end
end
event.ignore("modem_message", modemMsg)
event.cancel(timer)
event.pull("key_up")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment