Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co --
- local version, pastebinCode = "v0.1", "rCgFnkGA"
- --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
- -- INSTRUCTIONS ON USE --
- -- Instruction video on http://youtube.com/savagemessiah5
- -- Uses PlayerSensor from Peripherals++
- -- Lag friendly player sensor (timer decreases the closer the player gets)
- -- Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- -- CHANGELOG --
- -- V 0.1 - Working
- -- YET TO BE IMPLEMENTED --
- -- Player whitelist
- -- UPDATER --
- local tArgs = {...}
- local update = tostring(tArgs[1]) or nil
- if update == "update" then
- print("UPDATING") shell.run("pastebin get "..pastebinCode.." update")
- if fs.exists("update") then
- fs.delete(shell.getRunningProgram()) fs.copy("update", shell.getRunningProgram()) fs.delete("update") print("UPDATE SUCCESS")
- else
- print("UPDATE FAILED")
- end
- sleep(2) os.reboot()
- end
- -- SETUP --
- label = os.getComputerLabel() or false
- if label == false then
- os.setComputerLabel("PLAYERSENSOR"..tostring(math.random(1000, 9999)))
- end
- function boolean_check(params)
- local answer = nil
- if tostring(params) == "true" then
- answer = true
- else
- answer = false
- end
- return answer
- end
- local settings_file = "config"
- local settings_template = [[-- adjust these configuration options as necessary.
- -- IMPORTANT!, API won't load if there are spaces in the values
- COMPUTER_LABEL = "${label}"
- COMPUTER_ID = ${id}
- PLAYER_WHITELIST = "${player_whitelist}"
- DISTANCE_OUTER = ${distance_outer}
- DISTANCE_INNER = ${distance_inner}
- TIMER_OUTER = ${timer_outer}
- TIMER_INNER = ${timer_inner}
- OUTPUT = "${output}"
- INVERT = "${invert}"
- ]]
- local function interpolate(s, params)
- return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
- end
- do
- save_settings = function()
- local filehandler = fs.open(tostring(settings_file), "w")
- if tostring(CONFIG) == "nil" then
- settings = {
- label = os.getComputerLabel(),
- id = os.getComputerID(),
- distance_outer = 14,
- distance_inner = 7,
- player_whitelist = "false",
- timer_outer = 3,
- timer_inner = 1,
- output = "back",
- invert = "false"
- }
- else
- settings = {
- label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
- id = os.getComputerID(),
- distance_outer = CONFIG.DISTANCE_OUTER or 14,
- distance_inner = CONFIG.DISTANCE_INNER or 7,
- player_whitelist = CONFIG.PLAYER_WHITELIST or "false",
- timer_outer = CONFIG.TIMER_OUTER or 3,
- timer_inner = CONFIG.TIMER_INNER or 1,
- output = CONFIG.OUTPUT or "back",
- invert = CONFIG.INVERT or "false"
- }
- end
- filehandler.write(interpolate(settings_template, settings))
- filehandler.close()
- end
- if fs.exists(settings_file) == false then
- save_settings()
- return
- else
- os.unloadAPI(settings_file)
- if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
- CONFIG = nil
- for k, v in pairs(_G) do if k == settings_file then CONFIG = v break end end
- if CONFIG == nil then print("CONFIG came up nil") end
- end
- peripheral_find = function(param)
- local method, answer, sides = param or nil, nil, {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
- for a,b in pairs(sides) do
- if peripheral.isPresent(b) then
- local test_methods = peripheral.getMethods(b)
- for c,d in pairs(test_methods) do if d == tostring(method) then answer = peripheral.wrap(b) end end
- end
- end
- return answer
- end
- local invert = true
- if boolean_check(CONFIG.INVERT) then invert = false end
- scan_inner = function()
- local detect = sensor.getNearbyPlayers(tonumber(CONFIG.DISTANCE_INNER)) or nil
- if tostring(detect[1]) ~= "nil" then
- redstone.setOutput(tostring(CONFIG.OUTPUT), not boolean_check(invert)) print("DETECT INNER: "..detect[1]["distance"])
- else
- redstone.setOutput(tostring(CONFIG.OUTPUT), boolean_check(invert)) print("NO DETECT INNER")
- end
- end
- local timing = tonumber(CONFIG.TIMER_INNER)
- scan_outer = function()
- local detect, answer = sensor.getNearbyPlayers(tonumber(CONFIG.DISTANCE_OUTER)) or nil, false
- if tostring(detect[1]) ~= "nil" then
- timing, answer = tonumber(CONFIG.TIMER_INNER), true print("DETECT OUTER: "..detect[1]["distance"])
- else
- timing, answer = tonumber(CONFIG.TIMER_OUTER), false print("NO DETECT OUTER")
- redstone.setOutput(tostring(CONFIG.OUTPUT), boolean_check(invert))
- end
- return answer
- end
- sensor = peripheral_find("getNearbyPlayers")
- while true do
- term.clear() term.setCursorPos(1,1)
- if boolean_check(scan_outer()) then scan_inner() end
- os.startTimer(timing)
- os.pullEvent()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment