Advertisement
tdy2012

CC Auto Crafting

Jul 9th, 2024 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. -- Front chest is the chest where turtle retrieves crafting ingredients from.
  2. -- Top chest is the chest where turtle puts final product.
  3. -- Bottom chest is the chest where turtle dumps unrelated items.
  4.  
  5. local UNMATCHED = -1
  6. local EMPTY = 0
  7. local INSUFFICIENT = 1
  8. local MATCHED = 2
  9.  
  10. local string_recipe = {
  11.     {
  12.         name="immersiveengineering:hemp_fiber",
  13.         count=64
  14.     },
  15.     {
  16.         name="immersiveengineering:hemp_fiber",
  17.         count=64
  18.     },
  19.     {
  20.         name="immersiveengineering:hemp_fiber",
  21.         count=64
  22.     },
  23.     nil,
  24.     nil,
  25.     nil,
  26.     nil,
  27.     nil,
  28.     nil,
  29.     nil,
  30.     nil,
  31.     nil,
  32.     nil,
  33.     nil,
  34.     nil,
  35.     nil,
  36. }
  37.  
  38. function compare_item_detail(src_item_detail, dst_item_detail)
  39.     if dst_item_detail == nil then
  40.         if src_item_detail ~= nil then
  41.             return UNMATCHED
  42.         end
  43.         return MATCHED
  44.     end
  45.     if src_item_detail == nil then
  46.         return EMPTY
  47.     elseif src_item_detail.name == dst_item_detail.name then
  48.         if src_item_detail.count < dst_item_detail.count then
  49.             return INSUFFICIENT
  50.         end
  51.         return MATCHED
  52.     end
  53.     return UNMATCHED
  54. end
  55.  
  56. function do_nothing()
  57. end
  58.  
  59. function dump_unrelated_item(slot)
  60.     turtle.select(slot)
  61.     turtle.dropDown()
  62. end
  63.  
  64. function suck_more_item(slot, count)
  65.     turtle.select(slot)
  66.     turtle.suck(count)
  67. end
  68.  
  69. function is_ready_to_craft(recipe, on_unmatched_func, on_insufficient_func)
  70.     local is_ready = true
  71.  
  72.     for slot=1,16 do
  73.         turtle.select(slot)
  74.         local item_detail = turtle.getItemDetail()
  75.         local comparison = compare_item_detail(item_detail, recipe[slot])
  76.  
  77.         if comparison == UNMATCHED then
  78.             is_ready = false
  79.             on_unmatched_func(slot)
  80.         elseif comparison == EMPTY then
  81.             is_ready = false
  82.             local missing_count = recipe[slot].count
  83.             on_insufficient_func(slot, missing_count)
  84.         elseif comparison == INSUFFICIENT then
  85.             is_ready = false
  86.             local missing_count = recipe[slot].count - item_detail.count
  87.             on_insufficient_func(slot, missing_count)
  88.         end
  89.     end
  90.  
  91.     return is_ready
  92. end
  93.  
  94. function main()
  95.     while true do
  96.         if is_ready_to_craft(string_recipe, dump_unrelated_item, suck_more_item) then
  97.             turtle.craft()
  98.             turtle.dropUp()
  99.         end
  100.         os.sleep(1)
  101.     end
  102. end
  103.  
  104. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement