Advertisement
Guest User

run

a guest
Mar 29th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. function move_dir( direction )
  2.  local dir_char = direction:sub(1,1)
  3.  if dir_char == "f" or dir_char == "" then
  4.   return turtle.forward()
  5.  elseif dir_char == "b" then
  6.   return turtle.back()
  7.  elseif dir_char == "u" then
  8.   return turtle.up()
  9.  elseif dir_char == "d" then
  10.   return turtle.down()
  11.  else
  12.   print( "Invalid direction: "..direction )
  13.   return false
  14.  end
  15. end
  16.  
  17. function move( direction )
  18.  if not move_dir( direction ) then
  19.   if turtle.getFuelLevel() == 0 then
  20.    print( "Out of Fuel." )
  21.    turtle.select( fuel_slot )
  22.    if not turtle.refuel( 1 ) then
  23.     print( "Cannot refuel." )
  24.     print( "Waiting for fuel to continue." )
  25.     while not turtle.refuel( 1 ) do
  26.      sleep( 1 )
  27.     end
  28.    end
  29.    print( "Refueled." )
  30.    return move( direction )
  31.   else
  32.    print( "Path obstructed." )
  33.    print( "Waiting to move." )
  34.    while not move_dir( direction ) do
  35.     sleep( 1 )
  36.    end
  37.   end
  38.  end
  39.  return true
  40. end
  41.  
  42. fuel_slot = 1
  43. log_slot = 2
  44. sapling_slot = 3
  45. break_slot = 4
  46.  
  47. turtle.select( sapling_slot )
  48. turtle.suck()
  49. turtle.select( 5 )
  50. turtle.drop()
  51. turtle.turnLeft()
  52. turtle.select( 1 )
  53.  
  54. do_grid_loop = true
  55. while do_grid_loop do
  56.  
  57.  do_row_loop = true
  58.  while do_row_loop do
  59.  
  60.   if turtle.detect() then
  61.  
  62.    print( "Object found." )
  63.    
  64.    turtle.dig()
  65.    move( "f" )
  66.    
  67.    turtle.select( log_slot )
  68.    if turtle.compareUp() then
  69.    
  70.     print( "Tree found." )
  71.     print( "Cutting down." )
  72.    
  73.     while turtle.compareUp() do
  74.      turtle.digUp()
  75.      move( "u" )
  76.      turtle.select( log_slot )
  77.     end
  78.    
  79.     while not turtle.detectDown() do
  80.      move( "d" )
  81.     end
  82.    
  83.    end
  84.  
  85.   else
  86.    
  87.    print( "Nothing found." )
  88.    print( "Skipping." )
  89.    
  90.    move( "f" )
  91.    
  92.   end
  93.  
  94.   move( "f" )
  95.   print( "Replanting sapling." )
  96.  
  97.   turtle.turnRight()
  98.   turtle.turnRight()
  99.  
  100.   turtle.select( sapling_slot )
  101.   turtle.place()
  102.  
  103.   turtle.turnRight()
  104.   turtle.turnRight()
  105.  
  106.   print( "Advancing to either break loop or block." )
  107.   turtle.select( break_slot )
  108.  
  109.   while ( not turtle.compareDown() ) and ( not turtle.detect() ) do
  110.    move( "f" )
  111.    turtle.select( break_slot )
  112.   end
  113.  
  114.   if turtle.compareDown() then
  115.    do_row_loop = false
  116.   end
  117.  end
  118.  
  119.  print( "Break loop found." )
  120.  print( "Returning to start next row." )
  121.  
  122.  turtle.turnLeft()
  123.  move( "f" )
  124.  
  125.  while not turtle.compareDown() do
  126.   move( "f" )
  127.   turtle.select( break_slot )
  128.  end
  129.  
  130.  turtle.turnLeft()
  131.  move( "f" )
  132.  turtle.select( break_slot )
  133.  
  134.  while not turtle.compareDown() do
  135.   move( "f" )
  136.   turtle.select( break_slot )
  137.  end
  138.  
  139.  turtle.turnRight()
  140.  if turtle.detect() then
  141.   do_grid_loop = false
  142.  else
  143.  
  144.   move( "f" )
  145.   turtle.select( break_slot )
  146.  
  147.   while ( not turtle.compareDown() ) or turtle.detect() do
  148.    move( "f" )
  149.    turtle.select( break_slot )
  150.   end
  151.  
  152.   print( "Checking for next row." )
  153.  
  154.   if turtle.detect() then
  155.    do_grid_loop = false
  156.   end
  157.  
  158.  end
  159.  
  160.  turtle.turnRight()
  161. end
  162.  
  163. print( "End of grid." )
  164. print( "Returning to start/chest." )
  165.  
  166. turtle.turnRight()
  167.  
  168. while not turtle.detect() do
  169.  move( "f" )
  170. end
  171.  
  172. turtle.turnRight()
  173.  
  174. print( "Unloading chest." )
  175.  
  176. for slot = 1, 16, 1 do
  177.  if slot ~= fuel_slot and slot ~= log_slot and slot ~= sapling_slot and slot ~= break_slot then
  178.   turtle.select( slot )
  179.   turtle.drop()
  180.  elseif slot == log_slot then
  181.   turtle.select( slot )
  182.   turtle.drop( turtle.getItemCount( slot ) - 1 )
  183.  end
  184. end
  185.  
  186. turtle.turnLeft()
  187.  
  188. print( "Complete!" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement