Savage_Me55iah

CC_PLAYERSENSOR_V0.1

Jan 17th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.02 KB | None | 0 0
  1. -- Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co --
  2. local version, pastebinCode = "v0.1", "rCgFnkGA"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. -- INSTRUCTIONS ON USE --
  7. -- Instruction video on http://youtube.com/savagemessiah5
  8. -- Uses PlayerSensor from Peripherals++
  9. -- Lag friendly player sensor (timer decreases the closer the player gets)
  10.  
  11. -- Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  12.  
  13. -- CHANGELOG --
  14. -- V 0.1 - Working
  15.  
  16. -- YET TO BE IMPLEMENTED --
  17. -- Player whitelist
  18.  
  19.  
  20. -- UPDATER --
  21.  
  22. local tArgs = {...}
  23. local update = tostring(tArgs[1]) or nil
  24. if update == "update" then
  25.     print("UPDATING") shell.run("pastebin get "..pastebinCode.." update")
  26.     if fs.exists("update") then
  27.         fs.delete(shell.getRunningProgram()) fs.copy("update", shell.getRunningProgram()) fs.delete("update") print("UPDATE SUCCESS")
  28.     else
  29.         print("UPDATE FAILED")
  30.     end
  31.     sleep(2) os.reboot()
  32. end
  33.  
  34. -- SETUP --
  35.  
  36. label = os.getComputerLabel() or false
  37. if label == false then
  38.     os.setComputerLabel("PLAYERSENSOR"..tostring(math.random(1000, 9999)))
  39. end
  40.  
  41. function boolean_check(params)
  42.     local answer = nil
  43.     if tostring(params) == "true" then
  44.         answer = true
  45.     else
  46.         answer = false
  47.     end
  48.     return answer
  49. end
  50.  
  51. local settings_file = "config"
  52.  
  53. local settings_template = [[-- adjust these configuration options as necessary.
  54. -- IMPORTANT!, API won't load if there are spaces in the values
  55. COMPUTER_LABEL = "${label}"
  56. COMPUTER_ID = ${id}
  57.  
  58. PLAYER_WHITELIST = "${player_whitelist}"
  59. DISTANCE_OUTER = ${distance_outer}
  60. DISTANCE_INNER = ${distance_inner}
  61. TIMER_OUTER = ${timer_outer}
  62. TIMER_INNER = ${timer_inner}
  63. OUTPUT = "${output}"
  64. INVERT = "${invert}"
  65. ]]
  66.  
  67. local function interpolate(s, params)
  68.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  69. end
  70.  
  71. do
  72.     save_settings = function()
  73.         local filehandler = fs.open(tostring(settings_file), "w")
  74.  
  75.         if tostring(CONFIG) == "nil" then
  76.             settings = {
  77.             label = os.getComputerLabel(),
  78.             id = os.getComputerID(),
  79.             distance_outer = 14,
  80.             distance_inner = 7,
  81.             player_whitelist = "false",
  82.             timer_outer = 3,
  83.             timer_inner = 1,
  84.             output = "back",
  85.             invert = "false"
  86.             }
  87.         else
  88.             settings = {
  89.             label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  90.             id = os.getComputerID(),
  91.             distance_outer = CONFIG.DISTANCE_OUTER or 14,
  92.             distance_inner = CONFIG.DISTANCE_INNER or 7,
  93.             player_whitelist = CONFIG.PLAYER_WHITELIST or "false",
  94.             timer_outer = CONFIG.TIMER_OUTER or 3,
  95.             timer_inner = CONFIG.TIMER_INNER or 1,
  96.             output = CONFIG.OUTPUT or "back",
  97.             invert = CONFIG.INVERT or "false"
  98.             }
  99.         end
  100.  
  101.         filehandler.write(interpolate(settings_template, settings))
  102.         filehandler.close()
  103.     end
  104.  
  105.     if fs.exists(settings_file) == false then
  106.         save_settings()
  107.         return
  108.     else
  109.         os.unloadAPI(settings_file)
  110.         if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  111.         CONFIG = nil
  112.         for k, v in pairs(_G) do if k == settings_file then CONFIG = v break end end
  113.         if CONFIG == nil then print("CONFIG came up nil") end
  114.     end
  115.  
  116.     peripheral_find = function(param)
  117.         local method, answer, sides = param or nil, nil, {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  118.         for a,b in pairs(sides) do
  119.             if peripheral.isPresent(b) then
  120.                 local test_methods = peripheral.getMethods(b)
  121.                 for c,d in pairs(test_methods) do if d == tostring(method) then answer = peripheral.wrap(b) end end
  122.             end
  123.         end
  124.         return answer
  125.     end
  126.    
  127.     local invert = true
  128.     if boolean_check(CONFIG.INVERT) then invert = false end
  129.    
  130.     scan_inner = function()
  131.         local detect = sensor.getNearbyPlayers(tonumber(CONFIG.DISTANCE_INNER)) or nil
  132.         if tostring(detect[1]) ~= "nil" then
  133.             redstone.setOutput(tostring(CONFIG.OUTPUT), not boolean_check(invert)) print("DETECT INNER: "..detect[1]["distance"])
  134.         else
  135.             redstone.setOutput(tostring(CONFIG.OUTPUT), boolean_check(invert)) print("NO DETECT INNER")
  136.         end
  137.     end
  138.    
  139.     local timing = tonumber(CONFIG.TIMER_INNER)
  140.     scan_outer = function()
  141.         local detect, answer = sensor.getNearbyPlayers(tonumber(CONFIG.DISTANCE_OUTER)) or nil, false
  142.         if tostring(detect[1]) ~= "nil" then
  143.             timing, answer = tonumber(CONFIG.TIMER_INNER), true print("DETECT OUTER: "..detect[1]["distance"])
  144.         else
  145.             timing, answer = tonumber(CONFIG.TIMER_OUTER), false print("NO DETECT OUTER")
  146.             redstone.setOutput(tostring(CONFIG.OUTPUT), boolean_check(invert))
  147.         end
  148.         return answer
  149.     end
  150.    
  151.     sensor = peripheral_find("getNearbyPlayers")
  152.    
  153.     while true do
  154.         term.clear() term.setCursorPos(1,1)
  155.         if boolean_check(scan_outer()) then scan_inner() end
  156.         os.startTimer(timing)
  157.         os.pullEvent()
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment