Advertisement
Guest User

recipe.lua

a guest
Feb 6th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local shell = require("shell")
  3. local component = require("component")
  4.  
  5. local args = shell.parse(...)
  6. if (#args < 1) then
  7.   print("use: recipe <item>")
  8. end
  9.  
  10. local item = args[1]
  11. function getRecipe(item)
  12.   local fname = string.gsub(item, ":", "_")
  13.   if (not fs.exists("/home/satori/recipe/" .. fname)) then
  14.     return -1
  15.   end
  16.   local file = io.open(fname, "r")
  17.   local text = file:read("*all")
  18.   local w = "([%a:0]+)"
  19.   local regexp = w .. " " .. w
  20.   .. " " .. w .. "\n"
  21.   .. w .. " " .. w .. " " .. w .. "\n"
  22.   .. w .. " " .. w .. " " .. w
  23.   --print(regexp)
  24.   local _, _,
  25.   i1, i2, i3,
  26.   i4, i5, i6,
  27.   i7, i8, i9
  28.   = string.find(text, regexp)
  29.   local ans = {
  30.     i1, i2, i3, i4, i5, i6, i7, i8, i9
  31.   }
  32.   file:close()
  33.   return ans
  34. end
  35. local testItems = getRecipe(item)
  36. if (testItems == (-1)) then print ("no such item in database") return end
  37. function getDeps(handledRecipe)
  38.   local need = {}
  39.   for i=1,#handledRecipe do
  40.     if (handledRecipe[i] ~= "0") then
  41.       need[handledRecipe[i]] = 0
  42.     end
  43.   end
  44.   for i=1,#handledRecipe do
  45.     if (handledRecipe[i] ~= "0") then
  46.       need[handledRecipe[i]] = need[handledRecipe[i]] + 1
  47.     end
  48.   end
  49.   return need
  50. end
  51. local need = getDeps(testItems)
  52. for a,b in pairs(need) do
  53.   print(a .. ": " .. b)
  54.   local r = getRecipe(a)
  55.   if (r ~= -1) then
  56.     print("or " .. b .. "x")
  57.     for c,d in pairs(getDeps(r)) do
  58.       print(c .. ": " .. d)
  59.     end
  60.   end
  61. end
  62.  
  63. local storage = component.proxy(component.list("controller")(0))
  64. local robot = component.proxy(component.list("robot")(0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement