Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions cc-apps/harvest_crops.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package.path = '../?.lua;../?/init.lua;' .. package.path
local logging = require 'cc-libs.util.logging'
logging.basic_config {
level = logging.Level.INFO,
file_level = logging.Level.TRACE,
filepath = 'logs/harvest_crops.log',
}
local log = logging.get_logger('main')

local argparse = require 'cc-libs.util.argparse'
local parser = argparse.ArgParse:new('farm', 'Harvest crops from the bottom right corner')
parser:add_arg('width', { help = 'plot width' })
parser:add_arg('length', { help = 'plot length' })
local args = parser:parse_args({ ... })

local width = tonumber(args.width)
local length = tonumber(args.length)

log:info('Start farm with width=', width, 'length=', length)

local ccl_motion = require 'cc-libs.turtle.motion'
local Motion = ccl_motion.Motion

local actions = require 'cc-libs.turtle.actions'

local tmc = Motion:new()

local seed_name_map = {
['minecraft:wheat'] = 'minecraft:wheat_seeds',
['minecraft:potatoes'] = 'minecraft:potato',
}

local function replant(name)
log:debug('Replant', name)
local seed_name = seed_name_map[name]
log:debug('Selecting seed name', seed_name)
if not seed_name then
log:warning('Could not find seed name for', name)
return
end
if not actions.select_slot(seed_name) then
log:warning('Failed to find', seed_name, 'to replant')
return
end
turtle.placeDown()
end

local function harvest_crops()
log:info('Harvesting crops')

tmc:up()
tmc:forward()
for x = 1, width do
for z = 1, length do
local exists, info = turtle.inspectDown()
if exists and info.state ~= nil then
if info.state.age == 7 then
log:debug('Harvest crop', info.name, 'at', x, z)
turtle.digDown()
replant(info.name)
end
end
if z < length then
tmc:forward()
end
end

if x < width then
if x % 2 == 1 then
tmc:left()
tmc:forward()
tmc:left()
else
tmc:right()
tmc:forward()
tmc:right()
end
end
end

if width % 2 == 1 then
tmc:around()
tmc:forward(length)
else
tmc:forward()
end

log:info('Finished harvest')

tmc:left()
tmc:forward(width - 1)
tmc:left()
tmc:down()

for i = 1, 16 do
actions.dump_slot(i, 'down')
end

log:debug('Finished dumping inventory')
end

local function run()
while true do
harvest_crops()
sleep(60 * 20)
end
end

log:catch_errors(run)
23 changes: 23 additions & 0 deletions cc-apps/inspect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Setup import paths
package.path = '../?.lua;../?/init.lua;' .. package.path

-- Import libraries
local json = require 'cc-libs.util.json'

local actions = require 'cc-libs.turtle.actions'

local exists, info = turtle.inspect()
if exists then
print('Found block')
local file = assert(io.open('inspect.json', 'w'))
file:write(json.encode(info))
file:close()
end

local inv = actions.examine_inventory('front')
if inv then
print('Found inventory')
local file = assert(io.open('inventory.json', 'w'))
file:write(json.encode(inv))
file:close()
end
75 changes: 72 additions & 3 deletions cc-libs/turtle/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ end

---Drop all items from the given slot.
---@param slot integer 1 to 16
---@param direction string|nil face to dorp, forward, up or down
---@return integer count number of items dropped
function M.dump_slot(slot)
function M.dump_slot(slot, direction)
assert(slot > 0 and slot <= 16, 'slot must be a number between 1 and 16')
log:info('Dumping slot', slot)
log:debug('Dumping slot', slot)

turtle.select(slot)

Expand All @@ -93,9 +94,16 @@ function M.dump_slot(slot)
local count = 0
while turtle.getItemCount() > 0 do
log:trace('Dropping item', count)
turtle.drop()
if direction == 'up' then
turtle.dropUp()
elseif direction == 'down' then
turtle.dropDown()
else
turtle.drop()
end
count = count + 1
end

log:debug('Finished dropping items')
return count
end
Expand Down Expand Up @@ -125,4 +133,65 @@ function M.place_torch()
return true
end

---Open an inventory peripheral and inspect it's contents. If side is front, top
---or bottom, the block state will be reported with it's contents.
---@param side string peripheral side
---@return {size: integer, slots: table[], details: table, block: table | nil } | nil info inventory details if present
function M.examine_inventory(side)
log:debug('Opening peripheral on side', side)

local is_inv = false
for _, t in ipairs({ peripheral.getType(side) }) do
is_inv = is_inv or t == 'inventory'
end

if not is_inv then
log:debug('Peripheral on side', side, 'is not an inventory')
return nil
end

local inv = peripheral.wrap(side)

if not inv then
log:debug('No inventory found')
return nil
end

local slots = {}

for slot in pairs(inv.list()) do
local detail = inv.getItemDetail(slot)
log:trace('Slot', slot, 'has item', detail.name)
local limit = inv.getItemLimit(slot)

slots[slot] = {
slot = slot,
detail = detail,
limit = limit,
}
end

local info = {
size = inv.size(),
slots = slots,
}

local exists = false
local block = nil
if side == 'front' then
exists, block = turtle.inspect()
elseif side == 'top' then
exists, block = turtle.inspectUp()
elseif side == 'bottom' then
exists, block = turtle.inspectDown()
end

if exists then
log:debug('Adding block info')
info.block = block
end

return info
end

return M
Loading