WarPie90

Turtle lumberjack

Mar 25th, 2023 (edited)
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.62 KB | None | 0 0
  1. --==================================================--
  2. -- user setup:
  3.  
  4. -- less fuel than this? EAT UP!!!!
  5. EAT_FUEL_AT_LT = 80*64*2
  6.  
  7. -- min fuel level before return to sender
  8. MIN_FUEL_RETURN = 20
  9.  
  10. -- junk stuff (only partial match required)
  11. DROP_LIST = {}
  12. TREE_LIST = {'_log'}
  13.  
  14.  
  15. --==================================================--
  16. -- lumberjack constants
  17. FACE_N = 0
  18. FACE_E = 1
  19. FACE_S = 2
  20. FACE_W = 3
  21.  
  22. lumberjack        = {} -- the turtle
  23. lumberjack['direction'] = 0
  24. lumberjack['x']   = 0
  25. lumberjack['y']   = 0
  26. lumberjack['z']   = 0
  27.  
  28. lumberjack['start_x'], lumberjack['start_y'], lumberjack['start_z'] = lumberjack['x'], lumberjack['y'], lumberjack['z']
  29. lumberjack['start_direction'] = lumberjack['direction']
  30.  
  31.  
  32. --==================================================--
  33.  
  34.  
  35. function split(str, sep)
  36.   local res = {}
  37.   for x in string.gmatch(str,sep) do
  38.     table.insert(res, x)
  39.   end
  40.   return res
  41. end
  42.  
  43. function partialMatchAny(val, list)
  44.   for index, value in ipairs(list) do
  45.     if string.find(val, value) then
  46.       return true
  47.     end
  48.   end
  49.  
  50.   return false
  51. end
  52.  
  53. function matchAny(tab, val)
  54.   for index, value in ipairs(tab) do
  55.     if val == value then
  56.       return true
  57.     end
  58.   end
  59.  
  60.   return false
  61. end
  62.  
  63.  
  64. --==================================================--
  65.  
  66. function lumberjack.checkRefuel(minimum)
  67.   if turtle.getFuelLevel() > minimum then
  68.     return nil
  69.   end
  70.  
  71.   for i = 1, 16 do
  72.     turtle.select(i)
  73.    
  74.     -- if it's valid fuel:
  75.     if turtle.refuel(0) then
  76.       for j = 1, turtle.getItemCount(i) do
  77.         turtle.refuel(1)  -- do this one by one, some fuels are extreme
  78.         if turtle.getFuelLevel() > minimum then
  79.           return nil
  80.         end
  81.       end
  82.     end
  83.   end
  84. end
  85.  
  86. function lumberjack.updateHz(n)
  87.   n = n or 1
  88.   if     lumberjack['direction'] == FACE_N then lumberjack['y'] = lumberjack['y'] - n
  89.   elseif lumberjack['direction'] == FACE_S then lumberjack['y'] = lumberjack['y'] + n
  90.   elseif lumberjack['direction'] == FACE_W then lumberjack['x'] = lumberjack['x'] - n
  91.   elseif lumberjack['direction'] == FACE_E then lumberjack['x'] = lumberjack['x'] + n
  92.   end
  93. end
  94.  
  95. function lumberjack.turnRight(n)
  96.   n = n or 1
  97.   for i=1,n do
  98.     turtle.turnRight()
  99.     lumberjack['direction'] = (lumberjack['direction'] + 1) % 4
  100.   end
  101. end
  102.  
  103. function lumberjack.turnLeft(n)
  104.   n = n or 1
  105.   for i=1,n do
  106.     turtle.turnLeft()
  107.     lumberjack['direction'] = (lumberjack['direction'] - 1) % 4
  108.   end
  109. end
  110.  
  111. function lumberjack.forward(n)
  112.   n = n or 1
  113.   for i=1,n do
  114.     if not(turtle.forward()) then return false end
  115.     lumberjack.updateHz(1)
  116.   end
  117.   return true
  118. end
  119.  
  120. function lumberjack.back(n)
  121.   n = n or 1
  122.   for i=1,n do
  123.     if not turtle.back() then return false end
  124.     lumberjack.updateHz(-1)
  125.   end
  126.   return true
  127. end
  128.  
  129. function lumberjack.up(n)
  130.   n = n or 1
  131.   for i=1,n do
  132.     if not turtle.up() then return false end
  133.     lumberjack['z'] = lumberjack['z'] + 1
  134.   end
  135.   return true
  136. end
  137.  
  138. function lumberjack.down(n)
  139.   n = n or 1
  140.   for i=1,n do
  141.     if not turtle.down() then return false end
  142.     lumberjack['z'] = lumberjack['z'] - 1
  143.   end
  144.   return true
  145. end
  146.  
  147.  
  148. function lumberjack.digForward(n)
  149.   n = n or 1
  150.   for i=1,n do
  151.     if turtle.getFuelLevel() < MIN_FUEL_RETURN then
  152.       lumberjack.checkRefuel(EAT_FUEL_AT_LT)
  153.       turtle.select(1)
  154.      
  155.       if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
  156.         return false
  157.       end
  158.     end
  159.    
  160.     if (not lumberjack.forward()) then
  161.       -- this needs to be looped in case of airdrops (sand, gravel etc)
  162.       -- also to wait for despawn?
  163.       while turtle.detect() do
  164.         turtle.dig()
  165.         os.sleep(0.1)
  166.       end
  167.      
  168.       while not lumberjack.forward()  do
  169.         turtle.attack()
  170.       end
  171.     end
  172.   end
  173.  
  174.   return true
  175. end
  176.  
  177. function lumberjack.digDown(n)
  178.   n = n or 1
  179.   for i=1,n do
  180.     if turtle.getFuelLevel() < MIN_FUEL_RETURN then
  181.       lumberjack.checkRefuel(EAT_FUEL_AT_LT)
  182.       turtle.select(1)
  183.      
  184.       if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
  185.         return false
  186.       end
  187.     end
  188.  
  189.     if (not lumberjack.down()) then
  190.       -- this needs to be looped in case of airdrops (sand, gravel etc)
  191.       -- also to wait for despawn?
  192.       while turtle.detectDown() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
  193.         turtle.digDown()
  194.         os.sleep(0.1)
  195.       end
  196.      
  197.       while not lumberjack.down() and (turtle.getFuelLevel() > MIN_FUEL_RETURN)  do
  198.         turtle.attackDown()
  199.       end
  200.     end
  201.   end
  202.  
  203.   return true
  204. end
  205.  
  206. function lumberjack.digUp(n)
  207.   n = n or 1
  208.   for i=1,n do
  209.     if turtle.getFuelLevel() < MIN_FUEL_RETURN then
  210.       lumberjack.checkRefuel(EAT_FUEL_AT_LT)
  211.       turtle.select(1)
  212.      
  213.       if (turtle.getFuelLevel() < MIN_FUEL_RETURN) then
  214.         return false
  215.       end
  216.     end
  217.    
  218.     if (not lumberjack.up()) then
  219.       -- this needs to be looped in case of airdrops (sand, gravel etc)
  220.       -- also to wait for despawn?
  221.       while turtle.detectUp() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
  222.         turtle.digUp()
  223.         os.sleep(0.1)
  224.       end
  225.      
  226.       while not lumberjack.up() and (turtle.getFuelLevel() > MIN_FUEL_RETURN) do
  227.         turtle.attackUp()
  228.       end
  229.     end
  230.   end
  231.  
  232.   return true
  233. end
  234.  
  235. function lumberjack.face(direction)
  236.   -- local diff = math.abs(lumberjack['direction'] - direction)
  237.   while not(lumberjack['direction'] == direction) do
  238.     lumberjack.turnRight()
  239.   end
  240. end
  241.  
  242. function lumberjack.inventoryFull()
  243.   local usedSlots = 0
  244.   for i = 1,16 do
  245.     if turtle.getItemCount(i) > 0 then
  246.       usedSlots = usedSlots + 1
  247.     end
  248.   end
  249.  
  250.   -- if all slots are used and of 'em is filled, then return `true`
  251.   if usedSlots >= 15 then
  252.     return true
  253.   end
  254.   return false
  255. end
  256.  
  257. function lumberjack.findChest()
  258.   local success, data = false, ''
  259.  
  260.   for i=0,3 do
  261.     lumberjack.face(i)
  262.     success, data = turtle.inspect()
  263.  
  264.     if success and string.find(data.name, "chest") then
  265.       return true
  266.     end
  267.   end
  268.  
  269.   return false
  270. end
  271.  
  272. function lumberjack.depositAll()
  273.   local success, data = false, ''
  274.   local hasChest = false
  275.  
  276.   for i=0,3 do
  277.     lumberjack.face(i)
  278.     success, data = turtle.inspect()
  279.  
  280.     if success and string.find(data.name, "chest") then
  281.       hasChest = true
  282.       break
  283.     end
  284.   end
  285.  
  286.   if (hasChest) then
  287.     for i = 1,16 do
  288.       if turtle.getItemCount(i) > 0 then
  289.         turtle.select(i)
  290.         turtle.drop()
  291.       end
  292.     end
  293.   end
  294.  
  295.   return hasChest
  296. end
  297.  
  298. function lumberjack.digTo(x,y,z)
  299.   x = tonumber(x or lumberjack['x'])
  300.   y = tonumber(z or lumberjack['y'])
  301.   z = tonumber(y or lumberjack['z'])
  302.  
  303.   -- digging x-axis (forward / backward)
  304.   if x < lumberjack['x'] then
  305.     lumberjack.face(FACE_W)
  306.   elseif x > lumberjack['x'] then
  307.     lumberjack.face(FACE_E)
  308.   end
  309.   if not(x == lumberjack['x']) then
  310.     lumberjack.digForward(math.abs(x-lumberjack['x']))
  311.   end
  312.  
  313.   -- digging y-axis (forward / backward)
  314.   if y < lumberjack['y'] then
  315.     lumberjack.face(FACE_N)
  316.   elseif y > lumberjack['y'] then
  317.     lumberjack.face(FACE_S)
  318.   end
  319.   if not(y == lumberjack['y']) then
  320.     lumberjack.digForward(math.abs(y-lumberjack['y']))
  321.   end
  322.  
  323.   -- digging up/down
  324.   if z < lumberjack['z'] then
  325.     lumberjack.digDown(math.abs(z-lumberjack['z']))
  326.   elseif z > lumberjack['z'] then
  327.     lumberjack.digUp(math.abs(z-lumberjack['z']))
  328.   end
  329. end
  330.  
  331.  
  332. function lumberjack.reset(x, y, z)
  333.   x = tonumber(x or lumberjack['start_x'])
  334.   y = tonumber(z or lumberjack['start_y'])
  335.   z = tonumber(y or lumberjack['start_z'])
  336.   lumberjack.digTo(x,y,z)
  337. end
  338.  
  339. -- drop all unwanted ores
  340. function lumberjack.dropJunk()
  341.   for i = 1,16 do
  342.     if (turtle.getItemCount(i) > 0) and (partialMatchAny(turtle.getItemDetail(i).name, DROP_LIST)) then
  343.       turtle.select(i)
  344.       turtle.drop()
  345.     end
  346.   end
  347. end
  348.  
  349.  
  350. --====| Program | Free reign lumberjack |====--
  351.  
  352.  
  353.  
  354. -- no fuel? Go back and wait for refuel
  355. function refuel()
  356.   print('Fuel level is low, waiting for refuel')
  357.  
  358.   -- wait for insertion
  359.   while (turtle.getFuelLevel() <= MIN_FUEL_RETURN) do
  360.     lumberjack.checkRefuel(EAT_FUEL_AT_LT)
  361.     os.sleep(1)
  362.   end
  363. end
  364.  
  365.  
  366. function scanForTree360()
  367.   local success, data = false, ''
  368.   print('>>> Scanning for tree')
  369.  
  370.   for i=0,3 do
  371.     lumberjack.face(i)
  372.     success, data = turtle.inspect()
  373.    
  374.     if success and (partialMatchAny(data.name, TREE_LIST)) then
  375.       return true
  376.     end
  377.   end
  378.   return false
  379. end
  380.  
  381. function blockedByTree()
  382.   print('>>> Scanning for tree')
  383.  
  384.   local success, data = turtle.inspect()
  385.   if success and (partialMatchAny(data.name, TREE_LIST)) then
  386.     return true
  387.   end
  388.  
  389.   return false
  390. end
  391.  
  392. function plantSapling()
  393.   for i = 1,16 do
  394.     if (turtle.getItemCount(i) > 0) and (partialMatchAny(turtle.getItemDetail(i).name, {'_sapling'})) then
  395.       turtle.select(i)
  396.       turtle.place()
  397.     end
  398.   end
  399. end
  400.  
  401. function chopchop()
  402.   turtle.select(1)
  403.   tmp = lumberjack['direction']
  404.   lumberjack.digForward()
  405.    
  406.   while turtle.detectUp() do
  407.     lumberjack.digUp()
  408.     turtle.suck() -- get any saplings
  409.     os.sleep(0.1)
  410.   end
  411.  
  412.   -- return down
  413.   lumberjack.face((lumberjack['direction']+2) % 4) --180 degrees
  414.   lumberjack.digForward(1)
  415.   lumberjack.digDown(lumberjack['z'])
  416.   turtle.suck() -- get any saplings
  417.  
  418.   -- continue
  419.   lumberjack.face(tmp)
  420. end
  421.  
  422.  
  423. function main()
  424.   math.randomseed(os.time())
  425.  
  426.   lumberjack.face(math.random(0,3))
  427.  
  428.  
  429.   while true do
  430.     if (turtle.getFuelLevel() <= MIN_FUEL_RETURN) then
  431.       refuel()
  432.     end
  433.      
  434.     local i = 0
  435.     while lumberjack.forward() do
  436.       if (turtle.getFuelLevel() <= MIN_FUEL_RETURN) then
  437.         refuel()
  438.       end
  439.      
  440.       if i % 2 == 0 then  -- we dont have to suck all the time, as we shove the saplings
  441.         turtle.suck() -- get any saplings
  442.       end
  443.      
  444.       -- randomly change direction
  445.       if math.random(0,10) == 0 then
  446.         break
  447.       end
  448.      
  449.       i = i + 1
  450.     end
  451.    
  452.     if blockedByTree() then
  453.       print('Chop chop!')
  454.       chopchop()
  455.       plantSapling()
  456.     else
  457.       print('Setting new direction!')
  458.       lumberjack.face(math.random(0,3))
  459.     end
  460.    
  461.     if lumberjack.inventoryFull() then
  462.       print('Full inventory')
  463.       while lumberjack.inventoryFull() do
  464.         os.sleep(10)
  465.       end
  466.     end
  467.   end
  468. end
  469.  
  470. -- verify that we start off with a chest near by
  471. function hasChest()
  472.   if not lumberjack.findChest() then
  473.     print('Failed to recognize chest!')
  474.     lumberjack.face(lumberjack['start_direction'])
  475.     return false
  476.   end
  477.  
  478.   lumberjack.face(lumberjack['start_direction'])
  479.   return true
  480. end
  481.  
  482. main(area)
Add Comment
Please, Sign In to add comment