Jyzarc

Untitled

Dec 14th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. local modemSides = {"back"}
  2. local channels = {1}
  3. local computerType = "input"
  4. local rs_sides = {"top"}
  5. local analog = true
  6.  
  7. local version = "1.2.0"
  8. local message
  9. local analogNumber
  10. local bundledNumber
  11. local bundledString
  12. local rsState
  13. local hasColor
  14. local tempAnalog
  15. local _
  16. local i
  17. local x
  18. local y
  19. local serializedInfo
  20. local lastAnalog = 0
  21. local modems = {}
  22. local bundledArray = {}
  23. local rs_info = {}
  24. local stateStack = 0
  25. local analogStack = 0
  26. local lastBool = false
  27. local tempBool
  28. local lastBundledStates = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
  29. local compactBundledStates
  30. local bundledStates = {}
  31. local bundledStack = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  32. local colorValues = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
  33.  
  34. local function startup()
  35.     term.clear()
  36.     term.setCursorPos(1,1)
  37.     print("Running ModemRS version "..version.." by Jyzarc27")
  38.     for x = 1,#modemSides do
  39.         modems[x] = peripheral.wrap(modemSides[x])
  40.         for y = 1,#channels do
  41.         modems[x].open(channels[y])
  42.         end
  43.     end
  44. end
  45.  
  46. local function getRS()
  47.     analogNumber = 0
  48.     rsState = false
  49.     for i = 1,#rs_sides do
  50.         analogNumber = analogNumber + rs.getAnalogInput(rs_sides[i])
  51.         rsState = rsState or rs.getInput(rs_sides[i])
  52.     end
  53.     return rsState,analogNumber
  54. end
  55.  
  56. local function getBundledRS()
  57.     for x = 1,16 do
  58.         for y = 1,#rs_sides do
  59.             bundledStates[x] = rs.testBundledInput(rs_sides[y],colorValues[x])
  60.             bundledStates[x+16] = bundledStates[x]
  61.             if bundledStates[x] == lastBundledStates[x] then
  62.                 bundledStates[x] = nil
  63.             end
  64.             lastBundledStates[x] = bundledStates[x+16]
  65.         end
  66.     end
  67.     compactedBundledStates = textutils.serialize(bundledStates)
  68.     return compactedBundledStates
  69. end
  70.  
  71. local function setRS(analogNumber,rsState)
  72.     for i = 1,#rs_sides do
  73.         if analog then
  74.             if analogStack > 15 then
  75.                 rs.setAnalogOutput(rs_sides[i],15)
  76.             else
  77.                 rs.setAnalogOutput(rs_sides[i],analogStack)
  78.             end
  79.         else
  80.             if stateStack > 0 then
  81.                 rs.setOutput(rs_sides[i],true)
  82.             else
  83.                 rs.setOutput(rs_sides[i],false)
  84.             end
  85.         end
  86.     end
  87. end
  88.  
  89. local function setBundledRS(bundledString)
  90.     bundledStates = textutils.unserialize(bundledString)
  91.     bundledNumber = 0
  92.     for i = 1,16 do
  93.         if bundledStates[i] ~= nil then
  94.             if bundledStates[i] then
  95.                 bundledStack[i] = bundledStack[i] + 1
  96.             else
  97.                 if bundledStack[i] ~= 0 then
  98.                     bundledStack[i] = bundledStack[i] - 1
  99.                 end
  100.             end
  101.         end
  102.         if bundledStack[i] > 0 then
  103.             bundledNumber = bundledNumber + colorValues[i]
  104.         end
  105.     end
  106.     for i = 1,#rs_sides do
  107.         rs.setBundledOutput(rs_sides[i],bundledNumber)
  108.     end
  109. end
  110.  
  111. local function stackInfo(rsState,analogNumber)
  112.     if rsState ~= nil then
  113.         if rsState then
  114.             stateStack = stateStack + 1
  115.         else
  116.             if stateStack ~= 0 then
  117.                 stateStack = stateStack - 1
  118.             end
  119.         end
  120.     end
  121.     if analogNumber < 0 then
  122.         analogStack = analogStack - math.abs(analogNumber)
  123.     elseif analogNumber == 0 then
  124.         analogStack = analogStack
  125.     else
  126.         analogStack = analogStack + analogNumber
  127.     end
  128. end
  129.  
  130. startup()
  131.  
  132. if computerType == "input" then
  133.     while true do
  134.         rs_info[1],rs_info[2] = getRS()
  135.         rs_info[3] = getBundledRS()
  136.         tempAnalog = rs_info[2]
  137.         if lastAnalog == rs_info[2] then
  138.             rs_info[2] = 0
  139.         elseif lastAnalog > rs_info[2] then
  140.             rs_info[2] = 0 - (lastAnalog - rs_info[2])
  141.         else
  142.             rs_info[2] = rs_info[2] - lastAnalog
  143.         end
  144.         lastAnalog = tempAnalog
  145.         tempBool = rs_info[1]
  146.         if lastBool == rs_info[1] then
  147.             rs_info[1] = nil
  148.         end
  149.         serializedInfo = textutils.serialize(rs_info)
  150.         for x = 1,#channels do
  151.             for y = 1,#modems do
  152.                 modems[y].transmit(channels[x],channels[1],serializedInfo)
  153.             end
  154.         end
  155.         os.pullEvent("redstone")
  156.     end
  157. end
  158.  
  159. if computerType == "output" then
  160.     while true do
  161.         _,_,_,_,message = os.pullEvent("modem_message")
  162.         rs_info = textutils.unserialize(message)
  163.         stackInfo(rs_info[1],rs_info[2])
  164.         setRS(rs_info[2],rs_info[1])
  165.         setBundledRS(rs_info[3])
  166.     end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment