Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Credit to this for getting me started: http://www.youtube.com/watch?v=34R4KfLmMSY
- os.loadAPI("ocs/apis/sensors")
- -- From: http://www.youtube.com/watch?v=34R4KfLmMSY
- function printDict(data)
- for i,v in pairs(data) do
- print(tostring(i).." - "..tostring(v))
- end
- end
- function getDistance(sensorData,targetData)
- distanceData = {}
- distanceData["xDistance"] = math.abs(sensorData["xCoord"] - targetData["xCoord"])
- distanceData["yDistance"] = math.abs((sensorData["yCoord"] - 2) - targetData["yCoord"]) -- Note: the - 2 is because the sensor is above my door
- distanceData["zDistance"] = math.abs(sensorData["zCoord"] - targetData["zCoord"])
- -- Add the largest distance to the array
- distanceData["distance"] = distanceData["xDistance"]
- if distanceData["yDistance"] > distanceData["distance"] then
- distanceData["distance"] = distanceData["yDistance"]
- elseif distanceData["zDistance"] > distanceData["distance"] then
- distanceData["distance"] = distanceData["zDistance"]
- end
- return distanceData
- end
- function isPlayerClose()
- -- Get list of targets in range of probe
- targets = sensors.getAvailableTargetsforProbe(controller,sensorName,probeName)
- -- Loop through this list of targets getting the info about each one
- for i=1,# targets do
- -- Get the target data, i.e. x,y,z coods and type
- targetData = sensors.getSensorReadingAsDict(controller,sensorName,targets[i],probeName)
- -- If the target is a player, i.e. has the name "vq" then check if it is close
- if targetData["name"] == "vq" then
- -- Get the distances to the target
- distanceData = getDistance(sensorData,targetData)
- -- If a player is close enough return true
- if distanceData["distance"] < 4 then
- return true
- end
- end
- end
- -- If no players were close return false
- return false
- end
- -- Get the side the controller is on
- controller = sensors.getController()
- -- Get sensors, e.g. FactoryDoor. Useful to find the names of your sensors
- -- data = sensors.getSensors(controller)
- -- printDict(data)
- -- Set the sensor name for the one you wish to control
- sensorName = "Sensor"
- -- Get information about the sensor, e.g. its name, location, range
- sensorData = sensors.getSensorInfo(controller,sensorName)
- -- Change sensor range
- sensors.setSensorRange(controller,sensorName,"3")
- -- Get probes, e.g. TargetInfo, Players, etc
- -- IMPORTANT: the program will fail unless the line below is called, Im not sure why but I guess it is a bug
- data = sensors.getProbes(controller,sensorName)
- -- printDict(data)
- probeName = "TargetInfo"
- -- Loop the program so it keeps checking for targets, making sure it sleeps when in any infinite loop
- while true do
- if isPlayerClose() then
- redstone.setOutput("back",true)
- -- Keep the door open until the player gets a certain distance away
- while isPlayerClose() do
- sleep(.5)
- end
- redstone.setOutput("back",false)
- end
- sleep(.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement