k2green

Craft

Aug 4th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | Software | 0 0
  1. local function turn(direction, inverted)
  2.     if direction == "left" then
  3.         if inverted then
  4.             turtle.turnRight()
  5.         else
  6.             turtle.turnLeft()
  7.         end
  8.     elseif direction == "right" then
  9.         if inverted then
  10.             turtle.turnLeft()
  11.         else
  12.             turtle.turnRight()
  13.         end
  14.     end
  15. end
  16.  
  17. local function readInventory(inventory)
  18.     local data = { slots = {}, totals = {} }
  19.    
  20.     for i=1, inventory.size() do
  21.         local details = inventory.getItemDetail(i)
  22.        
  23.         if details ~= nil then
  24.             if data.slots[details.name] == nil then
  25.                 data.slots[details.name] = {{ slot = i, count = details.count }}
  26.             else
  27.                 table.insert(data.slots[details.name], { slot = i, count = details.count })
  28.             end
  29.            
  30.             if data.totals[details.name] == nil then
  31.                 data.totals[details.name] = details.count
  32.             else
  33.                 data.totals[details.name] = data.totals[details.name] + details.count
  34.             end
  35.         end
  36.     end
  37.    
  38.     return data
  39. end
  40.  
  41. local function pullItems(side, count)
  42.     if side == "up" or side == "top" then
  43.         turtle.suckUp(count)
  44.     elseif side == "down" or side == "bottom" then
  45.         turtle.suckDown(count)
  46.     end
  47. end
  48.  
  49. local function turnTo(direction, inverted, name)
  50.     while true do
  51.         local exists, details = turtle.inspect()
  52.         if exists and details ~= nil and details.name == name then
  53.             break
  54.         end
  55.        
  56.         turn(direction, inverted)
  57.     end
  58. end
  59.  
  60. local function craftLoop(data)
  61.     local storage = peripheral.wrap("front")
  62.     local inventory = readInventory(storage)
  63.        
  64.     for k,v in ipairs(data.ingredients) do
  65.         if inventory.totals[v.name] == nil or inventory.totals[v.name] < v.count then
  66.             return
  67.         end
  68.     end
  69.    
  70.     local localSlots = {}
  71.     local currentSlot = 1
  72.     for k1, ingredient in ipairs(data.ingredients) do
  73.         local count = ingredient.count
  74.        
  75.         for k2, slot in ipairs(inventory.slots[ingredient.name]) do
  76.             if count <= 0 then
  77.                 break
  78.             end
  79.        
  80.             local moveAmount = math.min(64, count, slot.count)
  81.             storage.pushItems(data.secondaryInventory, slot.slot, moveAmount, 1)
  82.             turtle.select(currentSlot)
  83.             pullItems(data.secondaryInventory, moveAmount)
  84.            
  85.             count = count - moveAmount
  86.         end
  87.        
  88.         table.insert(localSlots, { slot = currentSlot, count = ingredient.count })
  89.         currentSlot = currentSlot + 1
  90.     end
  91.    
  92.     turnTo(data.turnDirection, false, data.craftingFluid)
  93.    
  94.     for k,v in ipairs(localSlots) do
  95.         turtle.select(v.slot)
  96.         turtle.drop(v.count)
  97.     end
  98.    
  99.     sleep(data.delay)
  100.    
  101.     turtle.select(16)
  102.     turtle.suck()
  103.    
  104.     turnTo(data.turnDirection, true, data.primaryInventory)
  105.     turtle.drop()
  106. end
  107.  
  108. local args = { ... }
  109. local file = fs.open(args[1], "r")
  110. local data = textutils.unserializeJSON(file.readAll())
  111. file.close()
  112.  
  113. local ingredientCount = #data.ingredients
  114. if ingredientCount >= 16 then
  115.     print("Only recipes with 15 or fewer ingredients are supported.")
  116. else
  117.     turnTo(data.turnDirection, true, data.primaryInventory)
  118.  
  119.     for i=1, 16 do
  120.         turtle.select(i)
  121.         turtle.drop()
  122.     end
  123.  
  124.     turtle.select(1)
  125.  
  126.     while true do
  127.         craftLoop(data)
  128.     end
  129. end
Add Comment
Please, Sign In to add comment