Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [ (http://i.imgur.com/dDEV0) Image of my setup ] --
- controller=sensors.getController() -- Constant, but may start out nil, so we prompt in setup
- probe="Players" -- Constant
- -- Direction Constants --
- NEG_X=0
- POS_X=1
- NEG_Z=2
- POS_Z=3
- NO_DIR=4
- function directionMatch(x1,y1,z1,x2,y2,z2,dir)
- x1,y1,z1=tonumber(x1),tonumber(y1),tonumber(z1)
- x2,y2,z2=tonumber(x2),tonumber(y2),tonumber(z2)
- dir=tonumber(dir)
- if(dir == NO_DIR) then
- return true
- elseif (dir == NEG_X) then
- return x1<x2
- elseif (dir == POS_X) then
- return x2>x2
- elseif (dir == NEG_Z) then
- return z1<z2
- elseif (dir == POS_Z) then
- return z1>z2
- else
- return true
- end
- end
- function getDirection(text)
- if(text == nil) then
- return NO_DIR
- end
- pn,d=string.match(text, "([%-%+])([a-z])")
- if (pn ~= nil) then
- if(pn == "+") then
- if(d ~= nil) then
- if(d == "x") then
- return POS_X
- elseif (d == "z") then
- return POS_Z
- else
- return NO_DIR
- end
- end
- elseif (pn == "-") then
- if(d ~= nil) then
- if(d == "x") then
- return NEG_X
- elseif (d == "z") then
- return NEG_Z
- else
- return NO_DIR
- end
- end
- else
- return NO_DIR
- end
- else
- return NO_DIR
- end
- end
- function show(text)
- term.clear()
- mon.clear()
- term.setCursorPos(1,3)
- write(" "..text)
- mon.setCursorPos(1,3)
- mon.write(" "..text)
- end
- function findPlayer(availTargets)
- for i,val in ipairs(availTargets) do
- what,x,y,z=string.match(val,"([a-z]+),(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
- if(what=="vq") then
- return what,x,y,z
- end
- end
- return nil
- end
- function findClosestPlayer(availTargets)
- local what,x,y,z
- local closestDistance=math.huge
- for i,val in ipairs(availTargets) do
- local w,lx,ly,lz = string.match(val,"([a-z]+),(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
- if(w == "vq") then
- lx,ly,lz=tonumber(lx),tonumber(ly),tonumber(lz)
- local dist=distance(lx,ly,lz,doorX,doorY,doorZ)
- if(dist <= closestDistance) then
- what,x,y,z=w,lx,ly,lz
- end
- end
- end
- return what,x,y,z
- end
- function inRange(x,y,z)
- x,y,z=tonumber(x),tonumber(y),tonumber(z)
- return distance(x,y,z,doorX,doorY,doorZ) <= radius
- end
- function distance(x1,y1,z1,x2,y2,z2)
- local dx=math.pow(x2-x1,2)
- local dy=math.pow(y2-y1,2)
- local dz=math.pow(z2-z1,2)
- return math.sqrt(dx+dy+dz)
- end
- function setup()
- promptSensorSetup()
- promptMonitor()
- promptRednet()
- promptRedstone()
- promptDoorLocation()
- promptDisplayDirection()
- term.clear()
- mon.clear()
- --term.redirect(mon)
- hasPlayerBeenDetected=false
- end
- function promptSensorSetup()
- if(controller == nil) then
- write("What side is the sensor controller on? ")
- controller=read()
- while controller ~= nil do
- controller=read()
- os.sleep(0.1)
- end
- else
- print("Sensor Controller found on "..controller.." side")
- end
- write("What is the name of the sensor? ")
- sensor=read()
- while sensor == nil do
- sensor=read()
- os.sleep(0.1)
- end
- end
- function promptDoorLocation()
- write("Where is the door? (x,y,z) ")
- local loc=read()
- if(loc == nil) then
- while loc == nil do
- loc=read()
- os.sleep(0.1)
- end
- end
- local x,y,z=getXYZ(loc)
- doorX,doorY,doorZ=tonumber(x),tonumber(y),tonumber(z)
- write("What distance (in blocks) triggers the door? ")
- local rad=read()
- while rad == nil do
- rad=read()
- os.sleep(0.1)
- end
- radius=tonumber(rad)
- end
- function getXYZ(text)
- return string.match(text, "(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
- end
- function promptMonitor()
- write("What side is the monitor on? ")
- local monSide=read()
- while monSide == nil do
- monSide=read()
- os.sleep(0.1)
- end
- mon=peripheral.wrap(monSide)
- end
- function promptRednet()
- write("What side is the rednet modem on? ")
- local network=read()
- while network == nil do
- network=read()
- os.sleep(0.1)
- end
- if(network ~= "none") then
- rednet.open(network)
- usingRednet=true
- else
- usingRednet=false
- end
- end
- function promptRedstone()
- write("What side is the door's redstone controll on? ")
- doorRed=read()
- if(doorRed == nil) then
- while doorRed == nil do
- doorRed=read()
- os.sleep(0.1)
- end
- end
- end
- function promptDisplayDirection()
- write("what direction triggers the display? (-x,+x,-z,+z, or none) ")
- local dir=read()
- if(dir == nil) then
- while dir == nil do
- dir = read()
- end
- end
- displayDir=getDirection(dir)
- end
- function main()
- while true do
- local at=sensors.getAvailableTargetsforProbe(controller,sensor,probe)
- local what,x,y,z=findClosestPlayer(at)
- if(what ~= nil) then
- if(inRange(x,y,z)) then
- if(usingRednet and hasPlayerBeenDetected == false) then
- rednet.broadcast("Door accessed at "..os.time())
- end
- if(directionMatch(x,y,z,doorX,doorY,doorZ,displayDir)) then
- show("Player detected")
- end
- hasPlayerBeenDetected=true
- redstone.setOutput(doorRed, true)
- else
- show("")
- hasPlayerBeenDetected=false
- redstone.setOutput(doorRed, false)
- end
- end
- os.sleep(0.2)
- end
- term.restore()
- end
- setup()
- main()
Advertisement
Add Comment
Please, Sign In to add comment