From 4278da52b08fb970eb6c52ffee3277caa4771023 Mon Sep 17 00:00:00 2001 From: D4VID Date: Sun, 29 Sep 2024 19:29:52 +0200 Subject: [PATCH] Miner v2 More sophisticated program, goes back to home after mining and stores ores and other blocks separately in two chests --- chest.lua | 49 ++++++++++++++++++++++++++++++ control.lua | 20 +++++++++++++ mine.lua | 82 +++++++++++++++++++-------------------------------- miner.lua | 61 ++++++++++++++++++++++++++++++++++++++ navigator.lua | 38 ++++++++++++++++++++++++ 5 files changed, 198 insertions(+), 52 deletions(-) create mode 100644 chest.lua create mode 100644 control.lua create mode 100644 miner.lua create mode 100644 navigator.lua diff --git a/chest.lua b/chest.lua new file mode 100644 index 0000000..0d469c8 --- /dev/null +++ b/chest.lua @@ -0,0 +1,49 @@ +local component = require("component") +local robot = require("robot") +local sides = require("sides") + +local inventory = component.inventory_controller; + +SLOTS = 16 + +local function contains(arr, val) + for _, value in ipairs(arr) do + if value == val then + return true + end + end + return false +end + +local function store_stack(slot) + robot.select(slot) + local slots = inventory.getInventorySize(sides.forward) + for s = 1, slots do + local result, _ = inventory.dropIntoSlot(sides.forward, s) + if result then + local stack = inventory.getStackInInternalSlot(slot) + if stack == nil then + break + end + end + end +end + +local function store(whitelist) + for slot = 1, SLOTS do + local item = inventory.getStackInInternalSlot(slot) + if item then + if whitelist ~= nil then + if contains(whitelist, item.name) then + store_stack(slot) + end + else + store_stack(slot) + end + end + end +end + +return { + store = store, +} diff --git a/control.lua b/control.lua new file mode 100644 index 0000000..48399cd --- /dev/null +++ b/control.lua @@ -0,0 +1,20 @@ +local robot = require("robot") +local event = require("event") + +print("Control using w,s,q,e. Exit using c") + +while true do + local _, _, ascii, _, _ = event.pull("key_down") + local ch = string.char(ascii) + if ch == 'q' then + robot.turnLeft() + elseif ch == 'e' then + robot.turnRight() + elseif ch == 'w' then + robot.forward() + elseif ch == 's' then + robot.back() + elseif ch == 'c' then + break + end +end diff --git a/mine.lua b/mine.lua index f176b4b..3a4d2d7 100644 --- a/mine.lua +++ b/mine.lua @@ -1,59 +1,37 @@ local robot = require("robot") +local nav = require("navigator") +local chest = require("chest") +local miner = require("miner") + +ORES = { + 'minecraft:iron_ore', + 'minecraft:gold_ore', + 'minecraft:coal', + 'minecraft:diamond', + 'minecraft:redstone', +} -local function mine_single() - robot.swing() - - 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 - -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(1) ~= 1 then - print("Unable to continue mining rows") - return - end - robot.turnLeft() - end +local rows, row_length = ... +if not rows or not row_length then + print("Usage: mine ") end - -local rows, row_length = ... - print("Will mine " .. rows .. "x" .. row_length .. " rows") -mine(rows, row_length) \ No newline at end of file +-- mine, starts facing "right" +miner.mine(rows, row_length) +-- ends facing "left" + +-- store items +print("Storing ores") +chest.store(ORES) +robot.turnRight() +nav.forward(2) +robot.turnLeft() +print("Storing the rest") +chest.store() +robot.turnLeft() +nav.forward(2) +robot.turnLeft() +-- ends facing "right" diff --git a/miner.lua b/miner.lua new file mode 100644 index 0000000..3d8f0e8 --- /dev/null +++ b/miner.lua @@ -0,0 +1,61 @@ +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) +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) +end + +return { + mine = mine +} diff --git a/navigator.lua b/navigator.lua new file mode 100644 index 0000000..282785b --- /dev/null +++ b/navigator.lua @@ -0,0 +1,38 @@ +local robot = require("robot") + +-- Moves forward n blocks. +-- Waits if there is an obstruction +local function forward(n) + for _ = 1, n do + while true do + local result, error = robot.forward() + if result then + break + else + print("Cannot move: " .. error) + end + end + end +end + +-- Move until it runs into a solid block. +-- Waits if there is an entity obstrucion +-- Returns number of blocks moved. +local function forward_until_solid() + local moved = 0 + while true do + local result, error = robot.forward() + if result then + moved = moved + 1 + else + if error == "solid" then + return moved + end + end + end +end + +return { + forward = forward, + forward_until_solid = forward_until_solid, +}