Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --:: LOCAL SENSOR ::--
- os.loadAPI("config")
- os.loadAPI("update")
- local rSides = config.rSides
- --todo move these to config
- local RADIUS = 5
- local OFFSET = {
- X = 0,
- Y = 2,
- Z = 0
- }
- function wait_for_auth_response()
- while true do
- local e,s,m,d = os.pullEvent("rednet_message")
- if s == authID then
- return m
- end
- end
- end
- function println(text)
- print(text)
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.blue)
- x,y = term.getCursorPos()
- term.setCursorPos(1,1)
- term.write(config.self..' LOCAL DOOR CONTROLLER ')
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(x,y)
- end
- function distance(pos)
- local xd = pos.X - OFFSET.X
- local yd = pos.Y - OFFSET.Y
- local zd = pos.Z - OFFSET.Z
- return math.sqrt(xd*xd + yd*yd + zd*zd)
- end
- function auth_check(user)
- local tMsg = {door = config.self, user=user}
- local msg = textutils.serialize(tMsg)
- rednet.send(authID, msg,'system')
- return wait_for_auth_response()
- end
- -- OPEN / CLOSE FUNCTIONS --
- function open()
- print('open')
- for k,side in pairs(rSides) do
- rs.setOutput(side,false)
- end
- end
- function close2()
- print('close')
- for k,side in pairs(rSides) do
- rs.setOutput(side,true)
- end
- end
- ----------
- --MODEM INIT
- for _,side in pairs(rs.getSides()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- if not rednet.isOpen() then
- print('! Network error')
- error()
- end
- update.update(config.update_file, config.update_name)
- -- SENSOR INIT
- os.loadAPI("ocs/apis/sensor")
- s = sensor.wrap("top")
- -- INIT
- rednet.host('control',config.self)
- authID = rednet.lookup('system','database')
- for k,side in pairs(rSides) do
- rs.setOutput(side,config.default)
- end
- term.clear()
- println()
- while true do
- for name,b_det in pairs(s.getTargets()) do
- print('found'..name)
- if b_det.IsPlayer then
- print('they are player')
- local det = s.getTargetDetails(name)
- print('got deets')
- if det~=nil and distance(det.Position) < RADIUS and det.IsSneaking and auth_check(name) then
- open()
- sleep(2)
- close2()
- end
- end
- sleep(0.2)
- end
- sleep(0.2)
- end
Advertisement
Add Comment
Please, Sign In to add comment