Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local robot = require("robot")
- local inventory_controller = component.inventory_controller
- local computer = require("computer")
- local os = require("os")
- local cl = component.chunkloader
- local RECHARGE_THRESHOLD = 20 -- power percent left
- local function getEnergyPercent()
- return math.floor((computer.energy() / computer.maxEnergy()) * 100)
- end
- local function getStackInInternalSlot(slot)
- return inventory_controller.getStackInInternalSlot(slot)
- end
- local function getToolDurability() -- Assumes tool is not equipped and is in internal slot 1
- local tool_item = getStackInInternalSlot(1)
- return (tool_item["maxDamage"] - tool_item["damage"]) / tool_item["maxDamage"]
- end
- local function rechargeTool() -- Assumes robot is at starting position, and equipped tool is a rechargable mining item.
- robot.select(1)
- robot.turnAround()
- inventory_controller.equip()
- while getToolDurability() < 0.8 do
- robot.drop()
- os.sleep(10)
- robot.suck()
- end
- inventory_controller.equip()
- robot.turnAround()
- end
- local argv = {...}
- local size = tonumber(argv[1])
- local height_offset = tonumber(argv[2]) -- How far down the robot will go before starting to mine.
- cl.setActive(true)
- local target_x = 1
- local target_z = size
- local target_y = -2 - height_offset
- local curr_x = 1
- local curr_z = size
- local curr_y = 0
- local x_direction = true -- true = +x, false = -x
- local z_direction = false -- true = +z, false = -z
- local FACE_AXIS_ARRAY = {"+z", "+x", "-z", "-x"}
- local FACE_AXIS_MAP = {}
- for f=1, 4 do
- FACE_AXIS_MAP[FACE_AXIS_ARRAY[f]] = f
- end
- local facing = 2
- local function turn_towards(face)
- local face_index = FACE_AXIS_MAP[face]
- while not (facing == face_index) do
- local diff = facing - face_index
- if (diff == 1) or (diff == -3) then
- robot.turnLeft()
- facing = facing - 1
- else
- robot.turnRight()
- facing = facing + 1
- end
- if facing > 4 then
- facing = 1
- elseif facing < 1 then
- facing = 4
- end
- end
- end
- local function move_by(dist, no_mine)
- while dist > 0 do
- if robot.detect() then
- if no_mine then
- while robot.detectUp() do
- robot.swingUp()
- robot.up()
- curr_y = curr_y + 1
- end
- else
- robot.swing()
- end
- end
- if robot.forward() then
- dist = dist - 1
- if facing == 1 then
- curr_z = curr_z + 1
- elseif facing == 2 then
- curr_x = curr_x + 1
- elseif facing == 3 then
- curr_z = curr_z - 1
- elseif facing == 4 then
- curr_x = curr_x - 1
- end
- end
- end
- end
- local function inventory_full()
- for s=1, 16 do
- if robot.count(s) == 0 then
- return false
- end
- end
- return true
- end
- local function navigate_to_pos(x, y, z)
- local moving_to_start = curr_y < y
- while curr_y > y do
- if robot.detectDown() then
- robot.swingDown()
- end
- if robot.down() then
- curr_y = curr_y - 1
- end
- end
- if curr_z < z then
- turn_towards("+z")
- move_by(z - curr_z, moving_to_start)
- elseif curr_z > z then
- turn_towards("-z")
- move_by(curr_z - z, moving_to_start)
- end
- if curr_x < x then
- turn_towards("+x")
- move_by(x - curr_x, moving_to_start)
- elseif curr_x > x then
- turn_towards("-x")
- move_by(curr_x - x, moving_to_start)
- end
- while curr_y < y do
- if robot.detectUp() then
- robot.swingUp()
- end
- if robot.up() then
- curr_y = curr_y + 1
- end
- end
- end
- local function navigate_to_start()
- navigate_to_pos(1, 0, size)
- turn_towards("+x")
- end
- local function deposit_items()
- navigate_to_start()
- while getEnergyPercent() < 97 do
- os.sleep(10)
- end
- rechargeTool()
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- local deposit_failed = true
- while deposit_failed do
- deposit_failed = false
- for s=1, 16 do
- if robot.count(s) > 0 then
- robot.select(s)
- if not robot.drop() then
- deposit_failed = true
- end
- end
- end
- if deposit_failed then
- os.sleep(5)
- end
- end
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- end
- local function load_error_check()
- local slot_1_item = getStackInInternalSlot(1)
- if slot_1_item and (slot_1_item["label"] == "Diamond Drill") then
- robot.select(1)
- inventory_controller.equip()
- end
- end
- local state = "mining" -- "mining" or "returning"
- local min_y = 0
- while not (robot.detectDown() and (not robot.swingDown())) do
- min_y = min_y - 1
- curr_y = curr_y - 1
- robot.down()
- end
- min_y = min_y + 4
- navigate_to_start()
- local running = true
- while running do
- if state == "mining" then
- navigate_to_pos(target_x, target_y, target_z)
- if robot.detectUp() then
- robot.swingUp()
- end
- if robot.detectDown() then
- robot.swingDown()
- end
- load_error_check()
- inventory_controller.equip()
- if inventory_full() or (getEnergyPercent() < RECHARGE_THRESHOLD) or (getToolDurability() < 0.2) then
- state = "returning"
- end
- inventory_controller.equip()
- if x_direction then
- if curr_x < size then
- target_x = curr_x + 1
- elseif curr_x == size then
- if z_direction then
- if curr_z == size then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = false
- else
- target_z = target_z + 1
- end
- else
- if curr_z == 1 then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = true
- else
- target_z = target_z - 1
- end
- end
- x_direction = false
- end
- else
- if curr_x > 1 then
- target_x = curr_x - 1
- elseif curr_x == 1 then
- if z_direction then
- if curr_z == size then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = false
- else
- target_z = target_z + 1
- end
- else
- if curr_z == 1 then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = true
- else
- target_z = target_z - 1
- end
- end
- x_direction = true
- end
- end
- if target_y < min_y then
- target_y = min_y
- end
- elseif state == "returning" then
- deposit_items()
- navigate_to_start()
- turn_towards("+z")
- robot.select(1)
- robot.suck()
- state = "mining"
- end
- end
- deposit_items()
- navigate_to_start()
- turn_towards("+x")
- cl.setActive(false)
- print("Program Complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement