Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Redstone Gates API by Islanderon
- -- Note: Using any wireless redstone functions requires a rednet modem and will open any rednet modem on your computer automaticlly.
- -- v1.0.0
- local function openRednet()
- local listOfSides = rs.getSides()
- local listofPossibles = {}
- local counter1 = 0
- while true do
- counter1 = counter1 +1
- if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
- table.insert(listofPossibles,tostring(listOfSides[counter1]))
- end
- if counter1 == 6 and table.maxn(listofPossibles) == 0 then
- print("no wifi present")
- return nil
- end
- if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
- rednet.open(listofPossibles[1])
- return listofPossibles[1]
- end
- end
- end
- local function find(str, match, startIndex) --Find function by tommas (StrUtils API)
- if not match then return nil end
- str = tostring(str)
- local _ = startIndex or 1
- local _s = nil
- local _e = nil
- local _len = match:len()
- while true do
- local _t = str:sub( _ , _len + _ - 1)
- if _t == match then
- _s = _
- _e = _ + _len - 1
- break
- end
- _ = _ + 1
- if _ > str:len() then break end
- end
- if _s == nil then return nil else return _s, _e end
- end
- local function seperate(str, divider) -- Seperate function by tommas (StrUtils API)
- if not divider then return nil end
- str = tostring(str)
- local start = {}
- local endS = {}
- local n=1
- repeat
- if n==1 then
- start[n], endS[n] = find(str, divider)
- else
- start[n], endS[n] = find(str, divider, endS[n-1]+1)
- end
- n=n+1
- until start[n-1]==nil
- local subs = {}
- for n=1, #start+1 do
- if n==1 then
- subs[n] = str:sub(1, start[n]-1)
- elseif n==#start+1 then
- subs[n] = str:sub(endS[n-1]+1)
- else
- subs[n] = str:sub(endS[n-1]+1, start[n]-1)
- end
- end
- return subs
- end
- local function toboolean(input)
- if input == "true" then
- return true
- else
- return false
- end
- end
- local function fromboolean(input)
- if input == true then
- return "true"
- else
- return "false"
- end
- end
- function recState(freq, set, timeout)
- openRednet()
- if timeout == nil then
- id, msg = rednet.receive()
- else
- id, msg = rednet.receive(tonumber(timeout))
- end
- parseddata = seperate(msg, " : ")
- if parseddata[1] == "RSW" and parseddata[2] == freq then
- finald = { }
- finald[1] = parseddata[3]
- finald[2] = toboolean(parseddata[4])
- if set == true then
- redstone.setOutput(finald[1], finald[2])
- return finald
- else
- return finald
- end
- end
- end
- function pulser(side, interval, amount)
- if side == "left" or side == "right" or side == "front" or side == "back" or side == "top" or side == "bottom" then
- if amount == nil then
- while true do
- redstone.setOutput(side, true)
- sleep(interval)
- redstone.setOutput(side, false)
- sleep(interval)
- end
- else
- while amount > 0 do
- redstone.setOutput(side, true)
- sleep(interval)
- redstone.setOutput(side, false)
- amount = amount - 1
- sleep(interval)
- end
- end
- else
- return "Invalid side"
- end
- end
- --[[
- Note: function currently bugged. To be fixed in a later version.
- function notGate(side1, side2)
- while true do
- os.pullEvent("redstone")
- if redstone.getInput(side1) == true then
- redstone.setOutput(side2, false)
- else
- redstone.setOutput(side2, true)
- end
- end
- end
- ]]--
- function sendState(freq, side, state)
- openRednet()
- state = fromboolean(state)
- rednet.broadcast("RSW : "..freq.." : "..side.." : "..state)
- end
- function orGate(side1, side2, output)
- while true do
- os.pullEvent("redstone")
- if redstone.getInput(side1) == true or redstone.getInput(side2) == true then
- redstone.setOutput(output, true)
- else
- redstone.setOutput(output, false)
- end
- end
- end
- function spinner(interval, sleeptime)
- local currentside = "front"
- while true do
- redstone.setOutput(currentside, true)
- sleep(sleeptime)
- redstone.setOutput(currentside, false)
- if currentside == "front" then
- currentside = "right"
- elseif currentside == "left" then
- currentside = "back"
- elseif currentside == "back" then
- currentside = "left"
- elseif currentside == "left" then
- currentside = "front"
- end
- sleep(interval)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment