Lanzr

serial combine

Feb 6th, 2026 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.52 KB | None | 0 0
  1. local ccm = require "cc.completion"
  2. local completion = require("cc.shell.completion")
  3. function startup()
  4.     local function l_completion(shell, index, partial, previousArgs)
  5.         if index == 1 then
  6.             return ccm.choice(partial, {"tstOut", "combine", "saw", "press", "deployer", "spout"})
  7.         end
  8.         return nil
  9.     end
  10.  
  11.     shell.setCompletionFunction("work.lua", l_completion)
  12. end
  13.  
  14. startup()
  15. -- create 序列装备管理计算机
  16. local saw =  peripheral.find("create:saw")
  17. local deployer = peripheral.find("create:deployer")
  18. local spout = peripheral.find("create:spout")
  19. local tanks = {peripheral.find("create:fluid_tank")}
  20. local depots = {peripheral.find("create:depot")}
  21.  
  22. local box_input = peripheral.find("minecraft:chest")
  23.  
  24.  
  25.  
  26. -- 需要手动指定实际的设备输出置物台索引
  27. -- 如果在edit中调整测试可以使用这个注释了的arg
  28. -- arg = {"tstOut",1}
  29.  
  30. local depot_deployer = depots[1]
  31. local depot_saw = depots[1]
  32. local depot_press = depots[1]
  33. local depot_spout = depots[1]
  34.  
  35.  
  36. --[[    生成处理步骤,然后调用每个步骤的handler,获取材料通过handler本身处理,
  37.         如果没有材料,就调用通用的报警提示,然后睡眠等待下次尝试 ]]
  38.  
  39.  
  40.  
  41. local lastNbt = ""
  42. local itemMap = {}
  43. local ingredient = ""
  44. local recipe_ls = {}
  45.  
  46.  
  47. local recipe_map = {
  48.     ["create:mechanical_press"] = function (input_inv, input_slot)
  49.         return process_combine_press(input_inv, input_slot, depot_press)
  50.     end,
  51.     ["create:mechanical_saw"] = function (input_inv, input_slot)
  52.         return process_combine_saw(input_inv, input_slot, depot_saw)
  53.     end,
  54. }
  55.  
  56. local fluidBucketMap = {
  57.     ["minecraft:water_bucket"] = "minecraft:water",
  58.     ["minecraft:lava_bucket"] = "minecraft:lava",
  59.     ["immersiveengineering:redstone_acid_bucket"] = "immersiveengineering:redstone_acid"
  60. }
  61.  
  62. -- --------------------------------------------------------------------
  63. -- tools
  64. function pgn(peri)
  65.     return peripheral.getName(peri)
  66. end
  67.  
  68. function findItemSlot(itemType, inv)
  69.     for index,item in pairs(inv.list()) do
  70.         if item.name == itemType then
  71.             return index
  72.         end
  73.     end
  74.     return false
  75. end
  76. function findFluidInv(fluidType)
  77.     print("fluitypes:",fluidType)
  78.     for periindex,tankperi in pairs(tanks) do
  79.         for tankindex,tank in pairs(tankperi.tanks()) do
  80.             if tank.name == fluidType then
  81.                 if tank.amount > 1000 then
  82.                     return periindex
  83.                 end
  84.             end
  85.         end
  86.     end
  87.     return false
  88. end
  89. function invClean(inv)
  90.     if inv then
  91.         for index,item in pairs(inv.list()) do
  92.             inv.pushItems(pgn(box_input),index)
  93.         end
  94.     end
  95.    
  96. end
  97. -- --------------------------------------------------------------------
  98. -- init
  99. function cleanllSlot()
  100.     invClean(saw)
  101.     invClean(deployer)
  102.     if depots then
  103.         for index,depot in pairs(depots) do
  104.             invClean(depot)
  105.         end
  106.     end
  107. end
  108. function initRecipe(startIndex)
  109.     recipe_ls = {}
  110.     turtle.select(1)
  111.     ret = turtle.getItemDetail()
  112.     if ret~= nil then
  113.         ingredient = ret.name
  114.     else
  115.         return false
  116.     end
  117.     for i = startIndex, 16, 1 do
  118.         turtle.select(i)
  119.         ret = turtle.getItemDetail()
  120.         if ret~= nil then
  121.             table.insert(recipe_ls, ret.name)
  122.         else
  123.             return true
  124.         end
  125.     end
  126. end
  127.  
  128.  
  129. -- --------------------------------------------------------------------
  130. -- combine process
  131.  
  132. function process_combine_saw(input_inv, input_slot, out_inv)
  133.     input_inv.pushItems(pgn(saw),input_slot,1)
  134.     while true do
  135.         if #(out_inv.list()) ~= 0 then
  136.             return out_inv
  137.         end
  138.     end
  139. end
  140.  
  141. function process_combine_press(input_inv, input_slot, out_inv)
  142.     lastNbt = input_inv.getItemDetail(input_slot).nbt
  143.     input_inv.pushItems(pgn(out_inv),input_slot,1)
  144.     while true do
  145.         if out_inv.getItemDetail(1).nbt ~= lastNbt then
  146.             return out_inv
  147.         end
  148.     end
  149. end
  150.  
  151. function process_combine_deployer(itemType, input_inv, input_slot, out_inv)
  152.     lastNbt = input_inv.getItemDetail(input_slot).nbt
  153.     input_inv.pushItems(pgn(out_inv),input_slot,1)
  154.     while true do
  155.         itemSlot = findItemSlot(itemType, box_input)
  156.         if itemSlot ~= false then
  157.             box_input.pushItems(pgn(deployer),itemSlot,1)
  158.             break
  159.         end
  160.         sleep(5)
  161.     end
  162.     while true do
  163.         if out_inv.getItemDetail(1).nbt ~= lastNbt then
  164.             invClean(deployer)
  165.             return out_inv
  166.         end
  167.     end
  168. end
  169.  
  170.  
  171. function process_combine_spout(fluidType,input_inv, input_slot, out_inv)
  172.     lastNbt = input_inv.getItemDetail(input_slot).nbt
  173.     input_inv.pushItems(pgn(out_inv),input_slot,1)
  174.     while true do
  175.         tankIndex = findFluidInv(fluidType)
  176.         if tankIndex ~= false then
  177.             tanks[tankIndex].pushFluid(pgn(spout),1000)
  178.             break
  179.         end
  180.         sleep(5)
  181.     end
  182.     while true do
  183.         if out_inv.getItemDetail(1).nbt ~= lastNbt then
  184.             spout.pushFluid(pgn(tanks[tankIndex]),1000)
  185.             return out_inv
  186.         end
  187.     end
  188. end
  189.  
  190. -- --------------------------------------------------------------------
  191. -- singleProcess
  192. -- 需要闭环处理吗? 其实也不用,直接传送一组过来处理即可
  193. function process_single_saw(input_inv, input_slot, out_inv)
  194.     input_inv.pushItems(pgn(saw),input_slot)
  195.     while true do
  196.         if #(saw.list()) == 0 then
  197.             invClean(out_inv)
  198.             return true
  199.         end
  200.     end
  201. end
  202. function process_single_press(input_inv, input_slot, out_inv)
  203.     origName = input_inv.getItemDetail(input_slot).name
  204.     input_inv.pushItems(pgn(out_inv),input_slot)
  205.    
  206.     repeat
  207.         retFlag = true
  208.         for i,item in pairs(out_inv.list()) do
  209.             if item.name == origName then
  210.                 retFlag = false
  211.             end
  212.         end
  213.     until retFlag
  214.     invClean(out_inv)
  215. end
  216.  
  217. function process_single_spout(fluidType, input_inv, input_slot, out_inv)
  218.     origName = input_inv.getItemDetail(input_slot).name
  219.     input_inv.pushItems(pgn(out_inv),input_slot)
  220.     while true do
  221.         tankIndex = findFluidInv(fluidType)
  222.         if tankIndex ~= false then
  223.             tanks[tankIndex].pushFluid(pgn(spout),1000)
  224.             break
  225.         end
  226.         sleep(5)
  227.     end
  228.     repeat
  229.         retFlag = true
  230.         for i,item in pairs(out_inv.list()) do
  231.             if item.name == origName then
  232.                 retFlag = false
  233.             end
  234.         end
  235.     until retFlag
  236.     invClean(out_inv)
  237. end
  238.  
  239. function process_single_deployer(itemType, input_inv, input_slot, out_inv)
  240.    
  241.     origName = input_inv.getItemDetail(input_slot).name
  242.     -- 拿取材料到置物台
  243.     input_inv.pushItems(pgn(out_inv),input_slot)
  244.     repeat
  245.         retFlag = true
  246.         -- 尝试拿取材料到机械手
  247.         if #(deployer.list()) == 0 then
  248.             while true do
  249.                 itemSlot = findItemSlot(itemType, box_input)
  250.                 if itemSlot ~= false then
  251.                     box_input.pushItems(pgn(deployer),itemSlot)
  252.                     break
  253.                 end
  254.                 sleep(5)
  255.             end
  256.         end
  257.         for i,item in pairs(out_inv.list()) do
  258.             if item.name == origName then
  259.                 retFlag = false
  260.             end
  261.         end
  262.     until retFlag
  263.     print("process end  ")
  264.     invClean(out_inv)
  265.     invClean(deployer)
  266.        
  267. end
  268.  
  269. -- --------------------------------------------------------------------
  270.  
  271. function singleTask()
  272.     cleanllSlot()
  273.     turtle.select(1)
  274.     ret = turtle.getItemDetail()
  275.     if not initRecipe(1) then
  276.         print(" no appoint recipe")
  277.         return false
  278.     end
  279.     cb = nil
  280.     if arg[1] == "saw" then
  281.         cb = function (input_slot)
  282.             process_single_saw(box_input,input_slot,depot_saw)
  283.         end
  284.     elseif arg[1] == "press" then
  285.         cb = function (input_slot)
  286.             process_single_press(box_input,input_slot,depot_press)
  287.         end
  288.     elseif arg[1] == "deployer" then
  289.         cb = function (input_slot)
  290.             process_single_deployer(ingredient, box_input, input_slot, depot_deployer)
  291.         end
  292.         initRecipe(2)
  293.     elseif arg[1] == "spout" then
  294.         cb = function (input_slot)
  295.             fluidType = fluidBucketMap[ingredient]
  296.             process_single_spout(fluidType, box_input, input_slot, box_input)
  297.         end
  298.         initRecipe(2)
  299.     else
  300.         print(" no appoint process")
  301.         return false
  302.     end
  303.    
  304.     input_inv = box_input
  305.     repeat
  306.         retFlag = true
  307.         for index, value in pairs(recipe_ls) do
  308.             inputSlot = findItemSlot(value, box_input)
  309.             if inputSlot == false then
  310.                 print("no find any ",value)
  311.             else
  312.                 retFlag = false
  313.                 cb(inputSlot)
  314.             end
  315.         end
  316.     until retFlag
  317. end
  318.  
  319. function serialCombine()
  320.     cleanllSlot()
  321.     if not initRecipe(2) then
  322.         print(" no appoint recipe")
  323.         return false
  324.     end
  325.    
  326.     while true do
  327.         input_inv = box_input
  328.         inputSlot = findItemSlot(ingredient, box_input)
  329.         if inputSlot == false then
  330.             print("no find any ",ingredient)
  331.             return false
  332.         end
  333.         while true do
  334.            
  335.             for index, value in pairs(recipe_ls) do
  336.                 if recipe_map[value] ~= nil then
  337.                     input_inv = recipe_map[value](input_inv, inputSlot)
  338.                     inputSlot = 1
  339.                 elseif fluidBucketMap[value] ~= nil then
  340.                     input_inv = process_combine_spout(fluidBucketMap[value], input_inv, inputSlot, depot_spout)
  341.                     inputSlot = 1
  342.                 else
  343.                     input_inv = process_combine_deployer(value, input_inv, inputSlot, depot_deployer)
  344.                     inputSlot = 1
  345.                 end
  346.            
  347.             end
  348.  
  349.             if input_inv.getItemDetail(inputSlot).nbt == nil then
  350.                 input_inv.pushItems(pgn(box_input),inputSlot)
  351.                 break
  352.             end
  353.         end
  354.        
  355.     end
  356. end
  357.  
  358. function mainloop()
  359.     if arg[1] == "tstOut" then
  360.         -- 测试输出到指定的 置物台,用以调整对应的槽位
  361.         box_input.pushItems(pgn(depots[DEBUG_DEPOTS_SLOT]), tonumber(arg[2]))
  362.     elseif arg[1] == "combine" then
  363.         -- 序列组装
  364.         serialCombine()
  365.     else
  366.         singleTask()
  367.     end
  368. end
  369.  
  370.  
  371. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment