SHARE
TWEET

OpenComputers AE2 AutoCrafting V1.1

Zoidbert001 Aug 30th, 2018 300 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local term = require("term")
  3. local string = require("string")
  4. local meController = component.proxy(component.me_controller.address)
  5. local gpu = component.gpu
  6.  
  7. -- Each element of the array is "item, "damage", "number wanted", "max craft size"
  8. -- Damage value should be zero for base items
  9.  
  10. items = {
  11.   { "minecraft:ender_pearl",          0,  256, 32 },
  12.   { "minecraft:torch",                0,  100, 32 },
  13.   { "thermalexpansion:frame",         0,   10, 2  },
  14. }
  15.  
  16. loopDelay = 1 -- Seconds between runs
  17. maxCpuUsage = 1 -- How many CPUs the program can use
  18. twoColumnMode = true -- Show to columns on the screen
  19.  
  20.  
  21. -- Do not touch this variables
  22. currentCpuUsage = 0
  23. returnValues = {}
  24. displayTable = {}
  25. initDone = false
  26. --
  27.  
  28. function initDisplayTable()
  29.  
  30.   for itemIndex = 1, #items do
  31.     itemName = items[itemIndex][1]
  32.     itemDamage = items[itemIndex][2]
  33.     itemMinValue = items[itemIndex][3]
  34.     itemMaxCraftValue = items[itemIndex][4]
  35.     meItem = meController.getItemsInNetwork({ name = itemName, damage = itemDamage})
  36.     itemLabel = meItem[1].label
  37.     itemCurrentValue = meItem[1].size
  38.    
  39.     currentItem = { name = itemName, damage = itemDamage, minValue = itemMinValue, maxCraftValue = itemMaxCraftValue, label = itemLabel, currentValue = itemCurrentValue, currentCrafting = 0, error = false, errorCode = 0 }
  40.     table.insert(displayTable, currentItem)
  41.  
  42.     displayColumns()
  43.  
  44.     initDone = true
  45.   end
  46. end
  47.  
  48. function dump(o)
  49.    if type(o) == 'table' then
  50.       local s = '{ '
  51.       for k,v in pairs(o) do
  52.          if type(k) ~= 'number' then k = '"'..k..'"' end
  53.          s = s .. '['..k..'] = ' .. dump(v) .. ','
  54.       end
  55.       return s .. '} '
  56.    else
  57.       return tostring(o)
  58.    end
  59. end
  60.  
  61. function displayColumns()
  62.   term.clear()
  63.  
  64.   io.write("|-------------Name-------------|-Stock-|-Craft-|-----Fail-----|")
  65.  
  66.   if twoColumnMode then
  67.     io.write("          |-------------Name-------------|-Stock-|-Craft-|-----Fail-----|")
  68.   end
  69.  
  70.   io.write("\n")
  71.  
  72.   io.write("|-------------------------------------------------------------|")
  73.  
  74.   if twoColumnMode then
  75.     io.write("          |-------------------------------------------------------------|")
  76.   end
  77.  
  78.   leftColumnIndex = 1
  79.   rightColumnIndex = 74
  80.   k = 1
  81.  
  82.   lineNumber = 3
  83.  
  84.   for itemIndex = 1, #displayTable do
  85.     maxLabelLength = 30
  86.     label = displayTable[itemIndex].label
  87.     labelLength = string.len(label)
  88.     if labelLength > maxLabelLength then
  89.       label = string.sub(displayTable[itemIndex].label, 1, maxLabelLenth -3)
  90.     end
  91.  
  92.     labelLength = string.len(label)
  93.     pos = leftColumnIndex
  94.     if twoColumnMode and k % 2 == 0 then pos = rightColumnIndex end
  95.     gpu.set(pos, lineNumber, "|")
  96.  
  97.     gpu.setForeground(0xFFC125)
  98.     gpu.set(pos + 1, lineNumber, label)
  99.    
  100.     gpu.setForeground(0xFFFFFF)
  101.     gpu.set(pos + 31, lineNumber, "|       |       |              |")
  102.    
  103.     lineNumber = lineNumber + 1
  104.     if twoColumnMode and k % 2 == 1 then
  105.       lineNumber = lineNumber - 1
  106.     end
  107.  
  108.     k = k + 1
  109.   end
  110. end
  111.  
  112. function displayTableValues()
  113.   leftColumnIndex = 1
  114.   rightColumnIndex = 74
  115.   k = 1
  116.  
  117.   lineNumber = 3
  118.  
  119.   for itemIndex = 1, #displayTable do
  120.     pos = leftColumnIndex
  121.     if twoColumnMode and k % 2 == 0 then
  122.       pos = rightColumnIndex
  123.     end
  124.  
  125.     if displayTable[itemIndex].currentValue < displayTable[itemIndex].minValue then
  126.       gpu.setForeground(0xFF0000)
  127.     else
  128.       gpu.setForeground(0x00FF00)
  129.     end
  130.  
  131.     amountLength = string.len(dump(displayTable[itemIndex].currentValue))
  132.     spaceLength = 7 - amountLength
  133.     amountString = dump(displayTable[itemIndex].currentValue)
  134.     for i = 1, spaceLength do
  135.       amountString = " " .. amountString
  136.     end
  137.     gpu.set(pos + 32, lineNumber, amountString)
  138.  
  139.     if displayTable[itemIndex].currentCrafting > 0 then
  140.       craftLength = string.len(dump(displayTable[itemIndex].currentCrafting))
  141.       spaceLength = 7 - craftLength
  142.       craftString = dump(displayTable[itemIndex].currentCrafting)
  143.       for i = 1, spaceLength do
  144.         craftString = " " .. craftString
  145.       end
  146.      
  147.       gpu.setForeground(0x2F95D7)
  148.       gpu.set(pos + 40, lineNumber, craftString)
  149.     else
  150.       gpu.setForeground(0xFFFFFF)
  151.       gpu.set(pos + 40, lineNumber, "       ")
  152.     end
  153.  
  154.     if displayTable[itemIndex].error then
  155.       gpu.setForeground(0xFF0000)
  156.       if displayTable[itemIndex].errorCode == 1 then
  157.         gpu.set(pos + 48, lineNumber, "  No recipe   ")
  158.       end
  159.       if displayTable[itemIndex].errorCode == 2 then
  160.         gpu.set(pos + 48, lineNumber, "No ingredients")
  161.       end
  162.       if displayTable[itemIndex].errorCode == 3 then
  163.         gpu.set(pos + 48, lineNumber, "All Cpus busy ")
  164.       end
  165.       if displayTable[itemIndex].errorCode == 0 then
  166.         gpu.set(pos + 48, lineNumber, "              ")
  167.       end
  168.     else
  169.       gpu.setForeground(0xFFFFFF)
  170.       gpu.set(pos + 48, lineNumber, "              ")
  171.     end
  172.    
  173.     lineNumber = lineNumber + 1
  174.     if twoColumnMode and k % 2 == 1 then
  175.       lineNumber = lineNumber - 1
  176.     end
  177.  
  178.     k = k + 1
  179.   end
  180. end
  181.  
  182. while true do
  183.   if not initDone then
  184.     initDisplayTable()
  185.   end
  186.   -- term.clear()
  187.   for itemIndex = 1, #displayTable do  
  188.     displayTable[itemIndex].error = false
  189.     displayTable[itemIndex].errorCode = 0
  190.     meItem = meController.getItemsInNetwork({ name = displayTable[itemIndex].name, damage = displayTable[itemIndex].damage})
  191.     if not meItem[1].isCraftable then
  192.       displayTable[itemIndex].error = true
  193.       displayTable[itemIndex].errorCode = 1
  194.     end
  195.  
  196.     displayTable[itemIndex].currentValue = meItem[1].size
  197.  
  198.     for indexReturnValue = 1, #returnValues do
  199.       pcall(
  200.         function()
  201.           if returnValues[indexReturnValue].isDone() or returnValues[indexReturnValue].isCanceled() then
  202.             currentCpuUsage = currentCpuUsage - 1
  203.             table.remove(returnValues, indexReturnValue)
  204.           end
  205.         end
  206.       )
  207.     end
  208.    
  209.  
  210.     if displayTable[itemIndex].currentValue < displayTable[itemIndex].minValue and currentCpuUsage < maxCpuUsage then
  211.       tmpCpus = meController.getCpus()
  212.       occupiedCpus = 0
  213.       for cpuIndex = 1, #tmpCpus do
  214.         if tmpCpus[cpuIndex].busy then occupiedCpus = occupiedCpus + 1 end
  215.       end
  216.  
  217.       if #tmpCpus == occupiedCpus then
  218.         displayTable[itemIndex].error = true
  219.         displayTable[itemIndex].errorCode = 3
  220.       else
  221.         delta = displayTable[itemIndex].minValue - displayTable[itemIndex].currentValue
  222.         craftAmount = delta
  223.         if delta > displayTable[itemIndex].maxCraftValue then
  224.           craftAmount = displayTable[itemIndex].maxCraftValue
  225.         end
  226.        
  227.         craftables = meController.getCraftables({ name = displayTable[itemIndex].name, damage = displayTable[itemIndex].damage})
  228.        
  229.         if craftables.n >= 1 then
  230.           craftItem = craftables[1]
  231.           returnValue = craftItem.request(craftAmount)
  232.          
  233.           if returnValue.isCanceled() then
  234.             displayTable[itemIndex].error = true
  235.             displayTable[itemIndex].errorCode = 2
  236.           else
  237.             displayTable[itemIndex].currentCrafting = craftAmount
  238.             table.insert(returnValues, returnValue)
  239.             currentCpuUsage = currentCpuUsage + 1
  240.           end
  241.         end
  242.       end
  243.     end
  244.  
  245.     displayTableValues()
  246.   end
  247.  
  248.   os.sleep(loopDelay)
  249. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top