jille_Jr

CC: Treefarm - working

Nov 16th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.23 KB | None | 0 0
  1. local x,y,z = 0,0,0 -- starting position
  2. -- ^ dont change unless you know what you're doing ^
  3. local dir = "n" -- starting direction, example: n for north
  4. local dirs = {"w","n","e","s"}
  5. local saplingslot = 1
  6. local dirtslot = 2
  7.  
  8. local trees = {
  9.     -- positions are relative to the starting position.
  10.     -- does not use gps
  11.     -- example:
  12.     -- {x=-2,y=-1,z=-4,dir="n",real={x=-2,y=-1,z=-5}},
  13.     -- x,y,z is where to stand when chopping it
  14.     -- dir is direction the tree is in realtion to the position ^
  15.     -- real x,y,z is the position of the sapling
  16. }
  17.  
  18. local function checkfuel()
  19.     local fuel = turtle.getFuelLevel()
  20.     if fuel >= 5 then return end
  21.     print("looking for fuel")
  22.     while fuel < 5 do
  23.         for slot = 1,16 do
  24.             if turtle.getItemCount(slot) > 0 then
  25.                 turtle.select(slot)
  26.                 turtle.refuel(1)
  27.             end
  28.         end
  29.         fuel = turtle.getFuelLevel()
  30.     end
  31.     turtle.select(1)
  32.     print("refueled")
  33. end
  34.  
  35. local function forcemove(func,dig)
  36.     local state = false
  37.     repeat
  38.         checkfuel()
  39.         if turtle[func] == nil then error("Cant find function \""..func.."\"") end
  40.         state = turtle[func]()
  41.         if dig then
  42.             if func=="up" or func=="down"
  43.             or func=="forward" then
  44.                 local face = func
  45.                 if face=="forward" then face="" end
  46.                 face=func:gsub("^%l", string.upper)
  47.                 local tmpfunc=func:gsub("^%l", string.upper)
  48.                 if turtle["detect"..face] == true then
  49.                     forcemove("dig"..tmpfunc)
  50.                 end
  51.             end
  52.         end
  53.     until state
  54. end
  55.  
  56. local function forward()
  57.     if dir=="n" then
  58.         z=z-1
  59.     elseif dir=="e" then
  60.         x=x+1
  61.     elseif dir=="s" then
  62.         z=z+1
  63.     elseif dir=="w" then
  64.         x=x-1
  65.     end
  66. end
  67.  
  68. local function back()
  69.     if dir=="n" then
  70.         z=z-1
  71.     elseif dir=="e" then
  72.         x=x-1
  73.     elseif dir=="s" then
  74.         z=z+1
  75.     elseif dir=="w" then
  76.         x=x+1
  77.     end
  78. end
  79.  
  80. local function turnclockwise()
  81.     local dirid = 0
  82.     for id,face in ipairs(dirs) do
  83.         if face==dir then dirid=id break end
  84.     end
  85.     dirid=dirid+1
  86.     if dirid > #dirs then dirid = 1 end
  87.     dir=dirs[dirid] or error("Unable to calculate direction")
  88. end
  89.  
  90. local function turncounterclockwise()
  91.     local dirid = 0
  92.     for id,face in ipairs(dirs) do
  93.         if face==dir then dirid=id break end
  94.     end
  95.     dirid = dirid - 1
  96.     if dirid < 1 then dirid = #dirs end
  97.     dir=dirs[dirid] or error("Unable to calculate direction")
  98. end
  99.  
  100. local go = {}
  101. go.u=function(dig)
  102.     forcemove("up",dig)
  103.     y=y+1
  104. end
  105. go.d=function(dig)
  106.     forcemove("down",dig)
  107.     y=y-1
  108. end
  109. go.f=function(dig)
  110.     forcemove("forward",dig)
  111.     forward()
  112. end
  113. go.b=function()
  114.     forcemove("back")
  115.     back()
  116. end
  117. go.rt=function()
  118.     turtle.turnRight()
  119.     turnclockwise()
  120. end
  121. go.lt=function()
  122.     turtle.turnLeft()
  123.     turncounterclockwise()
  124. end
  125. go.turn=function(face)
  126.     local dirid = 0
  127.     for id,face in ipairs(dirs) do
  128.         if face==dir then dirid=id break end
  129.     end
  130.     local faceid = 0
  131.     for id,vface in ipairs(dirs) do
  132.         if vface==face then faceid=id break end
  133.     end
  134.    
  135.     dirid = dirid - 1
  136.     if dirid < 1 then dirid = #dirs end
  137.     if dirid == faceid then go.lt() end
  138.    
  139.     while face ~= dir do
  140.         go.rt()
  141.     end
  142. end
  143.  
  144. local function init()
  145.     print("expecting saplings in slot "..saplingslot)
  146.     print("and dirt in slot "..dirtslot)
  147.     print("press any button to continue")
  148.     repeat
  149.         os.pullEvent("key")
  150.     until turtle.getItemCount(saplingslot) >= tset.xcount*tset.zcount
  151.     and turtle.getItemCount(dirtslot) >= tset.xcount*tset.zcount
  152. end
  153.  
  154. local function chop()
  155.     if not turtle.detect() then
  156.         go.f()
  157.         if turtle.detectDown() then
  158.             repeat until turtle.digDown()
  159.         end
  160.         return
  161.     end
  162.     turtle.select(saplingslot)
  163.     if turtle.compare() then
  164.         forcemove("dig")
  165.         go.f()
  166.         forcemove("digDown")
  167.         return
  168.     elseif turtle.detect() then
  169.         forcemove("dig")
  170.         go.f()
  171.         local height = 0
  172.         while turtle.detectUp() do
  173.             forcemove("digUp")
  174.             go.u()
  175.             height = height + 1
  176.         end
  177.         for count = 1,height do
  178.             go.d()
  179.         end
  180.     end
  181.     if turtle.detectDown() then
  182.         forcemove("digDown")
  183.     end
  184. end
  185.  
  186. local function goto(xpos,ypos,zpos,newdir)
  187.     while xpos~=x
  188.     or ypos~=y
  189.     or zpos~=z do
  190.         -- vertical
  191.         for count=1,y-ypos do
  192.             if turtle.detectDown() then break end
  193.             go.d(true)
  194.         end
  195.         for count=1,ypos-y do
  196.             if turtle.detectUp() then break end
  197.             go.u(true)
  198.         end
  199.        
  200.         -- x
  201.         for count=1,x-xpos do
  202.             go.turn("w")
  203.             if turtle.detect() then
  204.                 go.rt() go.f() go.lt()
  205.             else
  206.                 go.f()
  207.             end
  208.         end
  209.         for count=1,xpos-x do
  210.             go.turn("e")
  211.             if turtle.detect() then
  212.                 go.lt() go.f() go.rt()
  213.             else
  214.                 go.f()
  215.             end
  216.         end
  217.        
  218.         -- z
  219.         for count=1,z-zpos do
  220.             go.turn("n")
  221.             if turtle.detect() then
  222.                 go.rt() go.f() go.lt()
  223.             else
  224.                 go.f()
  225.             end
  226.         end
  227.         for count=1,zpos-z do
  228.             go.turn("s")
  229.             if turtle.detect() then
  230.                 go.lt() go.f() go.rt()
  231.             else
  232.                 go.f()
  233.             end
  234.         end
  235.     end
  236.     if newdir then
  237.     go.turn(newdir)
  238.     end
  239. end
  240.  
  241. local function gohome()
  242.     goto(0,0,0,"n")
  243. end
  244.  
  245. local function collect()
  246.     goto(-4,-5,-3,"e")
  247.     repeat until not turtle.suckDown()
  248.     for count=1,tset.xcount*tset.xspace do
  249.         go.f()
  250.         repeat until not turtle.suckDown()
  251.     end
  252.     gohome()
  253. end
  254.  
  255. local function replant()
  256.     print("replanting")
  257.     for id,tree in pairs(trees) do
  258.         local real = tree.real
  259.         goto(tree.x,tree.y,tree.z,tree.dir)
  260.         if not turtle.detect() then
  261.             goto(real.x,real.y,real.z)
  262.             if not turtle.detectDown() then
  263.                 repeat
  264.                     turtle.select(dirtslot)
  265.                 until turtle.placeDown()
  266.             end
  267.             go.u()
  268.             repeat
  269.                 turtle.select(saplingslot)
  270.             until turtle.placeDown()
  271.         end
  272.     end
  273. end
  274.  
  275. local function harvest()
  276.     for id,tree in ipairs(trees) do
  277.         print("moving to x"..tree.x.." y"..tree.y.." z"..tree.z)
  278.         goto(tree.x,tree.y,tree.z,tree.dir)
  279.         print("chopping tree")
  280.         chop()
  281.     end
  282.     gohome()
  283. end
  284.  
  285. local function usage()
  286.     print("Usage: "..shell.getRunningProgram().." <task>")
  287.     print("Tasks avavible: harvest, replant, collect, cycle")
  288. end
  289.  
  290. local args = {...}
  291. local task = args[1]
  292.  
  293. if task == nil then
  294.     usage()
  295. elseif task == "harvest" then
  296.     harvest()
  297.     gohome()
  298.     print("all done.")
  299. elseif task == "replant" then
  300.     init()
  301.     replant()
  302.     gohome()
  303.     print("all done.")
  304. elseif task == "collect" then
  305.     collect()
  306.     print("all done.")
  307. elseif task == "cycle" then
  308.     init()
  309.     harvest()
  310.     for i=1,3 do
  311.         collect()
  312.         print("waiting 1min for leaves to despawn")
  313.         sleep(60)
  314.         print("collecting drops")
  315.         collect()
  316.     end
  317.     replant()
  318.     gohome()
  319.     print("all done.")
  320. end
Advertisement
Add Comment
Please, Sign In to add comment