Advertisement
Guest User

16k

a guest
Jan 25th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1.  
  2. -- Recipes to build
  3. recipes = {
  4. storagecell = {
  5.   redstone = {1,3,9,11},
  6.   quartzcrystal = {2,5,7,10},
  7.   mebasicprocessor = {6}
  8. },
  9.  
  10. storagesegment = {
  11.     redstone = {1,3,9,11},
  12.     mebasicprocessor = {2},
  13.     glass = {6},
  14.     storagecell = {5,6,10}
  15. },
  16.  
  17. storageblock = {
  18.     glowstone = {1,3,9,11},
  19.     storagesegment = {5,6,10},
  20.     glass = {6},
  21.     meadvancedprocessor = {2}
  22. },
  23.  
  24. me16kstorage = {
  25.     glass = {1,3},
  26.     redstone = {2,5,7},
  27.     storageblock = {6},
  28.     iron = {9,10,11}
  29. }
  30. }
  31.  
  32. appen1 = "appeng.materials." -- Need theese to get the correct name from applied energistics
  33. appen2 = ".name"
  34.  
  35.  
  36.  
  37. chest = peripheral.wrap("left")
  38.  
  39. function getSlot(slot)
  40.     local info = chest.getStackInSlot(slot)
  41.     if info then
  42.         return info
  43.     else
  44.         return false
  45.     end
  46. end
  47.  
  48. function split(inputstr, sep) -- splits a string into a table
  49.     if sep == nil then
  50.         sep = "%s"
  51.     end
  52.     t={} ; i=1
  53.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  54.         t[i] = str
  55.         i = i + 1
  56.     end
  57.     return t
  58. end
  59.  
  60. function getName(slot) -- Returns the name, And if its applied energistics remove the long names
  61.     local convert = {
  62.         appen1.."quartzcrystal"..appen2,
  63.         appen1.."mebasicprocessor"..appen2,
  64.         appen1.."storagecell"..appen2,
  65.         appen1.."storagesegment"..appen2,
  66.         appen1.."storageblock"..appen2,
  67.         appen1.."meadvancedprocessor"..appen2
  68.     }
  69.     local info = getSlot(slot)
  70.     if info then
  71.         info.name = string.lower(info.name)
  72.         for k,v in pairs(convert) do -- Search through table and find appen names
  73.             if info.name == v then
  74.                 info.name = split(info.name, ".")
  75.                 info.name = info.name[3]
  76.                 return info.name
  77.             end
  78.         end
  79.         return info.name
  80.     else
  81.         return false
  82.     end
  83. end
  84.  
  85. function getQty(slot)
  86.     local info = getSlot(slot)
  87.     if info then
  88.         return info.qty
  89.     else
  90.         return false
  91.     end
  92. end
  93.  
  94. function getChest() -- Returns the entire inventory of the chest as a table for faster access
  95.     local size = chest.getInventorySize()
  96.     local content = {}
  97.     chest.condenseItems()
  98.     for i = 1, size do
  99.         local name = getName(i)
  100.         local qty = getQty(i)
  101.         if name then
  102.             if content[name] then
  103.                 content[name] = content[name] + qty
  104.             else
  105.                 content[name] = qty
  106.             end
  107.         else
  108.             break
  109.         end
  110.     end
  111.     return content
  112. end
  113.  
  114. function getRecipe(name) -- Creates a table which displays needed item and quantity instead of need item and position as recipes does
  115.     local recipe = {}
  116.     for k,v in pairs(name) do
  117.         if recipe[k] then
  118.             recipe[k] = recipe[k] + #v
  119.         else
  120.             recipe[k] = #v
  121.         end
  122.     end
  123.     return recipe
  124. end
  125.  
  126. function compare(name, qty) -- Checks if name is in the chest, aswell as the correct quantity or more
  127.     local content = getChest()
  128.     local foundName = ""
  129.     local foundQty = 0
  130.     for k,v in pairs(content) do
  131.         if k == name then
  132.             if v >= qty then
  133.                 return true
  134.             else
  135.                 foundQty = v or 0
  136.             end
  137.         else
  138.             foundName = k
  139.         end
  140.     end
  141.     return false, foundQty
  142. end
  143.  
  144. function printT(t)
  145.     for k,v in pairs(t) do
  146.         print(k..": "..v)
  147.     end
  148. end
  149.  
  150. function gotRecipe(recipe, amount) -- Checks a recipe if everything is in the chest, Returns missing materials
  151.     amount = amount or 1
  152.     local missing = {}
  153.     local content = getChest()
  154.     local recipe = getRecipe(recipe)
  155.     for itemName,itemQty in pairs(recipe) do
  156.         found, qty = compare(itemName, itemQty * amount)
  157.         if not found then
  158.             if missing[itemName] then
  159.                 missing[itemName] = missing[itemName] + ((itemQty*amount)-qty)
  160.             else
  161.                 missing[itemName] = itemQty*amount-qty
  162.             end
  163.         end
  164.     end
  165.     return missing
  166. end
  167.  
  168. function isRecipe(name) -- Checks if name is a recipe in this code or not
  169.     if recipes[name] then
  170.         return true
  171.     else
  172.         return false
  173.     end
  174. end
  175.  
  176. function gotEnough(recipe, amount)
  177.     amount = amount or 1
  178.     local missing = gotRecipe(recipe, amount)
  179.     if missing then
  180.         for k,v in pairs(missing) do
  181.             if isRecipe(k) then
  182.                
  183.             end
  184.         end
  185.     end
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement