Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- function printUsage()
- print("Usage: ")
- print("redstone [pulse] <side> <state>")
- print("redstone [pulse] bundle <side1> <color1> [color2] ... [colorN] <state>")
- print("ex: ")
- print("redstone bundle top red blue on")
- print("redstone pulse top")
- print("redstone pulse bundle top white")
- error("Incorrect command")
- end
- function table.search(t, value)
- for i,v in pairs(t) do
- if v == value then
- return i
- end
- end
- return false
- end
- function isSide(s)
- local sides = {"front", "back", "top", "bottom", "left", "right"}
- return table.search(sides, s)
- end
- function all(state)
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for i,v in pairs(sides) do
- rs.setOutput(v, state)
- setBundledOutput(v, 65535, state)
- end
- return true
- end
- function colors.decompose(color, state)
- --Arguments: int color, [bool state]
- --The color is a color-sum like (colors.white + colors.orange)
- -- (which is 3)
- --
- --returns t={white=state, orange=state}
- if type(color) ~= "number" or color < 0 then
- return false
- end
- if color == 0 then
- return {}
- end
- if state == nil then
- state = true
- end
- local colorsName = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
- local a = color
- local i = 1
- local t = {}
- --divide by 2 successively
- --and it works and does stuff because uh
- --uh
- --yeah just look up successive division by 2
- while a > 0 do
- -- if a/2 has a rest then
- if a%2 == 1 then
- t[colorsName[i]] = state
- end
- a = math.floor(a/2)
- i = i+1
- end
- return t
- end
- function table.merge(t1, t2)
- --t2 takes priority
- local tn = {}
- for i,v in pairs(t1) do
- tn[i] = v
- end
- for i,v in pairs(t2) do
- tn[i] = v
- end
- return tn
- end
- --rs.setBundledOutput(side, 0)
- function setBundledOutput(side, composedColor, state)
- --Example: composedColors = colors.white + colors.orange
- -- that's 3. Because 1 + 2 = 3
- if not tonumber(composedColor) then
- error("Expected string, number, bool. Got string, string")
- end
- local newOutputs = colors.decompose(composedColor, state)
- -- newOutputs = {white=state, orange=state}
- local currentOutputs = colors.decompose(rs.getBundledOutput(side))
- --We are currently outputting to the front white cable.
- -- currentOutputs = {white=true}
- local outputs = table.merge(currentOutputs, newOutputs)
- --outputs = {white=state, orange=state}
- local colorValue = 0
- for i, v in pairs(outputs) do
- if v == true then
- colorValue = colorValue + colors[i]
- end
- end
- --if state == true,
- -- colorValue = colors.white + colors.orange
- --else,
- -- colorValue = 0
- rs.setBundledOutput(side, colorValue)
- end
- function stringToBoolean(s)
- if s == "on" or s == "1" or s == "true"
- then
- return true
- end
- if s == "off" or s == "0" or s == "false" then
- return false
- end
- return nil
- end
- function pulse(side, duration, state)
- local sleep = sleep
- if duration == nil then
- sleep = function() end
- end
- rs.setOutput(side, state)
- sleep(duration)
- rs.setOutput(side, not state)
- end
- local action, side, duration, state
- local action = tArgs[1]
- if action == "pulse" then
- duration = 1--tonumber(tArgs[3]) or 0.5
- table.remove(tArgs, 1)
- action = tArgs[1]
- end
- if stringToBoolean(action) == false then
- return all(false)
- elseif stringToBoolean(action) == true then
- return all(true)
- elseif action == "bundle" then
- local cables = {}
- local i,v = 2,nil
- local side = nil
- while i <= #tArgs do
- v = tArgs[i]
- if isSide(v) then
- side = v
- if type(cables[v]) ~= "table" then
- cables[v] = {}
- end
- elseif stringToBoolean(v) ~= nil then
- if cables[side].state then
- printUsage()
- end
- cables[side].state = stringToBoolean(v)
- else
- if not side then
- printUsage()
- end
- cables[side][v] = true
- end
- i=i+1
- end
- for sideN,sideV in pairs(cables) do
- --for each side
- for color,_ in pairs(sideV) do
- --of each color
- if color ~= "state" then
- --print(color .. " cable in " .. sideN .. " is " .. tostring(sideV.state))
- setBundledOutput(sideN, colors[color], sideV.state)
- end
- end
- end
- elseif tArgs[1] ~= nil then --if action == a side
- action = "set"
- side = tArgs[1]
- state = stringToBoolean(tArgs[2])
- if state == nil then
- state = not rs.getOutput(side)
- end
- return pulse(side, duration, state)
- else
- printUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement