You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
miner/miner.lua

90 lines
2.1 KiB

local robot = require("robot")
local sides = require("sides")
local nav = require("navigator")
local scanner = require("scanner")
local torch = require("torch")
TORCH_INTERVAL = 9
local function mine_single()
robot.swing()
local result, error = robot.forward()
if not result then
print("Cannot move: " .. error)
return false
end
robot.swingUp()
return true
end
local function mine_n(count, place_torches)
local mined = 0
for i = 1, count do
if mine_single() then
mined = mined + 1
else
break
end
-- place a torch every x blocks to keep the tunnel lit
if i % TORCH_INTERVAL == 0 and place_torches then
torch.place_torch(sides.right)
end
end
return mined
end
local function mine_row(length)
local mined = mine_n(length, true)
robot.turnAround()
nav.forward(mined, true, false)
end
---starts facing east
---ends facing west
---@param scan_depth number how deep to scan for ores
local function mine(scan_depth)
local moved_rows = nav.forward_until_solid()
while true do
local ore_depth = scanner.deep_scan(scan_depth, 0) -- scan the lower layer
local ore_depth_1 = scanner.deep_scan(scan_depth, 1) -- scan the upper layer
-- take the deeper one
if ore_depth_1 > ore_depth then
ore_depth = ore_depth_1
end
if ore_depth > 0 then
print("Found ore at row " .. moved_rows .. " max depth " .. ore_depth)
robot.turnRight() -- mine south
mine_row(ore_depth)
robot.turnRight()
end
-- place a torch every x blocks to keep the tunnel lit
if moved_rows % TORCH_INTERVAL == 0 then
torch.place_torch(sides.left)
end
if mine_n(1, false) ~= 1 then
print("Unable to continue mining rows")
break
end
moved_rows = moved_rows + 1
end
-- go back to start
robot.turnAround()
nav.forward(moved_rows, true, false)
end
return {
mine = mine
}