Advertisement
casillero

Redstoner

May 29th, 2014
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --//Redstoner - Modem Based Redstone Control
  2.  
  3. function wrapOnePeripheral(pType, check)
  4.     local wrap = nil
  5.     local constructor = peripheral.getNames
  6.     if pType == "modem" then
  7.         constructor = redstone.getSides
  8.     end
  9.     for i,v in pairs(constructor()) do
  10.         if peripheral.getType(v) == pType and (check == nil or check(v) == true) then
  11.             wrap = {
  12.                 ["wrap"] = peripheral.wrap(v),
  13.                 ["side"] = v,
  14.                 }
  15.             break
  16.         end
  17.     end
  18.     return wrap
  19. end
  20.  
  21. local modem = wrapOnePeripheral("modem")["wrap"]
  22. local localChannel = nil
  23. local filter = nil
  24. local tArgs = {...}
  25.  
  26. if tArgs[1] == nil or type(tonumber(tArgs[1])) ~= "number" or tonumber(tArgs[1]) < 0 or tonumber(tArgs[1]) > 65535 then
  27.     localChannel = math.random(0, 65535)
  28. else
  29.     localChannel = tonumber(tArgs[1])
  30. end
  31.  
  32. if tArgs[2] ~= nil then
  33.     filter = tArgs[2]
  34. end
  35.  
  36. function unwrapMessage(message, reply)
  37.     message = textutils.unserialize(message)
  38.     if message["filter"] == filter then
  39.         print(message["request"])
  40.         if message["request"] == "set" then
  41.             setOutput(message["side"], message["value"])
  42.             print("Side: "..message["side"])
  43.             print("State: "..tostring(message["value"]))
  44.         elseif message["request"] == "output" then
  45.             getOutput(message["side"], message["type"], reply)
  46.         elseif message["request"] == "input" then
  47.             getInput(message["side"], message["type"], reply)
  48.         end
  49.     end
  50. end
  51.  
  52. function setOutput(side, value)
  53.     local method = nil
  54.     if type(value) == "boolean" then       
  55.         redstone.setOutput(side, value)
  56.     elseif type(value) == "number" then
  57.         redstone.setBundledOutput(side, value)
  58.     else
  59.         print("Invalid redstone request attempted")
  60.     end
  61. end
  62.  
  63. function getOutput(side, methodType, reply)
  64.     local value = nil
  65.     if methodType == "bundled" then
  66.         value = redstone.getBundledOutput(side)
  67.     else
  68.         value = redstone.getOutput(side)
  69.     end
  70.     local t = {
  71.         ["redstoneReturn"] = true,
  72.         ["reply"] = value,
  73.     }
  74.     t = textutils.serialize(t)
  75.     modem.transmit(reply, localChannel, t)
  76. end
  77.  
  78. function getInput(side, methodType, reply)
  79.     local value = nil
  80.     if methodType == "bundled" then
  81.         value = redstone.getBundledInput(side)
  82.     else
  83.         value = redstone.getInput(side)
  84.     end
  85.     local t = {
  86.         ["redstoneReturn"] = true,
  87.         ["reply"] = value,
  88.     }
  89.     t = textutils.serialize(t)
  90.     modem.transmit(reply, localChannel, t)
  91. end
  92.  
  93. term.clear()
  94. term.setCursorPos(1,1)
  95.  
  96. print("Local Channel: "..tostring(localChannel))
  97. print("Filter: "..tostring(filter))
  98.  
  99. if modem ~= nil then
  100.     modem.open(localChannel)
  101.     while true do
  102.         local event = {os.pullEvent("modem_message")}
  103.         print("Got a message")
  104.         unwrapMessage(event[5], event[4])
  105.     end
  106. else
  107.     print("No modem found")
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement