Advertisement
hyndgrinder

/Woodhouse/crafting

Sep 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. local c = require('component')
  2. local r = require('robot')
  3. local s = require('serialization')
  4. local term = require('term')
  5. local ic = c.inventory_controller
  6.  
  7. local skipSlots = {[4]=true,[8]=true,[12]=true}
  8. local recipiesPath = "/usr/lib/recipies.lua"
  9.  
  10. local recipies = {}
  11.  
  12.   local function getAllStacks()
  13.     local invSize = r.inventorySize()
  14.     if not invSize then
  15.       print("internal inventory size not found")
  16.       return false
  17.     end
  18.     local stacks = {}
  19.         io.read()
  20.     for i = 1, invSize do
  21.       local stack = ic.getStackInInternalSlot(i)
  22.       if stack then
  23.         print("I found "..stack.size.." "..stack.label.."in slot: "..i)
  24.         stacks[stack.label]={
  25.             ["slot"]=i,
  26.             ["qty"]=stack.size
  27.          }
  28.       end
  29.     end
  30.       print("stacks in function"..s.serialize(stacks))
  31.       return stacks        
  32.   end
  33.  
  34.   local function getRequirements(recipe)
  35.     local requirements = {}
  36.     if recipe then
  37.       for k,v in pairs(recipe) do
  38.         if requirements[v] then
  39.             print("requirements[v] = "..v)
  40.           requirements[v].qty = requirements[v].qty+1
  41.         else
  42.             print("adding requirement "..v)
  43.           requirements[v] = { ["qty"] = 1 }
  44.         end
  45.       end
  46.       print("-----REQUIREMENTS-----")
  47.       for k,v in pairs(requirements) do
  48.         print(k, v)
  49.       end
  50.     end
  51.     return requirements
  52.   end
  53.  
  54.   local function checkReq(recipe)
  55.     local needsMsg = ""
  56.     local reqs = getRequirements(recipe)
  57.     if not reqs then print("CheckReq: Recipe not found")return false, "Recipe not found" end
  58.     local stacks = getAllStacks()
  59.     if not stacks then return false, "No items in my inventory" end
  60.     for k,v in pairs(reqs) do
  61.       if not stacks[k] then
  62.         needsMsg = needsMsg..k..": "..v.qty..", \n"
  63.       elseif stacks[k].qty<v.qty then
  64.         needsMsg = needsMsg..k..": "..v.qty-stacks[k].qty..", \n"
  65.       end  
  66.     end
  67.     print("CheckReq needsMsg: "..needsMsg)
  68.     if needsMsg == "" then
  69.       return true
  70.     else
  71.       return false, needsMsg
  72.     end
  73.   end
  74.  
  75. local function craft()
  76.   local rec = nil
  77.   repeat
  78.     term.clear()
  79.     print("-----Select a Recipe----")
  80.     for k,v in pairs(recipies) do print(k,s.serialize(v)) end
  81.     local recipeSel = io.read()
  82.     rec = recipies[recipeSel]
  83.   until  rec
  84.   local ready, msg = checkReq(rec)
  85.   print("ready? "..tostring(ready), msg)    
  86.   if ready then
  87.     local stacks = getAllStacks()
  88.     print("stacks"..s.serialize(stacks))
  89.     print("rec"..s.serialize(rec))
  90.     for k,v in pairs(rec) do
  91.       r.select(stacks[v].slot)
  92.       r.transferTo(k,1)
  93.     end
  94.     print(s.serialize(rec))
  95.   else
  96.     print("recipe not found")
  97.   end
  98. end
  99.  
  100.  
  101. local function learn()
  102.   local recipe = {}
  103.   for i = 1,11 do
  104.     local stack  = ic.getStackInInternalSlot(i)
  105.     if not  skipSlots[i] and  stack then
  106.       label = ic.getStackInInternalSlot(i).label
  107.       recipe[i]=(label)
  108.     end
  109.   end
  110.   print("Name the recipe")
  111.   recipies[io.read()]=recipe
  112.  
  113. end
  114.  
  115. learn()
  116. craft()
  117.  
  118. --[[return {
  119.   learn  = learn,
  120.   craft = craft,
  121.  }]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement