Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local c = require('component')
- local r = require('robot')
- local s = require('serialization')
- local term = require('term')
- local ic = c.inventory_controller
- local skipSlots = {[4]=true,[8]=true,[12]=true}
- local recipiesPath = "/usr/lib/recipies.lua"
- local recipies = {}
- local function getAllStacks()
- local invSize = r.inventorySize()
- if not invSize then
- print("internal inventory size not found")
- return false
- end
- local stacks = {}
- io.read()
- for i = 1, invSize do
- local stack = ic.getStackInInternalSlot(i)
- if stack then
- print("I found "..stack.size.." "..stack.label.."in slot: "..i)
- stacks[stack.label]={
- ["slot"]=i,
- ["qty"]=stack.size
- }
- end
- end
- print("stacks in function"..s.serialize(stacks))
- return stacks
- end
- local function getRequirements(recipe)
- local requirements = {}
- if recipe then
- for k,v in pairs(recipe) do
- if requirements[v] then
- print("requirements[v] = "..v)
- requirements[v].qty = requirements[v].qty+1
- else
- print("adding requirement "..v)
- requirements[v] = { ["qty"] = 1 }
- end
- end
- print("-----REQUIREMENTS-----")
- for k,v in pairs(requirements) do
- print(k, v)
- end
- end
- return requirements
- end
- local function checkReq(recipe)
- local needsMsg = ""
- local reqs = getRequirements(recipe)
- if not reqs then print("CheckReq: Recipe not found")return false, "Recipe not found" end
- local stacks = getAllStacks()
- if not stacks then return false, "No items in my inventory" end
- for k,v in pairs(reqs) do
- if not stacks[k] then
- needsMsg = needsMsg..k..": "..v.qty..", \n"
- elseif stacks[k].qty<v.qty then
- needsMsg = needsMsg..k..": "..v.qty-stacks[k].qty..", \n"
- end
- end
- print("CheckReq needsMsg: "..needsMsg)
- if needsMsg == "" then
- return true
- else
- return false, needsMsg
- end
- end
- local function craft()
- local rec = nil
- repeat
- term.clear()
- print("-----Select a Recipe----")
- for k,v in pairs(recipies) do print(k,s.serialize(v)) end
- local recipeSel = io.read()
- rec = recipies[recipeSel]
- until rec
- local ready, msg = checkReq(rec)
- print("ready? "..tostring(ready), msg)
- if ready then
- local stacks = getAllStacks()
- print("stacks"..s.serialize(stacks))
- print("rec"..s.serialize(rec))
- for k,v in pairs(rec) do
- r.select(stacks[v].slot)
- r.transferTo(k,1)
- end
- print(s.serialize(rec))
- else
- print("recipe not found")
- end
- end
- local function learn()
- local recipe = {}
- for i = 1,11 do
- local stack = ic.getStackInInternalSlot(i)
- if not skipSlots[i] and stack then
- label = ic.getStackInInternalSlot(i).label
- recipe[i]=(label)
- end
- end
- print("Name the recipe")
- recipies[io.read()]=recipe
- end
- learn()
- craft()
- --[[return {
- learn = learn,
- craft = craft,
- }]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement