CC Sensor (Player) based door control.

Discussion in 'ComputerCraft Programming' started by MyrddinE, 22 October 2013.

  1. MyrddinE

    MyrddinE Active Member

    Joined:
    4 December 2011
    Messages:
    220
    Likes Received:
    69
    This is a program to manage a player-detector based room, that opens when listed players are near, but stays locked if blacklisted players are near.

    startup
    Code:
    -- When this program gets a redstone signal, it
    -- scans for players. It continues scanning until
    -- the redstone has been off for the configured
    -- number of seconds.
    --
    -- While scanning, users on the whitelist will
    -- cause a redstone signal to be emitted. Users on
    -- the blacklist will prevent the redstone from
    -- activating, even when whitelisted players are
    -- near.
    --
    -- Author: MyrddinE
    -- 2013-10-16
     
    -- Configuration
    local side = {sensor="bottom",input="right",output="back"}
    local active_when = true
    local open_emit = false
    local continue_for = 30 -- seconds
     
    local file = fs.open("whitelist.dat","r")
    local whitelist = textutils.unserialize(file.readAll())
    file.close()
     
    file = fs.open("blacklist.dat","r")
    local blacklist = textutils.unserialize(file.readAll())
    file.close()
     
    function roomOpen(open)
      if open then
        print("Opening door.")
      else
        print("Closing door.")
      end
     
      -- The '(open == open_emit)' is a logical filter, setting
      -- the output based on whether open = true, or open = false.
      rs.setOutput(side.output,(open == open_emit))
    end
     
    -- Load sensor api, and connect to it.
    os.loadAPI("ocs/apis/sensor")
    local players = sensor.wrap(side.sensor)
     
    roomOpen(false)
     
    -- Infinite loop waiting for redstone change.
    while true do
      os.pullEvent("redstone")
     
      -- Initially just wait for 1 second when it
      -- detects a redstone change on any side.
      local stopTime = os.clock() + 1
     
      while os.clock() < stopTime do
        -- If the input side is in 'on mode' (as defined
        -- by the active_when var) then set the end time
        -- to be now + continue_for seconds (default of
        -- 30 seconds).
        if rs.getInput(side.input) == active_when then stopTime = os.clock() + continue_for end
     
        -- Compare all the names of nearby players to the
        -- lists.
        good = false
        bad = false
        targets = players.getTargets()
        for username,_ in pairs(targets) do
          write(username.." is ")
          if whitelist[username] then
            write("whitelisted. ")
            good = true
          elseif blacklist[username] then
            write("blacklisted. ")
            bad = true
          else
            write("unknown player. ")
          end
        end
       
        -- Open/close the door based on nearby listed
        -- players
        if bad then
          roomOpen(false)
        elseif good then
          roomOpen(true)
        else
          roomOpen(false)
        end
       
        write(os.time().." ")
        sleep(1)
      end
     
      -- Rebooting when the detection turns off allows dynamic
      -- re-loading of the software when updated.
      os.reboot()
    end
    whitelist or blacklist
    Code:
    {MyrddinE=true,DreamingDemon=true,}
    [​IMG]

    Just wire one side to start detection (pressure plates work well, but you could have a different source), and the other to open the door/room. Set the sides of these redstone inputs and outputs in the first line of this code. The later lines set whether to listen for 'on' or 'off' signal, and whether open means on, or open means off, on the output.

    Here is how I use it in my base. Detection is via obsidian pressure plates, and the Doors are vertical drawbridges (from Tinker's Construct):

    [​IMG]
    When open, imaged above, the portal room can be left, and the advanced dialer is accessible.

    [​IMG]
    When closed, because no whitelisted player is nearby, visitors are presented with a closed room. They can use the basic dialer (barely in the frame in the lower left corner) to leave, but cannot enter the base or view the private dialer codes.
     
  2. gknova61

    gknova61 Farbes Lover

    Joined:
    17 March 2012
    Messages:
    1.238
    Likes Received:
    350
    Beautiful'n'simple, just the way I like 'em. [​IMG]
     
  3. Jamjardavies

    Jamjardavies Programmer (Not for Skcraft)

    Joined:
    26 August 2013
    Messages:
    7
    Likes Received:
    1
    There is a typo in the code: write("wlacklisted player. ")

    Nice work though!
     
  4. MyrddinE

    MyrddinE Active Member

    Joined:
    4 December 2011
    Messages:
    220
    Likes Received:
    69
    Fixed. The word player was superfluous as well, removed.
     
  5. sk89q

    sk89q Administrator
    Staff Member

    Joined:
    1 December 2011
    Messages:
    2.490
    Likes Received:
    1.504
    dat non-smooth lighting