Hopefully working autocrafting
This commit is contained in:
parent
ea41e3574f
commit
0d9e0d4b9b
31
crafter.lua
31
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
|
||||
|
@ -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}
|
||||
|
31
util.lua
31
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 }
|
||||
-- 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 }
|
Loading…
Reference in New Issue
Block a user