Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSide = "top" -- Modem pidkliuchenyi do "top"
- local modem = peripheral.wrap(modemSide)
- modem.open(1) -- Kanal dlia zv'iazku z pidkliuchenymy PK
- -- Baza danykh dlia mekhanizmiv
- local machineDatabase = {
- ["kompresor"] = {
- inputChest = "reinfchest:gold_chest_1",
- outputChest = "reinfchest:gold_chest_0",
- queue = {}
- }
- }
- -- Baza danykh dlia retseptiv
- local recipes = {
- ["modern_industrialization:iron_plate"] = {{"minecraft:iron_ingot", 1, "kompresor"}}
- }
- -- Funtksia dlia otrymannia danykh pro inventar
- function getInventoryData()
- local inventoryData = {}
- -- Pereviriaiemo vsi peryferii
- for _, peripheralName in ipairs(peripheral.getNames()) do
- if peripheral.hasType(peripheralName, "inventory") then
- local inventory = peripheral.wrap(peripheralName)
- local inventoryItems = inventory.list()
- -- Dlia kozhnoyi skryni zberihaemo yii vmist
- inventoryData[peripheralName] = {}
- for slot, slotData in pairs(inventoryItems) do
- inventoryData[peripheralName][slot] = {
- name = slotData.name,
- count = slotData.count,
- maxCount = slotData.maxCount or 64 -- Vrazhaemo, shcho maxCount dorivniuie 64, yakshcho ne naidene
- }
- end
- end
- end
- return inventoryData
- end
- -- Funtksia dlia perevirky, chy ie skrynia vvodu abo vyvodu
- function isInputOrOutputChest(chestName)
- for _, data in pairs(machineDatabase) do
- if chestName == data.inputChest or chestName == data.outputChest then
- return true
- end
- end
- return false
- end
- -- Funtksia dlia otrymannia vsikh inventariv, krim skryn vvoda/vyvoda
- function getAvailableChests()
- local peripherals = peripheral.getNames()
- local availableChests = {}
- for _, name in ipairs(peripherals) do
- if peripheral.hasType(name, "inventory") and not isInputOrOutputChest(name) then
- table.insert(availableChests, name)
- end
- end
- return availableChests
- end
- -- Funtksia dlia peremishchennia predmetiv u dostupnu skryniu
- function moveToAvailableChest(item, amount, fromChest, fromSlot)
- local availableChests = getAvailableChests()
- for _, chestName in ipairs(availableChests) do
- local chest = peripheral.wrap(chestName)
- local chestItems = chest.list()
- for slot, slotData in pairs(chestItems) do
- if slotData.name == item and slotData.count and slotData.maxCount and slotData.count < slotData.maxCount then
- local spaceLeft = slotData.maxCount - slotData.count
- local toMove = math.min(amount, spaceLeft)
- local moved = peripheral.call(fromChest, "pushItems", chestName, fromSlot, toMove, slot)
- if moved > 0 then
- amount = amount - moved
- if amount <= 0 then
- return true -- Uspishno peremistuly vse
- end
- end
- end
- end
- if amount > 0 then
- -- Yakshcho ne znayshly pidkhodiashchyi slot, probuemo znayty novyi porozhnii slot
- local moved = peripheral.call(fromChest, "pushItems", chestName, fromSlot, amount)
- if moved > 0 then
- return true -- Uspishno peremistuly
- end
- end
- end
- return false -- Yakshcho ne vdalosia znayty mistse
- end
- -- Funtksia dlia zbirannia predmetiv
- function collectItems()
- local inventoryData = getInventoryData()
- local collectedItems = {}
- for chestName, chestItems in pairs(inventoryData) do
- if not isInputOrOutputChest(chestName) then
- for slot, slotData in pairs(chestItems) do
- if not collectedItems[slotData.name] then
- collectedItems[slotData.name] = 0
- end
- collectedItems[slotData.name] = collectedItems[slotData.name] + slotData.count
- -- Peremishchaiemo vsi predmety z tsikh skryn
- peripheral.call(chestName, "pushItems", chestName, slot)
- end
- end
- end
- return collectedItems
- end
- -- Funtksia dlia peremishchennia predmetiv do dostupnykh skryn
- function distributeItems(collectedItems)
- for itemName, itemCount in pairs(collectedItems) do
- local availableChests = getAvailableChests()
- for _, chestName in ipairs(availableChests) do
- local chest = peripheral.wrap(chestName)
- local chestItems = chest.list()
- for slot, slotData in pairs(chestItems) do
- if slotData.name == itemName and slotData.count and slotData.maxCount and slotData.count < slotData.maxCount then
- local spaceLeft = slotData.maxCount - slotData.count
- local toMove = math.min(itemCount, spaceLeft)
- local moved = peripheral.call(chestName, "pushItems", chestName, 1, toMove, slot)
- itemCount = itemCount - moved
- if itemCount <= 0 then
- break
- end
- end
- end
- if itemCount > 0 then
- -- Zapovnuiemo novi sloty
- local moved = peripheral.call(chestName, "pushItems", chestName, 1, itemCount)
- itemCount = itemCount - moved
- if itemCount <= 0 then
- break
- end
- end
- end
- end
- end
- -- Funtksia dlia sortuvannia inventariv
- function sortInventories()
- local collectedItems = collectItems()
- distributeItems(collectedItems)
- end
- -- Funtksia dlia obroblennia hotovoho rezultatu
- function processCraftingResults(outputChest, resultItem, expectedAmount)
- local inventoryData = getInventoryData()
- local chestItems = inventoryData[outputChest]
- if chestItems then
- for slot, slotData in pairs(chestItems) do
- if slotData.name == resultItem and slotData.count >= expectedAmount then
- -- Yakshcho predmety hotovi, peremishchaiemo yikh do dostupnoyi skryni
- if moveToAvailableChest(resultItem, slotData.count, outputChest, slot) then
- print("Predmety peremistuli do skladu.")
- return true
- else
- print("Ne vdalosia peremistuty predmety do skladu.")
- return false
- end
- end
- end
- end
- print("Rezultat kraftu ne znaydeno u vykhidnii skryni.")
- return false
- end
- -- Funtksia dlia perevirky nayavnosti predmetu v inventari
- function checkInventoryForItem(item, amount)
- local inventoryData = getInventoryData()
- for chestName, chestItems in pairs(inventoryData) do
- for slot, slotData in pairs(chestItems) do
- if slotData.name == item and slotData.count >= amount then
- return chestName, slot
- end
- end
- end
- return nil, nil
- end
- -- Funtksia dlia peremishchennia predmetiv mizh skryniamy
- function moveItemToChest(fromChest, fromSlot, toChest, amount)
- local moved = peripheral.call(fromChest, "pushItems", toChest, fromSlot, amount)
- return moved > 0
- end
- -- Funktksia dlia obroblennia cherhy
- function processQueue(machine)
- local data = machineDatabase[machine]
- if #data.queue > 0 then
- local task = table.remove(data.queue, 1)
- local item = task.item
- local amount = task.amount
- local inputChest = data.inputChest
- local outputChest = data.outputChest
- -- Znakhodimo resurs u skryni
- local sourceChest, sourceSlot = checkInventoryForItem(item, amount)
- if sourceChest then
- -- Peremishchaiemo resurs do mekhanizmu
- if moveItemToChest(sourceChest, sourceSlot, inputChest, amount) then
- print("Peredano do " .. machine .. ": " .. item .. " (" .. amount .. ")")
- -- Chekaiemo zavershennia roboty mekhanizmu
- local resultReady = false
- local expectedResultItem = "modern_industrialization:iron_plate" -- Zamina na ochikuvanyi rezultat
- while not resultReady do
- os.sleep(5)
- resultReady = processCraftingResults(outputChest, expectedResultItem, amount)
- end
- print("Kraft zaversheno, predmety peremistuli.")
- else
- print("Pomy`lka peredachi do " .. machine)
- end
- else
- print("Nemaie dostatnoho resursu: " .. item)
- end
- end
- end
- -- Funktksia dlia dodavannia zadachi v cherhu
- function addToQueue(machine, item, amount)
- table.insert(machineDatabase[machine].queue, {item = item, amount = amount})
- print("Dodano do cherhy " .. machine .. ": " .. item .. " (" .. amount .. ")")
- end
- -- Funktksia dlia kraftu
- function craftItem(item, amount)
- if not recipes[item] then
- print("Retsept dlia " .. item .. " ne znaydeno.")
- return
- end
- local components = recipes[item]
- for _, component in ipairs(components) do
- local compItem = component[1]
- local compAmount = component[2] * amount
- local machine = component[3]
- addToQueue(machine, compItem, compAmount)
- end
- for machine in pairs(machineDatabase) do
- processQueue(machine)
- end
- sortInventories()
- end
- while true do
- print("Vvedit komand (napr., 'craft modern_industrialization:iron_plate 1'): ")
- local input = read()
- local args = {}
- for word in input:gmatch("%S+") do
- table.insert(args, word)
- end
- local command = args[1]
- local item = args[2]
- local amount = tonumber(args[3]) or 1
- if command == "craft" then
- craftItem(item, amount)
- else
- print("Nevidoma komanda.")
- end
- end
Advertisement
Comments
-
- Наразі він в розробці. Поки не працює належним чином.
- Це система, де кидаєте основні ресурси у багато скринь під'єднаних до багатьох модемів. Комп'ютер приймає запит на крафт, який ви колись раніше ввели. Після чого він аналізує предмети у скринях, і також черги, де розраховує скільки яких ресурсів йому потрібно, і що з ними треба зробити, аби створити цей крафт. Він перекидає вже у окремо зазначені скрині, які будуть перекидати ресурси вже у механізми ресурси, аби виконався певний крафт(наприклад подрібнити, компресувати...). В цей час механізм увесь час скидає у інші скрині(скаімо скрині для виводу), де вже комп'ютер увесь час аналізує, чи зібалась потрібна кількість ресурсів, які він планує отримати з тої кількості, яку він спеціально клав для цього виконання. Як тільки потрібна кількість підтверджується, комп'ютер перекидає ці ресурси у основні скрині(наш склад), і ставить наступні ресурси у черзі на крафт.
Add Comment
Please, Sign In to add comment
Advertisement