Fill in outline of autocraft stuff

This commit is contained in:
osmarks 2018-04-29 20:55:16 +01:00
parent b9faa51406
commit ea41e3574f
5 changed files with 87 additions and 8 deletions

51
crafter.lua Normal file
View File

@ -0,0 +1,51 @@
--local util = require "util"
--local conf = util.conf
local patterns = loadfile("patterns.lua")()
local function descend(intermediateFn, terminalFn, i)
local pattern = patterns[i]
if pattern then
intermediateFn(pattern)
local pqty = pattern.qty -- Qty keys must be removed from the pattern for collation
-- Otherwise, it shows up as a number stuck in the items needed table, which is bad.
pattern.qty = nil
local needs = util.collate(pattern)
pattern.qty = pqty
local has = {}
for slot, item in pairs(pattern) do
if util.satisfied(needs, has) then break end
if patterns[item] then
descend(intermediateFn, terminalFn, item)
has[item] = (has[item] or 0) + (patterns[item].qty or 1)
end
end
else
terminalFn(i)
end
end
local function cost(i)
local items = {}
descend(function() end, function(i) table.insert(items, i) end, i)
return items
end
local function tasks(i)
local t = {}
descend(function(pat) table.insert(t, pat) end, function() end, i)
return t
end
local function craft(i)
local stored = utils.query { cmd = "list" }
local reqs = cost(i)
if util.satisfied(reqs, stored) then
-- do crafting stuff
else
return "ERROR"
end
end
return { cost = cost, descend = descend, collate = collate, tasks = tasks }

8
patterns.lua Normal file
View File

@ -0,0 +1,8 @@
local st = "minecraft:stone"
local stbr = "minecraft:stonebrick"
local stbrsl = "minecraft:stone_slab:5"
return {
[stbr] = {st, st, [4] = st, [5] = st, qty = 4},
[stbrsl] = {stbr, stbr, stbr, qty = 6}
}

View File

@ -121,7 +121,7 @@ function processRequest(msg)
return "OK"
-- Returns entire index
elseif msg.cmd == "list" then
return index
return util.collate(index)
-- Looks up supplied name in the cache.
elseif msg.cmd == "name" then
msg.meta = msg.meta or 0

View File

@ -1,19 +1,23 @@
local argv = {...}
local root = "https://osmarks.ml/git/osmarks/dragon/raw/branch/master/"
local function download(name, file)
local contents = http.get(root .. name).readAll()
local function download(url, file)
local contents = http.get(url).readAll()
local f = fs.open(file, "w")
f.write(contents)
f.close()
end
local files = { "client.lua", "server.lua", "util.lua", "setup.lua" }
local files = { "client.lua", "server.lua", "util.lua", "setup.lua", "crafter.lua", "patterns.lua" }
for _, f in pairs(files) do
download(f, f)
download(root .. f, f)
print("Downloaded", f)
end
-- Download functional Lua library
download("https://github.com/Yonaba/Moses/blob/master/moses_min.lua", "moses.lua")
print "Downloaded Moses library"
print "Files downloaded. Either client.lua or server.lua should be run on startup."
if argv[1] == "update" then os.reboot() end
@ -24,9 +28,9 @@ pcall(fs.move, "conf.lua", "conf") -- edit is really stupid, so un-.lua output f
local ty
repeat
print "Would you like this node set up as a server or client?"
print "Would you like this node set up as a server, crafter or client?"
ty = read()
until ty == "server" or ty == "client"
until ty == "server" or ty == "client" or ty == "crafter"
local f = fs.open("startup", "w")
f.write("shell.run '" .. ty .. "'")

View File

@ -39,4 +39,20 @@ function dump(slot)
query { cmd = "insert", fromInv = conf.name, fromSlot = slot }
end
return { conf = conf, query = query, fetch = fetch, dump = dump }
local function collate(items)
local ret = {}
for _, i in pairs(items) do
ret[i] = (ret[i] or 0) + 1
end
return ret
end
local function satisfied(needs, has)
local good = true
for k, qty in pairs(needs) do
if qty > (has[k] or 0) then good = false end
end
return good
end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied }