Posted 02 September 2012 - 09:41 AM
First of all thanks to the person who made the ccSensors mod and for the ComputerCraft mod, both are awesome and I will be using the ComputerCraft mod to teach my neice and nephew programming when they are a year or so older. :)/>/>
I wanted to make a program that detected entities close to a Proximity Sensor and then deal with them appropriately. At first I tried to get the Players probe, the one that gives the name, level, etc about a player, working and actually succeeded once. However this stopped working for some reason and I wasted a lot of time trying to fix my code when in fact the ccSensors code was at fault. I then switched to using the TargetInfo probe and after working around a few bugs I have found it to be reliable.
Here is the code I use to open my door when a player is close. This can be easily modified for other actions.
I wanted to make a program that detected entities close to a Proximity Sensor and then deal with them appropriately. At first I tried to get the Players probe, the one that gives the name, level, etc about a player, working and actually succeeded once. However this stopped working for some reason and I wasted a lot of time trying to fix my code when in fact the ccSensors code was at fault. I then switched to using the TargetInfo probe and after working around a few bugs I have found it to be reliable.
Here is the code I use to open my door when a player is close. This can be easily modified for other actions.
-- Credit to this for getting me started: http://www.youtube.com/watch?v=34R4KfLmMSY
os.unloadAPI("sensors")
os.loadAPI("/rom/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