Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot");
- local component = require("component")
- local inv = component.inventory_controller
- local geo = component.geolyzer
- local branchLength = 32
- local turnRight = true
- local toolSlotCount = 2
- local fortune = true
- local lastSlot = robot.inventorySize() - toolSlotCount
- if fortune then
- lastSlot = lastSlot - 1
- end
- local fortuneSlot = robot.inventorySize() - toolSlotCount
- local firstToolSlot = robot.inventorySize() - (toolSlotCount - 1)
- local validTools = {
- 'minecraft:stone_pickaxe',
- 'minecraft:iron_pickaxe',
- 'minecraft:diamond_pickaxe'
- }
- local oreList = {
- 'minecraft:dirt',
- 'minecraft:stone',
- }
- local ores = {}
- for i, ore in ipairs(oreList) do
- ores[ore] = true
- end
- local bottom = 0
- local top = 1
- local back = 2
- local front = 3
- local right = 4
- local left = 5
- function fortuneSwap()
- if fortune then
- robot.select(fortuneSlot)
- inv.equip()
- robot.select(1)
- end
- end
- function isOre(block)
- return ores[block.name] or false
- end
- function mineOre(inOrePos)
- for i = 0, 5, 1 do
- local block = geo.analyze(i)
- local oreInDir = isOre(block)
- if oreInDir or inOrePos then
- local isDiamond = block.name == 'minecraft:stone'
- if isDiamond then
- fortuneSwap()
- end
- mineInDir(i)
- if isDiamond then
- fortuneSwap()
- end
- mineOre(oreInDir)
- undoMineInDir(i)
- end
- end
- end
- function mineInDir(dir)
- if dir == 0 then
- swingDownIfTool()
- robot.down()
- elseif dir == 1 then
- swingUpIfTool()
- robot.up()
- elseif dir == 2 then
- robot.turnAround()
- swingIfTool()
- robot.forward()
- elseif dir == 3 then
- swingIfTool()
- robot.forward()
- elseif dir == 4 then
- robot.turnRight()
- swingIfTool()
- robot.forward()
- elseif dir == 5 then
- robot.turnLeft()
- swingIfTool()
- robot.forward()
- else
- error('ERROR: invalid dir')
- end
- end
- function undoMineInDir(dir)
- if dir == 0 then
- robot.up()
- elseif dir == 1 then
- robot.down()
- elseif dir == 2 then
- robot.turnAround()
- robot.forward()
- elseif dir == 3 then
- robot.back()
- elseif dir == 4 then
- robot.back()
- robot.turnLeft()
- elseif dir == 5 then
- robot.back()
- robot.turnRight()
- else
- error('ERROR: invalid dir')
- end
- end
- function isValidTool(stack)
- return includes(validTools, stack.name)
- end
- function turnInBranchDir()
- if turnRight then
- robot.turnRight()
- else
- robot.turnLeft()
- end
- end
- function swingIfTool()
- if robot.durability() == nil then
- grabTool()
- end
- robot.swing()
- raiseIfInventoryFull()
- end
- function swingUpIfTool()
- if robot.durability() == nil then
- grabTool()
- end
- robot.swingUp()
- raiseIfInventoryFull()
- end
- function swingDownIfTool()
- if robot.durability() == nil then
- grabTool()
- end
- robot.swingDown()
- raiseIfInventoryFull()
- end
- function raiseIfInventoryFull()
- if inv.getStackInInternalSlot(lastSlot) ~= nil then
- error('ERROR: inventory full')
- end
- end
- function includes(arr, item)
- for i, v in ipairs(arr) do
- if v == item then
- return true
- end
- end
- return false
- end
- function grabTool()
- local toolFound = false
- for i = robot.inventorySize(), firstToolSlot, -1 do
- local stack = inv.getStackInInternalSlot(i)
- if stack ~= nil and isValidTool(stack) then
- robot.select(i)
- inv.equip()
- robot.select(1)
- toolFound = true
- end
- end
- if not toolFound then
- error('ERROR: no tool')
- end
- end
- function mineForward()
- swingIfTool()
- robot.forward()
- placeDownIfAir()
- mineOre()
- swingUpIfTool()
- robot.up()
- mineOre()
- robot.down()
- end
- function placeDownIfAir()
- if not robot.detectDown() then
- robot.placeDown()
- end
- if not robot.detectDown() then
- error("ERROR: place failed!")
- end
- end
- function branch()
- turnInBranchDir()
- for i = 1, branchLength, 1 do
- mineForward()
- end
- robot.turnAround()
- for i = 1, branchLength, 1 do
- robot.forward()
- end
- turnInBranchDir()
- end
- while true do
- for i = 1, 4, 1 do
- mineForward()
- end
- branch()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement