iconmaster

dragonforge.lua

Jan 13th, 2022 (edited)
1,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.67 KB | None | 0 0
  1. local function logMsg(text) print("[" .. os.date("%T") .. "] " .. text) end
  2.  
  3. local function selectItem(id)
  4.     -- match exact
  5.     for i = 1, 16 do
  6.         local data = turtle.getItemDetail(i)
  7.         if data and data.name == id then
  8.             turtle.select(i)
  9.             return true
  10.         end
  11.     end
  12.     -- match by tags
  13.     for i = 1, 16 do
  14.         local data = turtle.getItemDetail(i, true)
  15.         if data then
  16.             for tag, _ in pairs(data.tags) do
  17.                 if tag == id then
  18.                     turtle.select(i)
  19.                     return true
  20.                 end
  21.             end
  22.         end
  23.     end
  24.     -- no match
  25.     return false
  26. end
  27.  
  28. local function nKeys(t)
  29.     local i = 0
  30.     for _, _ in pairs(t) do i = i + 1 end
  31.     return i
  32. end
  33.  
  34. local function getInventory()
  35.     local result = {}
  36.     for i = 1, 16 do
  37.         local details = turtle.getItemDetail(i, true)
  38.         if details then
  39.             if details.name then result[details.name] = true end
  40.             if details.tags then
  41.                 for tag, _ in pairs(details.tags) do
  42.                     result[tag] = true
  43.                 end
  44.             end
  45.         end
  46.     end
  47.     return result
  48. end
  49.  
  50. local function getItemInPeripheral(peripheral, id)
  51.     -- match exact
  52.     for i = 1, peripheral.size() do
  53.         local data = peripheral.getItemDetail(i)
  54.         if data and data.name == id then return i end
  55.     end
  56.     -- match by tags
  57.     for i = 1, peripheral.size() do
  58.         local data = peripheral.getItemDetail(i, true)
  59.         if data then
  60.             for tag, _ in pairs(data.tags) do
  61.                 if tag == id then return i end
  62.             end
  63.         end
  64.     end
  65.     -- no match
  66.     return nil
  67. end
  68.  
  69. local function selectEmptySlot()
  70.     for i = 1, 16 do
  71.         local data = turtle.getItemDetail(i)
  72.         if not data then
  73.             turtle.select(i)
  74.             return true
  75.         end
  76.     end
  77.     return false
  78. end
  79.  
  80. local function selectItemOrEmpty(id) return selectItem(id) or selectEmptySlot() end
  81.  
  82. local RECIPES = {
  83.     ["iceandfire:dragonsteel_fire_ingot"] = {
  84.         "minecraft:iron_ingot", "iceandfire:fire_dragon_blood"
  85.     },
  86.     ["iceandfire:dragonsteel_ice_ingot"] = {
  87.         "minecraft:iron_ingot", "iceandfire:ice_dragon_blood"
  88.     },
  89.     ["iceandfire:dragonsteel_lightning_ingot"] = {
  90.         "minecraft:iron_ingot", "iceandfire:lightning_dragon_blood"
  91.     },
  92.     ["allthemodium:vibranium_allthemodium_alloy_ingot"] = {
  93.         "allthemodium:vibranium_dust", "allthemodium:allthemodium_dust"
  94.     },
  95.     ["allthemodium:unobtainium_vibranium_alloy_ingot"] = {
  96.         "allthemodium:unobtainium_dust", "allthemodium:vibranium_dust"
  97.     },
  98.     ["allthemodium:unobtainium_allthemodium_alloy_ingot"] = {
  99.         "allthemodium:unobtainium_dust", "allthemodium:allthemodium_dust"
  100.     }
  101. }
  102.  
  103. local function possibleRecipes()
  104.     local inventory = getInventory()
  105.     local possible = {}
  106.     for result, recipe in pairs(RECIPES) do
  107.         local isPossible = true
  108.         for _, ingredient in ipairs(recipe) do
  109.             if not inventory[ingredient] then
  110.                 isPossible = false
  111.                 break
  112.             end
  113.         end
  114.         if isPossible then possible[result] = recipe end
  115.     end
  116.     return possible
  117. end
  118.  
  119. local forgeInput, forgeOutput
  120.  
  121. local function mainLoop()
  122.     if not forgeInput then forgeInput = peripheral.wrap("front") end
  123.     if not forgeOutput then forgeOutput = peripheral.wrap("top") end
  124.  
  125.     local recipes = possibleRecipes()
  126.     if nKeys(recipes) > 0 then
  127.         for result, ingredients in pairs(recipes) do
  128.             logMsg("Crafting " .. result .. "...")
  129.             for _, ingredient in ipairs(ingredients) do
  130.                 logMsg("Adding " .. ingredient .. "...")
  131.                 while not selectItem(ingredient) do os.sleep(0.1) end
  132.                 while not turtle.drop(1) do -- input to the front
  133.                     os.sleep(0.1)
  134.                 end
  135.             end
  136.             logMsg("Waiting for result...")
  137.             while not getItemInPeripheral(forgeOutput, result) do
  138.                 os.sleep(0.1)
  139.             end
  140.             while not turtle.suckUp(1) do os.sleep(.1) end -- output above
  141.             logMsg("Craft complete.")
  142.             break
  143.         end
  144.     end
  145.  
  146.     os.sleep(0.1)
  147. end
  148.  
  149. local function errorHandler(error)
  150.     if error == "Terminated" then
  151.         return true
  152.     else
  153.         logMsg("internal error: " .. error)
  154.         return false
  155.     end
  156. end
  157.  
  158. logMsg("Dragonforge crafter started up.")
  159. while true do
  160.     local _, shouldExit = xpcall(mainLoop, errorHandler)
  161.     if shouldExit then break end
  162.     -- mainLoop() -- for debugging
  163. end
  164.  
Advertisement
Add Comment
Please, Sign In to add comment