More sophisticated program, goes back to home after mining and stores
ores and other blocks separately in two chests
master
D4VID 12 months ago
parent ed08523d4a
commit 4278da52b0

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

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

@ -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 <rows> <row_length>")
end
local rows, row_length = ...
print("Will mine " .. rows .. "x" .. row_length .. " rows")
mine(rows, row_length)
-- 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"

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

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