Added buildscript start

This commit is contained in:
Andrew Lalis 2023-04-26 08:41:55 +02:00
parent 78d49f8abc
commit a1dd7a850a
4 changed files with 104 additions and 3 deletions

View File

@ -40,6 +40,7 @@ jobs:
script_dir=docs/src/.vuepress/public/scripts script_dir=docs/src/.vuepress/public/scripts
lua minify.lua minify src/movescript.lua > $script_dir/movescript-min.lua lua minify.lua minify src/movescript.lua > $script_dir/movescript-min.lua
lua minify.lua minify src/itemscript.lua > $script_dir/itemscript-min.lua lua minify.lua minify src/itemscript.lua > $script_dir/itemscript-min.lua
lua minify.lua minify src/buildscript.lua > $script_dir/buildscript-min.lua
- name: Clean Up Lua Artifacts - name: Clean Up Lua Artifacts
run: | run: |

91
src/buildscript.lua Normal file
View File

@ -0,0 +1,91 @@
--[[
Buildscript - A unified set of tools that make repetitive building tasks easier
with ComputerCraft robots.
Author: Andrew Lalis <andrewlalisofficial@gmail.com>
This module depends upon both Movescript and Itemscript.
]]--
local movescript = require("movescript")
local itemscript = require("itemscript")
-- The buildscript module.
local buildscript = {}
buildscript.VERSION = "0.0.1"
-- Runs a movescript script, while ensuring that a given item is always selected.
function buildscript.runWithItem(ms_script, filterExpr)
local instructions = movescript.parse(ms_script)
for idx, instruction in pairs(instructions) do
itemscript.selectOrWait(filterExpr)
movescript.executeInstruction(instruction)
end
end
local function parseArgValue(argSpec, str)
end
-- Parses arguments according to a specification table, for common building
-- scripts, and returns a table with key-value pairs for each arg.
-- The specification table should be formatted like so:
-- {
-- argName = { type = "string", required = true, idx = 1 }
-- }
function buildscript.parseArgs(args, spec)
local idxArgSpecs = {}
local namedArgSpecs = {}
for name, argSpec in pairs(spec) do
if argSpec.idx ~= nil then
-- Add this argSpec to the list of indexed arg specs for parsing first.
if type(argSpec.idx) ~= "number" or argSpec.idx < 1 do
return false, "Invalid argument specification: " .. name .. " does not have a valid numeric index."
end
idxArgSpecs[name] = argSpec
elseif argSpec.name ~= nil then
-- Otherwise, ensure that this argSpec has a name.
if type(argSpec.name) ~= "string" or #argSpec.name < 3 do
return false, "Invalid argument specification: " .. name .. " does not have a valid string name."
end
namedArgSpecs[name] = argSpec
else
return false, "Invalid argument specification: " .. name .. " doesn't have idx or name."
end
end
local results = {}
local idx = 1
while idx <= #args do
local parsed = false
-- Try and see if there's an idx arg spec for this index first.
for name, argSpec in pairs(idxArgSpecs) do
if argSpec.idx == idx then
local success, value = parseArgValue(argSpec, args[idx])
if success then
results[name] = value
idxArgSpecs[name] = nil
parsed = true
break
elseif not success and argSpec.required then
return false, "Failed to parse value for " .. name .. " argument: " .. value
end
end
end
-- If no idx arg spec could parse the argument, try a named one.
if not parsed then
if idx == #args then
return false, "Missing value for argument " .. args[idx]
end
for name, argSpec in pairs(idxArgSpecs) do
end
end
idx = idx + 1
end
return true, results
end
return buildscript

View File

@ -137,6 +137,15 @@ function itemscript.select(filterExpr)
return false return false
end end
-- Selects a slot containing at least one of the given item type, or waits for
-- the user to add an item otherwise.
function itemscript.selectOrWait(filterExpr)
while not itemscript.select(filterExpr) do
print("Couldn't find at least one item matching the filter expression: \"" .. filterExpr .. "\". Please add it.")
os.pullEvent("turtle_inventory")
end
end
-- Helper function to drop items in a flexible way, using a drop function and filtering function. -- Helper function to drop items in a flexible way, using a drop function and filtering function.
local function dropFiltered(dropFunction, filter) local function dropFiltered(dropFunction, filter)
for i = 1, 16 do for i = 1, 16 do

View File

@ -279,12 +279,12 @@ end
-- Executes a single instruction. An instruction is a table with an "action" -- Executes a single instruction. An instruction is a table with an "action"
-- and some attributes, such as if it needs fuel or not. -- and some attributes, such as if it needs fuel or not.
local function executeInstruction(instruction, settings) function movescript.executeInstruction(instruction, settings)
if instruction.type == INSTRUCTION_TYPES.repeated then if instruction.type == INSTRUCTION_TYPES.repeated then
debug("Executing repeated instruction " .. instruction.count .. " times.", settings) debug("Executing repeated instruction " .. instruction.count .. " times.", settings)
for i = 1, instruction.count do for i = 1, instruction.count do
for _, nestedInstruction in pairs(instruction.instructions) do for _, nestedInstruction in pairs(instruction.instructions) do
executeInstruction(nestedInstruction, settings) movescript.executeInstruction(nestedInstruction, settings)
end end
end end
elseif instruction.type == INSTRUCTION_TYPES.instruction then elseif instruction.type == INSTRUCTION_TYPES.instruction then
@ -428,7 +428,7 @@ function movescript.run(script, settings)
debug("Executing script: " .. script, settings) debug("Executing script: " .. script, settings)
local instructions = movescript.parse(script, settings) local instructions = movescript.parse(script, settings)
for idx, instruction in pairs(instructions) do for idx, instruction in pairs(instructions) do
executeInstruction(instruction, settings) movescript.executeInstruction(instruction, settings)
end end
end end