From 0d9e0d4b9b7c08b8a45c9accf2450a7c601b0d32 Mon Sep 17 00:00:00 2001 From: osmarks Date: Mon, 30 Apr 2018 19:55:50 +0100 Subject: [PATCH] Hopefully working autocrafting --- crafter.lua | 31 ++++++++++++++++++++++++++----- server.lua | 2 +- util.lua | 31 ++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/crafter.lua b/crafter.lua index f1ee76d..bfb2c97 100644 --- a/crafter.lua +++ b/crafter.lua @@ -1,7 +1,8 @@ ---local util = require "util" ---local conf = util.conf +local _ = require "moses" +local util = require "util" +local conf = util.conf -local patterns = loadfile("patterns.lua")() +local patterns = loadfile "patterns.lua"() local function descend(intermediateFn, terminalFn, i) local pattern = patterns[i] @@ -28,7 +29,7 @@ end local function cost(i) local items = {} descend(function() end, function(i) table.insert(items, i) end, i) - return items + return collate(items) end local function tasks(i) @@ -37,12 +38,32 @@ local function tasks(i) return t end +-- Splits "mod:item:meta" into {"mod:item", "meta"} +local function splitItemString(is) + local parts = util.split(is, ":") + return {parts[1] .. parts[2], tonumber(parts[3]) or 0} +end + +local function craftOne(pat) + for slot, itemName in pairs(pat) do + local ispl = splitItemString(itemName) + util.query { cmd = "extract", meta = ispl[2], name = ispl[1], destInv = conf.name, destSlot = slot } + end + turtle.craft() + util.query { cmd = "insert", fromInv = conf.name, fromSlot = 16 } +end + local function craft(i) + turtle.select(16) -- so that crafting outputs go here + local stored = utils.query { cmd = "list" } local reqs = cost(i) if util.satisfied(reqs, stored) then - -- do crafting stuff + local tsks = _.reverse(tasks(i)) -- tasks returns the highest level/most complex/most subtask-requring tasks first. + for _, tsk in pairs(tsks) do + craftOne(tsk) + end else return "ERROR" end diff --git a/server.lua b/server.lua index f97e264..7de84bd 100644 --- a/server.lua +++ b/server.lua @@ -94,7 +94,7 @@ function processRequest(msg) local moved = peripheral.call(conf.bufferOutInternal, "pullItems", inv, slot, msg.qty or 64, 1) if msg.destInv then - moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1) + moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1, 64, msg.destSlot) end return {moved, item} diff --git a/util.lua b/util.lua index 9985402..daee761 100644 --- a/util.lua +++ b/util.lua @@ -39,6 +39,7 @@ function dump(slot) query { cmd = "insert", fromInv = conf.name, fromSlot = slot } end +-- Converts a table of the form {"x", "x", "y"} into {x = 2, y = 1} local function collate(items) local ret = {} for _, i in pairs(items) do @@ -47,6 +48,7 @@ local function collate(items) return ret end +-- Checks whether "needs"'s (a collate-formatted table) values are all greater than those of "has" local function satisfied(needs, has) local good = true for k, qty in pairs(needs) do @@ -55,4 +57,31 @@ local function satisfied(needs, has) return good end -return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied } \ No newline at end of file +-- Python-style version from http://lua-users.org/wiki/SplitJoin +-- Why is this not in the standard Lua library?! +local function split(sSeparator, nMax, bRegexp) + assert(sSeparator ~= '') + assert(nMax == nil or nMax >= 1) + + local aRecord = {} + + if self:len() > 0 then + local bPlain = not bRegexp + nMax = nMax or -1 + + local nField, nStart = 1, 1 + local nFirst,nLast = self:find(sSeparator, nStart, bPlain) + while nFirst and nMax ~= 0 do + aRecord[nField] = self:sub(nStart, nFirst-1) + nField = nField+1 + nStart = nLast+1 + nFirst,nLast = self:find(sSeparator, nStart, bPlain) + nMax = nMax-1 + end + aRecord[nField] = self:sub(nStart) + end + + return aRecord + end + +return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split } \ No newline at end of file