Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sensor = peripheral.find "manipulator"
- local function bitpack(a, b, c, d)
- return bit.bor(bit.blshift(a, 6), bit.blshift(b, 4), bit.blshift(c, 2), d)
- end
- local function bitunpack(x)
- 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)
- end
- local function has_struck()
- for _, x in pairs(sensor.sense()) do
- if x.displayName == "item.tile.wood.oak" then return false end
- end
- return true
- end
- local function ticks()
- return os.time() * 1000
- end
- local function syncto(interval)
- local t_now = ticks()
- local x = interval - t_now % interval
- if x > 0 then sleep(x * 0.05) end
- end
- local function instantiate_creeper()
- commands.execAsync [[kill @e[type=item,r=3] ]]
- commands.execAsync [[summon item ~ ~1 ~ {Item:{id:"minecraft:planks",Count:1}}]]
- sleep()
- end
- local function cause_lightning()
- for i = 1, 10 do commands.execAsync "summon lightning_bolt 27387 50 -86351" end
- end
- local function recv()
- syncto(20)
- instantiate_creeper()
- -- receive preamble
- sleep(0.05)
- if not has_struck() then return false end
- syncto(3)
- if not has_struck() then return false end
- syncto(5)
- if not has_struck() then return false end
- print "Preamble acquired."
- local q = {}
- while true do
- syncto(20)
- instantiate_creeper()
- sleep(0.1)
- local ctr = 0
- while true do
- if has_struck() then break end
- sleep()
- if ctr > 50 then return end
- ctr = ctr + 1
- end
- local delay = ticks() % 20
- print("delay", math.floor(delay / 3))
- table.insert(q, math.floor(delay / 3))
- end
- local out = ""
- for i = 1, #q, 4 do
- table.insert(string.byte(bitpack(q[i], q[i+1], q[i+2], q[i+3])))
- end
- return out
- end
- local function send(x)
- syncto(20)
- sleep(0.05)
- cause_lightning()
- syncto(3)
- cause_lightning()
- syncto(5)
- cause_lightning()
- local pkt = {string.byte(x, 1, #x)}
- local rawpkt = {}
- for _, v in pairs(pkt) do
- local a, b, c, d = bitunpack(v)
- table.insert(rawpkt, a)
- table.insert(rawpkt, b)
- table.insert(rawpkt, c)
- table.insert(rawpkt, d)
- end
- for _, v in pairs(rawpkt) do
- syncto(20)
- sleep(0.15 * v + 0.1)
- print("TX", ticks(), v)
- cause_lightning()
- end
- end
- if ... == "send" then
- send "Test.."
- else
- while true do print(recv()) end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement