diff --git a/chest.lua b/chest.lua index 0d469c8..0b03d46 100644 --- a/chest.lua +++ b/chest.lua @@ -4,8 +4,6 @@ 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 @@ -30,7 +28,9 @@ local function store_stack(slot) end local function store(whitelist) - for slot = 1, SLOTS do + local slots = robot.inventorySize() + + for slot = 2, slots do local item = inventory.getStackInInternalSlot(slot) if item then if whitelist ~= nil then diff --git a/mine.lua b/mine.lua index 3a4d2d7..977ce68 100644 --- a/mine.lua +++ b/mine.lua @@ -9,29 +9,25 @@ ORES = { 'minecraft:coal', 'minecraft:diamond', 'minecraft:redstone', + 'minecraft:dye', -- Lapis } -local rows, row_length = ... +print("Mining...") -if not rows or not row_length then - print("Usage: mine ") -end +-- mine, starts facing east, goes east and scans south +miner.mine(32) +-- ends facing "west" -print("Will mine " .. rows .. "x" .. row_length .. " rows") - --- mine, starts facing "right" -miner.mine(rows, row_length) --- ends facing "left" +robot.turnRight() -- store items print("Storing ores") chest.store(ORES) -robot.turnRight() -nav.forward(2) robot.turnLeft() +nav.forward(2) +robot.turnRight() print("Storing the rest") chest.store() -robot.turnLeft() +robot.turnRight() nav.forward(2) -robot.turnLeft() --- ends facing "right" +-- ends facing east diff --git a/miner.lua b/miner.lua index 9e01f8f..093e7ef 100644 --- a/miner.lua +++ b/miner.lua @@ -1,5 +1,9 @@ local robot = require("robot") +local sides = require("sides") + local nav = require("navigator") +local scanner = require("scanner") +local torch = require("torch") local function mine_single() robot.swing() @@ -15,45 +19,68 @@ local function mine_single() return true end -local function mine_n(count) +local function mine_n(count, place_torches) local mined = 0 - for _ = 1, count do + for i = 1, count do if mine_single() then mined = mined + 1 else break end + + -- place a torch every 11 blocks to keep the tunnel lit + if i % 11 == 0 and place_torches then + torch.place_torch(sides.west) + end end return mined end local function mine_row(length) - local mined = mine_n(length) + local mined = mine_n(length, true) robot.turnAround() - nav.forward(mined, true) + nav.forward(mined, true, false) end --- starts facing right, mines rows to the right --- ends facing "left" -local function mine(mine_rows, mine_row_length) +---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() - for i = 1, mine_rows do - print("Mining row " .. i .. " of " .. mine_rows) - if mine_n(1) ~= 1 then + 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 11 blocks to keep the tunnel lit + if moved_rows % 11 == 0 then + torch.place_torch(sides.north) + end + + if mine_n(1, false) ~= 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) + nav.forward(moved_rows, true, false) end return { diff --git a/navigator.lua b/navigator.lua index a879e53..b36f6c5 100644 --- a/navigator.lua +++ b/navigator.lua @@ -1,9 +1,14 @@ local robot = require("robot") --- Moves forward n blocks. --- Waits if there is an obstruction -local function forward(n, mine) - for _ = 1, n do +local torch = require("torch") + +---Moves forward n blocks. +---Waits if there is an obstruction +---@param n number how many block to go forward +---@param mine boolean|nil whether to mine block to go through +---@param place_torches boolean|nil whether to place torches in intervals +local function forward(n, mine, place_torches) + for i = 1, n do while true do local result, error = robot.forward() if result then @@ -16,12 +21,17 @@ local function forward(n, mine) end end end + + -- place a torch every 11 blocks to keep the tunnel lit + if i % 11 == 0 and place_torches then + torch.place_torch(nil) + end end end --- Move until it runs into a solid block. --- Waits if there is an entity obstrucion --- Returns number of blocks moved. +---Move until it runs into a solid block. +---Waits if there is an entity obstrucion +---@return number n number of blocks moved. local function forward_until_solid() local moved = 0 while true do diff --git a/scanner.lua b/scanner.lua new file mode 100644 index 0000000..c87a0c2 --- /dev/null +++ b/scanner.lua @@ -0,0 +1,27 @@ +local component = require('component') + +local geo = component.geolyzer + +---Scan 64 blocks south and return farthest found ore +---@param depth number how many block deep to scan +---@param y_offset number y offset, 0 means same height as the robot, 1 means the layer above the robot +---@return integer +local function deep_scan(depth, y_offset) + local results = geo.scan(0, 1, y_offset, 1, depth, 1) + + local max_depth = 0 + for i = 1, depth do + if results[i] >= 2 and results[i] ~= 100 then -- 100 is lava or water, ignore that + if i > max_depth then + max_depth = i + end + -- print("Got " .. results[i] .. " at " .. i .. " (y+" .. y_offset .. ")") + end + end + + return max_depth +end + +return { + deep_scan = deep_scan +} diff --git a/torch.lua b/torch.lua new file mode 100644 index 0000000..29f4cd7 --- /dev/null +++ b/torch.lua @@ -0,0 +1,21 @@ +local robot = require("robot") +local component = require("component") + +local inventory = component.inventory_controller; + +---Place a torch above the robot +---@param side number|nil which side of the empty space to place the torch +local function place_torch(side) + local stack = inventory.getStackInInternalSlot(1) + + if stack.name == "minecraft:torch" then + robot.select(1) + robot.placeUp(side) + else + print('Out of torches') + end +end + +return { + place_torch = place_torch +}