Advertisement
Greyman27

chop.lua

Apr 15th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. --Version:4/15/2024 21:51
  2.  
  3. os.loadAPI("grey_api/move.lua")
  4. os.loadAPI("grey_api/util.lua")
  5.  
  6. local seconds_to_wait = 60*18
  7. local wait_first = false
  8. local minimum_fuel_level = 100
  9. local log = "minecraft:oak_log"
  10. local leaves = "minecraft:oak_leaves"
  11. local sapling = "minecraft:oak_sapling"
  12. local fuel = "minecraft:charcoal"
  13. local breakable_blocks = {[log]=true,[leaves]=true}
  14. local fuels = {fuel,"minecraft:stick",log}
  15. local log_drop_off_height = 4
  16. local log_drop_off_direction = 2
  17. local fuel_pick_up_height = 0
  18. local fuel_pick_up_direction = 2
  19. local misc_drop_off_height = 4
  20. local misc_drop_off_direction = 1
  21.  
  22. function chop()
  23.     local chopping = true
  24.     local b,block = nil
  25.     while true do
  26.         chopping = true
  27.         b,block = nil
  28.         while chopping do
  29.             --find tree
  30.             while turtle.getFuelLevel()<=minimum_fuel_level and util.fuel(1,fuels) do end
  31.             if turtle.getFuelLevel()<=0 then
  32.                 print("out of fuel")
  33.             elseif turtle.getFuelLevel()<=minimum_fuel_level then
  34.                 print("low on fuel")
  35.             end
  36.             b,block = turtle.inspect()
  37.             while not b do
  38.                 if move.get_y() > 0 then
  39.                     move.go("down")
  40.                 end
  41.                 move.go()
  42.                 b,block = turtle.inspect()
  43.             end
  44.             if b and (breakable_blocks[block["name"]] or block["name"]==sapling)then
  45.                --print("found block")
  46.                 chopping = chop_oak()
  47.             else
  48.                 chopping = false
  49.             end
  50.         end--end while chopping
  51.        
  52.         move.face(2)
  53.         print("replanting saplings")
  54.         b,block = turtle.inspect()
  55.         while not b or breakable_blocks[block["name"]] or block["name"]==sapling do
  56.             --if in place for sapling
  57.             if move.get_x() % 3 == 1 and util.select_item(sapling) then
  58.                 move.face(0)
  59.                 turtle.place()
  60.                 move.face(2)
  61.             end
  62.            
  63.             if b then
  64.                 turtle.dig()
  65.             end
  66.             move.go()
  67.             b,block = turtle.inspect()
  68.         end
  69.         --drop off logs
  70.         print("dropping off logs")
  71.         move.goto(0,log_drop_off_height,0,log_drop_off_direction)
  72.         for i=1,16 do
  73.             if turtle.getItemCount(i)>0 and turtle.getItemDetail(i)["name"]==log then
  74.                 turtle.select(i)
  75.                 turtle.drop()
  76.             end
  77.         end
  78.         --drop off misc
  79.         print("dropping off misc")
  80.         move.goto(0,misc_drop_off_height,0,misc_drop_off_direction)
  81.         for i=1,16 do
  82.             if turtle.getItemCount(i)>0 and turtle.getItemDetail(i)["name"]~=fuel then
  83.                 turtle.select(i)
  84.                 turtle.drop()
  85.             end
  86.         end        
  87.         --pick up fuel
  88.         print("picking up fuel")
  89.         local fuel_needed = 64
  90.         if util.select_item(fuel) then
  91.             fuel_needed = turtle.getItemSpace(turtle.getSelectedSlot())
  92.         end
  93.         if fuel_needed > 0 then
  94.             move.goto(0,fuel_pick_up_height,0,fuel_pick_up_direction)
  95.             turtle.suck(fuel_needed)
  96.         end
  97.         move.return_to_home()            
  98.         --refuel
  99.         while turtle.getFuelLevel()<=minimum_fuel_level and util.fuel(1,fuels) do end
  100.         --wait for trees to grow
  101.         util.wait(seconds_to_wait)
  102.     end--end while true
  103. end
  104.  
  105. function chop_oak()
  106.     local b,block = turtle.inspect()
  107.     --check if sapling
  108.     if b and block["name"]=="minecraft:oak_sapling" then
  109.         --go over sapling
  110.         move.go("up")
  111.         move.go("forward",2)
  112.         move.go("down")
  113.         print("    found sapling")
  114.         return true
  115.     end
  116.    
  117.     local bx=move.get_x()
  118.     local by=move.get_y()
  119.     local bz=move.get_z()
  120.     local bf=move.get_facing()
  121.     print("breaking logs")
  122.     rtn = break_all()
  123.     move.goto(bx,by,bz,bf)
  124.     return rtn
  125. end
  126.  
  127. function break_all()
  128.     --refuel if needed
  129.     while turtle.getFuelLevel() <= minimum_fuel_level and util.fuel(1,fuels) do end
  130.     if turtle.getFuelLevel()<=0 then
  131.         print("Out of fuel")
  132.     elseif turtle.getFuelLevel()<=minimum_fuel_level then
  133.         print("Low on fuel")
  134.     end
  135.    
  136.     local b,block = turtle.inspectDown()
  137.     --break down
  138.     if b and breakable_blocks[block["name"]] then
  139.             turtle.digDown()
  140.             move.go("down")
  141.             break_all()
  142.             move.go("up")
  143.     end
  144.    
  145.     --break backwards
  146.     move.face(2)
  147.     b,block = turtle.inspect()
  148.     if b and breakable_blocks[block["name"]] then
  149.         turtle.dig()
  150.         move.go()
  151.         break_all()
  152.         move.face(0)
  153.         move.go()
  154.     end
  155.    
  156.     --break left
  157.     move.face(3)
  158.     b,block = turtle.inspect()
  159.     if b and breakable_blocks[block["name"]] then
  160.         turtle.dig()
  161.         move.go()
  162.         break_all()
  163.         move.face(1)
  164.         move.go()
  165.     end
  166.    
  167.     --break right
  168.     move.face(1)
  169.     b,block = turtle.inspect()
  170.     if b and breakable_blocks[block["name"]] then
  171.         turtle.dig()
  172.         move.go()
  173.         break_all()
  174.         move.face(3)
  175.         move.go()
  176.     end
  177.    
  178.     --break forward
  179.     move.face(0)
  180.     b,block = turtle.inspect()
  181.     if b and breakable_blocks[block["name"]] then
  182.         turtle.dig()
  183.         move.go()
  184.         break_all()
  185.         move.face(2)
  186.         move.go()
  187.     end
  188.    
  189.     --break up
  190.     b,block = turtle.inspectUp()
  191.     if b and breakable_blocks[block["name"]] then
  192.         turtle.digUp()
  193.         move.go("up")
  194.         break_all()
  195.         move.go("down")
  196.     end
  197.     return true
  198. end
  199.  
  200. if wait_first then util.wait(seconds_to_wait) end
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement