Jyzarc

ModemRS 2.2

Dec 20th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local modemSides = {"top"}
  2. local rsSides = {"front"}
  3. local channels = {4}
  4. local computerType = "output"
  5. local analog = true
  6. local logic = {true,1,0}
  7. local invert = false
  8. local showConsole = false
  9.  
  10. local version = "2.2.0"
  11. local i
  12. local l
  13. local _
  14. local temp
  15. local message
  16. local isError
  17. local consoleMessage
  18. local rsState
  19. local lastState = false
  20. local stateStack = 0
  21. local analogNumber
  22. local lastAnalog = 0
  23. local analogStack = 0
  24. local bundledStates = {}
  25. local bundledString
  26. local lastBundled = {}
  27. local bundledStack = {}
  28. local bundledNumber
  29. local modems = {}
  30. local bundledValues = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
  31. local rsData = {state,analog,bundled}
  32.  
  33. local function printToConsole(consoleMessage,isError)
  34.     if showConsole then
  35.         if not isError
  36.         or not term.getColor() then
  37.             print(consoleMessage)
  38.         else
  39.             term.setTextColor(colors.red)
  40.             print(consoleMessage)
  41.             term.setTextColor(colors.white)
  42.         end
  43.     end
  44. end
  45.            
  46.  
  47. local function startup()
  48.     term.clear()
  49.     term.setCursorPos(1,1)
  50.     print("Running ModemRS version "..version.." by Jyzarc27")
  51.         printToConsole("Checking modems",false)
  52.     for i = 1,#modemSides do
  53.         if peripheral.getType(modemSides[i]) == "modem" then
  54.             modems[#modems+1] = peripheral.wrap(modemSides[i])
  55.         else
  56.             printToConsole("Modem on "..modemSides[i].." is missing; skipped",true)
  57.         end
  58.     end
  59.     for i = 1,#modems do
  60.         for l = 1,#channels do
  61.             modems[i].open(channels[l])
  62.         end
  63.     end
  64.     for i = 1,16 do
  65.         lastBundled[i] = false
  66.         bundledStack[i] = 0
  67.     end
  68. end
  69.  
  70. local function getState()
  71.     rsState = false
  72.     for i = 1,#rsSides do
  73.         rsState = rsState or rs.getInput(rsSides[i])
  74.     end
  75.     temp = rsState
  76.     if rsState == lastState then
  77.         rsState = 0
  78.     else
  79.         if rsState then
  80.             if not invert then
  81.                 rsState = 1
  82.             else
  83.                 rsState = -1
  84.             end
  85.         else
  86.             if not invert then
  87.                 rsState = -1
  88.             else
  89.                 rsState = 1
  90.             end
  91.         end
  92.     end
  93.     lastState = temp
  94.     return rsState
  95. end
  96.  
  97. local function setState(rsState)
  98.     stateStack = stateStack + rsState
  99.     for i = 1, #rsSides do
  100.         if stateStack < 0 then
  101.             stateStack = 0
  102.         elseif stateStack > 0 then
  103.             rs.setOutput(rsSides[i],true)
  104.         else
  105.             rs.setOutput(rsSides[i],false)
  106.         end
  107.     end
  108. end
  109.  
  110. local function getAnalog()
  111.     analogNumber = 0
  112.     for i = 1,#rsSides do
  113.         analogNumber = analogNumber + rs.getAnalogInput(rsSides[i])
  114.     end
  115.     temp = analogNumber
  116.     analogNumber = analogNumber - lastAnalog
  117.     lastAnalog = temp
  118.     return analogNumber
  119. end
  120.  
  121. local function setAnalog(analogNumber)
  122.     analogStack = analogStack + analogNumber
  123.     if analogStack < 0 then
  124.         analogStack = 0
  125.     end
  126.     for i = 1,#rsSides do
  127.         if analogStack > 15 then
  128.             rs.setAnalogOutput(rsSides[i],15)
  129.         else
  130.             rs.setAnalogOutput(rsSides[i],analogStack)
  131.         end
  132.     end
  133. end
  134.  
  135. local function getBundled()
  136.     for i = 1,16 do
  137.         bundledStates[i] = false
  138.     end
  139.     for i = 1,16 do
  140.         for l = 1,#rsSides do
  141.             bundledStates[i] = bundledStates[i] or rs.testBundledInput(rsSides[l],bundledValues[i])
  142.         end
  143.         temp = bundledStates[i]
  144.         if bundledStates[i] == lastBundled[i] then
  145.             bundledStates[i] = 0
  146.         else
  147.             if bundledStates[i] then
  148.                 bundledStates[i] = 1
  149.             else
  150.                 bundledStates[i] = -1
  151.             end
  152.         end
  153.         lastBundled[i] = temp
  154.     end
  155.     bundledString = textutils.serialize(bundledStates)
  156.     return bundledString
  157. end
  158.  
  159. local function setBundled(bundledString)
  160.     bundledNumber = 0
  161.     bundledStates = textutils.unserialize(bundledString)
  162.     for i = 1,16 do
  163.         bundledStack[i] = bundledStack[i] + bundledStates[i]
  164.         if bundledStack[i] < 0 then
  165.             bundledStack[i] = 0
  166.         elseif bundledStack[i] > 0 then
  167.             bundledNumber = bundledNumber + bundledValues[i]
  168.         end
  169.     end
  170.     for i = 1,#rsSides do
  171.         rs.setBundledOutput(rsSides[i],bundledNumber)
  172.     end
  173. end
  174.  
  175. startup()
  176.  
  177. if computerType == "input" then
  178.     while true do
  179.         rsData.state = getState()
  180.         rsData.analog = getAnalog()
  181.         rsData.bundled = getBundled()
  182.         message = textutils.serialize(rsData)
  183.         for i = 1,#modems do
  184.             for l = 1,#channels do
  185.                 modems[i].transmit(channels[l],channels[1],message)
  186.             end
  187.         end
  188.         os.pullEvent("redstone")
  189.     end
  190. end
  191.  
  192. if computerType == "output" then
  193.     while true do
  194.         _,_,_,_,message = os.pullEvent("modem_message")
  195.         rsData = textutils.unserialize(message)
  196.         if analog then
  197.             setAnalog(rsData.analog)
  198.         else
  199.             setState(rsData.state)
  200.         end
  201.         setBundled(rsData.bundled)
  202.     end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment