More sophisticated program, goes back to home after mining and stores ores and other blocks separately in two chestsmaster
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 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()
|
local rows, row_length = ...
|
||||||
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)
|
if not rows or not row_length then
|
||||||
local mined = 0
|
print("Usage: mine <rows> <row_length>")
|
||||||
for _ = 1, count do
|
|
||||||
if mine_single() then
|
|
||||||
mined = mined + 1
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
return mined
|
|
||||||
end
|
|
||||||
|
|
||||||
local function mine_row(length)
|
|
||||||
local mined = mine_n(length)
|
|
||||||
|
|
||||||
robot.turnAround()
|
print("Will mine " .. rows .. "x" .. row_length .. " rows")
|
||||||
|
|
||||||
for _ = 1, mined do
|
|
||||||
while not robot.forward() do end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function mine(mine_rows, mine_row_length)
|
-- mine, starts facing "right"
|
||||||
|
miner.mine(rows, row_length)
|
||||||
|
-- ends facing "left"
|
||||||
|
|
||||||
for i = 1, mine_rows do
|
-- store items
|
||||||
print("Mining row " .. i .. " of " .. mine_rows)
|
print("Storing ores")
|
||||||
mine_row(mine_row_length)
|
chest.store(ORES)
|
||||||
|
robot.turnRight()
|
||||||
|
nav.forward(2)
|
||||||
robot.turnLeft()
|
robot.turnLeft()
|
||||||
if mine_n(1) ~= 1 then
|
print("Storing the rest")
|
||||||
print("Unable to continue mining rows")
|
chest.store()
|
||||||
return
|
|
||||||
end
|
|
||||||
robot.turnLeft()
|
robot.turnLeft()
|
||||||
end
|
nav.forward(2)
|
||||||
|
robot.turnLeft()
|
||||||
end
|
-- ends facing "right"
|
||||||
|
|
||||||
|
|
||||||
local rows, row_length = ...
|
|
||||||
|
|
||||||
print("Will mine " .. rows .. "x" .. row_length .. " rows")
|
|
||||||
|
|
||||||
mine(rows, row_length)
|
|
||||||
|
@ -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…
Reference in new issue