Introduce Geolyzer and implement placing torches

master
D4VID 10 months ago
parent e31d0986b8
commit 93ecf28013

@ -4,8 +4,6 @@ local sides = require("sides")
local inventory = component.inventory_controller; local inventory = component.inventory_controller;
SLOTS = 16
local function contains(arr, val) local function contains(arr, val)
for _, value in ipairs(arr) do for _, value in ipairs(arr) do
if value == val then if value == val then
@ -30,7 +28,9 @@ local function store_stack(slot)
end end
local function store(whitelist) local function store(whitelist)
for slot = 1, SLOTS do local slots = robot.inventorySize()
for slot = 2, slots do
local item = inventory.getStackInInternalSlot(slot) local item = inventory.getStackInInternalSlot(slot)
if item then if item then
if whitelist ~= nil then if whitelist ~= nil then

@ -9,29 +9,25 @@ ORES = {
'minecraft:coal', 'minecraft:coal',
'minecraft:diamond', 'minecraft:diamond',
'minecraft:redstone', 'minecraft:redstone',
'minecraft:dye', -- Lapis
} }
local rows, row_length = ... print("Mining...")
if not rows or not row_length then -- mine, starts facing east, goes east and scans south
print("Usage: mine <rows> <row_length>") miner.mine(32)
end -- ends facing "west"
print("Will mine " .. rows .. "x" .. row_length .. " rows") robot.turnRight()
-- mine, starts facing "right"
miner.mine(rows, row_length)
-- ends facing "left"
-- store items -- store items
print("Storing ores") print("Storing ores")
chest.store(ORES) chest.store(ORES)
robot.turnRight()
nav.forward(2)
robot.turnLeft() robot.turnLeft()
nav.forward(2)
robot.turnRight()
print("Storing the rest") print("Storing the rest")
chest.store() chest.store()
robot.turnLeft() robot.turnRight()
nav.forward(2) nav.forward(2)
robot.turnLeft() -- ends facing east
-- ends facing "right"

@ -1,5 +1,9 @@
local robot = require("robot") local robot = require("robot")
local sides = require("sides")
local nav = require("navigator") local nav = require("navigator")
local scanner = require("scanner")
local torch = require("torch")
local function mine_single() local function mine_single()
robot.swing() robot.swing()
@ -15,45 +19,68 @@ local function mine_single()
return true return true
end end
local function mine_n(count) local function mine_n(count, place_torches)
local mined = 0 local mined = 0
for _ = 1, count do for i = 1, count do
if mine_single() then if mine_single() then
mined = mined + 1 mined = mined + 1
else else
break break
end 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 end
return mined return mined
end end
local function mine_row(length) local function mine_row(length)
local mined = mine_n(length) local mined = mine_n(length, true)
robot.turnAround() robot.turnAround()
nav.forward(mined, true) nav.forward(mined, true, false)
end end
-- starts facing right, mines rows to the right ---starts facing east
-- ends facing "left" ---ends facing west
local function mine(mine_rows, mine_row_length) ---@param scan_depth number how deep to scan for ores
local function mine(scan_depth)
local moved_rows = nav.forward_until_solid() local moved_rows = nav.forward_until_solid()
for i = 1, mine_rows do while true do
print("Mining row " .. i .. " of " .. mine_rows) local ore_depth = scanner.deep_scan(scan_depth, 0) -- scan the lower layer
if mine_n(1) ~= 1 then 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") print("Unable to continue mining rows")
break break
end end
robot.turnLeft()
mine_row(mine_row_length)
moved_rows = moved_rows + 1 moved_rows = moved_rows + 1
robot.turnLeft()
end end
-- go back to start -- go back to start
robot.turnAround() robot.turnAround()
nav.forward(moved_rows, true) nav.forward(moved_rows, true, false)
end end
return { return {

@ -1,9 +1,14 @@
local robot = require("robot") local robot = require("robot")
-- Moves forward n blocks. local torch = require("torch")
-- Waits if there is an obstruction
local function forward(n, mine) ---Moves forward n blocks.
for _ = 1, n do ---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 while true do
local result, error = robot.forward() local result, error = robot.forward()
if result then if result then
@ -16,12 +21,17 @@ local function forward(n, mine)
end end
end 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
end end
-- Move until it runs into a solid block. ---Move until it runs into a solid block.
-- Waits if there is an entity obstrucion ---Waits if there is an entity obstrucion
-- Returns number of blocks moved. ---@return number n number of blocks moved.
local function forward_until_solid() local function forward_until_solid()
local moved = 0 local moved = 0
while true do while true do

@ -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
}

@ -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
}
Loading…
Cancel
Save