Advertisement
osmarks

lightning comms

Jun 3rd, 2024 (edited)
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. local sensor = peripheral.find "manipulator"
  2.  
  3. local function bitpack(a, b, c, d)
  4.     return bit.bor(bit.blshift(a, 6), bit.blshift(b, 4), bit.blshift(c, 2), d)
  5. end
  6.  
  7. local function bitunpack(x)
  8.     return bit.brshift(bit.band(x, 192), 6), bit.brshift(bit.band(x, 48), 4), bit.band(bit.brshift(x, 12), 2), bit.band(x, 3)
  9. end
  10.  
  11. local function has_struck()
  12.     for _, x in pairs(sensor.sense()) do
  13.         if x.displayName == "item.tile.wood.oak" then return false end
  14.     end
  15.     return true
  16. end
  17.  
  18. local function ticks()
  19.     return os.time() * 1000
  20. end
  21.  
  22. local function syncto(interval)
  23.     local t_now = ticks()
  24.     local x = interval - t_now % interval
  25.     if x > 0 then sleep(x * 0.05) end
  26. end
  27.  
  28. local function instantiate_creeper()
  29.     commands.execAsync [[kill @e[type=item,r=3] ]]
  30.     commands.execAsync [[summon item ~ ~1 ~ {Item:{id:"minecraft:planks",Count:1}}]]
  31.     sleep()
  32. end
  33.  
  34. local function cause_lightning()
  35.     for i = 1, 10 do commands.execAsync "summon lightning_bolt 27387 50 -86351" end
  36. end
  37.  
  38. local function recv()
  39.     syncto(20)
  40.     instantiate_creeper()
  41.     -- receive preamble
  42.     sleep(0.05)
  43.     if not has_struck() then return false end
  44.     syncto(3)
  45.     if not has_struck() then return false end
  46.     syncto(5)
  47.     if not has_struck() then return false end
  48.     print "Preamble acquired."
  49.     local q = {}
  50.     while true do
  51.         syncto(20)
  52.         instantiate_creeper()
  53.         sleep(0.1)
  54.         local ctr = 0
  55.         while true do
  56.             if has_struck() then break end
  57.             sleep()
  58.             if ctr > 50 then return end
  59.             ctr = ctr + 1
  60.         end
  61.         local delay = ticks() % 20
  62.         print("delay", math.floor(delay / 3))
  63.         table.insert(q, math.floor(delay / 3))
  64.     end
  65.     local out = ""
  66.     for i = 1, #q, 4 do
  67.         table.insert(string.byte(bitpack(q[i], q[i+1], q[i+2], q[i+3])))
  68.     end
  69.     return out
  70. end
  71.  
  72. local function send(x)
  73.     syncto(20)
  74.     sleep(0.05)
  75.     cause_lightning()
  76.     syncto(3)
  77.     cause_lightning()
  78.     syncto(5)
  79.     cause_lightning()
  80.     local pkt = {string.byte(x, 1, #x)}
  81.     local rawpkt = {}
  82.     for _, v in pairs(pkt) do
  83.         local a, b, c, d = bitunpack(v)
  84.         table.insert(rawpkt, a)
  85.         table.insert(rawpkt, b)
  86.         table.insert(rawpkt, c)
  87.         table.insert(rawpkt, d)
  88.     end
  89.     for _, v in pairs(rawpkt) do
  90.         syncto(20)
  91.         sleep(0.15 * v + 0.1)
  92.         print("TX", ticks(), v)
  93.         cause_lightning()
  94.     end
  95. end
  96.  
  97. if ... == "send" then
  98.     send "Test.."
  99. else
  100.     while true do print(recv()) end
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement