ivanzrer

colony3

Nov 7th, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. args = {...}
  2.  
  3. ci = peripheral.find("colonyIntegrator")  or error("No ColonyIntegrator attached", 0)
  4. bridge = peripheral.find("meBridge")  or error("No meBridge attached", 0)
  5.  
  6. function dlog (wColor, wCritical, wMessage)
  7.     if wCritical then
  8.         term.setTextColor(wColor)
  9.         print(wMessage)
  10.         term.setTextColor(colors.white)
  11.     elseif args[1] ~= "-s" then
  12.         term.setTextColor(wColor)
  13.         print(wMessage)
  14.         term.setTextColor(colors.white)
  15.     end
  16. end
  17.  
  18. dlog(colors.white, true, "------")
  19. dlog(colors.white, true, "Interaction Requested...")
  20. dlog(colors.white, false, "------")
  21.  
  22. -- ME system extraction
  23. function meExtract (eItem, eCount, eTable)
  24.     sItem = tostring(eItem)
  25.     retState = false
  26.  
  27.     -- export from system
  28.     function exporter (sItem, eCount)
  29.         result, err = bridge.exportItem({name=sItem, count=eCount}, "up")
  30.         if err then
  31.             dlog(colors.white, false,"export failure")
  32.         else    
  33.             dlog(colors.green, true, string.format("Exporting %d %s", result, sItem))
  34.             dlog(colors.green, false, "Export success.")
  35.             retState = true
  36.         end
  37.     end
  38.  
  39.     -- craft resource
  40.     function crafter (sItem, eCount)
  41.         craftSuccess = bridge.craftItem({name=sItem, count=eCount})
  42.         if craftSuccess then
  43.             dlog(colors.white, true, "ME crafting success")
  44.             return true
  45.         else
  46.             dlog(colors.red, true, "ME crafting failure")
  47.             return false
  48.         end
  49.     end
  50.  
  51.     -- check for item in storage
  52.     hasItem, err = bridge.getItem({name = eTable.name})
  53.     if err or (hasItem.name == nil) then
  54.         --doesnt have it
  55.         dlog(colors.white, false, "None found..")
  56.         if hasItem.isCraftable then
  57.             -- Can be crafted
  58.             dlog(colors.white, false, "Attempting ME crafting..")
  59.             if crafter(sItem, eCount) then
  60.                 exporter(sItem, eCount)
  61.             else
  62.                 dlog(colors.red, false, "ME Craft failure")
  63.                 dlog(colors.red, false, "Unable to provide")
  64.             end
  65.         else
  66.             -- Cannot be crafted
  67.             dlog(colors.red, false, "ME Crafting unable")
  68.             dlog(colors.white, false, "Unable to provide")
  69.         end
  70.  
  71.     else
  72.         --has it
  73.         if hasItem.amount >= eCount then
  74.             -- attempt export
  75.             exporter(sItem, eCount)
  76.         else
  77.             dlog(colors.white, false, "ME system cannot provide full amount")
  78.             dlog(colors.white, false, "Attempting to craft remainder...")
  79.  
  80.             restCount = eCount - hasItem.amount
  81.             if crafter(sItem, restCount) then
  82.                 exporter(sItem, restCount)
  83.             else
  84.                 dlog(colors.white, false, "Crafting failure")
  85.                 dlog(colors.white, false, string.format("exporting: %d of %d", hasItem.amount, eCount))
  86.                 exporter(sItem, hasItem.amount)
  87.             end
  88.         end
  89.     end
  90.    
  91.     return retState
  92. end
  93.  
  94. -- retreive active work orders
  95. woOrder = ci.getWorkOrders()
  96.  
  97. -- retreive active requests
  98. requests = ci.getRequests()
  99.  
  100. -- Check for availability of resources in work orders
  101. -- loop through work orders
  102. dlog(colors.cyan, true, "Checking work orders...")
  103.  
  104. for i=1, #woOrder,1 do
  105.     dlog(colors.white, false, "------")
  106.     dlog(colors.cyan, false, string.format("Checking work order number %d of %d", i, #woOrder))
  107.    
  108.  
  109.     -- retrieve table for resources for work order
  110.     woID = tonumber(woOrder[i].id)
  111.     woOrResources = ci.getWorkOrderResources(woID)
  112.    
  113.     -- check through resources
  114.     if woOrResources ~= nil then
  115.         for r=1, #woOrResources,1 do
  116.             availability = woOrResources[r].available
  117.             dlog(colors.white, false, "----")
  118.             dlog(colors.white, false, string.format("%s, available: %d",   woOrResources[r].displayName, availability))
  119.             -- if resource is not available, then call for extract
  120.             if availability == 0 then
  121.                 dlog(colors.white, false, "none found, attempting extract..")
  122.                 meExtract(woOrResources[r].item.name, woOrResources[r].needed, woOrResources[r].item)
  123.             else
  124.                 dlog(colors.white, false, "skipping")
  125.             end
  126.         end
  127.     end
  128.     dlog(colors.green, false, string.format("%d complete", i))
  129. end
  130. dlog(colors.green, true, "Work orders complete.")
  131.  
  132.  
  133. dlog(colors.cyan, true, "Checking requests...")
  134. for i=1, #requests,1 do
  135.     dlog(colors.white, false, "------")
  136.     dlog(colors.cyan, false, string.format("Checking request number %d of %d", i, #requests))
  137.     reqItems = requests[i].items
  138.     -- check if there is a request in slot
  139.  
  140.     if reqItems ~= nil then
  141.         if reqItems[i] ~= nil then
  142.  
  143.             -- cycle through items in request
  144.             pass = false
  145.             for n=1, #reqItems,1 do
  146.                 if args[1] ~= "-s" then
  147.  
  148.                 end
  149.                 dlog(colors.white, false, "-----")
  150.                 dlog(colors.white, false, string.format("Attempting to request %s from ME system...", reqItems[n].name))
  151.                 if meExtract(reqItems[n].name, reqItems[n].count, reqItems[n]) then
  152.                     -- exit loop if suitable item has been exported
  153.                     pass = true
  154.                     break
  155.                 end
  156.             end
  157.  
  158.             if pass == false then
  159.                 dlog(colors.red, false, "failed to extract suitable item")
  160.  
  161.             end
  162.         end
  163.         dlog(colors.green, false, i, " complete")
  164.     end
  165. end
  166. dlog(colors.green, true, "Requests complete.")
  167.  
  168.  
  169. dlog(colors.white, true, "------")
  170. dlog(colors.green, true, "Interaction Complete.")
Advertisement
Add Comment
Please, Sign In to add comment