Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local INTERFACES_NAMES = {}
- local INTERFACES = {}
- -- config loadable info
- local REDSTONE_TRIGGER_SIDES = {}
- local INTERFACE_PULL_DIRECTION = nil
- function CanonicaliseSide(side)
- if (side == nil or #side < 3 or #side > 6) then
- return nil
- end
- local s = string.lower(side)
- if (s == "left" or s == "top" or s == "right" or s == "bottom" or s == "front" or s == "back") then
- return s
- end
- return nil
- end
- function CanonicaliseNSWE(dir)
- if (dir == nil or #dir < 3 or #dir > 6) then
- return nil
- end
- local s = string.lower(dir)
- if (s == "north" or s == "east" or s == "south" or s == "west" or s == "up" or s == "down") then
- return s
- end
- return nil
- end
- function SplitStringByComma(input_str)
- return string.gmatch(input_str, "([^,]+)")
- end
- function LoadConnectedInterfaces()
- INTERFACES_NAMES = {}
- INTERFACES = {}
- local modem_connections = peripheral.getNames("back")
- if (modem_connections == nil) then
- return
- end
- for k, peripheralName in pairs(modem_connections) do
- if (tostring(peripheralName):find("me_interface") ~= nil) then
- local methodTable = peripheral.wrap(peripheralName)
- if (methodTable ~= nil) then
- table.insert(INTERFACES, methodTable)
- table.insert(INTERFACES_NAMES, peripheralName)
- end
- end
- end
- end
- function LoadConfigInfo()
- if (not fs.exists("config")) then
- return false
- end
- local hFile = fs.open("config", "r")
- if (hFile == nil) then
- print("Failed to open config file")
- return false
- end
- local isFail = false
- REDSTONE_TRIGGER_SIDES = {}
- local rs_trig = hFile.readLine()
- if (rs_trig ~= nil and #rs_trig > 0) then
- for side in SplitStringByComma(rs_trig) do
- local realSide = CanonicaliseSide(side)
- if (realSide ~= nil) then
- table.insert(REDSTONE_TRIGGER_SIDES, realSide)
- else
- print("Invalid side in config: " .. realSide)
- isFail = true
- end
- end
- end
- INTERFACE_PULL_DIRECTION = nil
- local itf_pull_dir_raw = hFile.readLine()
- local itf_pull_dir = CanonicaliseNSWE(itf_pull_dir_raw)
- if (itf_pull_dir ~= nil) then
- INTERFACE_PULL_DIRECTION = itf_pull_dir
- else
- print("Invalid interface pull direction: " .. itf_pull_dir_raw)
- isFail = true
- end
- hFile.close()
- return not isFail and #REDSTONE_TRIGGER_SIDES > 0 and INTERFACE_PULL_DIRECTION ~= nil
- end
- function SaveConfigInfo()
- local hFile = fs.open("config", "w")
- if (hFile == nil) then
- print("Failed to open config file for writing")
- return
- end
- if (REDSTONE_TRIGGER_SIDES ~= nil) then
- local str = table.concat(REDSTONE_TRIGGER_SIDES, ",")
- hFile.writeLine(str)
- print("Writing to config: " .. str)
- else
- hFile.writeLine("")
- end
- if (INTERFACE_PULL_DIRECTION ~= nil) then
- hFile.writeLine(INTERFACE_PULL_DIRECTION)
- print("Writing to config: " .. INTERFACE_PULL_DIRECTION)
- else
- hFile.writeLine("")
- end
- end
- function SetupConfig()
- print("Input the sides on the computer that, when a redstone signal is received, will clear all induction furnaces")
- print("(comma separated list of: top, bottom, left, right, front or back)")
- term.write(">")
- local inputSides = {}
- local sides = read()
- for side in SplitStringByComma(sides) do
- local realSide = CanonicaliseSide(side)
- if (realSide == nil) then
- print("Invalid side: " .. side)
- return false
- end
- table.insert(inputSides, realSide)
- end
- print("Input the world direction of the induction furnaces, relative to the ME Interfaces")
- print("(top, bottom, north, east, south, west)")
- term.write(">")
- local pull_dir_raw = read()
- local pull_dir = CanonicaliseNSWE(pull_dir_raw)
- if (pull_dir == nil) then
- print("Invalid direction: " .. pull_dir_raw)
- return false
- end
- REDSTONE_TRIGGER_SIDES = inputSides
- INTERFACE_PULL_DIRECTION = pull_dir
- return true
- end
- function OnRedstoneInput()
- -- clear interfaces
- print("Clearing " .. #INTERFACES .. " induction furnaces at " .. INTERFACE_PULL_DIRECTION)
- for i, itf in pairs(INTERFACES) do
- itf.pullItem(INTERFACE_PULL_DIRECTION, 1)
- itf.pullItem(INTERFACE_PULL_DIRECTION, 2)
- end
- end
- function Main()
- if (not LoadConfigInfo()) then
- while (not SetupConfig()) do end
- SaveConfigInfo()
- end
- LoadConnectedInterfaces()
- print("Loaded " .. #INTERFACES .. " interfaces via modem connections")
- print("TIP1: To reset the config file, terminate the program and run 'rm config'")
- print("")
- print("TIP2: The ME interface must also be placed on the output of the induction furnaces (red, yellow or orange sides)")
- while true do
- os.pullEvent("redstone")
- local hasInput = false
- for i, side in pairs(REDSTONE_TRIGGER_SIDES) do
- if (rs.getInput(side)) then
- hasInput = true
- break
- end
- end
- if (hasInput) then
- OnRedstoneInput()
- print("[" .. os.clock() .. "] Redstone received!")
- end
- end
- end
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement