From 58e14bdf352e66df9111132e9482cfdf136fd24d Mon Sep 17 00:00:00 2001 From: D4VID Date: Wed, 8 May 2024 21:04:46 +0200 Subject: [PATCH] Miner v1 Mines N rows of given length and always comes back the exact amount it mined so if there is a problem it doesn't mess it up --- mine.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/mine.lua b/mine.lua index 2619f89..38d61c6 100644 --- a/mine.lua +++ b/mine.lua @@ -1,15 +1,59 @@ local robot = require("robot") -local arg = ... +local function mine_single() + robot.swing() -local function mine(count) - for i = 1, count do - robot.swing() - robot.up() - robot.swing() - robot.down() - robot.forward() + local result, error = robot.forward() + if not result then + print("Cannot mine: " .. 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() + + for _ = 1, mined do + while not robot.forward() do end end end -mine(arg) +local function mine(mine_rows, mine_row_length) + + for i = 1, mine_rows do + print("Mining row " .. i .. " of " .. mine_rows) + mine_row(mine_row_length) + robot.turnLeft() + if mine_n(3) ~= 3 then + print("Unable to continue mining rows") + return + end + robot.turnLeft() + end + +end + + +local rows, row_length = ... + +print("Will mine " .. rows .. "x" .. row_length .. " rows") + +mine(rows, row_length)