Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSides = {"back"}
- local channels = {1}
- local computerType = "input"
- local rs_sides = {"top"}
- local analog = true
- local version = "1.2.0"
- local message
- local analogNumber
- local bundledNumber
- local bundledString
- local rsState
- local hasColor
- local tempAnalog
- local _
- local i
- local x
- local y
- local serializedInfo
- local lastAnalog = 0
- local modems = {}
- local bundledArray = {}
- local rs_info = {}
- local stateStack = 0
- local analogStack = 0
- local lastBool = false
- local tempBool
- local lastBundledStates = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
- local compactBundledStates
- local bundledStates = {}
- local bundledStack = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- local colorValues = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
- local function startup()
- term.clear()
- term.setCursorPos(1,1)
- print("Running ModemRS version "..version.." by Jyzarc27")
- for x = 1,#modemSides do
- modems[x] = peripheral.wrap(modemSides[x])
- for y = 1,#channels do
- modems[x].open(channels[y])
- end
- end
- end
- local function getRS()
- analogNumber = 0
- rsState = false
- for i = 1,#rs_sides do
- analogNumber = analogNumber + rs.getAnalogInput(rs_sides[i])
- rsState = rsState or rs.getInput(rs_sides[i])
- end
- return rsState,analogNumber
- end
- local function getBundledRS()
- for x = 1,16 do
- for y = 1,#rs_sides do
- bundledStates[x] = rs.testBundledInput(rs_sides[y],colorValues[x])
- bundledStates[x+16] = bundledStates[x]
- if bundledStates[x] == lastBundledStates[x] then
- bundledStates[x] = nil
- end
- lastBundledStates[x] = bundledStates[x+16]
- end
- end
- compactedBundledStates = textutils.serialize(bundledStates)
- return compactedBundledStates
- end
- local function setRS(analogNumber,rsState)
- for i = 1,#rs_sides do
- if analog then
- if analogStack > 15 then
- rs.setAnalogOutput(rs_sides[i],15)
- else
- rs.setAnalogOutput(rs_sides[i],analogStack)
- end
- else
- if stateStack > 0 then
- rs.setOutput(rs_sides[i],true)
- else
- rs.setOutput(rs_sides[i],false)
- end
- end
- end
- end
- local function setBundledRS(bundledString)
- bundledStates = textutils.unserialize(bundledString)
- bundledNumber = 0
- for i = 1,16 do
- if bundledStates[i] ~= nil then
- if bundledStates[i] then
- bundledStack[i] = bundledStack[i] + 1
- else
- if bundledStack[i] ~= 0 then
- bundledStack[i] = bundledStack[i] - 1
- end
- end
- end
- if bundledStack[i] > 0 then
- bundledNumber = bundledNumber + colorValues[i]
- end
- end
- for i = 1,#rs_sides do
- rs.setBundledOutput(rs_sides[i],bundledNumber)
- end
- end
- local function stackInfo(rsState,analogNumber)
- if rsState ~= nil then
- if rsState then
- stateStack = stateStack + 1
- else
- if stateStack ~= 0 then
- stateStack = stateStack - 1
- end
- end
- end
- if analogNumber < 0 then
- analogStack = analogStack - math.abs(analogNumber)
- elseif analogNumber == 0 then
- analogStack = analogStack
- else
- analogStack = analogStack + analogNumber
- end
- end
- startup()
- if computerType == "input" then
- while true do
- rs_info[1],rs_info[2] = getRS()
- rs_info[3] = getBundledRS()
- tempAnalog = rs_info[2]
- if lastAnalog == rs_info[2] then
- rs_info[2] = 0
- elseif lastAnalog > rs_info[2] then
- rs_info[2] = 0 - (lastAnalog - rs_info[2])
- else
- rs_info[2] = rs_info[2] - lastAnalog
- end
- lastAnalog = tempAnalog
- tempBool = rs_info[1]
- if lastBool == rs_info[1] then
- rs_info[1] = nil
- end
- serializedInfo = textutils.serialize(rs_info)
- for x = 1,#channels do
- for y = 1,#modems do
- modems[y].transmit(channels[x],channels[1],serializedInfo)
- end
- end
- os.pullEvent("redstone")
- end
- end
- if computerType == "output" then
- while true do
- _,_,_,_,message = os.pullEvent("modem_message")
- rs_info = textutils.unserialize(message)
- stackInfo(rs_info[1],rs_info[2])
- setRS(rs_info[2],rs_info[1])
- setBundledRS(rs_info[3])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment