Advertisement
DenDor_dAs

Автоватизація крафтів у майнкрафті

Nov 21st, 2024 (edited)
191
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.92 KB | None | 0 0
  1. local modemSide = "top"  -- Modem pidkliuchenyi do "top"
  2. local modem = peripheral.wrap(modemSide)
  3.  
  4. modem.open(1)  -- Kanal dlia zv'iazku z pidkliuchenymy PK
  5.  
  6. -- Baza danykh dlia mekhanizmiv
  7. local machineDatabase = {
  8.     ["kompresor"] = {
  9.         inputChest = "reinfchest:gold_chest_1",
  10.         outputChest = "reinfchest:gold_chest_0",
  11.         queue = {}
  12.     }
  13. }
  14.  
  15. -- Baza danykh dlia retseptiv
  16. local recipes = {
  17.     ["modern_industrialization:iron_plate"] = {{"minecraft:iron_ingot", 1, "kompresor"}}
  18. }
  19.  
  20. -- Funtksia dlia otrymannia danykh pro inventar
  21. function getInventoryData()
  22.     local inventoryData = {}
  23.  
  24.     -- Pereviriaiemo vsi peryferii
  25.     for _, peripheralName in ipairs(peripheral.getNames()) do
  26.         if peripheral.hasType(peripheralName, "inventory") then
  27.             local inventory = peripheral.wrap(peripheralName)
  28.             local inventoryItems = inventory.list()
  29.  
  30.             -- Dlia kozhnoyi skryni zberihaemo yii vmist
  31.             inventoryData[peripheralName] = {}
  32.             for slot, slotData in pairs(inventoryItems) do
  33.                 inventoryData[peripheralName][slot] = {
  34.                     name = slotData.name,
  35.                     count = slotData.count,
  36.                     maxCount = slotData.maxCount or 64  -- Vrazhaemo, shcho maxCount dorivniuie 64, yakshcho ne naidene
  37.                 }
  38.             end
  39.         end
  40.     end
  41.  
  42.     return inventoryData
  43. end
  44.  
  45. -- Funtksia dlia perevirky, chy ie skrynia vvodu abo vyvodu
  46. function isInputOrOutputChest(chestName)
  47.     for _, data in pairs(machineDatabase) do
  48.         if chestName == data.inputChest or chestName == data.outputChest then
  49.             return true
  50.         end
  51.     end
  52.     return false
  53. end
  54.  
  55. -- Funtksia dlia otrymannia vsikh inventariv, krim skryn vvoda/vyvoda
  56. function getAvailableChests()
  57.     local peripherals = peripheral.getNames()
  58.     local availableChests = {}
  59.  
  60.     for _, name in ipairs(peripherals) do
  61.         if peripheral.hasType(name, "inventory") and not isInputOrOutputChest(name) then
  62.             table.insert(availableChests, name)
  63.         end
  64.     end
  65.  
  66.     return availableChests
  67. end
  68.  
  69. -- Funtksia dlia peremishchennia predmetiv u dostupnu skryniu
  70. function moveToAvailableChest(item, amount, fromChest, fromSlot)
  71.     local availableChests = getAvailableChests()
  72.  
  73.     for _, chestName in ipairs(availableChests) do
  74.         local chest = peripheral.wrap(chestName)
  75.         local chestItems = chest.list()
  76.  
  77.         for slot, slotData in pairs(chestItems) do
  78.             if slotData.name == item and slotData.count and slotData.maxCount and slotData.count < slotData.maxCount then
  79.                 local spaceLeft = slotData.maxCount - slotData.count
  80.                 local toMove = math.min(amount, spaceLeft)
  81.                 local moved = peripheral.call(fromChest, "pushItems", chestName, fromSlot, toMove, slot)
  82.                 if moved > 0 then
  83.                     amount = amount - moved
  84.                     if amount <= 0 then
  85.                         return true  -- Uspishno peremistuly vse
  86.                     end
  87.                 end
  88.             end
  89.         end
  90.  
  91.         if amount > 0 then
  92.             -- Yakshcho ne znayshly pidkhodiashchyi slot, probuemo znayty novyi porozhnii slot
  93.             local moved = peripheral.call(fromChest, "pushItems", chestName, fromSlot, amount)
  94.             if moved > 0 then
  95.                 return true  -- Uspishno peremistuly
  96.             end
  97.         end
  98.     end
  99.  
  100.     return false  -- Yakshcho ne vdalosia znayty mistse
  101. end
  102.  
  103. -- Funtksia dlia zbirannia predmetiv
  104. function collectItems()
  105.     local inventoryData = getInventoryData()
  106.     local collectedItems = {}
  107.  
  108.     for chestName, chestItems in pairs(inventoryData) do
  109.         if not isInputOrOutputChest(chestName) then
  110.             for slot, slotData in pairs(chestItems) do
  111.                 if not collectedItems[slotData.name] then
  112.                     collectedItems[slotData.name] = 0
  113.                 end
  114.                 collectedItems[slotData.name] = collectedItems[slotData.name] + slotData.count
  115.                 -- Peremishchaiemo vsi predmety z tsikh skryn
  116.                 peripheral.call(chestName, "pushItems", chestName, slot)
  117.             end
  118.         end
  119.     end
  120.  
  121.     return collectedItems
  122. end
  123.  
  124. -- Funtksia dlia peremishchennia predmetiv do dostupnykh skryn
  125. function distributeItems(collectedItems)
  126.     for itemName, itemCount in pairs(collectedItems) do
  127.         local availableChests = getAvailableChests()
  128.         for _, chestName in ipairs(availableChests) do
  129.             local chest = peripheral.wrap(chestName)
  130.             local chestItems = chest.list()
  131.  
  132.             for slot, slotData in pairs(chestItems) do
  133.                 if slotData.name == itemName and slotData.count and slotData.maxCount and slotData.count < slotData.maxCount then
  134.                     local spaceLeft = slotData.maxCount - slotData.count
  135.                     local toMove = math.min(itemCount, spaceLeft)
  136.                     local moved = peripheral.call(chestName, "pushItems", chestName, 1, toMove, slot)
  137.                     itemCount = itemCount - moved
  138.                     if itemCount <= 0 then
  139.                         break
  140.                     end
  141.                 end
  142.             end
  143.  
  144.             if itemCount > 0 then
  145.                 -- Zapovnuiemo novi sloty
  146.                 local moved = peripheral.call(chestName, "pushItems", chestName, 1, itemCount)
  147.                 itemCount = itemCount - moved
  148.                 if itemCount <= 0 then
  149.                     break
  150.                 end
  151.             end
  152.         end
  153.     end
  154. end
  155.  
  156. -- Funtksia dlia sortuvannia inventariv
  157. function sortInventories()
  158.     local collectedItems = collectItems()
  159.     distributeItems(collectedItems)
  160. end
  161.  
  162. -- Funtksia dlia obroblennia hotovoho rezultatu
  163. function processCraftingResults(outputChest, resultItem, expectedAmount)
  164.     local inventoryData = getInventoryData()
  165.     local chestItems = inventoryData[outputChest]
  166.  
  167.     if chestItems then
  168.         for slot, slotData in pairs(chestItems) do
  169.             if slotData.name == resultItem and slotData.count >= expectedAmount then
  170.                 -- Yakshcho predmety hotovi, peremishchaiemo yikh do dostupnoyi skryni
  171.                 if moveToAvailableChest(resultItem, slotData.count, outputChest, slot) then
  172.                     print("Predmety peremistuli do skladu.")
  173.                     return true
  174.                 else
  175.                     print("Ne vdalosia peremistuty predmety do skladu.")
  176.                     return false
  177.                 end
  178.             end
  179.         end
  180.     end
  181.  
  182.     print("Rezultat kraftu ne znaydeno u vykhidnii skryni.")
  183.     return false
  184. end
  185.  
  186. -- Funtksia dlia perevirky nayavnosti predmetu v inventari
  187. function checkInventoryForItem(item, amount)
  188.     local inventoryData = getInventoryData()
  189.  
  190.     for chestName, chestItems in pairs(inventoryData) do
  191.         for slot, slotData in pairs(chestItems) do
  192.             if slotData.name == item and slotData.count >= amount then
  193.                 return chestName, slot
  194.             end
  195.         end
  196.     end
  197.  
  198.     return nil, nil
  199. end
  200.  
  201. -- Funtksia dlia peremishchennia predmetiv mizh skryniamy
  202. function moveItemToChest(fromChest, fromSlot, toChest, amount)
  203.     local moved = peripheral.call(fromChest, "pushItems", toChest, fromSlot, amount)
  204.     return moved > 0
  205. end
  206.  
  207. -- Funktksia dlia obroblennia cherhy
  208. function processQueue(machine)
  209.     local data = machineDatabase[machine]
  210.     if #data.queue > 0 then
  211.         local task = table.remove(data.queue, 1)
  212.         local item = task.item
  213.         local amount = task.amount
  214.         local inputChest = data.inputChest
  215.         local outputChest = data.outputChest
  216.  
  217.         -- Znakhodimo resurs u skryni
  218.         local sourceChest, sourceSlot = checkInventoryForItem(item, amount)
  219.         if sourceChest then
  220.             -- Peremishchaiemo resurs do mekhanizmu
  221.             if moveItemToChest(sourceChest, sourceSlot, inputChest, amount) then
  222.                 print("Peredano do " .. machine .. ": " .. item .. " (" .. amount .. ")")
  223.  
  224.                 -- Chekaiemo zavershennia roboty mekhanizmu
  225.                 local resultReady = false
  226.                 local expectedResultItem = "modern_industrialization:iron_plate"  -- Zamina na ochikuvanyi rezultat
  227.                 while not resultReady do
  228.                     os.sleep(5)
  229.                     resultReady = processCraftingResults(outputChest, expectedResultItem, amount)
  230.                 end
  231.  
  232.                 print("Kraft zaversheno, predmety peremistuli.")
  233.             else
  234.                 print("Pomy`lka peredachi do " .. machine)
  235.             end
  236.         else
  237.             print("Nemaie dostatnoho resursu: " .. item)
  238.         end
  239.     end
  240. end
  241.  
  242. -- Funktksia dlia dodavannia zadachi v cherhu
  243. function addToQueue(machine, item, amount)
  244.     table.insert(machineDatabase[machine].queue, {item = item, amount = amount})
  245.     print("Dodano do cherhy " .. machine .. ": " .. item .. " (" .. amount .. ")")
  246. end
  247.  
  248. -- Funktksia dlia kraftu
  249. function craftItem(item, amount)
  250.     if not recipes[item] then
  251.         print("Retsept dlia " .. item .. " ne znaydeno.")
  252.         return
  253.     end
  254.  
  255.     local components = recipes[item]
  256.     for _, component in ipairs(components) do
  257.         local compItem = component[1]
  258.         local compAmount = component[2] * amount
  259.         local machine = component[3]
  260.  
  261.         addToQueue(machine, compItem, compAmount)
  262.     end
  263.  
  264.     for machine in pairs(machineDatabase) do
  265.         processQueue(machine)
  266.     end
  267.  
  268.     sortInventories()
  269. end
  270.  
  271. while true do
  272.     print("Vvedit komand (napr., 'craft modern_industrialization:iron_plate 1'): ")
  273.     local input = read()
  274.     local args = {}
  275.     for word in input:gmatch("%S+") do
  276.         table.insert(args, word)
  277.     end
  278.  
  279.     local command = args[1]
  280.     local item = args[2]
  281.     local amount = tonumber(args[3]) or 1
  282.  
  283.     if command == "craft" then
  284.         craftItem(item, amount)
  285.     else
  286.         print("Nevidoma komanda.")
  287.     end
  288. end
  289.  
Advertisement
Comments
  • DenDor_dAs
    209 days
    # text 1.64 KB | 0 0
    1. Наразі він в розробці. Поки не працює належним чином.
    2.  
    3. Це система, де кидаєте основні ресурси у багато скринь під'єднаних до багатьох модемів. Комп'ютер приймає запит на крафт, який ви колись раніше ввели. Після чого він аналізує предмети у скринях, і також черги, де розраховує скільки яких ресурсів йому потрібно, і що з ними треба зробити, аби створити цей крафт. Він перекидає вже у окремо зазначені скрині, які будуть перекидати ресурси вже у механізми ресурси, аби виконався певний крафт(наприклад подрібнити, компресувати...). В цей час механізм увесь час скидає у інші скрині(скаімо скрині для виводу), де вже комп'ютер увесь час аналізує, чи зібалась потрібна кількість ресурсів, які він планує отримати з тої кількості, яку він спеціально клав для цього виконання. Як тільки потрібна кількість підтверджується, комп'ютер перекидає ці ресурси у основні скрині(наш склад), і ставить наступні ресурси у черзі на крафт.
Add Comment
Please, Sign In to add comment
Advertisement