Advertisement
Zoidbert001

OpenComputers AE2 AutoCrafting V0.1 Beta

Aug 27th, 2018
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. local component = require("component")
  2. local meController = component.proxy(component.me_controller.address)
  3. local gpu = component.gpu
  4.  
  5. -- Each element of the array is "item", "damage", "number wanted", "max craft size"
  6. -- Damage value should be zero for base items
  7.  
  8. maxProcessors = 2
  9. retVals = {}
  10.  
  11. items = {
  12.     { "harvestcraft:delightedmealitem",  0,  64,   8 },
  13.     { "minecraft:ender_pearl",           0, 256,  64 },
  14.     { "wct:infinity_booster_card",       0,  64,   8 },
  15.     { "appliedenergistics2:material",   24,  64,  16 },
  16.     { "appliedenergistics2:material",   23,  64,  16 },
  17.     { "appliedenergistics2:material",   22,  64,  16 },
  18.     { "appliedenergistics2:material",   12,  64,  16 },
  19.     { "appliedenergistics2:material",   10, 128,  32 },
  20.     { "appliedenergistics2:material",    7, 128,  32 },
  21.     { "appliedenergistics2:material",    2, 128,  32 },
  22.     { "appliedenergistics2:material",    1, 256,  64 },
  23.     { "appliedenergistics2:material",    0, 512,  64 },
  24.     { "enderio:item_alloy_ingot",        1,  32,   8 },
  25.     { "minecraft:torch",                 0, 128,  32 },
  26.     { "thermaldynamics:duct_0",          3,  64,  16 },
  27.     { "thermalfoundation:material",    160, 128,  16 },
  28.     { "thermalfoundation:material",    163, 256,  16 },
  29.     { "minecraft:clay_ball",             0, 512, 128 },
  30.     { "minecraft:redstone_torch",        0,  64,  16 },
  31.     { "minecraft:glowstone_dust",        0, 512,  64 },
  32.     { "minecraft:obsidian",              0, 512,  64 },
  33.     { "minecraft:diamond",               0, 512,  64 },
  34.     { "minecraft:emerald",               0, 512,  64 },
  35.     { "ic2:ingot",                       8, 128,  64 },
  36.     { "enderio:item_alloy_ingot",        2,  64,   4 },
  37.     { "minecraft:stick",                 0, 512,  64 },
  38. }
  39.  
  40. loopDelay = 10 -- Seconds between runs
  41. usedProcessors = 0
  42. indexesRetVal = 0
  43.  
  44.  
  45. while true do
  46.     for curIdx = 1, #items do
  47.         curName = items[curIdx][1]
  48.         curDamage = items[curIdx][2]
  49.         curMinValue = items[curIdx][3]
  50.         curMaxRequest = items[curIdx][4]
  51.  
  52.         while usedProcessors == maxProcessors do
  53.           for intRetVals = 1, #retVals do
  54.             pcall(function()
  55.             if retVals[intRetVals].isDone() or retVals[intRetVals].isCanceled() then
  56.               usedProcessors = usedProcessors - 1
  57.               table.remove(retVals, intRetVals)
  58.             end
  59.             end)
  60.            
  61.             -- io.write("CurRetVal" .. intRetVals .. "\n")
  62.           end
  63.           -- io.write("Max Processors reached\n")
  64.           os.sleep(1)
  65.         end          
  66.                
  67.  
  68.           -- io.write("Checking for " .. curMinValue .. " of " .. curName .. "\n")
  69.           storedItem = meController.getItemsInNetwork({
  70.               name = curName,
  71.               damage = curDamage
  72.               })
  73.           io.write("Network contains ")
  74.           gpu.setForeground(0xCC24C0) -- Purple-ish
  75.           io.write(storedItem[1].size)
  76.           gpu.setForeground(0xFFFFFF) -- White
  77.           io.write(" items with label ")
  78.           gpu.setForeground(0x00FF00) -- Green
  79.           io.write(storedItem[1].label .. "\n")
  80.           gpu.setForeground(0xFFFFFF) -- White
  81.           if storedItem[1].size < curMinValue then
  82.               delta = curMinValue - storedItem[1].size
  83.               craftAmount = delta
  84.               if delta > curMaxRequest then
  85.                   craftAmount = curMaxRequest
  86.               end
  87.  
  88.               io.write("  Need to craft ")
  89.               gpu.setForeground(0xFF0000) -- Red
  90.               io.write(delta)
  91.               gpu.setForeground(0xFFFFFF) -- White
  92.               io.write(", requesting ")
  93.               gpu.setForeground(0xCC24C0) -- Purple-ish
  94.               io.write(craftAmount .. "... ")
  95.               gpu.setForeground(0xFFFFFF) -- White
  96.  
  97.               craftables = meController.getCraftables({
  98.                   name = curName,
  99.                   damage = curDamage
  100.                   })
  101.  
  102.               numberCpus = meController.getCpus()
  103.               freeCpu = false
  104.  
  105.               for indexCpu = 1, #numberCpus do
  106.                  freeCpu = freeCpu or not numberCpus[indexCpu].busy
  107.               end
  108.  
  109.               if craftables.n >= 1 then
  110.                   cItem = craftables[1]
  111.                   -- io.write("0\n")
  112.                   if freeCpu and usedProcessors < maxProcessors then
  113.                     -- io.write("1\n")
  114.                     -- indexProcessor = usedProcessors + 1
  115.                     ret = cItem.request(craftAmount)
  116.                     -- table.insert(retVals, ret)
  117.                    
  118.                     if ret.isCanceled() then
  119.                      
  120.                       gpu.setForeground(0xFF0000) -- Red
  121.                       io.write("Fail. Needed Materials not in the System\n")
  122.                     else              
  123.                       table.insert(retVals, ret)    
  124.                       usedProcessors = usedProcessors + 1
  125.                       -- io.write("2\n")
  126.                       gpu.setForeground(0x00FF00) -- Green
  127.                       io.write("OK\n")
  128.                     end
  129.                     -- indexesRetVal = indexesRetVal + 1
  130.                   else
  131.                     gpu.setForeground(0xFF0000) -- Red
  132.                     io.write("Fail. No Free CPU available or max used CPUs reached.\n")
  133.                   end
  134.                   gpu.setForeground(0xFFFFFF) -- White
  135.               else
  136.                   gpu.setForeground(0xFF0000) -- Red
  137.                   io.write("    Unable to locate craftable for " .. storedItem[1].name .. "\n")
  138.                   gpu.setForeground(0xFFFFFF) -- White
  139.               end
  140.           end
  141.        end
  142.  
  143.     intRetValNumber = #retVals
  144.     intRetValDone = 0
  145.     while intRetValDone < intRetValNumber do
  146.       for i = 1, #retVals do
  147.         if retVals[i].isDone() or retVals[i].isCanceled() then intRetValDone = intRetValDone + 1 end
  148.       end
  149.       if intRetValDone < intRetValNumber then intRetValDone = 0 end
  150.       os.sleep(1)
  151.     end
  152.  
  153.     indexRetVal = 0
  154.  
  155.     io.write("Sleeping for " .. loopDelay .. " seconds...\n\n")
  156.     os.sleep(loopDelay)
  157.     term.clear()
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement