jille_Jr icon

CC: Tree farm

jille_Jr | PRO | 10/31/12 01:53:09 AM UTC | 0 ⭐ | 451 👁️ | Never ⏰ | []
Lua |

2.8 KB

|

None

|

0 👍

/

0 👎

-- Not ready yet!
-- Does not work yet!
 
--(( Variables ))--
 
local cutCount = 0
 
local moves = {
  dig = {
    ["f"] = turtle.dig,
    ["u"] = turtle.digUp,
    ["d"] = turtle.digDown,
  },
  move = {
    ["f"] = turtle.forward,
    ["u"] = turtle.up,
    ["d"] = turtle.down,
  },
  attack = {
    ["f"] = turtle.attack,
    ["u"] = turtle.attackUp,
    ["d"] = turtle.attackDown,
  },
}
 
--(( Functions ))--
 
function go(direction, fromNum, toNum,option)
  while not moves.move[direction]() do
    if fromNum and toNum then
      select(fromNum,toNum,option)
    end
    if not moves.dig[direction]() then
      moves.attack[direction]()
    end
    sleep(.5)
  end
end
 
function select(fromNum, toNum, option)
  if not toNum or fromNum == toNum then
    turtle.select(fromNum)
    return true
  end
  for slot = fromNum, toNum do
    if turtle["getItem"..option]() > 0 then
      turtle.select(slot)
      return true
    end
  end
  return false
end
 
function cutTree()
  if turtle.getItemSpace(1) > 0 then
    turtle.select(1)
  else
   if not select(5,16,"Space") then
     turtle.select(1)
     return false
   end
  end
  go("f",5,16,"Space")
  turtle.select(1)
  while turtle.compareUp() do
    if not select(5,16,"Space") then
      turtle.select(1)
      return false
    end
    go("u",5,16,"Space")
  end
  while not turtle.detectDown() do
    go("d",5,16,"Space")
  end
  if not turtle.back() then
    for turns = 1, 2 do
      turtle.turnLeft()
    end
    go("f",5,16,"Space")
    for turns = 1, 2 do
      turtle.turnRight()
    end
  end
  return true
end
 
--(( Main program ))--
 
if not fs.exists("."..shell.getRunningProgram().."Data") then
  file = fs.open("."..shell.getRunningProgram().."Data","w")
  file.write(cutCount)
  file.close()
else
  file = fs.open("."..shell.getRunningProgram().."Data","r")
  cutCount = tonumber(file.readLine())
  file.close()
end
 
term.setCursorPos(1,1)
term.clear()
 
while true do
  if turtle.getItemCount(1) < 1 then
    break
  end
  term.setCursorPos(1,1)
  term.clear()
  print("Remove the log to stop!")
  print()
  print("Have log in slot 1!")
  print("Have saplings in slot 2-4!")
  print()
  print("Trees cut: "..cutCount)
  print()
  
  if not turtle.detect() then
    if not select(2,4,"Count") then
      print("Out of saplings!")
      break
    end
    while not turtle.place() do
      moves.attack["f"]()
      sleep(.5)
    end
  end
  
  turtle.select(1)
  if turtle.compare() then
    if chopTree() then
      cutCount = cutCount + 1
      file = fs.open("."..shell.getRunningProgram().."Data","w")
      file.write(cutCount)
      file.close()
    else
      print("Failed to chop tree!")
      break
    end
  end
  
  sleep(.5)
end
 
turtle.select(1)

Comments