본문 바로가기

마인크래프트(Minecraft)/Computer Craft

[Computer Craft] 14. <응용기술> 플로피디스크 비밀번호 문

14. 플로피디스크 비밀번호 문


이번에는 예전에 썼던 대부분 소스들이 있기 떄문에 짧게 쓰겠습니다.


fs.exists(dir) : 디렉토리나 파일이 존재하는가 체크해주는 함수

h.eject() : 디스크를 배출해주는 함수


h.eject() 에서 h 에는 peripheral.wrap(side) 함수로 디스크 드라이버가 지정이 되었을때,

그 변수의 이름을 h값으로 가집니다.


예)

local drive = peripheral.wrap('left')

drive.eject()


local chick = peripheral.wrap('left')

chick.eject()

1. 주석소스



local side = 'bottom' -- 문을 열때 보낼 레드스톤 신호 위치
local drive = peripheral.wrap('left') -- 디스크드라이버의 위치
local dir = "disk/password" -- 비밀번호가 든 파일의 위치


while true do
  if not fs.exists(dir) then -- 비밀번호가든 파일의 위치가 존재 하느냐를 체크( 없으면 디스크 배출)
    drive.eject()
    os.sleep(1)
  else
    local password = fs.open(dir,"r")
    if password.readLine() == 'chick' then -- 디스크에 든 파일의 데이터가 chick 비밀번호와 일치하는가?
      redstone.setOutput(side,true)
      while true do -- 무한반복 : 디스크를 꺼낼때까지 대기
        if not fs.exists(dir) then
          break
        end
        os.sleep(1)
      end -- 디스크를 꺼냈으므로 종료
      redstone.setOutput(side,false)
    else
      drive.eject() -- 디스크 배출
      os.sleep(1)
    end
    password.close()
  end
end


2. 복붙용 소스

local side = 'bottom'
local drive = peripheral.wrap('left')
local dir = "disk/password"

while true do
  if not fs.exists(dir) then
    drive.eject()
    os.sleep(1)
  else
    local password = fs.open(dir,"r")
    if password.readLine() == 'chick' then
      redstone.setOutput(side,true)
      while true do
        if not fs.exists(dir) then
          break
        end
        os.sleep(1)
      end
      redstone.setOutput(side,false)
    else
      drive.eject()
      os.sleep(1)
    end
    password.close()
  end
end



3. 시연영상



4. 구시렁구시렁

생각난 김에 덧붙입니다.

비밀번호가 틀렸을때 플로피디스크 배출 하는게 싫으신분들을 위해 마련해두었습니다.


local side = 'bottom'
local drive = peripheral.wrap('left')
local dir = "disk/password"

while true do
  if not fs.exists(dir) then
    os.sleep(1)
  else
    local password = fs.open(dir,"r")
    if password.readLine() == 'chick' then
      redstone.setOutput(side,true)
      while true do
        if not fs.exists(dir) then
          break
        end
        os.sleep(1)
      end
      redstone.setOutput(side,false)
    else
      os.sleep(1)
    end
    password.close()
  end
end