Create a gist now

Instantly share code, notes, and snippets.

computercraft redstone receive/transmission server
print(os.getComputerID())
rednet.open("bottom")
redstone.setOutput("front", true)
function getinbounds(varColorInt, varSideInt)
if varSideInt >= 32768 then varSideInt = varSideInt - 32768 if varColorInt=="32768" then return true end end
if varSideInt >= 16384 then varSideInt = varSideInt - 16384 if varColorInt=="16384" then return true end end
if varSideInt >= 8192 then varSideInt = varSideInt - 8192 if varColorInt=="8192" then return true end end
if varSideInt >= 4096 then varSideInt = varSideInt - 4096 if varColorInt=="4096" then return true end end
if varSideInt >= 2048 then varSideInt = varSideInt - 2048 if varColorInt=="2048" then return true end end
if varSideInt >= 1024 then varSideInt = varSideInt - 1024 if varColorInt=="1024" then return true end end
if varSideInt >= 512 then varSideInt = varSideInt - 512 if varColorInt=="512" then return true end end
if varSideInt >= 256 then varSideInt = varSideInt - 256 if varColorInt=="256" then return true end end
if varSideInt >= 128 then varSideInt = varSideInt - 128 if varColorInt=="128" then return true end end
if varSideInt >= 64 then varSideInt = varSideInt - 64 if varColorInt=="64" then return true end end
if varSideInt >= 32 then varSideInt = varSideInt - 32 if varColorInt=="32" then return true end end
if varSideInt >= 16 then varSideInt = varSideInt - 16 if varColorInt=="16" then return true end end
if varSideInt >= 8 then varSideInt = varSideInt - 8 if varColorInt=="8" then return true end end
if varSideInt >= 4 then varSideInt = varSideInt - 4 if varColorInt=="4" then return true end end
if varSideInt >= 2 then varSideInt = varSideInt - 2 if varColorInt=="2" then return true end end
if varSideInt >= 1 then varSideInt = varSideInt - 1 if varColorInt=="1" then return true end end
return false
end
function getstatus(varSide, varColor)
if varSide=="r" then return getinbounds(varColor, redstone.getBundledInput("right")) end
if varSide=="l" then return getinbounds(varColor, redstone.getBundledInput("left")) end
return false
end
-- returns true/false
function resolveinput(rninput)
-- find side
sideVar = string.sub(rninput, 1, 1)
sideVar = string.lower(sideVar)
if sideVar~="r" and sideVar ~="l" then return nil end
-- sideVar is the side (l/r) --
-- find ID of color
colorVar = string.sub(rninput, 2)
if colorVar~="1" and colorVar~="2" and colorVar~="4" and colorVar~="8" and colorVar~="16" and colorVar~="32" and colorVar~="64" and colorVar~="128" and colorVar~="256" and colorVar~="512" and colorVar~="1024" and colorVar~="2048" and colorVar~="4096" and colorVar~="8192" and colorVar~="16384" and colorVar~="32768" then return nil end
-- colorVar is color number
return getstatus(sideVar, colorVar)
end
while true do
id, msg = rednet.receive()
print("received '"..msg.."' from ID: "..id)
if string.lower(msg)=="ping" then
rednet.send(id, "PONG")
else
resolveAns = resolveinput(msg)
if resolveAns then rednet.send(id, "ON")
elseif not resolveAns then rednet.send(id, "OFF")
else rednet.send(id, "INVALID") end
end
end
print(os.getComputerID())
rednet.open("bottom")
redstone.setOutput("front", true)
L0 = false
L1 = false
L2 = false
L3 = false
L4 = false
L5 = false
L6 = false
L7 = false
L8 = false
L9 = false
La = false
Lb = false
Lc = false
Ld = false
Le = false
Lf = false
R0 = false
R1 = false
R2 = false
R3 = false
R4 = false
R5 = false
R6 = false
R7 = false
R8 = false
R9 = false
Ra = false
Rb = false
Rc = false
Rd = false
Re = false
Rf = false
function combineL()
combinedInt = 0
if L0 then combinedInt = combinedInt + 1 end
if L1 then combinedInt = combinedInt + 2 end
if L2 then combinedInt = combinedInt + 4 end
if L3 then combinedInt = combinedInt + 8 end
if L4 then combinedInt = combinedInt + 16 end
if L5 then combinedInt = combinedInt + 32 end
if L6 then combinedInt = combinedInt + 64 end
if L7 then combinedInt = combinedInt + 128 end
if L8 then combinedInt = combinedInt + 256 end
if L9 then combinedInt = combinedInt + 512 end
if La then combinedInt = combinedInt + 1024 end
if Lb then combinedInt = combinedInt + 2048 end
if Lc then combinedInt = combinedInt + 4096 end
if Ld then combinedInt = combinedInt + 8192 end
if Le then combinedInt = combinedInt + 16384 end
if Lf then combinedInt = combinedInt + 32768 end
return combinedInt
end
function combineR()
combinedInt = 0
if R0 then combinedInt = combinedInt + 1 end
if R1 then combinedInt = combinedInt + 2 end
if R2 then combinedInt = combinedInt + 4 end
if R3 then combinedInt = combinedInt + 8 end
if R4 then combinedInt = combinedInt + 16 end
if R5 then combinedInt = combinedInt + 32 end
if R6 then combinedInt = combinedInt + 64 end
if R7 then combinedInt = combinedInt + 128 end
if R8 then combinedInt = combinedInt + 256 end
if R9 then combinedInt = combinedInt + 512 end
if Ra then combinedInt = combinedInt + 1024 end
if Rb then combinedInt = combinedInt + 2048 end
if Rc then combinedInt = combinedInt + 4096 end
if Rd then combinedInt = combinedInt + 8192 end
if Re then combinedInt = combinedInt + 16384 end
if Rf then combinedInt = combinedInt + 32768 end
return combinedInt
end
function setvariable(varoption, varside, varcolor)
if varside=="l" then
if varcolor=="1" then L0 = varoption end
if varcolor=="2" then L1 = varoption end
if varcolor=="4" then L2 = varoption end
if varcolor=="8" then L3 = varoption end
if varcolor=="16" then L4 = varoption end
if varcolor=="32" then L5 = varoption end
if varcolor=="64" then L6 = varoption end
if varcolor=="128" then L7 = varoption end
if varcolor=="256" then L8 = varoption end
if varcolor=="512" then L9 = varoption end
if varcolor=="1024" then La = varoption end
if varcolor=="2048" then Lb = varoption end
if varcolor=="4096" then Lc = varoption end
if varcolor=="8192" then Ld = varoption end
if varcolor=="16384" then Le = varoption end
if varcolor=="32768" then Lf = varoption end
end
if varside=="r" then
if varcolor=="1" then R0 = varoption end
if varcolor=="2" then R1 = varoption end
if varcolor=="4" then R2 = varoption end
if varcolor=="8" then R3 = varoption end
if varcolor=="16" then R4 = varoption end
if varcolor=="32" then R5 = varoption end
if varcolor=="64" then R6 = varoption end
if varcolor=="128" then R7 = varoption end
if varcolor=="256" then R8 = varoption end
if varcolor=="512" then R9 = varoption end
if varcolor=="1024" then Ra = varoption end
if varcolor=="2048" then Rb = varoption end
if varcolor=="4096" then Rc = varoption end
if varcolor=="8192" then Rd = varoption end
if varcolor=="16384" then Re = varoption end
if varcolor=="32768" then Rf = varoption end
end
end
-- returns combined int or nil when wrong input
function resolveinput(rninput)
-- find if option should go on or off --
if string.len(rninput) < 3 then return nil end
optionVar0 = string.sub(rninput, 1, 1)
optionVar = false
if optionVar0=="1" then optionVar = true
elseif optionVar0=="0" then optionVar = false
else return nil end
-- optionVar is if it should be on or off --
-- find side
sideVar = string.sub(rninput, 2, 2)
sideVar = string.lower(sideVar)
if sideVar~="r" and sideVar ~="l" then return nil end
-- sideVar is the side (l/r) --
-- find ID of color
colorVar = string.sub(rninput, 3)
if colorVar~="1" and colorVar~="2" and colorVar~="4" and colorVar~="8" and colorVar~="16" and colorVar~="32" and colorVar~="64" and colorVar~="128" and colorVar~="256" and colorVar~="512" and colorVar~="1024" and colorVar~="2048" and colorVar~="4096" and colorVar~="8192" and colorVar~="16384" and colorVar~="32768" then return nil end
-- colorVar is color number
setvariable(optionVar, sideVar, colorVar)
redstone.setBundledOutput("left", combineL())
redstone.setBundledOutput("right", combineR())
return true
end
while true do
id, msg = rednet.receive()
print("received '"..msg.."' from ID: "..id)
if string.lower(msg)=="ping" then
rednet.send(id, "PONG")
else
resolveAns = resolveinput(msg)
if resolveAns then rednet.send(id, "YES")
else rednet.send(id, "NO") end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment