filloax

Untitled

Oct 15th, 2022 (edited)
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. --[[
  2.     Automate Spectrum Potion Workshop for liquid crystal;
  3.     needs to insert bucket ebfore everything else, or other stuff
  4.     will be put into the container slot
  5. ]]
  6.  
  7. local RUN_EVERY_SEC = 0.5
  8. local GET_ITEMS_EVERY_ITERS = 1
  9.  
  10. -- Config these two depending on sides
  11. local DropFunction = turtle.dropDown
  12. local SuckFunction = turtle.suck
  13.  
  14. local Items = {
  15.     MERMAIDS = "spectrum:mermaids_gem",
  16.     YELLOW_P = "spectrum:yellow_pigment",
  17.     PINK_P = "spectrum:pink_pigment",
  18.     SHIMMERSTONE = "spectrum:sparklestone_gem",
  19.     BUCKET = "minecraft:bucket",
  20. }
  21.  
  22. local MAX_SLOT = 16
  23.  
  24. local function getItemsAndCheck(suckFun, dropFun)
  25.     local done = false
  26.     repeat
  27.         local success = suckFun(64)
  28.         done = not success
  29.     until done
  30.  
  31.     for slot = 1, MAX_SLOT do
  32.         local data = turtle.getItemDetail(slot)
  33.         if data
  34.         and not includes(Items, data.name) then
  35.             turtle.select(slot)
  36.             dropFun(64)
  37.         end
  38.     end
  39. end
  40.  
  41. local function hasItem(itemId)
  42.     local currentRecipe
  43.     local recipeSlots
  44.  
  45.     for slot = 1, MAX_SLOT do
  46.         local data = turtle.getItemDetail(slot)
  47.         if data and data.name == itemId then
  48.             return true
  49.         end
  50.     end
  51.     return false
  52. end
  53.  
  54. local function dropItem(dropFun, itemId, num)
  55.     for slot = 1, MAX_SLOT do
  56.         local data = turtle.getItemDetail(slot)
  57.         if data and data.name == itemId then
  58.             turtle.select(slot)
  59.             dropFun(num)
  60.         end
  61.     end
  62. end
  63.  
  64. local function main()
  65.     print("Spectrum Crystal automation: starting!")
  66.  
  67.     local iters = 0
  68.     while true do
  69.         if iters == 0 then
  70.             getItemsAndCheck(SuckFunction, DropFunction)
  71.         end
  72.  
  73.         if hasItem(Items.BUCKET) then
  74.             print("Has bucket! Transferring items...")
  75.             dropItem(DropFunction, Items.BUCKET, 1)
  76.             dropItem(DropFunction, Items.SHIMMERSTONE, 32)
  77.             dropItem(DropFunction, Items.PINK_P, 32)
  78.             dropItem(DropFunction, Items.YELLOW_P, 32)
  79.             dropItem(DropFunction, Items.MERMAIDS, 4)
  80.         end
  81.  
  82.         iters = (iters + 1) % GET_ITEMS_EVERY_ITERS
  83.         ---@diagnostic disable-next-line: undefined-field
  84.         os.sleep(RUN_EVERY_SEC)
  85.     end
  86. end
  87.  
  88. -- library stuff
  89.  
  90. function includes(tbl, val)
  91.     for k, v in pairs(tbl) do
  92.         if v == val then
  93.             return true
  94.         end
  95.     end
  96.  
  97.     return false
  98. end
  99.  
  100. function indexOf(list, val)
  101.     for i, v in ipairs(list) do
  102.         if v == val then
  103.             return i
  104.         end
  105.     end
  106. end
  107.  
  108. -- If the tables have the same items,
  109. -- with eventually different order
  110. function matches(tbl1, tbl2)
  111.     local tbl2copy = {}
  112.     for k2, v2 in pairs(tbl2) do
  113.         tbl2copy[k2] = v2
  114.     end
  115.     for k1, v1 in pairs(tbl1) do
  116.         local found = false
  117.         for k2, v2 in pairs(tbl2copy) do
  118.             if v2 == v1 then
  119.                 found = true
  120.                 tbl2copy[k2] = nil
  121.                 break
  122.             end
  123.         end
  124.         if not found then
  125.             return false
  126.         end
  127.     end
  128.     return true
  129. end
  130.  
  131. function dump(o)
  132.     if type(o) == 'table' then
  133.        local s = '{ '
  134.        for k,v in pairs(o) do
  135.           if type(k) ~= 'number' then k = '"'..k..'"' end
  136.           s = s .. '['..k..'] = ' .. dump(v) .. ','
  137.        end
  138.        return s .. '} '
  139.     else
  140.        return tostring(o)
  141.     end
  142.  end
  143.  
  144. -- Actually run
  145.  
  146. main()
  147.  
Advertisement
Add Comment
Please, Sign In to add comment