Advertisement
BLucky_pb

Fusion crafter

Apr 20th, 2020
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -- fusioncrafter.lua
  2. -- Author: sedlak477
  3. -- This program automates Draconic Evolution Fusion Crafting
  4.  
  5. local transposer = require("component").transposer
  6. local config = require("config")
  7.  
  8.  
  9. -- Load side mappings from config
  10. local input = config.inventories.input
  11. local core = config.inventories.core
  12. local injectors = config.inventories.injectors
  13. local output = config.inventories.output
  14. local result = config.inventories.result
  15.  
  16. -- Load recipes from config
  17. local recipes = config.recipes
  18.  
  19.  
  20. -- Greet user with a friendly message
  21. local function printHello()
  22.   print("FusionCrafter v1.0 by sedlak477")
  23.   print()
  24. end
  25.  
  26.  
  27. -- Retrun amount of item in inventory
  28. local function getItemAmount(id, side)
  29.   -- Look through all items to see if the requested exists
  30.   for stack in transposer.getAllStacks(side) do
  31.     if stack.name == id then
  32.       return stack.size
  33.     end
  34.   end
  35.   return 0
  36. end
  37.  
  38.  
  39. -- Check if recipe is craftable with
  40. -- current items
  41. local function canCraft(recipe)
  42.   for id, neededAmount in pairs(recipe.input) do
  43.     local availableAmount = getItemAmount(id, input)
  44.  
  45.     if availableAmount < neededAmount then
  46.       return false
  47.     end
  48.   end
  49.   return true
  50. end
  51.  
  52.  
  53. -- Find item slot in inventory
  54. -- Pass nil for item to find empty slot
  55. -- Returns nil if no slot is found
  56. local function find(inventory, item)
  57.   for slot = 1, transposer.getInventorySize(inventory), 1 do
  58.     if item == nil then
  59.       if transposer.getSlotStackSize(inventory, slot) == 0 then
  60.         return slot
  61.       end
  62.     else
  63.       local stack = transposer.getStackInSlot(inventory, slot)
  64.       if stack ~= nil and stack.name == item then
  65.         return slot
  66.       end
  67.     end
  68.   end
  69. end
  70.  
  71.  
  72. -- Transfer 'amount' of item 'id' from 'from' to a free slot in 'to'
  73. local function transfer(id, amount, from, to)
  74.   -- Get slots with items
  75.   local slotFrom = find(from, id)
  76.   local slotTo = find(to, nil)
  77.  
  78.   -- Do some error checking
  79.   if slotFrom == nil then
  80.     print("Error: transfer: No item '" .. id .. "' in source inventory")
  81.     return
  82.   end
  83.  
  84.   if slotTo == nil then
  85.     print("Error: transfer: No free slot in target inventory")
  86.     return
  87.   end
  88.  
  89.   transposer.transferItem(from, to, amount, slotFrom, slotTo)
  90. end
  91.  
  92.  
  93. -- Wait until items are in inventory
  94. local function waitItems(inventory, items)
  95.   while true do
  96.     local itemsExist = true
  97.     for item, amount in pairs(items) do
  98.       local slot = find(inventory, item)
  99.       if slot == nil then
  100.         itemsExist = false
  101.         break
  102.       end
  103.       if transposer.getSlotStackSize(inventory, slot) < amount then
  104.         itemsExist = false
  105.         break
  106.       end
  107.     end
  108.     if itemsExist then
  109.       return
  110.     end
  111.     os.sleep(1)
  112.   end
  113. end
  114.  
  115.  
  116. -- Craft provided recipe
  117. local function craft(recipe)
  118.  
  119.   -- Tell the user what we are doing
  120.   local resultString = ""
  121.   for item, amount in pairs(recipe.output) do
  122.     resultString = resultString .. item .. " "
  123.   end
  124.   io.write("Crafting " .. resultString .. "...")
  125.  
  126.   -- Put input items into their places
  127.   for item, amount in pairs(recipe.input) do
  128.  
  129.     -- If item is the core item transfer it to the core inventory
  130.     if item == recipe.core then
  131.       transfer(item, amount, input, core)
  132.     else  -- Else put it into the injectors
  133.       transfer(item, amount, input, injectors)
  134.     end
  135.  
  136.   end
  137.  
  138.   -- Wait for output
  139.   waitItems(result, recipe.output)
  140.  
  141.   -- Transfer items to output inventory
  142.   for item, amount in pairs(recipe.output) do
  143.     transfer(item, amount, result, output)
  144.   end
  145.  
  146.   print(" Done")
  147. end
  148.  
  149.  
  150. -- Check if a valid recipe is in the input
  151. local function checkCrafting()  
  152.  
  153.   -- Check all recipes if one is craftable
  154.   for i, recipe in pairs(recipes) do
  155.  
  156.     -- If we can craft it, we craft it
  157.     if canCraft(recipe) then
  158.       craft(recipe)
  159.       return
  160.     end
  161.  
  162.   end
  163.  
  164.   os.sleep(2)
  165. end
  166.  
  167.  
  168. -- The fun starts here
  169. local function main()
  170.   printHello()
  171.  
  172.   print("Ready! Waiting for craftable recipes")
  173.   while true do
  174.     checkCrafting()
  175.   end  
  176. end
  177.  
  178. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement