ivanzrer

colony_v3

Nov 1st, 2024 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. ci = peripheral.find("colonyIntegrator")  or error("No CoIn attached", 0)
  2. bridge = peripheral.find("meBridge")  or error("No meBridge attached", 0)
  3.  
  4. -- ME system extraction
  5. function meExtract (eItem, eCount, eTable)
  6.     sItem = tostring(eItem)
  7.     --print(sItem)
  8.  
  9.     -- export from system
  10.     function exporter (sItem, eCount)
  11.         result, err = bridge.exportItem({name=sItem, count=eCount}, "up")
  12.         if err then
  13.             print("export failure")
  14.         else    
  15.             print("exporting ", result)
  16.             print("export success")
  17.         end
  18.     end
  19.  
  20.     -- craft resource
  21.     function crafter (sItem, eCount)
  22.         if craftSuccess == bridge.craftItem({name=sItem, count=eCount}) then
  23.             print("ME crafting success")
  24.             return true
  25.         else
  26.             return false
  27.         end
  28.     end
  29.  
  30.     -- check for item in storage
  31.     hasItem, err = bridge.getItem({name = eTable.name})
  32.     if err or (hasItem.name == nil) then
  33.         --doesnt have it
  34.         print("None found..")
  35.         if hasItem.isCraftable then
  36.             -- Can be crafted
  37.             print("Attempting ME crafting..")
  38.             if crafter(sItem, eCount) then
  39.                 exporter(sItem, eCount)
  40.             else
  41.                 print("ME Craft failure")
  42.                 print("Unable to provide")
  43.             end
  44.         else
  45.             -- Cannot be crafted
  46.             print("ME Crafting unable")
  47.             print("Unable to provide")
  48.         end
  49.  
  50.     else
  51.         --has it
  52.         if hasItem.amount >= eCount then
  53.             -- attempt export
  54.             exporter(sItem, eCount)
  55.         else
  56.             print("ME system cannot provide full amount")
  57.             print("Attempting to craft remainder...")
  58.  
  59.             restCount = eCount - hasItem.amount
  60.             if crafter(sItem, restCount) then
  61.                 exporter(sItem, eCount)
  62.             else
  63.                 print("Crafting failure")
  64.                 print("exporting: ", hasItem.amount, " of ", eCount)
  65.                 exporter(sItem, hasItem.amount)
  66.             end
  67.         end
  68.     end
  69.    
  70. end
  71.  
  72. -- retreive active work orders
  73. woOrder = ci.getWorkOrders()
  74.  
  75. -- Check for availability of resources in work orders
  76. -- loop through work orders
  77. for i=1, #woOrder,1 do
  78.     print("Checking work order number ", i, " of ", #woOrder)
  79.     -- retrieve table for resources for work order
  80.     woID = tonumber(woOrder[i].id)
  81.     woOrResources = ci.getWorkOrderResources(woID)
  82.    
  83.     -- check through resources
  84.     if woOrResources ~= nil then
  85.         for r=1, #woOrResources,1 do
  86.             availability = woOrResources[r].available
  87.             print("----")
  88.             print(woOrResources[r].displayName, ", available: ", availability)
  89.             -- if resource is not available, then call for extract
  90.             if availability == 0 then
  91.                 print("none found, attempting extract..")
  92.                 meExtract(woOrResources[r].item.name, woOrResources[r].needed, woOrResources[r].item)
  93.             else
  94.                 print("skipping")
  95.             end
  96.         end
  97.     end
  98.     print(i, " complete")
  99. end
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment