Advertisement
Espen

Redstone-Event Extenstion API

Mar 21st, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local tRedstoneState = {}  -- Holds the state of all redstone sides on the computer.
  2. local tCableState = {}  -- Holds the state of all bundled cables on the computer.
  3.  
  4. -- Get initial redstone state for all 6 sides.
  5. for _, side in pairs( rs.getSides() ) do
  6.   tRedstoneState[ side ] = rs.getInput( side )
  7. end
  8.  
  9. -- Get initial bundled cable state for all 6 sides.
  10. for _, v in pairs( rs.getSides() ) do
  11.   tCableState[ v ] = rs.getBundledInput( v )
  12. end
  13.  
  14. -- If a redstone side changes its state, then this function returns the event 'redstone' plus the string 'vanilla' and a table.
  15. -- The returned table contains only sides that have changed and it has this format: { side = new_value }
  16. -- If a bundled cable changes its state, then this function returns the event 'redstone' plus the string 'bundled_cable' and a table.
  17. -- The returned table contains only sides that have changed and it has this format: { side = { new_value, old_value } }
  18. function pullEvent( _sEvent, _tSide )
  19.   local bRedstoneChanged = false
  20.   local bCableChanged = false
  21.   local tChangedCable = {}
  22.   local tChangedRedstone = {}
  23.   local event, p1, p2, p3, p4, p5 = os.pullEvent( _sEventFilter )
  24.  
  25.   local function checkRedstoneSides( _tSide )
  26.     -- Iterate over all _tSide sides of the computer.
  27.     -- If _tSide is not given, iterate over all 6 sides.
  28.     for _, side in pairs( _tSide or rs.getSides() ) do
  29.       local bCurrentInput = rs.getInput( side )  -- Get redstone input for current side.
  30.      
  31.       if bCurrentInput ~= tRedstoneState[ side ] then  -- Has the state changed?
  32.         tChangedRedstone[ side ] = bCurrentInput  -- Add the changed side + its new value to the return table.
  33.         tRedstoneState[ side ] = bCurrentInput  -- Update the local state table.
  34.         bRedstoneChanged = true
  35.       end
  36.     end
  37.     return bRedstoneChanged, tChangedRedstone
  38.   end
  39.  
  40.   local function checkBundledCableSides( _tSide )
  41.     -- Iterate over all _tSide sides of the computer.
  42.     -- If _tSide is not given, iterate over all 6 sides.
  43.     for _, side in pairs( _tSide or rs.getSides() ) do
  44.       local nCableInput = rs.getBundledInput( side )  -- Get cable input for current side.
  45.      
  46.       if nCableInput ~= tCableState[ side ] then  -- Has the state changed?
  47.         tChangedCable[ side ] = { nCableInput, tCableState[ side ] }  -- Add the changed side + its new and old values to the return table.
  48.         tCableState[ side ] = nCableInput  -- Update the local state table.
  49.         bCableChanged = true
  50.       end
  51.     end
  52.     return bCableChanged, tChangedCable
  53.   end
  54.  
  55.   if event == "redstone" then
  56.     local bChanged, tSides = checkRedstoneSides( _tSide )
  57.     if bChanged then return event, "vanilla", tSides, p2, p3, p4, p5 end
  58.    
  59.     local bChanged, tSides = checkBundledCableSides( _tSide )
  60.     if bChanged then return event, "bundled_cable", tSides, p2, p3, p4, p5 end
  61.   end
  62.  
  63.   return event, p1, p2, p3, p4, p5
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement