Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ocs/apis/sensor")
- local id = "door_main"
- local modem = "right"
- local sens = "back"
- local door = "bottom"
- local permit = "sven"
- local range = 5
- local invert = true
- local pollrate = 0.2
- local prox = sensor.wrap(sens)
- local net = peripheral.wrap(modem)
- local b = invert
- local permission
- rs.setOutput(door, b)
- net.open(2)
- net.open(3)
- net.open(4)
- function loadPermission()
- net.transmit(1, 2, permit)
- local ev, ms, send, reply, msg, dist = os.pullEvent("modem_message")
- permission = textutils.unserialize(msg)
- end
- function printPerm()
- print("--->")
- print(textutils.serialize(permission))
- print("<---")
- end
- function dist(p)
- local add = p.X^2 + p.Y^2 + p.Z^2
- local rt = math.sqrt(add)
- return rt <= range
- end
- function split(str, del)
- local f = str:find(del, 1, true)
- if f == nil then
- local ret = {str}
- return ret
- end
- local ret = {}
- local i = 0
- repeat
- ret[i] = str:sub(1, f - 1)
- str = str:sub(f - str:len())
- i = i + 1
- f = str:find(del, 1, true)
- until f == nil
- ret[i] = str
- return ret
- end
- function sendLog(str)
- net.transmit(5, 0, str)
- end
- loadPermission()
- while true do
- local timer = os.startTimer(pollrate)
- local trg = prox.getTargets()
- b = invert
- for k, v in pairs(trg) do
- for i=1,#permission do
- if k == permission[1] then
- if dist(v.Position) then
- b = not invert
- end
- end
- end
- end
- rs.setOutput(door, b)
- repeat
- ev, ms, s, r, msg, dst = os.pullEvent()
- until ev == "modem_message" or (ev == "timer" and ms == timer)
- if ev == "modem_message" then
- if s == 3 then
- if msg == permit then
- loadPermission()
- end
- elseif s == 4 then
- local splcmd = split(msg, ":")
- if #splcmd == 2 then
- if splcmd[1] == id then
- local cmdtimer = os.startTimer(tonumber(splcmd[2]))
- local cmdb = invert
- if splcmd[0] == "open" then cmdb = not invert end
- rs.setOutput(door, cmdb)
- repeat
- ev, tim = os.pullEvent("timer")
- until tim == cmdtimer
- rs.setOutput(door, invert)
- end
- end
- end
- end
- --printPerm()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement