Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local computer = require("computer")
- local robot = require("robot")
- local inventory = component.inventory_controller
- local generator = component.generator
- -- Notify if no generator is present
- if generator == nil then
- print("No generator found, does robot have this component?")
- end
- -- local variables
- local status = "running"
- local issuesDetected = false
- local curMinedHeight = 0
- local curMinedDepth = 0
- local curMinedLength = 0
- function refuel()
- -- try inserting anything burnable from the inventory
- for i = 1, 16 do
- robot.select(i)
- generator.insert()
- end
- -- Check if we did manage to refuel
- if generator.count() > 0 then
- return true
- else
- return false
- end
- end
- function checkFuel()
- if generator.count() > 5 then
- return true
- else
- refuel()
- if generator.count() > 5 then
- return true
- else
- return false
- end
- end
- end
- -- Check held item durability
- function checkDurability()
- -- robot.durability() returns durability percentage from 0-1
- if robot.durability() == nil then
- print("Robot has no tool in tool slot")
- return false
- else
- if robot.durability() > 0.05 then
- return true
- else
- return false
- end
- end
- end
- -- Check Energy in robot
- function checkEnergy()
- Energy = computer.energy() / computer.maxEnergy() * 100
- if Energy < 10 and checkFuel() == false then
- return false
- else
- return true
- end
- end
- -- Check if we have space in the inventory
- function checkInventorySpace()
- for i = 1, 16 do
- robot.select(i)
- if robot.space() == 64 then
- return true
- end
- end
- return false
- end
- -- Notify for chest position
- print("Please place a chest directly to the right of the robot")
- os.sleep(1)
- -- Move Forward Function
- function moveForward()
- if robot.detect(forward) == true then
- robot.swing()
- robot.forward()
- else
- robot.forward()
- end
- end
- -- Move Down Function
- function moveDown()
- robot.swingDown()
- robot.down()
- end
- -- Split string into a table separated by x
- function splitString(input, separator)
- if separator == nil then
- separator = "x"
- end
- local t={}
- for str in string.gmatch(input, "([^"..separator.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function depositItemsToChest()
- if robot.detect(front) == true then
- local front = 3
- -- Loop through internal inventory and drop items to chest
- for i = 1, 16 do
- robot.select(i)
- if robot.space() ~= 64 then
- for chestSlot = 1, 27 do
- if inventory.getStackInSlot(front,chestSlot) == nil then
- inventory.dropIntoSlot(front, chestSlot, 64)
- end
- end
- end
- end
- --wait for emptying
- for i = 1, 16 do
- robot.select(i)
- if robot.space() ~= 64 then
- status = "no_inv_chest_full"
- print("Chest is full..")
- end
- end
- -- Reset status if inventory was a problem
- if status == "no_inv" then
- status = "running"
- end
- robot.select(1)
- else
- print("ERROR: No Chest Found in front of expected position")
- end
- end
- function returnToChest()
- print("RETURNING TO CHEST!")
- if curMinedDepth > 0 then
- for i = 1, curMinedDepth do
- robot.back()
- end
- curMinedDepth = 0
- end
- if curMinedLength > 0 then
- robot.turnLeft()
- for i = 1, curMinedLength do
- moveForward()
- end
- curMinedLength = 0
- robot.turnRight()
- end
- if curMinedHeight > 0 then
- for i = 1, curMinedHeight do
- robot.up()
- end
- curMinedHeight = 0
- end
- robot.back()
- robot.turnRight() -- turn to face chest
- depositItemsToChest()
- robot.turnLeft() -- Turn back to reset position
- end
- function checkForIssues()
- if status == "running" then
- if checkEnergy() == true then
- if checkDurability() == true then
- if checkInventorySpace() == true then
- return "running" -- no issues detected
- else
- return "no_inv"
- end
- else
- return "no_durability"
- end
- else
- return "no_energy"
- end
- else
- return status
- end
- end
- -- Start Cube Mining
- -- First we need some values from the operator=
- --#########################################################################################
- print("Please entery the cube dimensions! ( length x depth ) E.g. ( 3x3 ) ")
- local dimensions = io.read()
- os.sleep(0.5)
- local depth = splitString(dimensions)[1]
- local length = splitString(dimensions)[2]
- print(depth.." by "..length.."Confirmed!")
- os.sleep(0.5)
- print("How far down? eg. ( 10 ) ")
- local height = io.read()
- os.sleep(0.5)
- print("Cube: "..depth.." x "..length.." x "..height.." Confirmed!")
- os.sleep(0.5)
- print("Proceeding..")
- os.sleep(0.5)
- --#########################################################################################
- -- First we start the height loop
- moveForward()
- for i = 1, height do
- if checkForIssues() == "running" then
- moveDown()
- curMinedHeight = curMinedHeight + 1
- for i = 1, length do
- if checkForIssues() == "running" then
- curMinedLength = curMinedLength + 1
- -- Move to a new line, but not the first time
- if i ~= 1 then
- -- Move to next length-line
- robot.turnRight()
- moveForward()
- robot.turnLeft()
- end
- -- Dig forward
- for k = 1, depth-1 do
- if checkForIssues() == "running" then
- moveForward()
- curMinedDepth = curMinedDepth + 1
- else
- if issuesDetected == false then
- status = checkForIssues()
- returnToChest()
- issuesDetected = true
- break
- end
- end
- end
- -- Backtrack
- for j = 1, depth-1 do
- robot.back()
- end
- curMinedDepth = 0
- else
- if issuesDetected == false then
- status = checkForIssues()
- returnToChest()
- issuesDetected = true
- break
- end
- end
- end
- -- return to start
- robot.turnLeft()
- for i = 1, length-1 do
- moveForward()
- end
- curMinedLength = 0
- robot.turnRight()
- else
- if issuesDetected == false then
- status = checkForIssues()
- returnToChest()
- issuesDetected = true
- break
- end
- end
- end
- if status == "running" and issuesDetected == false then
- -- Return back up after completion
- for i = 1, height do
- robot.up()
- end
- robot.back()
- end
RAW Paste Data
Copied