본문 바로가기

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

[Computer Craft] 16. <응용기술> 유저 인식 문

1. 개요


본 강좌는 컴퓨터크래프트 애드온 모드인 OpenCCSensors 가 설치되어 있어야 사용할 수 있습니다

모드 자료 : OpenCCSensors


지난 시간엔 채팅인식하는 시스템을 구축해보았는데요

타이핑을 하기조차 귀찮아 하는 여러분들을 위해 유저감지센서라는 아이템을 써봅시다.


Sensor 조합법


Proximity Sensor Card (MK1) 조합법



2. 새로운 API들

os.loadAPI("ocs/apis/sensor") : API 함수를 불러오는 명령어입니다.

math.sqrt() : 루트를 의미합니다.

math.pow(밑,지수) : 제곱을 의미 합니다. = math.pow(2,3) 으로 표현 할 수 있습니다.

sensor.wrap(side) : 센서의 방향을 결정합니다.

detect.getTargets() : 센서와 가까운곳 타겟을 감지해 테이블 값으로 정리해줍니다.

detect.getTargetDetails() : 위의값에서 받아온 데이터의 자세한 디테일 값을 표기해줍니다.



3. 주석소스


function Distance(User,System) -- 거리 계산해주는 함수 입니다.
  return math.floor(math.sqrt(math.pow((User['X']-System['X']),2)+math.pow((User['Y']-System['Y']),2)+math.pow((User['Z']-System['Z']),2)))
end

os.loadAPI("ocs/apis/sensor")
detect = sensor.wrap('back')
local Range = { -- 센서의 초기값
  ['X'] = 0.5,
  ['Y'] = 0.5,
  ['Z'] = 0.5,
  ['Distance'] = 5
}

local Player = { -- 유저 좌표를 받아들일 변수
  ['X'] = 0,
  ['Y'] = 0,
  ['Z'] = 0,
}
local side = 'bottom'
local check = 0 -- 비상용체크
while true do
  os.sleep(0.05)
  for name, MainDetail in pairs(detect.getTargets()) do -- 감지한 값을 이름, 디테일값 나눠 저장 단.. 아무것도 없으면 실행 조차 안된다.
    Player['Name'] = name -- 유저이름
    Detail = MainDetail.Position -- 디테일값중 좌표값을 받아온다.
    check = 1
    if Player['Name'] ~= nil then -- 가까운곳에 유저/몹 이 있있는가를 체크
      local PlayerCheck = detect.getTargetDetails(Player['Name'])
      if PlayerCheck ~= nill then
    if tostring(PlayerCheck.Name) == 'Player' then -- 체크한값이 유저일경우
          for Way, Value in pairs(Detail) do Player[Way] = Value end -- 유저위치를 변수로저장
          if Distance(Range,Player) <= Range['Distance'] then redstone.setOutput(side,true) -- 유저위치가 저장된 값이내일경우 문 열어주기
      else redstone.setOutput(side,false) end -- 멀면 문닫기
         end
      else redstone.setOutput(side,false) end -- 유저가 없어 문닫기
    else redstone.setOutput(side,false) end -- 아무것도 없어 문닫기
  end
  if check == 0 then redstone.setOutput(side,false) end -- 아무것도 없어 문닫기
  check = 0
end



4. 복붙용 소스

function Distance(User,System)
  return math.floor(math.sqrt(math.pow((User['X']-System['X']),2)+math.pow((User['Y']-System['Y']),2)+math.pow((User['Z']-System['Z']),2)))
end

os.loadAPI("ocs/apis/sensor")
detect = sensor.wrap('back')
local Range = {
  ['X'] = 0.5,
  ['Y'] = 0.5,
  ['Z'] = 0.5,
  ['Distance'] = 8
}

local Player = {
  ['X'] = 0,
  ['Y'] = 0,
  ['Z'] = 0,
}
local side = 'bottom'
local check = 0
while true do
  os.sleep(0.05)
  for name, MainDetail in pairs(detect.getTargets()) do
    Player['Name'] = name
    Detail = MainDetail.Position
    check = 1
    if Player['Name'] ~= nil then
      local PlayerCheck = detect.getTargetDetails(Player['Name'])
      if PlayerCheck ~= nill then
    if tostring(PlayerCheck.Name) == 'Player' then
          for Way, Value in pairs(Detail) do Player[Way] = Value end
          if Distance(Range,Player) <= Range['Distance'] then redstone.setOutput(side,true)
      else redstone.setOutput(side,false) end
        end
      else redstone.setOutput(side,false) end
    else redstone.setOutput(side,false) end
  end
  if check == 0 then redstone.setOutput(side,false) end
  check = 0
end



5. 시연영상






6. 후기


개인적으로 이 프로그래밍은 간단하게 봤는데 그게 아니더군요 :(

API 내장함수가 굉장히 조잡한데다 센서성능이 뛰어날수록 받아들이는 불필요한 데이터도 정말 많아요.

저는. 이 모드는 모니터 프로그래밍급이라고 생각하는바 입니다.