Advertisement
Necrotico

OC Auto Miner

Sep 26th, 2021 (edited)
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.17 KB | None | 0 0
  1.  
  2. local component = require("component")
  3. local robot = require("robot")
  4. local inventory_controller = component.inventory_controller
  5. local computer = require("computer")
  6. local os = require("os")
  7. local cl = component.chunkloader
  8.  
  9. local RECHARGE_THRESHOLD = 20 -- power percent left
  10.  
  11. local function getEnergyPercent()
  12.     return math.floor((computer.energy() / computer.maxEnergy()) * 100)
  13. end
  14.  
  15. local function getStackInInternalSlot(slot)
  16.     return inventory_controller.getStackInInternalSlot(slot)
  17. end
  18.  
  19. local function getToolDurability() -- Assumes tool is not equipped and is in internal slot 1
  20.     local tool_item = getStackInInternalSlot(1)
  21.     return (tool_item["maxDamage"] - tool_item["damage"]) / tool_item["maxDamage"]
  22. end
  23.  
  24. local function rechargeTool() -- Assumes robot is at starting position, and equipped tool is a rechargable mining item.
  25.     robot.select(1)
  26.     robot.turnAround()
  27.     inventory_controller.equip()
  28.     while getToolDurability() < 0.8 do
  29.         robot.drop()
  30.         os.sleep(10)
  31.         robot.suck()
  32.     end
  33.     inventory_controller.equip()
  34.     robot.turnAround()
  35. end
  36.  
  37. local argv = {...}
  38.  
  39. local size = tonumber(argv[1])
  40. local height_offset = tonumber(argv[2]) -- How far down the robot will go before starting to mine.
  41.  
  42. cl.setActive(true)
  43.  
  44. local target_x = 1
  45. local target_z = size
  46. local target_y = -2 - height_offset
  47.  
  48. local curr_x = 1
  49. local curr_z = size
  50. local curr_y = 0
  51.  
  52. local x_direction = true -- true = +x, false = -x
  53. local z_direction = false -- true = +z, false = -z
  54.  
  55. local FACE_AXIS_ARRAY = {"+z", "+x", "-z", "-x"}
  56. local FACE_AXIS_MAP = {}
  57. for f=1, 4 do
  58.     FACE_AXIS_MAP[FACE_AXIS_ARRAY[f]] = f
  59. end
  60. local facing = 2
  61.  
  62. local function turn_towards(face)
  63.     local face_index = FACE_AXIS_MAP[face]
  64.     while not (facing == face_index) do
  65.         local diff = facing - face_index
  66.         if (diff == 1) or (diff == -3) then
  67.             robot.turnLeft()
  68.             facing = facing - 1
  69.         else
  70.             robot.turnRight()
  71.             facing = facing + 1
  72.         end
  73.         if facing > 4 then
  74.             facing = 1
  75.         elseif facing < 1 then
  76.             facing = 4
  77.         end
  78.     end
  79. end
  80.  
  81. local function move_by(dist, no_mine)
  82.     while dist > 0 do
  83.         if robot.detect() then
  84.             if no_mine then
  85.                 while robot.detectUp() do
  86.                     robot.swingUp()
  87.                     robot.up()
  88.                     curr_y = curr_y + 1
  89.                 end
  90.             else
  91.                 robot.swing()
  92.             end
  93.         end
  94.         if robot.forward() then
  95.             dist = dist - 1
  96.             if facing == 1 then
  97.                 curr_z = curr_z + 1
  98.             elseif facing == 2 then
  99.                 curr_x = curr_x + 1
  100.             elseif facing == 3 then
  101.                 curr_z = curr_z - 1
  102.             elseif facing == 4 then
  103.                 curr_x = curr_x - 1
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109. local function inventory_full()
  110.     for s=1, 16 do
  111.         if robot.count(s) == 0 then
  112.             return false
  113.         end
  114.     end
  115.     return true
  116. end
  117.  
  118. local function navigate_to_pos(x, y, z)
  119.     local moving_to_start = curr_y < y
  120.     while curr_y > y do
  121.         if robot.detectDown() then
  122.             robot.swingDown()
  123.         end
  124.         if robot.down() then
  125.             curr_y = curr_y - 1
  126.         end
  127.     end
  128.     if curr_z < z then
  129.         turn_towards("+z")
  130.         move_by(z - curr_z, moving_to_start)
  131.     elseif curr_z > z then
  132.         turn_towards("-z")
  133.         move_by(curr_z - z, moving_to_start)
  134.     end
  135.     if curr_x < x then
  136.         turn_towards("+x")
  137.         move_by(x - curr_x, moving_to_start)
  138.     elseif curr_x > x then
  139.         turn_towards("-x")
  140.         move_by(curr_x - x, moving_to_start)
  141.     end
  142.     while curr_y < y do
  143.         if robot.detectUp() then
  144.             robot.swingUp()
  145.         end
  146.         if robot.up() then
  147.             curr_y = curr_y + 1
  148.         end
  149.     end
  150. end
  151.  
  152. local function navigate_to_start()
  153.     navigate_to_pos(1, 0, size)
  154.     turn_towards("+x")
  155. end
  156.  
  157. local function deposit_items()
  158.     navigate_to_start()
  159.     while getEnergyPercent() < 97 do
  160.         os.sleep(10)
  161.     end
  162.     rechargeTool()
  163.     robot.turnRight()
  164.     robot.forward()
  165.     robot.turnRight()
  166.     local deposit_failed = true
  167.     while deposit_failed do
  168.         deposit_failed = false
  169.         for s=1, 16 do
  170.             if robot.count(s) > 0 then
  171.                 robot.select(s)
  172.                 if not robot.drop() then
  173.                     deposit_failed = true
  174.                 end
  175.             end
  176.         end
  177.         if deposit_failed then
  178.             os.sleep(5)
  179.         end
  180.     end
  181.     robot.turnRight()
  182.     robot.forward()
  183.     robot.turnRight()
  184. end
  185.  
  186. local function load_error_check()
  187.     local slot_1_item = getStackInInternalSlot(1)
  188.     if slot_1_item and (slot_1_item["label"] == "Diamond Drill") then
  189.         robot.select(1)
  190.         inventory_controller.equip()
  191.     end
  192. end
  193.  
  194. local state = "mining" -- "mining" or "returning"
  195. local min_y = 0
  196.  
  197. while not (robot.detectDown() and (not robot.swingDown())) do
  198.     min_y = min_y - 1
  199.     curr_y = curr_y - 1
  200.     robot.down()
  201. end
  202. min_y = min_y + 4
  203. navigate_to_start()
  204.  
  205. local running = true
  206.  
  207. while running do
  208.     if state == "mining" then
  209.         navigate_to_pos(target_x, target_y, target_z)
  210.         if robot.detectUp() then
  211.             robot.swingUp()
  212.         end
  213.         if robot.detectDown() then
  214.             robot.swingDown()
  215.         end
  216.         load_error_check()
  217.         inventory_controller.equip()
  218.         if inventory_full() or (getEnergyPercent() < RECHARGE_THRESHOLD) or (getToolDurability() < 0.2) then
  219.             state = "returning"
  220.         end
  221.         inventory_controller.equip()
  222.         if x_direction then
  223.             if curr_x < size then
  224.                 target_x = curr_x + 1
  225.             elseif curr_x == size then
  226.                 if z_direction then
  227.                     if curr_z == size then
  228.                         if curr_y == min_y then
  229.                             running = false
  230.                         else
  231.                             target_y = target_y - 3
  232.                         end
  233.                         z_direction = false
  234.                     else
  235.                         target_z = target_z + 1
  236.                     end
  237.                 else
  238.                     if curr_z == 1 then
  239.                         if curr_y == min_y then
  240.                             running = false
  241.                         else
  242.                             target_y = target_y - 3
  243.                         end
  244.                         z_direction = true
  245.                     else
  246.                         target_z = target_z - 1
  247.                     end
  248.                 end
  249.                 x_direction = false
  250.             end
  251.         else
  252.             if curr_x > 1 then
  253.                 target_x = curr_x - 1
  254.             elseif curr_x == 1 then
  255.                 if z_direction then
  256.                     if curr_z == size then
  257.                         if curr_y == min_y then
  258.                             running = false
  259.                         else
  260.                             target_y = target_y - 3
  261.                         end
  262.                         z_direction = false
  263.                     else
  264.                         target_z = target_z + 1
  265.                     end
  266.                 else
  267.                     if curr_z == 1 then
  268.                         if curr_y == min_y then
  269.                             running = false
  270.                         else
  271.                             target_y = target_y - 3
  272.                         end
  273.                         z_direction = true
  274.                     else
  275.                         target_z = target_z - 1
  276.                     end
  277.                 end
  278.                 x_direction = true
  279.             end
  280.         end
  281.         if target_y < min_y then
  282.             target_y = min_y
  283.         end
  284.     elseif state == "returning" then
  285.         deposit_items()
  286.         navigate_to_start()
  287.         turn_towards("+z")
  288.         robot.select(1)
  289.         robot.suck()
  290.         state = "mining"
  291.     end
  292. end
  293.  
  294. deposit_items()
  295. navigate_to_start()
  296. turn_towards("+x")
  297.  
  298. cl.setActive(false)
  299.  
  300. print("Program Complete!")
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement