This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Falesh's profile picture

ccSensors target identifier/door opener

Started by Falesh, 02 September 2012 - 07:41 AM
Falesh #1
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.


-- 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
Falesh #2
Posted 02 September 2012 - 10:16 AM
One word of warning. This program might suddenly stop working. If that happens you can get it to start again by destroying the Sensor Controller and the Proximity Sensor, then placing them down and connecting them with a new, from blank, frequency card. If anyone finds a way around this please let me know!
rightman #3
Posted 15 April 2013 - 01:36 PM
Do you know if this works properly on multiplayer?
lolsethlol #4
Posted 27 May 2013 - 11:29 PM
Just tried it. Got multiple errors after getting it directly from the here. Does not work.
alakazard12 #5
Posted 28 May 2013 - 07:53 AM
Just tried it. Got multiple errors after getting it directly from the here. Does not work.

How can you get multiple errors? Lua stops to code right at one o.O (Except at certain exceptions, but that couldn't happen here.)
ThatGamingGnome #6
Posted 29 May 2013 - 06:20 PM
You might want to check out my player detecing proximity door.
[media]http://www.youtube.com/watch?v=HxUQlLKc1NE[/media].
You can also check it at this thread http://www.computerc...detecting-door/