Advertisement
ben_mkiv

smelteryLib.lua

Dec 22nd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.75 KB | None | 0 0
  1. component = require("component")
  2. sides = require("sides")
  3. term = require("term")
  4. event = require("event")
  5.  
  6. db = component.database
  7. gpu = component.gpu
  8.  
  9. resX, resY = gpu.getResolution()
  10.  
  11. materials = {}
  12. smeltery = {}
  13. recipes = {}
  14.  
  15. verbose = false
  16. stopme = false
  17.  
  18. transposer = false
  19. me = false
  20. interface = false
  21. ingotFormer = false
  22.  
  23. function splitName(input)
  24.     return { modid = string.sub(string.match(input, ".*:"), 1, -2), name = string.sub(string.match(input, ":.*"), 2) }
  25. end
  26.  
  27. function findInventory(name)
  28.     gpu.setForeground(0x969696)
  29.     for i=1,#sides do
  30.         inv = transposer.getInventoryName(i-1)
  31.         if inv ~= nil then
  32.             if string.match(inv, name) then
  33.                 print("(i) found " .. inv .. " on side " .. sides[i-1])
  34.                 gpu.setForeground(0xFFFFFF)
  35.                 return (i-1)
  36.     end end end
  37.        
  38.     gpu.setForeground(0xFF8A00)
  39.     print("(!) no inventory found for '" .. name .. "'")
  40.     gpu.setForeground(0xFFFFFF)
  41.     return nil
  42. end
  43.  
  44. function readFile(file)
  45.     fd = io.open(file, "r")
  46.     local data = fd:read("*a")
  47.     fd:close()
  48.     return data
  49. end
  50.  
  51. function getFreeSlots()
  52.     emptySlots = 0
  53.     stacks = transposer.getAllStacks(smeltery.controller).getAll()
  54.     for i=1,#stacks do
  55.         if stacks[i].size == 0 then
  56.             emptySlots = emptySlots + 1
  57.         end
  58.     end
  59.     return emptySlots
  60. end
  61.  
  62. function updateStorage()
  63.     print("updating storage stats")
  64.     for i=1,#materials do
  65.         storage = me.getItemsInNetwork({ name = materials[i].name })
  66.         if #storage > 0 then
  67.             materials[i].stored = math.floor(storage[1].size)
  68.         else
  69.             materials[i].stored = 0    
  70.         end
  71.     end
  72. end
  73.  
  74. function loadMaterials()
  75.     print("\n\n(!) reading material db")
  76.     gpu.setForeground(0x969696)
  77.    
  78.     for i=1,25 do
  79.         material = db.get(i)
  80.         if material ~= nil then
  81.             print("(i) " .. material.name .. " added")
  82.             material.slot = i
  83.             material.stored = 0
  84.             table.insert(materials, material)
  85.     end end
  86.    
  87.     gpu.setForeground(0xFFFFFF)
  88.     print("(i) "..#materials .. " materials loaded\n\n")
  89. end
  90.  
  91. function getSlot(materialName)
  92.     for i=1,#materials do
  93.         if materials[i].name == materialName then
  94.             return materials[i].slot
  95.     end end
  96.    
  97.     return nil
  98. end
  99.  
  100. function moveIngots(recipe)
  101.     ingots = transposer.getStackInSlot(ingotFormer, 1)
  102.     if ingots ~= nil and ingots.size > 0 then
  103.         if transposer.transferItem(ingotFormer, interface, ingots.size) then
  104.             gpu.setForeground(0x969696)
  105.             if verbose then print("moved " .. math.floor(ingots.size) .. " ingots to storage") end
  106.             recipe.ingots.done = math.floor(recipe.ingots.done + ingots.size)
  107.             gpu.setForeground(0xFFFFFF)
  108.         else
  109.             gpu.setForeground(0xFF0000)
  110.             print("failed to move " .. math.floor(ingots.size) .. " ingots to storage")
  111.             gpu.setForeground(0xFFFFFF)
  112.     end end
  113. end
  114.  
  115. function canCraft(recipe)
  116.     maxCraft = math.huge
  117.    
  118.     for i=1,#recipe.input do
  119.         for m=1,#materials do
  120.             if materials[m].name == recipe.input[i].name then
  121.                 c = math.floor(materials[m].stored / recipe.input[i].count)
  122.                 maxCraft = math.min(maxCraft, c)
  123.     end end end
  124.    
  125.     if maxCraft ~= math.huge then
  126.         return maxCraft
  127.     else
  128.         return 0
  129.     end
  130. end
  131.  
  132. function setupInterfaceSlot(recipe, slot)
  133.     gpu.setForeground(0x969696)
  134.     io.write("setting up interface for material " .. recipe.input[slot].name .. "...")
  135.     gpu.setForeground(0xFFFFFF)
  136.     recipe.input[slot].useCount = recipe.input[slot].count * recipe.amount 
  137.     dbSlot = getSlot(recipe.input[slot].name)
  138.     if slot == nil then
  139.         gpu.setForeground(0xFF0000)
  140.         io.write("no slot found for material '".. recipe.input[slot].name "'\n")
  141.         gpu.setForeground(0xFFFFFF)
  142.         return false
  143.     end    
  144.     me.setInterfaceConfiguration(slot, db.address, dbSlot, math.min(64, recipe.input[slot].useCount))
  145.     gpu.setForeground(0x00FF00)
  146.     io.write("done!\n")
  147.     gpu.setForeground(0xFFFFFF)
  148. end
  149.  
  150. function resetInterface(slot)
  151.     gpu.setForeground(0x969696)
  152.     print("resetting interface slot " .. slot)
  153.     gpu.setForeground(0xFFFFFF)
  154.     me.setInterfaceConfiguration(slot)
  155. end
  156.  
  157. function moveItemsToSmeltery(recipe)
  158.     freeSlots = smeltery.size
  159.     print("transfering items to smeltery")
  160.     for i=1,#recipe.input do
  161.         setupInterfaceSlot(recipe, i)
  162.    
  163.         while recipe.input[i].useCount > 0 do
  164.             if freeSlots == 0 then
  165.                 gpu.setForeground(0x969696)
  166.                 io.write("waiting for free slots ..")
  167.                 while freeSlots == 0 do
  168.                     io.write(".")
  169.                     freeSlots = getFreeSlots()
  170.                     os.sleep(0.5)
  171.                 end
  172.                 io.write("\n")
  173.                 gpu.setForeground(0xFFFFFF)
  174.             end
  175.            
  176.             canTransfer = math.min(freeSlots, transposer.getStackInSlot(interface, i).size)
  177.             if transposer.transferItem(interface, smeltery.controller, math.min(canTransfer, recipe.input[i].useCount), i) then
  178.                 recipe.input[i].useCount = recipe.input[i].useCount - canTransfer
  179.                 freeSlots = freeSlots - canTransfer
  180.             end
  181.         end
  182.        
  183.         resetInterface(i)
  184.      end
  185. end
  186.  
  187. function updateAlloy(recipe)
  188.     fluid = transposer.getFluidInTank(smeltery.drain)
  189.    
  190.     for fluidIndex=1,#fluid do
  191.         if fluid[fluidIndex].name == recipe.fluid then
  192.             perc = math.floor(((fluid[fluidIndex].amount + recipe.alloy.done) / recipe.alloy.total * 100) * 10 + 0.5) / 10
  193.             recipe.alloy.percent = perc
  194.            
  195.             if fluidIndex == 1 then
  196.                 recipe.alloy.waiting = math.floor(fluid[fluidIndex].amount)
  197.                
  198.                 if recipe.alloy.waiting > 0 and recipe.alloy.total > fluid[fluidIndex].amount + recipe.alloy.done then
  199.                     recipe.alloy.waiting = recipe.alloy.waiting - 1 --remove 1mb from the batch to keep 1mb on bottom of the smeltery
  200.                 end
  201.             else
  202.                 recipe.alloy.waiting = 0
  203.             end
  204.         end
  205.     end
  206.    
  207.     marginRight = 50
  208.     gpu.setForeground(0xFFBD00)
  209.     gpu.fill(resX-marginRight, 3, marginRight, 1, " ")
  210.     gpu.set(resX-marginRight, 3, recipe.alloy.percent .. "%")
  211.     gpu.set(resX-marginRight+10, 3, "("..(recipe.alloy.done + recipe.alloy.waiting).."mB of "..recipe.alloy.total.."mB)");
  212.     gpu.setForeground(0xFFFFFF)
  213. end
  214.  
  215. function moveAlloy(recipe)
  216.     if recipe.alloy.waiting < 144 and recipe.alloy.waiting + recipe.alloy.done < recipe.alloy.total then return false; end 
  217.     transfer = math.min(8000, recipe.alloy.waiting)
  218.     if transposer.transferFluid(smeltery.drain, ingotFormer, transfer) then
  219.         if verbose then print("moved " .. transfer .. "mB of alloy to ingot former") end
  220.         recipe.alloy.done = recipe.alloy.done + transfer
  221.     else
  222.         gpu.setForeground(0xFF0000)
  223.         print("failed to move " .. transfer .. "mB of alloy to ingot former")
  224.         gpu.setForeground(0xFFFFFF)
  225.     end
  226. end
  227.  
  228. function updateIngots(recipe)
  229.     marginRight = 50   
  230.     gpu.setForeground(0xFFBD00)
  231.     gpu.fill(resX-marginRight, 5, marginRight, 1, " ")
  232.     gpu.set(resX-marginRight, 5, math.floor(recipe.ingots.done/recipe.ingots.total * 100) .. "%")
  233.     gpu.set(resX-marginRight+10, 5, "("..recipe.ingots.done.." of "..recipe.ingots.total.." ingots done)");
  234.     gpu.setForeground(0xFFFFFF)
  235. end
  236.  
  237. function craftRecipe(recipe, amount)
  238.     recipe.amount = amount
  239.     recipe.alloy = { done = 0, percent = 0, waiting = 0, total = (recipe.output.count * recipe.amount * 144) }
  240.     recipe.ingots = { total = (recipe.output.count * amount), done = 0 }
  241.            
  242.     term.clear()
  243.     print("\n### crafting: "..recipe.output.count .. "x " .. recipe.output.name.." ###\n")
  244.    
  245.     if recipe.input_fluids ~= nil then
  246.         for i=1,#recipe.input_fluids do
  247.             print("please add " .. (recipe.input_fluids[i].amount * amount) .. "mB of " .. recipe.input_fluids[i].name)
  248.         end
  249.     end
  250.    
  251.     moveItemsToSmeltery(recipe)
  252.    
  253.     print("waiting for alloy")
  254.     repeat
  255.         updateAlloy(recipe)
  256.         moveAlloy(recipe)      
  257.         moveIngots(recipe)     
  258.         updateIngots(recipe)       
  259.         os.sleep(0.5)  
  260.     until recipe.alloy.done >= recipe.alloy.total and recipe.ingots.done >= recipe.ingots.total
  261.    
  262.     os.sleep(1)
  263.     updateStorage()
  264. end
  265.  
  266. function castLiquid()
  267.     fluid = transposer.getFluidInTank(smeltery.drain)
  268.     if #fluid == 0 then return false; end
  269.  
  270.     fakeRecipe = {}
  271.     fakeRecipe.alloy = { done = 0, percent = 0, waiting = 0, total = math.min(math.floor(fluid[1].amount / 144) * 144, 15984) }
  272.     fakeRecipe.ingots = { done = 0, total = math.floor(fakeRecipe.alloy.total / 144) }
  273.     fakeRecipe.ingots.done = 0
  274.     fakeRecipe.fluid = fluid[1].name
  275.    
  276.     if fakeRecipe.ingots == 0 then
  277.         gpu.setForeground(0xFFC900)
  278.         print("theres not enough liquid left to cast a ingot")
  279.         gpu.setForeground(0xFFFFFF)
  280.         os.sleep(2)
  281.         return;
  282.     end
  283.    
  284.     transposer.transferFluid(smeltery.drain, ingotFormer, fakeRecipe.alloy.total)  
  285.     print("casting ".. fakeRecipe.ingots.total .. " ingots from " .. fakeRecipe.fluid)
  286.     gpu.setForeground(0x969696)
  287.     while fakeRecipe.ingots.done < fakeRecipe.ingots.total do
  288.         moveIngots(fakeRecipe)
  289.         io.write(".")
  290.         os.sleep(1)
  291.     end
  292.     gpu.setForeground(0xFFFFFF)
  293.    
  294.     gpu.setForeground(0x00FF00)
  295.     print("")
  296.     print("done!")
  297.     gpu.setForeground(0xFFFFFF)
  298.     os.sleep(1)
  299.    
  300.     fluid = transposer.getFluidInTank(smeltery.drain)
  301.     if #fluid > 0 and fluid[1].name == fakeRecipe.fluid then
  302.         print("theres fluid left, starting another run")
  303.         os.sleep(2)
  304.         castLiquid()
  305.     end
  306. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement