local robot = require("robot") local nav = require("navigator") 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) local mined = 0 for _ = 1, count do if mine_single() then mined = mined + 1 else break end end return mined end local function mine_row(length) local mined = mine_n(length) robot.turnAround() nav.forward(mined, true) end -- starts facing right, mines rows to the right -- ends facing "left" local function mine(mine_rows, mine_row_length) local moved_rows = nav.forward_until_solid() for i = 1, mine_rows do print("Mining row " .. i .. " of " .. mine_rows) if mine_n(1) ~= 1 then print("Unable to continue mining rows") break end robot.turnLeft() mine_row(mine_row_length) moved_rows = moved_rows + 1 robot.turnLeft() end -- go back to start robot.turnAround() nav.forward(moved_rows, true) end return { mine = mine }