Advertisement
Necrotico

CC My Excavate

Sep 10th, 2021 (edited)
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.13 KB | None | 0 0
  1. -- My Excavate
  2.  
  3. -- DESCRIPTION:
  4. --  This program will function in a similar manner to the default excavate program, but a little bit more efficiently.
  5.  
  6. --  When its inventory is full, it will attempt to place its items in chests behind and above where it started, attempting to place items in the next chest up
  7. --  if the last one didn't accept one or more items. If it runs out of chests to place into and it still has items, those items will be discarded and when
  8. --  its inventory is empty (aside from coal in slot 1), it will return to where it left off and continue.
  9.  
  10. -- When it is out of coal (don't use any weaker kind of fuel, please), it will attempt to pull more from a chest to the left of where it started. Because it is assumed
  11. -- that you are using coal for fuel and a relatively low size argument
  12.  
  13. -- USAGE: myExcavate range [useBlacklist]
  14. -- The range argument is self-explanatory.
  15. -- The useBlacklist argument is optional, and if defined, item names defined in the blacklist (see "BLACKLIST ITEMS HERE" in the code below) will be discarded by the
  16. -- turtle's inventory when its inventory is full or before it returns to deposit collected items.
  17.  
  18. -- pastebin get WgESfSf1 myExcavate
  19. -- myExcavate 5 1
  20.  
  21. local argv = {...}
  22.  
  23. local item_blacklist = {}
  24. local function bl_add(item_name)
  25.     item_blacklist[item_name] = true
  26. end
  27.  
  28. -- BLACKLIST ITEMS HERE
  29.  
  30. bl_add("minecraft:cobblestone")
  31.  
  32. -----------------------
  33.  
  34. local size = tonumber(argv[1])
  35. local useBlacklist = false
  36. if argv[2] then
  37.     useBlacklist = true
  38. end
  39.  
  40. local target_x = 1
  41. local target_z = size
  42. local target_y = -2
  43.  
  44. local curr_x = 1
  45. local curr_z = size
  46. local curr_y = 0
  47.  
  48. local x_direction = true -- true = +x, false = -x
  49. local z_direction = false -- true = +z, false = -z
  50.  
  51. local FACE_AXIS_ARRAY = {"+z", "+x", "-z", "-x"}
  52. local FACE_AXIS_MAP = {}
  53. for f=1, 4 do
  54.     FACE_AXIS_MAP[FACE_AXIS_ARRAY[f]] = f
  55. end
  56. local facing = 2
  57.  
  58. local function check_fuel()
  59.     if turtle.getFuelLevel() < 80 then
  60.         turtle.select(1)
  61.         turtle.refuel(1)
  62.     end
  63. end
  64.  
  65. local function turn_towards(face)
  66.     local face_index = FACE_AXIS_MAP[face]
  67.     while not (facing == face_index) do
  68.         local diff = facing - face_index
  69.         if (diff == 1) or (diff == -3) then
  70.             turtle.turnLeft()
  71.             facing = facing - 1
  72.         else
  73.             turtle.turnRight()
  74.             facing = facing + 1
  75.         end
  76.         if facing > 4 then
  77.             facing = 1
  78.         elseif facing < 1 then
  79.             facing = 4
  80.         end
  81.     end
  82. end
  83.  
  84. local function move_by(dist)
  85.     while dist > 0 do
  86.         check_fuel()
  87.         if turtle.detect() then
  88.             turtle.dig()
  89.         end
  90.         if turtle.forward() then
  91.             dist = dist - 1
  92.             if facing == 1 then
  93.                 curr_z = curr_z + 1
  94.             elseif facing == 2 then
  95.                 curr_x = curr_x + 1
  96.             elseif facing == 3 then
  97.                 curr_z = curr_z - 1
  98.             elseif facing == 4 then
  99.                 curr_x = curr_x - 1
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. local function discard_blacklist()
  106.     if not useBlacklist then
  107.         return
  108.     end
  109.     for s=2, 16 do
  110.         local data = turtle.getItemDetail(s)
  111.         if data and item_blacklist[data.name] then
  112.             turtle.select(s)
  113.             turtle.drop()
  114.         end
  115.     end
  116.     turtle.select(1)
  117. end
  118.  
  119. local function inventory_full()
  120.     for s=2, 16 do
  121.         if turtle.getItemCount(s) == 0 then
  122.             return false
  123.         end
  124.     end
  125.     return true
  126. end
  127.  
  128. local function navigate_to_pos(x, y, z)
  129.     if curr_x < x then
  130.         turn_towards("+x")
  131.         move_by(x - curr_x)
  132.     elseif curr_x > x then
  133.         turn_towards("-x")
  134.         move_by(curr_x - x)
  135.     end
  136.     if curr_z < z then
  137.         turn_towards("+z")
  138.         move_by(z - curr_z)
  139.     elseif curr_z > z then
  140.         turn_towards("-z")
  141.         move_by(curr_z - z)
  142.     end
  143.     while curr_y > y do
  144.         if turtle.detectDown() then
  145.             turtle.digDown()
  146.         end
  147.         check_fuel()
  148.         if turtle.down() then
  149.             curr_y = curr_y - 1
  150.         end
  151.     end
  152.     while curr_y < y do
  153.         if turtle.detectUp() then
  154.             turtle.digUp()
  155.         end
  156.         check_fuel()
  157.         if turtle.up() then
  158.             curr_y = curr_y + 1
  159.         end
  160.     end
  161. end
  162.  
  163. local function navigate_to_start()
  164.     navigate_to_pos(1, 0, size)
  165. end
  166.  
  167. local function deposit_items()
  168.     discard_blacklist()
  169.     navigate_to_start()
  170.     turn_towards("-x")
  171.     local deposit_failed = true
  172.     while deposit_failed do
  173.         deposit_failed = false
  174.         for s=2, 16 do
  175.             if turtle.getItemCount(s) > 0 then
  176.                 turtle.select(s)
  177.                 if not turtle.drop() then
  178.                     deposit_failed = true
  179.                 end
  180.             end
  181.         end
  182.         if deposit_failed then
  183.             if turtle.detectUp() then
  184.                 turtle.digUp()
  185.             end
  186.             curr_y = curr_y + 1
  187.             turtle.up()
  188.         end
  189.     end
  190. end
  191.  
  192. local state = "mining" -- "mining" or "returning"
  193. local min_y = 0
  194.  
  195. while not (turtle.detectDown() and (not turtle.digDown())) do
  196.     min_y = min_y - 1
  197.     curr_y = curr_y - 1
  198.     check_fuel()
  199.     turtle.down()
  200. end
  201. min_y = min_y + 4
  202. navigate_to_start()
  203.  
  204. local running = true
  205.  
  206. while running do
  207.     if state == "mining" then
  208.         navigate_to_pos(target_x, target_y, target_z)
  209.         if turtle.detectUp() then
  210.             turtle.digUp()
  211.         end
  212.         if turtle.detectDown() then
  213.             turtle.digDown()
  214.         end
  215.         if inventory_full() or (turtle.getItemCount(1) == 0) then
  216.             state = "returning"
  217.         end
  218.         if x_direction then
  219.             if curr_x < size then
  220.                 target_x = curr_x + 1
  221.             elseif curr_x == size then
  222.                 if z_direction then
  223.                     if curr_z == size then
  224.                         if curr_y == min_y then
  225.                             running = false
  226.                         else
  227.                             target_y = target_y - 3
  228.                         end
  229.                         z_direction = false
  230.                     else
  231.                         target_z = target_z + 1
  232.                     end
  233.                 else
  234.                     if curr_z == 1 then
  235.                         if curr_y == min_y then
  236.                             running = false
  237.                         else
  238.                             target_y = target_y - 3
  239.                         end
  240.                         z_direction = true
  241.                     else
  242.                         target_z = target_z - 1
  243.                     end
  244.                 end
  245.                 x_direction = false
  246.             end
  247.         else
  248.             if curr_x > 1 then
  249.                 target_x = curr_x - 1
  250.             elseif curr_x == 1 then
  251.                 if z_direction then
  252.                     if curr_z == size then
  253.                         if curr_y == min_y then
  254.                             running = false
  255.                         else
  256.                             target_y = target_y - 3
  257.                         end
  258.                         z_direction = false
  259.                     else
  260.                         target_z = target_z + 1
  261.                     end
  262.                 else
  263.                     if curr_z == 1 then
  264.                         if curr_y == min_y then
  265.                             running = false
  266.                         else
  267.                             target_y = target_y - 3
  268.                         end
  269.                         z_direction = true
  270.                     else
  271.                         target_z = target_z - 1
  272.                     end
  273.                 end
  274.                 x_direction = true
  275.             end
  276.         end
  277.         if target_y < min_y then
  278.             target_y = min_y
  279.         end
  280.     elseif state == "returning" then
  281.         deposit_items()
  282.         navigate_to_start()
  283.         turn_towards("+z")
  284.         turtle.select(1)
  285.         turtle.suck()
  286.         state = "mining"
  287.     end
  288. end
  289.  
  290. deposit_items()
  291. navigate_to_start()
  292. turn_towards("+x")
  293.  
  294. print("Program Complete!")
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement