본문 바로가기

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

[Computer Craft] 19. <응용기술> 패턴 인식

오랜만에 컴퓨터크래프트 관련 글을 쓰네요

소스는 생각외로 단순하게되어 있습니다

이전에 썼던 강좌글에 다 나와있던 API 함수를 활용한것이죠.


5개의 함수로 이루어져있으며 각자의 역할은 구분 지어져 있습니다.



1. T_C()


turtle.compare() 함수를 쓰는것이며, 8번슬롯부터 16번 슬롯까지 하나씩 선택을 해줍니다.

만일 선택된 블럭이 아래의 길과 일치하면 그 블럭에 맞는 행동값 코드를 부여해줍니다.



2. T_S(num)


turtle.select() 함수를 쓰는것이며 길게 치기 귀찮아서 함수화했습니다.



3. T_GetItemCount()


아이템수를 계산해주는 turtle.getItemCount() 함수를 썻습니다.

대게 상자에 넣을때 ([현재들고있는 아이템의 수량 ] -1)을 지정하기위해서 쓰는편이죠.



4. T_P(pattern)


T_C함수에서 넘어온 패턴값을 받아서 특정한 패턴에 맞게 행동을 하게 해주는것입니다.

패턴은 아래와 같습니다

1 : 전진

2 : 우회전후 전진

3 : 좌회전후 전진

4 : 우회전후 나무베고 묘목을 심고 좌회전후 전진

5 : 좌회전후 나무베고 묘목을 심고 우회전후 전진

6 : 좌회전후 나무베고 묘목심기 좌회전, 좌회전후 나무베고 묘목심기, 우회전후 전진

7 : 두칸 전진

8 : [좌회전후 나무베고 묘목심기 좌회전] 2회, 두칸 전진

9 : 16번 슬롯 선택(상자) 전방이 상자일때 베어온 나무를 상자에 투입 및 아이템 리필 이후 나무 묘목이 있는 방향으로 바라봄. 이후 아래의 블럭 패턴을 체크



5. T_L()


나무를 베는 패턴을 지정해두었습니다.

단순히 아래에 내려가는 길에 몹이나 유저가 막으면 공격을 하게 해두었습니다.



현 소스의 문제점


Startup으로 하기엔 부적합 합니다. 10분만에 만든 소스이다보니,

유저가 길을 막을때 일어나는 현상(커브길에서 다른 길로 가버리겠죠?)을 전혀 고려하지 않았으며.

청크 리로딩시에 일어나는 현상을 고려하지 않았습니다.

멀티서버에 테러 행위가 있을경우 터틀은 경로를 절대 못찾아주겠죠.


본 소스는 그저 아래의 블럭을 비교해서 움직임을 보여주는 시연영상입니다.



복붙용 소스


function T_C()
  -- T_C : Turtle Compare
  local pattern = 0
  for i=8,16 do
    T_S(tonumber(i))
    if turtle.compareDown() then
      pattern = i-7
      break
    end
  end
  return pattern
end

function T_S(num)
  -- T_S : Turtle Select
  turtle.select(tonumber(num))
end

function T_GetItemCount()
  for i=3,7 do
    T_S(i)
    if turtle.getItemCount(i) > 1 then
      turtle.dropDown((turtle.getItemCount(i) - 1))
    end
  end
end

function T_P(pattern)
  -- T_P : Turtle Pattern
  if tonumber(pattern) == 1 then
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 2 then
    turtle.turnRight()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 3 then
    turtle.turnLeft()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 4 then
    turtle.turnRight()
    T_L()
    turtle.turnLeft()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 5 then
    turtle.turnLeft()
    T_L()
    turtle.turnRight()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 6 then
    turtle.turnLeft()
    T_L()
    turtle.turnLeft()
    turtle.turnLeft()
    T_L()
    turtle.turnRight()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 7 then
    T_S(1)
    T_F(1)
    turtle.forward()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 8 then
    turtle.turnLeft()
    T_L()
    turtle.turnLeft()
    turtle.turnLeft()
    T_L()
    turtle.turnLeft()
    T_S(1)
    T_F(1)
    turtle.forward()
    T_S(1)
    T_F(1)
    turtle.forward()
  elseif tonumber(pattern) == 9 then
    T_S(16)
    if turtle.compare() then
      T_GetItemCount()
      turtle.suck()
      turtle.turnLeft()
      turtle.suck()
      turtle.turnLeft()
      turtle.turnLeft()
    end
   
    T_S(3)
    if turtle.compare() then
      T_L()
      turtle.turnRight()
      T_F(1)
      turtle.forward()
    end
  else
    -- error
  end
end

function T_L()
  T_S(3)
  while turtle.compare() do
    turtle.dig()
    turtle.digUp()
    T_S(1)
    T_F(1)
    turtle.up()
    T_S(3)
  end

  while not turtle.detectDown() do
    if not turtle.down() then
      turtle.attackDown()
    end
    T_S(1)
    T_F(1)
  end
  T_S(2)
  turtle.place()
end

function T_F(slot)
  if turtle.getFuelLevel() <= 0 then
    if turtle.getItemCount(slot) > 1 then
      turtle.refuel(slot)
    end
  end
end

while true do
  T_P(T_C())
  os.sleep(0.05)
end


시연 영상