Krobys

[Computer] Auto Craft Resourses

Mar 25th, 2023 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.86 KB | Gaming | 0 0
  1. local com = require("component")
  2. local interface = com.isAvailable("me_interface") and com.me_interface or error("No ME interface")
  3. local shell = require("shell")
  4.  
  5. local mainItemToCraft = {name = "AdvancedSolarPanel:BlockAdvSolarPanel", damage = 7}
  6.  
  7. local products = {
  8.     redstone = { name = "minecraft:redstone", damage = 0, count = 100000 },
  9.     iron = { name = "minecraft:iron_ingot", damage = 0, count = 216000 },
  10.     copper = { name = "ThermalFoundation:material", damage = 64, count = 116000 },
  11.     tin = { name = "ThermalFoundation:material", damage = 65, count = 66000 },
  12.     coal = { name = "minecraft:coal", damage = 0, count = 400000 }
  13. }
  14.  
  15. local orePriceWis = 3
  16. local wisItem = {displayName= "Вис Материя", name="dwcity:Vis_materia", dmg = 0, mod_name="dwcity"}
  17.  
  18. local oresForCraft = {
  19.     redstone = { displayName = "Красная руда", name = "minecraft:redstone_ore", damage = 0, product = products.redstone, craftCount = 8, productOutput = 128  },
  20.     iron = { displayName = "Железная руда", name = "minecraft:iron_ore", damage = 0, product = products.iron, craftCount = 8, productOutput = 16  },
  21.     copper = { displayName = "Медная руда", name = "IC2:copperOre", damage = 0, product = products.copper, craftCount = 8, productOutput = 16  },
  22.     coal = { displayName = "Угольная руда", name = "minecraft:coal_ore", damage = 0, product = products.coal, craftCount = 12, productOutput = 36},
  23.     tin = { displayName = "Оловянная руда", name = "IC2:tinOre", damage = 0, product = products.tin, craftCount = 8, productOutput = 16}
  24. }
  25.  
  26. local function getCraftablesAll()
  27.     local craftables = {}
  28.     for key, value in pairs(oresForCraft) do
  29.         local craftableLocal = interface.getCraftables({name = value.name, damage = value.damage})[1]
  30.         if craftableLocal then
  31.             table.insert(craftables, craftableLocal)
  32.         end
  33.     end
  34.     return craftables
  35. end
  36.  
  37. local craftables = getCraftablesAll()
  38.  
  39. local function getTableOfWhatInNetworkNow()
  40.     local tableToCraftLeft = {}
  41.     local network = interface.getItemsInNetwork()
  42.     for ind1 = 1, #network do
  43.         local itemInNetwork = network[ind1]
  44.         for key, itemProduct in pairs(products) do
  45.             if itemInNetwork.name == itemProduct.name and itemInNetwork.damage == itemProduct.damage then
  46.                 --найдено в сети продукт
  47.                 local productCountInNetwork = itemInNetwork.size
  48.                 --теперь нужно определить к какой руде относится продукт
  49.                 local oreForProduct = oresForCraft[key]
  50.                 --считаем сколько руды в продукте (например 36 слитков это 18 руды)
  51.                 local countInOre = productCountInNetwork * oreForProduct.craftCount / oreForProduct.productOutput
  52.                 --создаем обьект для помещения в  таблицу
  53.                 if tableToCraftLeft[key] then
  54.                     tableToCraftLeft[key] = tableToCraftLeft[key] + countInOre
  55.                 else
  56.                     tableToCraftLeft[key] = countInOre
  57.                 end
  58.             end
  59.         end
  60.         for key, itemForCraft in pairs(oresForCraft) do
  61.             if itemInNetwork.name == itemForCraft.name and itemInNetwork.damage == itemForCraft.damage then
  62.                 --найдено в сети предмет для крафта
  63.                 local oreCountInNetwork = itemInNetwork.size
  64.                 if tableToCraftLeft[key] then
  65.                     tableToCraftLeft[key] = tableToCraftLeft[key] + oreCountInNetwork
  66.                 else
  67.                     tableToCraftLeft[key] = oreCountInNetwork
  68.                 end
  69.             end
  70.         end
  71.     end
  72.     return tableToCraftLeft
  73. end
  74.  
  75. local function getTableOfWhatNeedToCreate()
  76.     print("Инициализация таблицы того, что нужно создать")
  77.     local tableOfWhatInNetwork = getTableOfWhatInNetworkNow()
  78.     local tableOfOresNeedToCrateFull = {}
  79.     for key, ore in pairs(oresForCraft) do
  80.         local oreCountNeed = (ore.product.count / ore.productOutput) * ore.craftCount
  81.         local needToCreate = oreCountNeed - tableOfWhatInNetwork[key]
  82.         if needToCreate > 0 then
  83.             tableOfOresNeedToCrateFull[key] = needToCreate
  84.         end
  85.     end
  86.     print("Инициализация таблицы того, что нужно создать кончена. ")
  87.     return tableOfOresNeedToCrateFull
  88. end
  89.  
  90. local function addCraftablesToItemsForCraft()
  91.     shell.execute("cls")
  92.     print("Инициализация таблиц для крафта")
  93.     for key, itemForCraft in pairs(oresForCraft) do
  94.         for _, itemCraftTable in pairs(craftables) do
  95.             if type(itemCraftTable) == "table" then
  96.                 local itemCrafted = itemCraftTable.getItemStack()
  97.                 --print(itemCrafted.name.." == "..itemForCraft.name.." and "..itemCrafted.damage.." == "..itemForCraft.damage)
  98.                 if itemCrafted.name == itemForCraft.name and itemCrafted.damage == itemForCraft.damage then
  99.                     print("Найдена таблица для "..itemForCraft.displayName)
  100.                     itemForCraft.craftable = itemCraftTable
  101.                 end
  102.             end
  103.         end
  104.     end
  105.     print("Инициализация таблиц для крафта кончена. ")
  106. end
  107.  
  108. function getWisInNetwork(network)
  109.     for index = 1, #network do
  110.         if network[index].name == wisItem.name then
  111.             return network[index].size
  112.         end
  113.     end
  114. end
  115.  
  116. addCraftablesToItemsForCraft()
  117.  
  118. local tableToCreate = getTableOfWhatNeedToCreate()
  119.  
  120. local function createFromTableToCreate(tableCreation)
  121.     local wisSpent = 0
  122.     for key, countToCreate in pairs(tableCreation) do
  123.         local oreToCreate = oresForCraft[key]
  124.         print("Сейчас создаю "..oreToCreate.displayName .. " "..countToCreate.." штук")
  125.         local oreLeft = countToCreate
  126.         while oreLeft > 0 do
  127.             local wisInNetwork = interface.getItemDetail({id = wisItem.name, dmg = wisItem.dmg}).all().qty
  128.             print("Вис в сети: "..wisInNetwork)
  129.             local canCraftFromWis = math.floor((wisInNetwork / orePriceWis) * oreToCreate.craftCount) - 8
  130.             print("Можно создать из вис: "..canCraftFromWis)
  131.             local requestOreValue = 0
  132.             if canCraftFromWis > oreLeft then
  133.                 requestOreValue = oreLeft
  134.             elseif canCraftFromWis < oreLeft and canCraftFromWis > 0 then
  135.                 requestOreValue = canCraftFromWis
  136.             elseif canCraftFromWis == 0 then
  137.                 os.sleep(60)
  138.                 print("Простаиваю 60 секунд. Ожидаю вис в системе")
  139.             end
  140.             local wisSpentForCraft = math.floor((requestOreValue / oreToCreate.craftCount) * orePriceWis )
  141.             wisSpent = wisSpent + wisSpentForCraft
  142.             local request = oreToCreate.craftable.request(requestOreValue)
  143.             print("В процессе: "..requestOreValue.." штук")
  144.             repeat
  145.                 os.sleep(60)
  146.             until (request.isCanceled() or request.isDone())
  147.             oreLeft = oreLeft - requestOreValue
  148.             print("Создано "..countToCreate-oreLeft.."/"..countToCreate)
  149.         end
  150.     end
  151.  
  152.     print("Создание окончено. Потрачено "..wisSpent.." вис.")
  153.     local mainItemToCraftTable = interface.getCraftables({name = mainItemToCraft.name, damage = mainItemToCraft.damage})[1]
  154.     if mainItemToCraftTable then
  155.         --сделать ожидание нужного количества ресурсов в мэ
  156.         --после этого запустить крафт
  157.     end
  158. end
  159.  
  160. createFromTableToCreate(tableToCreate)
Advertisement
Add Comment
Please, Sign In to add comment