SHARE
TWEET

OpenComputers AE2 AutoCrafting V1

Zoidbert001 Aug 29th, 2018 87 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.   for itemIndex = 1, #items do
  30.     itemName = items[itemIndex][1]
  31.     itemDamage = items[itemIndex][2]
  32.     itemMinValue = items[itemIndex][3]
  33.     itemMaxCraftValue = items[itemIndex][4]
  34.     meItem = meController.getItemsInNetwork({ name = itemName, damage = itemDamage})
  35.     itemLabel = meItem[1].label
  36.     itemCurrentValue = meItem[1].size
  37.    
  38.     currentItem = { name = itemName, damage = itemDamage, minValue = itemMinValue, maxCraftValue = itemMaxCraftValue, label = itemLabel, currentValue = itemCurrentValue, currentCrafting = 0, error = false }
  39.     table.insert(displayTable, currentItem)
  40.  
  41.     displayColumns()
  42.  
  43.     initDone = true
  44.   end
  45. end
  46.  
  47. function dump(o)
  48.    if type(o) == 'table' then
  49.       local s = '{ '
  50.       for k,v in pairs(o) do
  51.          if type(k) ~= 'number' then k = '"'..k..'"' end
  52.          s = s .. '['..k..'] = ' .. dump(v) .. ','
  53.       end
  54.       return s .. '} '
  55.    else
  56.       return tostring(o)
  57.    end
  58. end
  59.  
  60. function displayColumns()
  61.   term.clear()
  62.  
  63.   io.write("|-------------Name-------------|-Stock-|-Craft-|-----Fail-----|")
  64.  
  65.   if twoColumnMode then
  66.     io.write("          |-------------Name-------------|-Stock-|-Craft-|-----Fail-----|")
  67.   end
  68.  
  69.   io.write("\n")
  70.  
  71.   io.write("|-------------------------------------------------------------|")
  72.  
  73.   if twoColumnMode then
  74.     io.write("          |-------------------------------------------------------------|")
  75.   end
  76.  
  77.   leftColumnIndex = 1
  78.   rightColumnIndex = 74
  79.   k = 1
  80.  
  81.   lineNumber = 3
  82.  
  83.   for itemIndex = 1, #displayTable do
  84.     maxLabelLength = 30
  85.     label = displayTable[itemIndex].label
  86.     labelLength = string.len(label)
  87.     if labelLength > maxLabelLength then
  88.       label = string.sub(displayTable[itemIndex].label, 1, maxLabelLenth -3)
  89.     end
  90.  
  91.     labelLength = string.len(label)
  92.     pos = leftColumnIndex
  93.     if twoColumnMode and k % 2 == 0 then pos = rightColumnIndex end
  94.     gpu.set(pos, lineNumber, "|")
  95.  
  96.     gpu.setForeground(0xFFC125)
  97.     gpu.set(pos + 1, lineNumber, label)
  98.    
  99.     gpu.setForeground(0xFFFFFF)
  100.     gpu.set(pos + 31, lineNumber, "|       |       |              |")
  101.    
  102.     lineNumber = lineNumber + 1
  103.     if twoColumnMode and k % 2 == 1 then
  104.       lineNumber = lineNumber - 1
  105.     end
  106.  
  107.     k = k + 1
  108.   end
  109. end
  110.  
  111. function displayTableValues()
  112.   leftColumnIndex = 1
  113.   rightColumnIndex = 74
  114.   k = 1
  115.  
  116.   lineNumber = 3
  117.  
  118.   for itemIndex = 1, #displayTable do
  119.     pos = leftColumnIndex
  120.     if twoColumnMode and k % 2 == 0 then
  121.       pos = rightColumnIndex
  122.     end
  123.  
  124.     if displayTable[itemIndex].currentValue < displayTable[itemIndex].minValue then
  125.       gpu.setForeground(0xFF0000)
  126.     else
  127.       gpu.setForeground(0x00FF00)
  128.     end
  129.  
  130.     amountLength = string.len(dump(displayTable[itemIndex].currentValue))
  131.     spaceLength = 7 - amountLength
  132.     amountString = dump(displayTable[itemIndex].currentValue)
  133.     for i = 1, spaceLength do
  134.       amountString = " " .. amountString
  135.     end
  136.     gpu.set(pos + 32, lineNumber, amountString)
  137.  
  138.     if displayTable[itemIndex].currentCrafting > 0 then
  139.       craftLength = string.len(dump(displayTable[itemIndex].currentCrafting))
  140.       spaceLength = 7 - craftLength
  141.       craftString = dump(displayTable[itemIndex].currentCrafting)
  142.       for i = 1, spaceLength do
  143.         craftString = " " .. craftString
  144.       end
  145.      
  146.       gpu.setForeground(0x2F95D7)
  147.       gpu.set(pos + 40, lineNumber, craftString)
  148.     else
  149.       gpu.setForeground(0xFFFFFF)
  150.       gpu.set(pos + 40, lineNumber, "       ")
  151.     end
  152.  
  153.     if displayTable[itemIndex].error then
  154.       gpu.setForeground(0xFF0000)
  155.       gpu.set(pos + 48, lineNumber, "No ingredients")
  156.     else
  157.       gpu.setForeground(0xFFFFFF)
  158.       gpu.set(pos + 48, lineNumber, "              ")
  159.     end
  160.    
  161.     lineNumber = lineNumber + 1
  162.     if twoColumnMode and k % 2 == 1 then
  163.       lineNumber = lineNumber - 1
  164.     end
  165.  
  166.     k = k + 1
  167.   end
  168. end
  169.  
  170. while true do
  171.   if not initDone then
  172.     initDisplayTable()
  173.   end
  174.   -- term.clear()
  175.   for itemIndex = 1, #displayTable do  
  176.     meItem = meController.getItemsInNetwork({ name = displayTable[itemIndex].name, damage = displayTable[itemIndex].damage})
  177.     displayTable[itemIndex].currentValue = meItem[1].size
  178.  
  179.     for indexReturnValue = 1, #returnValues do
  180.       pcall(
  181.         function()
  182.           if returnValues[indexReturnValue].isDone() or returnValues[indexReturnValue].isCanceled() then
  183.             currentCpuUsage = currentCpuUsage - 1
  184.             table.remove(returnValues, indexReturnValue)
  185.           end
  186.         end
  187.       )
  188.     end
  189.    
  190.  
  191.     if displayTable[itemIndex].currentValue < displayTable[itemIndex].minValue and currentCpuUsage < maxCpuUsage then
  192.       delta = displayTable[itemIndex].minValue - displayTable[itemIndex].currentValue
  193.       craftAmount = delta
  194.       if delta > displayTable[itemIndex].maxCraftValue then
  195.         craftAmount = displayTable[itemIndex].maxCraftValue
  196.       end
  197.      
  198.       craftables = meController.getCraftables({ name = displayTable[itemIndex].name, damage = displayTable[itemIndex].damage})
  199.      
  200.       if craftables.n >= 1 then
  201.         craftItem = craftables[1]
  202.         returnValue = craftItem.request(craftAmount)
  203.        
  204.         if returnValue.isCanceled() then
  205.           displayTable[itemIndex].error = true
  206.         else
  207.           displayTable[itemIndex].error = false
  208.           displayTable[itemIndex].currentCrafting = craftAmount
  209.           table.insert(returnValues, returnValue)
  210.           currentCpuUsage = currentCpuUsage + 1
  211.         end
  212.       end
  213.     else
  214.       displayTable[itemIndex].currentCrafting = 0
  215.       displayTable[itemIndex].error = false
  216.     end
  217.  
  218.     displayTableValues()
  219.   end
  220.  
  221.   os.sleep(loopDelay)
  222. 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