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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ name: ci
on:
push:
branches:
- main

pull_request:
types:
- opened
- reopened
- synchronize

jobs:
lint_test:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ luac.out
/wiki
/logs
*.json
!structures/*.json
36 changes: 36 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ globals = {
"peripheral",
"term",
"textutils",
"colors",
"disk",
"gps",
"http",
"io",
"keys",
"paintutils",
"parallel",
"vector",
"window",
"assert_true",
"assert_false",
"assert_eq",
"assert_ne",
"assert_float_eq",
"assert_float_ne",
"expect_true",
"expect_false",
"expect_eq",
"expect_ne",
"expect_float_eq",
"expect_float_ne",
"store_check_fail",
"expect_gt",
"expect_ge",
"expect_lt",
"expect_le",
"assert_gt",
"assert_ge",
"assert_lt",
"assert_le",
"patch",
"patch_local",
"reset_patches",
"MagicMock",
"sleep",
}
ignore = {
"111",
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"assert_le",
"patch",
"patch_local",
"reset_patches"
"reset_patches",
"sleep"
],
"Lua.runtime.version": "Lua 5.2",
"Lua.format.defaultConfig": {
Expand Down
38 changes: 38 additions & 0 deletions cc-apps/_template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Setup import paths
package.path = '../?.lua;../?/init.lua;' .. package.path

-- Import and configure logging
local logging = require 'cc-libs.util.logging'
logging.basic_config {
level = logging.Level.INFO,
file_level = logging.Level.TRACE,
filepath = 'logs/_template.log',
}
local log = logging.get_logger('main')

-- Argument parsing
local argparse = require 'cc-libs.util.argparse'
local parser = argparse.ArgParse:new('_template', 'Fill this in with your program')
parser:add_arg('n', { help = 'count' })
local args = parser:parse_args({ ... })

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

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

-- Gather arguments
local n = args.new

-- Create objects
local tmc = Motion:new()
tmc:enable_dig()

-- Main function
local function run()
log:info('This is just a template...', n)
end

-- Call rnu and log an error if raised
log:log_errors(run)
68 changes: 29 additions & 39 deletions cc-apps/lumber.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,46 @@ 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.DEBUG,
file_level = logging.Level.TRACE,
filepath = 'logs/lumber.log',
}
logging.file_level = logging.Level.DEBUG
local log = logging.get_logger('main')

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

local log_types = {
['minecraft:oak_log'] = true,
['minecraft:spruce_log'] = true,
['minecraft:birch_log'] = true,
['minecraft:jungle_log'] = true,
['minecraft:acacia_log'] = true,
['minecraft:dark_oak_log'] = true,
['minecraft:mangrove_log'] = true,
['minecraft:cherry_log'] = true,
['minecraft:crimson_stem'] = true,
['minecraft:warped_stem'] = true,
['minecraft:stripped_oak_log'] = true,
['minecraft:stripped_spruce_log'] = true,
['minecraft:stripped_birch_log'] = true,
['minecraft:stripped_jungle_log'] = true,
['minecraft:stripped_acacia_log'] = true,
['minecraft:stripped_dark_oak_log'] = true,
['minecraft:stripped_mangrove_log'] = true,
['minecraft:stripped_cherry_log'] = true,
['minecraft:stripped_crimson_stem'] = true,
['minecraft:stripped_warped_stem'] = true,
}

local tmc = ccl_motion.Motion:new()
local tmc = Motion:new()
tmc:enable_dig()

local height = 0

while true do
local function is_log()
local exists, info = turtle.inspect()
if not exists or not log_types[info.name] then
break
if exists then
local name = info.name
return string.find(name, 'log') ~= nil
end
turtle.dig()
tmc:up()
height = height + 1
return false
end

-- Return
local function harvest()
log:info('Starting harvest')
local height = 0
while is_log() do
log:trace('Mining log at height', height)
turtle.dig()
tmc:up()
height = height + 1
end
log:info('Finished mining', height, 'logs')
tmc:down(height)
end

log:info('Returning to station')
tmc:down(height)
local function run()
while true do
if is_log() then
harvest()
end
sleep(1)
end
end

log:info('Done!')
log:log_errors(run)
103 changes: 103 additions & 0 deletions cc-apps/structure.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package.path = '../?.lua;../?/init.lua;' .. package.path
local logging = require 'cc-libs.util.logging'
logging.basic_config {
filepath = 'logs/structure.log',
}
local log = logging.get_logger('main')

local argparse = require 'cc-libs.util.argparse'
local parser = argparse.ArgParse:new('structure', 'Build basic structures')
parser:add_arg('name', { help = 'structure name' })
local args = parser:parse_args({ ... })

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

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

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

local structure_path = 'structures/' .. args.name .. '.json'

if not fs.exists(structure_path) then
log:fatal('Could not find a structure at', structure_path)
end

log:info('Loading structure from', structure_path)

local file = assert(io.open(structure_path, 'r'))
local structure = json.decode(file:read('*all'))
file:close()

local tmc = Motion:new()

local aliases = structure.aliases
local layers = structure.layers
local structure_size = structure.size

-- TODO get list of blocks needed

log:info('Begin construction')

for layer_no, layer in ipairs(layers) do
local pattern = layer.pattern

log:debug('Begin layer', layer_no)

tmc:up()
tmc:right()

for r, row in ipairs(pattern) do
for c, col in ipairs(row) do
if aliases[col] ~= nil then
col = aliases[col]
end
if col then
if actions.select_slot(col) then
log:trace('Place block', col)
turtle.placeDown()
else
log:warning('Failed to find block', col)
end
else
log:trace('Skipping block')
end
if c < #row then
tmc:forward()
end
end

if r == #pattern then
break
end

log:trace('Turning around')

if r % 2 == 0 then
tmc:right()
tmc:forward()
tmc:right()
else
tmc:left()
tmc:forward()
tmc:left()
end
end

log:debug('Returning to start for next layer')

if structure_size.z % 2 == 1 then
tmc:around()
tmc:forward(structure_size.x - 1)
end

tmc:left()
tmc:forward(structure_size.z - 1)
tmc:around()
end

tmc:backward()
tmc:around()
tmc:down(structure_size.y)

log:info('Done!')
50 changes: 50 additions & 0 deletions cc-apps/watch_lava.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- Setup import paths
package.path = '../?.lua;../?/init.lua;' .. package.path

-- Import and configure logging
local logging = require 'cc-libs.util.logging'
logging.basic_config {
level = logging.Level.INFO,
file_level = logging.Level.TRACE,
filepath = 'logs/watch_lava.log',
}
local log = logging.get_logger('main')

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

local function cauldron_has_lava()
local exists, data = turtle.inspect()
return exists and data.name == 'minecraft:lava_cauldron'
end

local function take_lava()
log:info('Taking lava from cauldron')

if not actions.select_slot('minecraft:bucket') then
log:fatal('No more buckets')
end

if not turtle.place() then
log:fatal('Failed to extract lava')
end

log:trace('Got lava bucket')

if not actions.select_slot('minecraft:lava_bucket') then
log:error('Failed to select lava bucket')
else
turtle.dropDown()
log:debug('Pushed lava bucket into chest bellow')
end
end

local function run()
while true do
if cauldron_has_lava() then
take_lava()
end
sleep(1)
end
end

log:log_errors(run)
4 changes: 4 additions & 0 deletions cc-libs/turtle/motion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function Motion:forward(n)
log:debug('move forward', n, 'blocks')
for _ = 1, n do
if not self:_attempt_move(turtle.forward, (self.can_dig and turtle.dig or nil)) then
-- TODO is this warn in the right place?
log:warn('Failed to move forward after ' .. self.max_tries .. 'attempts')
return false
end
Expand All @@ -90,6 +91,7 @@ function Motion:backward(n)
log:debug('move backward', n, 'blocks')
for _ = 1, n do
if not self:_attempt_move(turtle.back) then
-- TODO is this warn in the right place?
log:warn('Failed to move back after ' .. self.max_tries .. 'attempts')
return false
end
Expand All @@ -109,6 +111,7 @@ function Motion:up(n)
log:debug('move up', n, 'blocks')
for _ = 1, n do
if not self:_attempt_move(turtle.up, (self.can_dig and turtle.digUp or nil)) then
-- TODO is this warn in the right place?
log:warn('Failed to move up after ' .. self.max_tries .. 'attempts')
return false
end
Expand All @@ -128,6 +131,7 @@ function Motion:down(n)
log:debug('move down', n, 'blocks')
for _ = 1, n do
if not self:_attempt_move(turtle.down, (self.can_dig and turtle.digDown or nil)) then
-- TODO is this warn in the right place?
log:warn('Failed to move down after ' .. self.max_tries .. 'attempts')
return false
end
Expand Down
Loading
Loading