Archchancellor64

AE2/ComputerCraft Autostocker

Dec 1st, 2020 (edited)
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. -- turn this on to enable debug logging
  2. debugLogging = false
  3. -- where to write the things
  4. logfile = fs.open("/autocrafter9000.log", "w")
  5.  
  6. -- Initialize ME Interface
  7. interface = peripheral.wrap("right")
  8.  
  9. -- Initialize Monitor
  10. monitor = peripheral.wrap("left")
  11. monitor.setBackgroundColor(colors.black)
  12. monitor.setTextColor(colors.black)
  13. monitor.setTextScale(1)
  14. monitor.clear()
  15.  
  16. -- time to sleep between checks
  17. sleepSeconds = .05
  18.  
  19. -- Max Crafting CPUs to be active at once
  20. maxCPUs = 8
  21.  
  22. -- Number of jobs started by this code rather than manually
  23. autocraftJobs = 0
  24.  
  25. -- Stock Table
  26. stock = {
  27.   { name = "Enriched Alloy", id = "mekanism:enrichedalloy", quantity = 60000, maxCraft = 2000},
  28.   { name = "Reinforced Alloy", id = "mekanism:reinforcedalloy", quantity = 20000, maxCraft = 2000},
  29.   { name = "Atomic Alloy", id = "mekanism:atomicalloy", quantity = 10000, maxCraft = 2000},
  30.   { name = "Energy Tablet", id = "mekanism:energytablet", quantity = 20000, maxCraft = 1000},
  31.   { name = "Ultimate Energy Cube", id = "mekanism:energycube", quantity = 64, maxCraft = 4, metadata = {"displayName", "Ultimate Energy Cube"}},
  32.   { name = "Ultimate Induction Cell", id = "mekanism:basicblock2@3", quantity = 64, maxCraft = 1, metadata = {"displayName", "Ultimate Induction Cell"}},
  33.   { name = "Lithium Dust", id = "ic2:dust@11", quantity = 10000},
  34.   { name = "Logic Processor", id = "appliedenergistics2:material@22", quantity = 16000, maxCraft = 400},
  35.   { name = "Calculation Processor", id = "appliedenergistics2:material@23", quantity = 16000, maxCraft = 400},
  36.   { name = "Engineering Processor", id = "appliedenergistics2:material@24", quantity = 16000, maxCraft = 400}
  37. }
  38.  
  39. -- Logs event to file and prints to screen
  40. function log(str)
  41.   logfile.writeLine(str)
  42.   print(str)
  43. end
  44.  
  45. -- Logs event to file and prints to screen if debug is set to true
  46. function debug(str)
  47.   if debugLogging then
  48.     log(str)
  49.   end
  50. end
  51.  
  52. -- Tests if any crafting CPUs are in use
  53. function isAvailableCpu(interface)
  54.   cpus = interface.getCraftingCPUs()
  55.   count = 0
  56.   for cpu, metadata in pairs(cpus) do
  57.    if metadata.busy == true then
  58.     count = count + 1
  59.    end
  60.   end
  61.   return (count == autocraftJobs) and (count < maxCPUs)
  62. end
  63.  
  64. -- Finds the item in the attached ME network
  65. function findItem(stockInfo)
  66.   items = interface.findItems(stockInfo["id"])
  67.   if items[1] then
  68.     if stockInfo["metadata"] then
  69.       meta = stockInfo["metadata"]
  70.       for i, item in ipairs(items) do
  71.         if item.getMetadata()[meta[1]] == meta[2] then
  72.           items = {item}
  73.         end
  74.       end
  75.     end
  76.     return items[1]
  77.   end
  78. end
  79.  
  80. function padStringRight(str, length)
  81.   pad = length - string.len(str)
  82.   for i = 1, pad do
  83.     str = str .. " "
  84.   end
  85.   return str
  86. end
  87.  
  88. -- Outputs GUI to attached monitor
  89. function drawGUI()
  90.   x, y = monitor.getSize()
  91.   for i, stockInfo in ipairs(stock) do
  92.     monitor.setCursorPos(1, y)
  93.     item = findItem(stockInfo)
  94.     if item.getMetadata().count >= stockInfo.quantity then
  95.       monitor.setBackgroundColor(colors.green)
  96.     elseif stockInfo["job"] then
  97.       monitor.setBackgroundColor(colors.lightBlue)
  98.     else
  99.       monitor.setBackgroundColor(colors.red)
  100.     end
  101.     str = padStringRight(findItem(stockInfo).getMetadata().count .. "/" .. stockInfo["quantity"], 12) .. " | " .. stockInfo["name"]
  102.     monitor.write(padStringRight(str, x))
  103.     y = y - 1
  104.   end
  105. end
  106.  
  107. -- Initialize connected monitor
  108. if monitor then
  109.   monitor.setCursorBlink(false)
  110.   monitor.clear()
  111. end
  112.  
  113. -- Core autostocking loop
  114. while true do
  115.   -- For each item in the Stock Table
  116.   for i, stockInfo in ipairs(stock) do
  117.     id = stockInfo["id"]
  118.     quantity = stockInfo["quantity"]
  119.    
  120.     debug("Testing Stock for " .. id)
  121.    
  122.     -- Find item in attached network
  123.     item = findItem(stockInfo)
  124.     if item then
  125.       metadata = item.getMetadata()
  126.       count = metadata.count
  127.       debug("Got network item " .. id .. " qty " .. count)
  128.     else break end
  129.    
  130.     -- Test if any open jobs for this item have finished
  131.     if (stockInfo["job"] and stockInfo["job"].isFinished()) then
  132.       stockInfo["job"] = nil
  133.       autocraftJobs = autocraftJobs - 1
  134.       log("Crafting Job for " .. id .. " finished")
  135.     end
  136.      
  137.     -- Check that crafting is necessary
  138.     if quantity > count and stockInfo["job"] == nil and redstone.getInput("top") then
  139.       if isAvailableCpu(interface) then
  140.      
  141.         -- Check max craft size
  142.         if stockInfo["maxCraft"] then
  143.           craftAmount = math.min(quantity - count, stockInfo["maxCraft"])
  144.         else
  145.           craftAmount = quantity - count
  146.         end
  147.        
  148.         -- Start crafting job
  149.         job = item.craft(craftAmount)
  150.         log("Crafting " .. craftAmount .. " of " .. id)
  151.         if job.status() == "missing" then
  152.           log("Missing Ingredients to craft " .. id)
  153.         else
  154.           stockInfo["job"] = job
  155.           autocraftJobs = autocraftJobs + 1
  156.         end
  157.       else
  158.         debug("Missing CPU to craft " .. id)
  159.       end
  160.     end
  161.     drawGUI()
  162.   end
  163.   os.sleep(sleepSeconds)
  164. end
Add Comment
Please, Sign In to add comment