Advertisement
jille_Jr

CC: Tree farm

Oct 30th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. -- Not ready yet!
  2. -- Does not work yet!
  3.  
  4. --(( Variables ))--
  5.  
  6. local cutCount = 0
  7.  
  8. local moves = {
  9.   dig = {
  10.     ["f"] = turtle.dig,
  11.     ["u"] = turtle.digUp,
  12.     ["d"] = turtle.digDown,
  13.   },
  14.   move = {
  15.     ["f"] = turtle.forward,
  16.     ["u"] = turtle.up,
  17.     ["d"] = turtle.down,
  18.   },
  19.   attack = {
  20.     ["f"] = turtle.attack,
  21.     ["u"] = turtle.attackUp,
  22.     ["d"] = turtle.attackDown,
  23.   },
  24. }
  25.  
  26. --(( Functions ))--
  27.  
  28. function go(direction, fromNum, toNum,option)
  29.   while not moves.move[direction]() do
  30.     if fromNum and toNum then
  31.       select(fromNum,toNum,option)
  32.     end
  33.     if not moves.dig[direction]() then
  34.       moves.attack[direction]()
  35.     end
  36.     sleep(.5)
  37.   end
  38. end
  39.  
  40. function select(fromNum, toNum, option)
  41.   if not toNum or fromNum == toNum then
  42.     turtle.select(fromNum)
  43.     return true
  44.   end
  45.   for slot = fromNum, toNum do
  46.     if turtle["getItem"..option]() > 0 then
  47.       turtle.select(slot)
  48.       return true
  49.     end
  50.   end
  51.   return false
  52. end
  53.  
  54. function cutTree()
  55.   if turtle.getItemSpace(1) > 0 then
  56.     turtle.select(1)
  57.   else
  58.    if not select(5,16,"Space") then
  59.      turtle.select(1)
  60.      return false
  61.    end
  62.   end
  63.   go("f",5,16,"Space")
  64.   turtle.select(1)
  65.   while turtle.compareUp() do
  66.     if not select(5,16,"Space") then
  67.       turtle.select(1)
  68.       return false
  69.     end
  70.     go("u",5,16,"Space")
  71.   end
  72.   while not turtle.detectDown() do
  73.     go("d",5,16,"Space")
  74.   end
  75.   if not turtle.back() then
  76.     for turns = 1, 2 do
  77.       turtle.turnLeft()
  78.     end
  79.     go("f",5,16,"Space")
  80.     for turns = 1, 2 do
  81.       turtle.turnRight()
  82.     end
  83.   end
  84.   return true
  85. end
  86.  
  87. --(( Main program ))--
  88.  
  89. if not fs.exists("."..shell.getRunningProgram().."Data") then
  90.   file = fs.open("."..shell.getRunningProgram().."Data","w")
  91.   file.write(cutCount)
  92.   file.close()
  93. else
  94.   file = fs.open("."..shell.getRunningProgram().."Data","r")
  95.   cutCount = tonumber(file.readLine())
  96.   file.close()
  97. end
  98.  
  99. term.setCursorPos(1,1)
  100. term.clear()
  101.  
  102. while true do
  103.   if turtle.getItemCount(1) < 1 then
  104.     break
  105.   end
  106.   term.setCursorPos(1,1)
  107.   term.clear()
  108.   print("Remove the log to stop!")
  109.   print()
  110.   print("Have log in slot 1!")
  111.   print("Have saplings in slot 2-4!")
  112.   print()
  113.   print("Trees cut: "..cutCount)
  114.   print()
  115.  
  116.   if not turtle.detect() then
  117.     if not select(2,4,"Count") then
  118.       print("Out of saplings!")
  119.       break
  120.     end
  121.     while not turtle.place() do
  122.       moves.attack["f"]()
  123.       sleep(.5)
  124.     end
  125.   end
  126.  
  127.   turtle.select(1)
  128.   if turtle.compare() then
  129.     if chopTree() then
  130.       cutCount = cutCount + 1
  131.       file = fs.open("."..shell.getRunningProgram().."Data","w")
  132.       file.write(cutCount)
  133.       file.close()
  134.     else
  135.       print("Failed to chop tree!")
  136.       break
  137.     end
  138.   end
  139.  
  140.   sleep(.5)
  141. end
  142.  
  143. turtle.select(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement