Advertisement
Sv443

ComputerCraft Wireless Redstone Receiver

Apr 3rd, 2025
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | Gaming | 0 0
  1. -- Transmit interval in seconds, must be in an increment of 0.1 (1 redstone tick)
  2. INTERVAL = 0.1
  3.  
  4. -- Port to receive on
  5. RECEIVE_PORT = 1820
  6.  
  7.  
  8.  
  9.  
  10. -- do not edit below this line or cobuder gets angry >:(
  11.  
  12.  
  13. local modem = peripheral.find("modem") or error("\n> No modem attached!\n", 0)
  14. modem.open(RECEIVE_PORT)
  15.  
  16. local lastSigStrength = nil
  17. local lastSigTime = nil
  18.  
  19. function run()
  20.     local event, side, channel, replyChannel, sigStrength, distance
  21.     repeat
  22.         event, side, channel, replyChannel, sigStrength, distance = os.pullEvent("modem_message")
  23.     until channel == RECEIVE_PORT
  24.  
  25.     if sigStrength ~= lastSigStrength then
  26.         local timeDelta = nil
  27.         if lastSigStrength ~= nil and lastSigTime ~= nil then
  28.             timeDelta = "(after " .. fmtSeconds(os.clock() - lastSigTime) .. ")"
  29.         end
  30.         local sigDiff = ""
  31.         if lastSigStrength ~= nil then
  32.             sigDiff = " (" .. (sigStrength > lastSigStrength and "+" or "-") .. math.abs(sigStrength - lastSigStrength) .. ")"
  33.         end
  34.  
  35.         if timeDelta ~= nil then
  36.             print("  " .. timeDelta .. "\n")
  37.         end
  38.         print("* " .. (lastSigStrength == nil and "Initial" or "New") .. " signal: " .. sigStrength .. sigDiff)
  39.  
  40.         lastSigStrength = sigStrength
  41.         lastSigTime = os.clock()
  42.     end
  43. end
  44.  
  45. function trimString(str)
  46.     return str:gsub("^%s*(.-)%s*$", "%1")
  47. end
  48.  
  49. function fmtSeconds(seconds)
  50.     local hours = math.floor(seconds / 3600)
  51.     local minutes = math.floor((seconds % 3600) / 60)
  52.     local seconds = seconds % 60
  53.  
  54.     local ret = ""
  55.     if hours > 0 then
  56.         ret = string.format("%2dh %2dm %2ds", hours, minutes, seconds)
  57.     elseif minutes > 0 then
  58.         ret = string.format("%2dm %2ds", minutes, seconds)
  59.     else
  60.         ret = string.format("%2ds", seconds)
  61.     end
  62.     return trimString(ret)
  63. end
  64.  
  65. function b64dec(data)
  66.     local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  67.     data = string.gsub(data, "[^"..b.."=]", "")
  68.  
  69.     return (data:gsub(".", function(x)
  70.         if x == "=" then return "" end
  71.         local r, f = "", (b:find(x) - 1)
  72.         for i = 6, 1, -1 do
  73.             r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0")
  74.         end
  75.         return r
  76.     end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x)
  77.         if #x ~= 8 then return "" end
  78.         local c = 0
  79.         for i = 1, 8 do
  80.             c = c + (x:sub(i, i) == "1" and 2 ^ (8 - i) or 0)
  81.         end
  82.         return string.char(c)
  83.     end))
  84. end
  85.  
  86. -- cause pastebin hates links
  87. print(b64dec("CnwgUlhfUlMgYnkgU3Y0NDMKfCBzdjQ0My5uZXQvci9jYy1wcm9qZWN0cwo="))
  88.  
  89. while true do
  90.     run()
  91.     sleep(INTERVAL)
  92. end
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement