Unh0ly_Tigg

Tekkit ccSensors ComputerCraft proximity door

Sep 5th, 2012
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. -- [ (http://i.imgur.com/dDEV0) Image of my setup ] --
  2.  
  3. controller=sensors.getController() -- Constant, but may start out nil, so we prompt in setup
  4. probe="Players" -- Constant
  5.  
  6. -- Direction Constants --
  7. NEG_X=0
  8. POS_X=1
  9. NEG_Z=2
  10. POS_Z=3
  11. NO_DIR=4
  12.  
  13. function directionMatch(x1,y1,z1,x2,y2,z2,dir)
  14.   x1,y1,z1=tonumber(x1),tonumber(y1),tonumber(z1)
  15.   x2,y2,z2=tonumber(x2),tonumber(y2),tonumber(z2)
  16.   dir=tonumber(dir)
  17.   if(dir == NO_DIR) then
  18.     return true
  19.   elseif (dir == NEG_X) then
  20.     return x1<x2
  21.   elseif (dir == POS_X) then
  22.     return x2>x2
  23.   elseif (dir == NEG_Z) then
  24.     return z1<z2
  25.   elseif (dir == POS_Z) then
  26.     return z1>z2
  27.   else
  28.     return true
  29.   end
  30. end
  31.  
  32. function getDirection(text)
  33.   if(text == nil) then
  34.     return NO_DIR
  35.   end
  36.   pn,d=string.match(text, "([%-%+])([a-z])")
  37.   if (pn ~= nil) then
  38.     if(pn == "+") then
  39.       if(d ~= nil) then
  40.         if(d == "x") then
  41.           return POS_X
  42.         elseif (d == "z") then
  43.           return POS_Z
  44.         else
  45.           return NO_DIR
  46.         end
  47.       end
  48.     elseif (pn == "-") then
  49.       if(d ~= nil) then
  50.         if(d == "x") then
  51.           return NEG_X
  52.         elseif (d == "z") then
  53.           return NEG_Z
  54.         else
  55.           return NO_DIR
  56.         end
  57.       end
  58.     else
  59.       return NO_DIR
  60.     end
  61.   else
  62.     return NO_DIR
  63.   end
  64. end
  65.  
  66. function show(text)
  67.   term.clear()
  68.   mon.clear()
  69.   term.setCursorPos(1,3)
  70.   write(" "..text)
  71.   mon.setCursorPos(1,3)
  72.   mon.write(" "..text)
  73. end
  74.  
  75. function findPlayer(availTargets)
  76.   for i,val in ipairs(availTargets) do
  77.     what,x,y,z=string.match(val,"([a-z]+),(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
  78.     if(what=="vq") then
  79.       return what,x,y,z
  80.     end
  81.   end
  82.   return nil
  83. end
  84.  
  85. function findClosestPlayer(availTargets)
  86.   local what,x,y,z
  87.   local closestDistance=math.huge
  88.   for i,val in ipairs(availTargets) do
  89.     local w,lx,ly,lz = string.match(val,"([a-z]+),(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
  90.     if(w == "vq") then
  91.       lx,ly,lz=tonumber(lx),tonumber(ly),tonumber(lz)
  92.       local dist=distance(lx,ly,lz,doorX,doorY,doorZ)
  93.       if(dist <= closestDistance) then
  94.         what,x,y,z=w,lx,ly,lz
  95.       end
  96.     end
  97.   end
  98.   return what,x,y,z
  99. end
  100.  
  101. function inRange(x,y,z)
  102.   x,y,z=tonumber(x),tonumber(y),tonumber(z)
  103.   return distance(x,y,z,doorX,doorY,doorZ) <= radius
  104. end
  105.  
  106. function distance(x1,y1,z1,x2,y2,z2)
  107.   local dx=math.pow(x2-x1,2)
  108.   local dy=math.pow(y2-y1,2)
  109.   local dz=math.pow(z2-z1,2)
  110.   return math.sqrt(dx+dy+dz)
  111. end
  112.  
  113. function setup()
  114.   promptSensorSetup()
  115.   promptMonitor()
  116.   promptRednet()
  117.   promptRedstone()
  118.   promptDoorLocation()
  119.   promptDisplayDirection()
  120.   term.clear()
  121.   mon.clear()
  122.   --term.redirect(mon)
  123.   hasPlayerBeenDetected=false
  124. end
  125.  
  126. function promptSensorSetup()
  127.   if(controller == nil) then
  128.     write("What side is the sensor controller on? ")
  129.     controller=read()
  130.     while controller ~= nil do
  131.       controller=read()
  132.       os.sleep(0.1)
  133.     end
  134.   else
  135.     print("Sensor Controller found on "..controller.." side")
  136.   end
  137.   write("What is the name of the sensor? ")
  138.   sensor=read()
  139.   while sensor == nil do
  140.     sensor=read()
  141.     os.sleep(0.1)
  142.   end
  143. end
  144.  
  145. function promptDoorLocation()
  146.   write("Where is the door? (x,y,z) ")
  147.   local loc=read()
  148.   if(loc == nil) then
  149.     while loc == nil do
  150.       loc=read()
  151.       os.sleep(0.1)
  152.     end
  153.   end
  154.   local x,y,z=getXYZ(loc)
  155.   doorX,doorY,doorZ=tonumber(x),tonumber(y),tonumber(z)
  156.   write("What distance (in blocks) triggers the door? ")
  157.   local rad=read()
  158.   while rad == nil do
  159.     rad=read()
  160.     os.sleep(0.1)
  161.   end
  162.   radius=tonumber(rad)
  163. end
  164.  
  165. function getXYZ(text)
  166.   return string.match(text, "(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)")
  167. end
  168.  
  169. function promptMonitor()
  170.   write("What side is the monitor on? ")
  171.   local monSide=read()
  172.   while monSide == nil do
  173.     monSide=read()
  174.     os.sleep(0.1)
  175.   end
  176.   mon=peripheral.wrap(monSide)
  177. end
  178.  
  179. function promptRednet()
  180.   write("What side is the rednet modem on? ")
  181.   local network=read()
  182.   while network == nil do
  183.     network=read()
  184.     os.sleep(0.1)
  185.   end
  186.   if(network ~= "none") then
  187.     rednet.open(network)
  188.     usingRednet=true
  189.   else
  190.     usingRednet=false
  191.   end
  192. end
  193.  
  194. function promptRedstone()
  195.   write("What side is the door's redstone controll on? ")
  196.   doorRed=read()
  197.   if(doorRed == nil) then
  198.     while doorRed == nil do
  199.       doorRed=read()
  200.       os.sleep(0.1)
  201.     end
  202.   end
  203. end
  204.  
  205. function promptDisplayDirection()
  206.   write("what direction triggers the display? (-x,+x,-z,+z, or none) ")
  207.   local dir=read()
  208.   if(dir == nil) then
  209.     while dir == nil do
  210.       dir = read()
  211.     end
  212.   end
  213.   displayDir=getDirection(dir)
  214. end
  215.  
  216. function main()
  217.  while true do
  218.     local at=sensors.getAvailableTargetsforProbe(controller,sensor,probe)
  219.     local what,x,y,z=findClosestPlayer(at)
  220.     if(what ~= nil) then
  221.       if(inRange(x,y,z)) then
  222.         if(usingRednet and hasPlayerBeenDetected == false) then
  223.           rednet.broadcast("Door accessed at "..os.time())
  224.         end
  225.         if(directionMatch(x,y,z,doorX,doorY,doorZ,displayDir)) then
  226.           show("Player detected")
  227.         end
  228.         hasPlayerBeenDetected=true
  229.         redstone.setOutput(doorRed, true)
  230.       else
  231.         show("")
  232.         hasPlayerBeenDetected=false
  233.         redstone.setOutput(doorRed, false)
  234.       end
  235.     end
  236.     os.sleep(0.2)
  237.   end
  238.   term.restore()
  239. end
  240.  
  241. setup()
  242. main()
Advertisement
Add Comment
Please, Sign In to add comment